From debb943de6ddd85cb7822f9fa4201cf4440332ce Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Tue, 18 Oct 2016 11:31:42 -0700 Subject: [PATCH 1/3] Separate ListingByTag into group & subscription level --- .../management/resources/Deployments.java | 2 ++ .../resources/GenericResources.java | 4 +-- .../management/resources/ResourceGroups.java | 2 ++ .../SupportsListingInGroupByTag.java | 31 +++++++++++++++++++ .../collection/SupportsListingByTag.java | 5 ++- .../implementation/DeploymentsImpl.java | 15 +++++++++ .../implementation/ResourceGroupsImpl.java | 15 +++++++++ .../resources/ResourceGroupsTests.java | 2 +- 8 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/SupportsListingInGroupByTag.java rename azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/{arm => }/collection/SupportsListingByTag.java (80%) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/Deployments.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/Deployments.java index 5b3c5b294160e..edae8918c61b3 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/Deployments.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/Deployments.java @@ -12,6 +12,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingById; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByName; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByGroup; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingInGroupByTag; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing; @@ -24,6 +25,7 @@ public interface Deployments extends SupportsCreating, SupportsListing, SupportsListingByGroup, + SupportsListingInGroupByTag, SupportsGettingByName, SupportsGettingByGroup, SupportsGettingById, diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResources.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResources.java index 7222e0337bd8c..093ebedc635ec 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResources.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResources.java @@ -9,7 +9,7 @@ import com.microsoft.azure.management.apigeneration.Fluent; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingById; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByGroup; -import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByTag; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingInGroupByTag; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing; @@ -22,7 +22,7 @@ public interface GenericResources extends SupportsListing, SupportsListingByGroup, - SupportsListingByTag, + SupportsListingInGroupByTag, SupportsGettingById, SupportsCreating { /** diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/ResourceGroups.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/ResourceGroups.java index 42b11dd40e921..0c54ed0a63c38 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/ResourceGroups.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/ResourceGroups.java @@ -12,6 +12,7 @@ import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListingByTag; /** * Entry point to resource group management API. @@ -19,6 +20,7 @@ @Fluent public interface ResourceGroups extends SupportsListing, + SupportsListingByTag, SupportsGettingByName, SupportsCreating, SupportsDeleting, diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/SupportsListingInGroupByTag.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/SupportsListingInGroupByTag.java new file mode 100644 index 0000000000000..c6d680d514d8f --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/SupportsListingInGroupByTag.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.azure.management.resources.fluentcore.arm.collection; + +import com.microsoft.azure.PagedList; +import com.microsoft.azure.management.apigeneration.LangDefinition; +import com.microsoft.azure.management.apigeneration.LangDefinition.MethodConversion; + +/** + * Provides access to listing Azure resources of a specific type based on their tag. + *

+ * (Note: this interface is not intended to be implemented by user code) + * + * @param the fluent type of the resource + */ +@LangDefinition(ContainerName = "CollectionActions", MethodConversionType = MethodConversion.OnlyMethod) +public interface SupportsListingInGroupByTag { + /** + * Lists all the resources with the specified tag. + * + * @param resourceGroupName the name of the resource group + * @param tagName tag's name as the key + * @param tagValue tag's value + * @return list of resources + */ + PagedList listByTag(String resourceGroupName, String tagName, String tagValue); +} diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/SupportsListingByTag.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsListingByTag.java similarity index 80% rename from azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/SupportsListingByTag.java rename to azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsListingByTag.java index 684968a9775a3..75105d3e03c57 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/SupportsListingByTag.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsListingByTag.java @@ -4,7 +4,7 @@ * license information. */ -package com.microsoft.azure.management.resources.fluentcore.arm.collection; +package com.microsoft.azure.management.resources.fluentcore.collection; import com.microsoft.azure.PagedList; import com.microsoft.azure.management.apigeneration.LangDefinition; @@ -22,10 +22,9 @@ public interface SupportsListingByTag { /** * Lists all the resources with the specified tag. * - * @param resourceGroupName the name of the resource group * @param tagName tag's name as the key * @param tagValue tag's value * @return list of resources */ - PagedList listByTag(String resourceGroupName, String tagName, String tagValue); + PagedList listByTag(String tagName, String tagValue); } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentsImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentsImpl.java index 137d58be04dde..7e292acd7ce08 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentsImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentsImpl.java @@ -61,6 +61,21 @@ public PagedList listByGroup(String groupName) { return converter.convert(client.list(groupName)); } + @Override + public PagedList listByTag(String resourceGroupName, String tagName, String tagValue) { + if (tagName == null) { + throw new IllegalArgumentException("tagName == null"); + } + String odataFilter; + if (tagValue == null) { + odataFilter = String.format("tagname eq '%s'", tagName); + } else { + odataFilter = String.format("tagname eq '%s' and tagvalue eq '%s'", tagName, tagValue); + } + return converter.convert(client.list( + resourceGroupName, odataFilter, null)); + } + @Override public Deployment getByName(String name) { for (ResourceGroup group : this.resourceManager.resourceGroups().list()) { diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupsImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupsImpl.java index 5f0703eb1c285..a5793bdb3e09a 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupsImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupsImpl.java @@ -36,6 +36,21 @@ public PagedList list() { return wrapList(client.list()); } + @Override + public PagedList listByTag(String tagName, String tagValue) { + if (tagName == null) { + throw new IllegalArgumentException("tagName == null"); + } + String odataFilter; + if (tagValue == null) { + odataFilter = String.format("tagname eq '%s'", tagName); + } else { + odataFilter = String.format("tagname eq '%s' and tagvalue eq '%s'", tagName, tagValue); + } + return wrapList(client.list( + odataFilter, null)); + } + @Override public ResourceGroupImpl getByName(String name) { return wrapModel(client.get(name)); diff --git a/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/ResourceGroupsTests.java b/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/ResourceGroupsTests.java index 2634893a72498..ce8cecbf0dc44 100644 --- a/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/ResourceGroupsTests.java +++ b/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/ResourceGroupsTests.java @@ -26,7 +26,7 @@ public void canCreateResourceGroup() throws Exception { .create(); // List ResourceGroup groupResult = null; - for (ResourceGroup rg : resourceGroups.list()) { + for (ResourceGroup rg : resourceGroups.listByTag("department", "finance")) { if (rg.name().equals(rgName)) { groupResult = rg; break; From a33bdaad41408c2cdef3b3ecf996161863d8448e Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Tue, 18 Oct 2016 11:33:17 -0700 Subject: [PATCH 2/3] Deployments dont have tags --- .../azure/management/resources/Deployments.java | 2 -- .../resources/implementation/DeploymentsImpl.java | 15 --------------- 2 files changed, 17 deletions(-) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/Deployments.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/Deployments.java index edae8918c61b3..5b3c5b294160e 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/Deployments.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/Deployments.java @@ -12,7 +12,6 @@ import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingById; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByName; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByGroup; -import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingInGroupByTag; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing; @@ -25,7 +24,6 @@ public interface Deployments extends SupportsCreating, SupportsListing, SupportsListingByGroup, - SupportsListingInGroupByTag, SupportsGettingByName, SupportsGettingByGroup, SupportsGettingById, diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentsImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentsImpl.java index 7e292acd7ce08..137d58be04dde 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentsImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentsImpl.java @@ -61,21 +61,6 @@ public PagedList listByGroup(String groupName) { return converter.convert(client.list(groupName)); } - @Override - public PagedList listByTag(String resourceGroupName, String tagName, String tagValue) { - if (tagName == null) { - throw new IllegalArgumentException("tagName == null"); - } - String odataFilter; - if (tagValue == null) { - odataFilter = String.format("tagname eq '%s'", tagName); - } else { - odataFilter = String.format("tagname eq '%s' and tagvalue eq '%s'", tagName, tagValue); - } - return converter.convert(client.list( - resourceGroupName, odataFilter, null)); - } - @Override public Deployment getByName(String name) { for (ResourceGroup group : this.resourceManager.resourceGroups().list()) { From 34bc22d2e9eaac0c12ae9374a5965446d14ccff7 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Tue, 18 Oct 2016 17:01:55 -0700 Subject: [PATCH 3/3] Move odata creation to utils --- .../resources/fluentcore/utils/Utils.java | 17 +++++++++++++++++ .../implementation/GenericResourcesImpl.java | 12 ++---------- .../implementation/ResourceGroupsImpl.java | 13 ++----------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java index 474f89834c5e2..8f2961afa69e2 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java @@ -23,6 +23,23 @@ public static boolean toPrimitiveBoolean(Boolean value) { return value; } + /** + * Creates an Odata filter string that can be used for filtering list results by tags. + * + * @param tagName the name of the tag. If not provided, all resources will be returned. + * @param tagValue the value of the tag. If not provided, only tag name will be filtered. + * @return the Odata filter to pass into list methods + */ + public static String createOdataFilterForTags(String tagName, String tagValue) { + if (tagName == null) { + return null; + } else if (tagValue == null) { + return String.format("tagname eq '%s'", tagName); + } else { + return String.format("tagname eq '%s' and tagvalue eq '%s'", tagName, tagValue); + } + } + private Utils() { } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourcesImpl.java index 6eb3c3dd1256d..408d323f93be2 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourcesImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourcesImpl.java @@ -15,6 +15,7 @@ import com.microsoft.azure.management.resources.ResourceGroup; import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.GroupableResourcesImpl; +import com.microsoft.azure.management.resources.fluentcore.utils.Utils; import rx.Observable; import java.util.List; @@ -50,17 +51,8 @@ public PagedList listByGroup(String groupName) { @Override public PagedList listByTag(String resourceGroupName, String tagName, String tagValue) { - if (tagName == null) { - throw new IllegalArgumentException("tagName == null"); - } - String odataFilter; - if (tagValue == null) { - odataFilter = String.format("tagname eq '%s'", tagName); - } else { - odataFilter = String.format("tagname eq '%s' and tagvalue eq '%s'", tagName, tagValue); - } return wrapList(this.serviceClient.resourceGroups().listResources( - resourceGroupName, odataFilter, null, null)); + resourceGroupName, Utils.createOdataFilterForTags(tagName, tagValue), null, null)); } @Override diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupsImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupsImpl.java index a5793bdb3e09a..cab2dc3827c9f 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupsImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupsImpl.java @@ -10,6 +10,7 @@ import com.microsoft.azure.management.resources.ResourceGroup; import com.microsoft.azure.management.resources.ResourceGroups; import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.CreatableResourcesImpl; +import com.microsoft.azure.management.resources.fluentcore.utils.Utils; import rx.Observable; /** @@ -38,17 +39,7 @@ public PagedList list() { @Override public PagedList listByTag(String tagName, String tagValue) { - if (tagName == null) { - throw new IllegalArgumentException("tagName == null"); - } - String odataFilter; - if (tagValue == null) { - odataFilter = String.format("tagname eq '%s'", tagName); - } else { - odataFilter = String.format("tagname eq '%s' and tagvalue eq '%s'", tagName, tagValue); - } - return wrapList(client.list( - odataFilter, null)); + return wrapList(client.list(Utils.createOdataFilterForTags(tagName, tagValue), null)); } @Override