diff --git a/.gitignore b/.gitignore index 8b699a0..f8e42b8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ *.settings *.project *.classpath + +nbactions-jenkins.xml diff --git a/src/main/java/com/microsoft/azure/AzureVMAgentCleanUpTask.java b/src/main/java/com/microsoft/azure/AzureVMAgentCleanUpTask.java index ea42cc6..a31052e 100644 --- a/src/main/java/com/microsoft/azure/AzureVMAgentCleanUpTask.java +++ b/src/main/java/com/microsoft/azure/AzureVMAgentCleanUpTask.java @@ -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()); diff --git a/src/main/java/com/microsoft/azure/AzureVMCloud.java b/src/main/java/com/microsoft/azure/AzureVMCloud.java index af8e82e..1484af3 100644 --- a/src/main/java/com/microsoft/azure/AzureVMCloud.java +++ b/src/main/java/com/microsoft/azure/AzureVMCloud.java @@ -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 ops = rmc.getDeploymentOperationsOperations(). list(resourceGroupName, deploymentName, null).getOperations(); diff --git a/src/main/java/com/microsoft/azure/AzureVMManagementServiceDelegate.java b/src/main/java/com/microsoft/azure/AzureVMManagementServiceDelegate.java index ebef929..5709e10 100644 --- a/src/main/java/com/microsoft/azure/AzureVMManagementServiceDelegate.java +++ b/src/main/java/com/microsoft/azure/AzureVMManagementServiceDelegate.java @@ -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 getStorageAccountsInfo(final Configuration config) throws Exception { List storageAccounts = new ArrayList(); - StorageManagementClient client = StorageManagementService.create(config); + StorageManagementClient client = StorageManagementService.create(config) + .withRequestFilterFirst(new AzureUserAgentFilter()); StorageAccountListResponse response = client.getStorageAccountsOperations().list(); for (StorageAccount sa : response.getStorageAccounts()) { diff --git a/src/main/java/com/microsoft/azure/ServiceDelegateHelper.java b/src/main/java/com/microsoft/azure/ServiceDelegateHelper.java index 86252ae..3874f57 100644 --- a/src/main/java/com/microsoft/azure/ServiceDelegateHelper.java +++ b/src/main/java/com/microsoft/azure/ServiceDelegateHelper.java @@ -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); } diff --git a/src/main/java/com/microsoft/azure/StorageServiceDelegate.java b/src/main/java/com/microsoft/azure/StorageServiceDelegate.java index e98a4a1..7af1823 100644 --- a/src/main/java/com/microsoft/azure/StorageServiceDelegate.java +++ b/src/main/java/com/microsoft/azure/StorageServiceDelegate.java @@ -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 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( diff --git a/src/main/java/com/microsoft/azure/util/AzureUserAgentFilter.java b/src/main/java/com/microsoft/azure/util/AzureUserAgentFilter.java new file mode 100644 index 0000000..595ba6e --- /dev/null +++ b/src/main/java/com/microsoft/azure/util/AzureUserAgentFilter.java @@ -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); + } +}