Update UserAgent header with JenkinsAgent string

This commit is contained in:
Claudiu Guiman 2016-10-28 09:44:48 -07:00
parent f2624e3603
commit 39503db77a
7 changed files with 78 additions and 14 deletions

2
.gitignore vendored
View File

@ -6,3 +6,5 @@
*.settings
*.project
*.classpath
nbactions-jenkins.xml

View File

@ -25,6 +25,7 @@ import com.microsoft.azure.management.resources.ResourceManagementService;
import com.microsoft.azure.management.resources.models.DeploymentGetResult;
import com.microsoft.azure.management.resources.models.ProvisioningState;
import com.microsoft.azure.retry.DefaultRetryStrategy;
import com.microsoft.azure.util.AzureUserAgentFilter;
import com.microsoft.azure.util.ExecutionEngine;
import com.microsoft.azure.util.CleanUpAction;
import com.microsoft.windowsazure.Configuration;
@ -100,7 +101,8 @@ public final class AzureVMAgentCleanUpTask extends AsyncPeriodicWork {
try {
final Configuration config = ServiceDelegateHelper.getConfiguration(cloud);
final ResourceManagementClient rmc = ResourceManagementService.create(config);
final ResourceManagementClient rmc = ResourceManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
DeploymentGetResult deployment =
rmc.getDeploymentsOperations().get(info.getResourceGroupName(), info.getDeploymentName());

View File

@ -24,6 +24,7 @@ import com.microsoft.windowsazure.Configuration;
import com.microsoft.azure.exceptions.AzureCloudException;
import com.microsoft.azure.util.AzureUtil;
import com.microsoft.azure.util.CleanUpAction;
import com.microsoft.azure.util.AzureUserAgentFilter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -376,7 +377,8 @@ public class AzureVMCloud extends Cloud {
// Create a new RM client each time because the config may expire while
// in this long running operation
Configuration config = ServiceDelegateHelper.getConfiguration(template);
final ResourceManagementClient rmc = ResourceManagementService.create(config);
final ResourceManagementClient rmc = ResourceManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
final List<DeploymentOperation> ops = rmc.getDeploymentOperationsOperations().
list(resourceGroupName, deploymentName, null).getOperations();

View File

@ -73,6 +73,7 @@ import com.microsoft.azure.exceptions.AzureCloudException;
import com.microsoft.azure.exceptions.UnrecoverableCloudException;
import com.microsoft.azure.retry.ExponentialRetryStrategy;
import com.microsoft.azure.retry.NoRetryStrategy;
import com.microsoft.azure.util.AzureUserAgentFilter;
import com.microsoft.azure.util.AzureUtil;
import com.microsoft.azure.util.CleanUpAction;
import com.microsoft.azure.util.Constants;
@ -353,16 +354,18 @@ public class AzureVMManagementServiceDelegate {
final String ipRef = vm.getVirtualMachine().getNetworkProfile().getNetworkInterfaces().get(0).
getReferenceUri();
final NetworkInterface netIF = NetworkResourceProviderService.create(config).
getNetworkInterfacesOperations().get(
final NetworkInterface netIF = NetworkResourceProviderService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter())
.getNetworkInterfacesOperations().get(
template.getResourceGroupName(),
ipRef.substring(ipRef.lastIndexOf("/") + 1, ipRef.length())).
getNetworkInterface();
final String nicRef = netIF.getIpConfigurations().get(0).getPublicIpAddress().getId();
final PublicIpAddress pubIP = NetworkResourceProviderService.create(config).
getPublicIpAddressesOperations().get(
final PublicIpAddress pubIP = NetworkResourceProviderService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter())
.getPublicIpAddressesOperations().get(
template.getResourceGroupName(),
nicRef.substring(nicRef.lastIndexOf("/") + 1, nicRef.length())).
getPublicIpAddress();
@ -488,7 +491,8 @@ public class AzureVMManagementServiceDelegate {
public static List<String> getStorageAccountsInfo(final Configuration config) throws Exception {
List<String> storageAccounts = new ArrayList<String>();
StorageManagementClient client = StorageManagementService.create(config);
StorageManagementClient client = StorageManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
StorageAccountListResponse response = client.getStorageAccountsOperations().list();
for (StorageAccount sa : response.getStorageAccounts()) {

View File

@ -32,6 +32,7 @@ import com.microsoft.windowsazure.management.ManagementClient;
import com.microsoft.windowsazure.management.ManagementService;
import com.microsoft.azure.exceptions.AzureCloudException;
import com.microsoft.azure.exceptions.UnrecoverableCloudException;
import com.microsoft.azure.util.AzureUserAgentFilter;
import com.microsoft.azure.util.TokenCache;
import hudson.slaves.Cloud;
import java.util.logging.Level;
@ -152,7 +153,8 @@ public class ServiceDelegateHelper {
Thread.currentThread().setContextClassLoader(AzureVMManagementServiceDelegate.class.getClassLoader());
try {
return ResourceManagementService.create(config);
return ResourceManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
} finally {
Thread.currentThread().setContextClassLoader(thread);
}
@ -169,7 +171,8 @@ public class ServiceDelegateHelper {
Thread.currentThread().setContextClassLoader(AzureVMManagementServiceDelegate.class.getClassLoader());
try {
return ComputeManagementService.create(config);
return ComputeManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
} finally {
Thread.currentThread().setContextClassLoader(thread);
}
@ -187,7 +190,8 @@ public class ServiceDelegateHelper {
Thread.currentThread().setContextClassLoader(AzureVMManagementServiceDelegate.class.getClassLoader());
try {
return StorageManagementService.create(config);
return StorageManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
} finally {
Thread.currentThread().setContextClassLoader(thread);
}
@ -199,7 +203,8 @@ public class ServiceDelegateHelper {
Thread.currentThread().setContextClassLoader(AzureVMManagementServiceDelegate.class.getClassLoader());
try {
return ManagementService.create(config);
return ManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
} finally {
Thread.currentThread().setContextClassLoader(thread);
}
@ -211,7 +216,8 @@ public class ServiceDelegateHelper {
Thread.currentThread().setContextClassLoader(AzureVMManagementServiceDelegate.class.getClassLoader());
try {
return NetworkResourceProviderService.create(config);
return NetworkResourceProviderService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
} finally {
Thread.currentThread().setContextClassLoader(thread);
}

View File

@ -42,6 +42,7 @@ import java.util.logging.Logger;
import com.microsoft.windowsazure.Configuration;
import com.microsoft.azure.exceptions.AzureCloudException;
import com.microsoft.azure.util.AzureUserAgentFilter;
import com.microsoft.azure.util.Constants;
import java.util.ArrayList;
import java.util.List;
@ -69,7 +70,8 @@ public class StorageServiceDelegate {
private static List<URI> getStorageAccountURIs(final Configuration config, final String storageAccountName,
final String resourceGroupName) throws
AzureCloudException {
StorageManagementClient client = StorageManagementService.create(config);
StorageManagementClient client = StorageManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
StorageAccountGetPropertiesResponse response;
try {
@ -119,7 +121,8 @@ public class StorageServiceDelegate {
public static StorageAccountGetPropertiesResponse getStorageAccountProps(
final Configuration config, final String storageAccountName, final String resourceGroupName)
throws AzureCloudException {
StorageManagementClient client = StorageManagementService.create(config);
StorageManagementClient client = StorageManagementService.create(config)
.withRequestFilterFirst(new AzureUserAgentFilter());
StorageAccountGetPropertiesResponse response = null;
try {
response = client.getStorageAccountsOperations().getProperties(

View File

@ -0,0 +1,45 @@
/*
Copyright 2016 Microsoft, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.microsoft.azure.util;
import com.microsoft.windowsazure.core.pipeline.filter.ServiceRequestContext;
import com.microsoft.windowsazure.core.pipeline.filter.ServiceRequestFilter;
public class AzureUserAgentFilter implements ServiceRequestFilter {
private static String PLUGIN_NAME = "AzureJenkinsVMAgent";
public void filter(ServiceRequestContext request) {
String version = null;
try {
version = getClass().getPackage().getImplementationVersion();
} catch (Exception e) {
}
if(version == null) {
version = "local";
}
String userAgent;
if (request.getHeader("User-Agent") != null) {
String currentUserAgent = request.getHeader("User-Agent");
userAgent = PLUGIN_NAME + "/" + version + " " + currentUserAgent;
request.removeHeader("User-Agent");
} else {
userAgent = PLUGIN_NAME + "/" + version;
}
request.setHeader("User-Agent", userAgent);
}
}