Skip to content

Commit

Permalink
Merge pull request #1202 from jianghaolu/listbytag
Browse files Browse the repository at this point in the history
Separate ListingByTag into group & subscription level
  • Loading branch information
Martin Sawicki authored Oct 19, 2016
2 parents 932f059 + 34bc22d commit 72c88a7
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,7 +22,7 @@
public interface GenericResources extends
SupportsListing<GenericResource>,
SupportsListingByGroup<GenericResource>,
SupportsListingByTag<GenericResource>,
SupportsListingInGroupByTag<GenericResource>,
SupportsGettingById<GenericResource>,
SupportsCreating<GenericResource.DefinitionStages.Blank> {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
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.
*/
@Fluent
public interface ResourceGroups extends
SupportsListing<ResourceGroup>,
SupportsListingByTag<ResourceGroup>,
SupportsGettingByName<ResourceGroup>,
SupportsCreating<ResourceGroup.DefinitionStages.Blank>,
SupportsDeleting,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* (Note: this interface is not intended to be implemented by user code)
*
* @param <T> the fluent type of the resource
*/
@LangDefinition(ContainerName = "CollectionActions", MethodConversionType = MethodConversion.OnlyMethod)
public interface SupportsListingInGroupByTag<T> {
/**
* 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<T> listByTag(String resourceGroupName, String tagName, String tagValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,10 +22,9 @@ public interface SupportsListingByTag<T> {
/**
* 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<T> listByTag(String resourceGroupName, String tagName, String tagValue);
PagedList<T> listByTag(String tagName, String tagValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,17 +51,8 @@ public PagedList<GenericResource> listByGroup(String groupName) {

@Override
public PagedList<GenericResource> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -36,6 +37,11 @@ public PagedList<ResourceGroup> list() {
return wrapList(client.list());
}

@Override
public PagedList<ResourceGroup> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 72c88a7

Please sign in to comment.