Skip to content

Commit

Permalink
Merge pull request #1003 from martinsawicki/issue998
Browse files Browse the repository at this point in the history
fix for issue 998 - uniquely get a generic resource using name, group, type and provider info
  • Loading branch information
anuchandy authored Aug 2, 2016
2 parents c0b09ce + 5d51a23 commit 0fdd748
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package com.microsoft.azure.management.resources;

import com.microsoft.azure.CloudException;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByGroup;
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.collection.SupportsCreating;
Expand All @@ -20,7 +19,6 @@
*/
public interface GenericResources extends
SupportsListingByGroup<GenericResource>,
SupportsGettingByGroup<GenericResource>,
SupportsGettingById<GenericResource>,
SupportsCreating<GenericResource.DefinitionBlank> {
/**
Expand All @@ -36,7 +34,13 @@ public interface GenericResources extends
* @throws IOException serialization failures
* @throws CloudException failures thrown from Azure
*/
boolean checkExistence(String resourceGroupName, String resourceProviderNamespace, String parentResourcePath, String resourceType, String resourceName, String apiVersion) throws IOException, CloudException;
boolean checkExistence(
String resourceGroupName,
String resourceProviderNamespace,
String parentResourcePath,
String resourceType,
String resourceName,
String apiVersion) throws IOException, CloudException;

/**
* Returns a resource belonging to a resource group.
Expand All @@ -51,7 +55,29 @@ public interface GenericResources extends
* @throws CloudException exception thrown from REST call
* @throws IOException exception thrown from serialization/deserialization
*/
GenericResource get(String resourceGroupName, String resourceProviderNamespace, String parentResourcePath, String resourceType, String resourceName, String apiVersion) throws CloudException, IOException;
GenericResource get(
String resourceGroupName,
String resourceProviderNamespace,
String parentResourcePath,
String resourceType,
String resourceName,
String apiVersion) throws CloudException, IOException;

/**
* Returns a resource belonging to a resource group.
* @param resourceGroupName the resource group name
* @param providerNamespace the provider namespace
* @param resourceType the resource type
* @param resourceName the name of the resource
* @return the generic resource
* @throws IOException exception thrown from serialization/deserialization
* @throws CloudException exception thrown from REST call
*/
GenericResource get(
String resourceGroupName,
String providerNamespace,
String resourceType,
String resourceName) throws CloudException, IOException;

/**
* Move resources from one resource group to another.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected GroupableResourcesImpl(
public abstract T getByGroup(String groupName, String name) throws CloudException, IOException;

@Override
public final T getById(String id) throws CloudException, IOException {
public T getById(String id) throws CloudException, IOException {
return this.getByGroup(
ResourceUtils.groupFromResourceId(id),
ResourceUtils.nameFromResourceId(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,53 @@ public boolean checkExistence(String resourceGroupName, String resourceProviderN
}

@Override
public GenericResource get(String resourceGroupName, String resourceProviderNamespace, String parentResourcePath, String resourceType, String resourceName, String apiVersion) throws CloudException, IOException {
GenericResourceInner inner = this.innerCollection.get(resourceGroupName, resourceProviderNamespace, parentResourcePath, resourceType, resourceName, apiVersion).getBody();
public GenericResource getById(String id) throws CloudException, IOException {
return this.get(
ResourceUtils.groupFromResourceId(id),
ResourceUtils.resourceProviderFromResourceId(id),
ResourceUtils.resourceTypeFromResourceId(id),
ResourceUtils.nameFromResourceId(id));
}

@Override
public GenericResource get(
String resourceGroupName,
String providerNamespace,
String resourceType,
String name) throws CloudException, IOException {

PagedList<GenericResource> genericResources = this.listByGroup(resourceGroupName);
for (GenericResource resource : genericResources) {
if (resource.name().equalsIgnoreCase(name)
&& resource.resourceProviderNamespace().equalsIgnoreCase(providerNamespace)
&& resource.resourceType().equalsIgnoreCase(resourceType)) {
return resource;
}
}
throw new CloudException("Generic resource not found.");
}

@Override
public GenericResource get(
String resourceGroupName,
String resourceProviderNamespace,
String parentResourcePath,
String resourceType,
String resourceName,
String apiVersion) throws CloudException, IOException {

// Correct for auto-gen'd API's treatment parent path as required even though it makes sense only for child resources
if (parentResourcePath == null) {
parentResourcePath = "";
}

GenericResourceInner inner = this.innerCollection.get(
resourceGroupName,
resourceProviderNamespace,
parentResourcePath,
resourceType,
resourceName,
apiVersion).getBody();
GenericResourceImpl resource = new GenericResourceImpl(
resourceName,
inner,
Expand Down Expand Up @@ -92,17 +137,6 @@ public void delete(String resourceGroupName, String resourceProviderNamespace, S
this.innerCollection.delete(resourceGroupName, resourceProviderNamespace, parentResourcePath, resourceType, resourceName, apiVersion);
}

@Override
public GenericResource getByGroup(String groupName, String name) throws CloudException, IOException {
PagedList<GenericResource> genericResources = this.listByGroup(groupName);
for (GenericResource resource : genericResources) {
if (resource.name().equalsIgnoreCase(name)) {
return resource;
}
}
throw new CloudException("Generic resource not found.");
}

@Override
protected GenericResourceImpl wrapModel(String id) {
return new GenericResourceImpl(
Expand Down Expand Up @@ -130,4 +164,10 @@ protected GenericResourceImpl wrapModel(GenericResourceInner inner) {
.withResourceType(ResourceUtils.resourceTypeFromResourceId(inner.id()))
.withParentResource(ResourceUtils.parentResourcePathFromResourceId(inner.id()));
}

@Override
public GenericResource getByGroup(String groupName, String name) throws CloudException, IOException {
// Not needed, can't be supported, provided only to satisfy GroupableResourceImpl's requirements
return null;
}
}
13 changes: 9 additions & 4 deletions azure/src/test/java/com/microsoft/azure/AzureTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,15 @@ public void setup() throws Exception {
* @throws Exception
*/
@Test public void testGenericResources() throws Exception {
azure.genericResources().listByGroup("sdkpriv");
GenericResource resourceByGroup = azure.genericResources().getByGroup("sdkpriv", "sdkpriv");
GenericResource resourceById = azure.genericResources().getById(resourceByGroup.id());
Assert.assertTrue(resourceById.id().equalsIgnoreCase(resourceByGroup.id()));
PagedList<GenericResource> resources = azure.genericResources().listByGroup("sdkpriv");
GenericResource firstResource = resources.get(0);
GenericResource resourceById = azure.genericResources().getById(firstResource.id());
GenericResource resourceByDetails = azure.genericResources().get(
firstResource.resourceGroupName(),
firstResource.resourceProviderNamespace(),
firstResource.resourceType(),
firstResource.name());
Assert.assertTrue(resourceById.id().equalsIgnoreCase(resourceByDetails.id()));
}

/**
Expand Down

0 comments on commit 0fdd748

Please sign in to comment.