From 5d51a233a268f098d09ba19bb4be79728d141953 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 1 Aug 2016 17:50:16 -0700 Subject: [PATCH] fix for issue https://github.com/Azure/azure-sdk-for-java/issues/998 - ability to uniquely get a generic resource in case of duplicate names in same resource group GenericResources#getByGroup() has been removed since it doesn't make sense on GenericResources Instead, a new overload for GenericResources#get() has been added, requiring the 4 params uniquely identifying a resource --- .../resources/GenericResources.java | 34 ++++++++-- .../GroupableResourcesImpl.java | 2 +- .../implementation/GenericResourcesImpl.java | 66 +++++++++++++++---- .../java/com/microsoft/azure/AzureTests.java | 13 ++-- 4 files changed, 93 insertions(+), 22 deletions(-) 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 2504b7c9312d3..d4ad3640329ef 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 @@ -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; @@ -20,7 +19,6 @@ */ public interface GenericResources extends SupportsListingByGroup, - SupportsGettingByGroup, SupportsGettingById, SupportsCreating { /** @@ -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. @@ -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. diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/GroupableResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/GroupableResourcesImpl.java index c4f266e3fb7ee..3e1abb830986a 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/GroupableResourcesImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/GroupableResourcesImpl.java @@ -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)); 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 37873bdae0e65..626ec3d84b7c2 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 @@ -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 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, @@ -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 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( @@ -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; + } } diff --git a/azure/src/test/java/com/microsoft/azure/AzureTests.java b/azure/src/test/java/com/microsoft/azure/AzureTests.java index b62fc83097a69..4d0e098cd11b5 100644 --- a/azure/src/test/java/com/microsoft/azure/AzureTests.java +++ b/azure/src/test/java/com/microsoft/azure/AzureTests.java @@ -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 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())); } /**