Skip to content

Commit

Permalink
Move odata creation to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Oct 19, 2016
1 parent a33bdaa commit 34bc22d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
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 Down Expand Up @@ -38,17 +39,7 @@ public PagedList<ResourceGroup> list() {

@Override
public PagedList<ResourceGroup> 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
Expand Down

0 comments on commit 34bc22d

Please sign in to comment.