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 7222e0337bd8..093ebedc635e 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 42b11dd40e92..0c54ed0a63c3 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 000000000000..c6d680d514d8 --- /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 684968a9775a..75105d3e03c5 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/fluentcore/utils/Utils.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java index 474f89834c5e..8f2961afa69e 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 6eb3c3dd1256..408d323f93be 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 5f0703eb1c28..cab2dc3827c9 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; /** @@ -36,6 +37,11 @@ public PagedList list() { return wrapList(client.list()); } + @Override + public PagedList listByTag(String tagName, String tagValue) { + return wrapList(client.list(Utils.createOdataFilterForTags(tagName, tagValue), 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 2634893a7249..ce8cecbf0dc4 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;