From 27e312c9b33e10732bc1ddc07012e0d37c8f594f Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Mon, 24 Oct 2016 23:40:50 -0700 Subject: [PATCH 1/2] Support beginDelete and extend generic resources --- .../resources/GenericResources.java | 6 ++- .../management/resources/ResourceGroups.java | 2 + .../collection/SupportsBeginDeleting.java | 47 +++++++++++++++++++ .../implementation/GenericResourcesImpl.java | 28 +++++++++++ .../implementation/ResourceGroupsImpl.java | 25 ++++++++++ .../resources/GenericResourcesTests.java | 7 ++- 6 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBeginDeleting.java 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 093ebedc635e..ac1ce938ac29 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 @@ -11,6 +11,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByGroup; 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.SupportsDeleting; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing; import java.util.List; @@ -24,7 +25,8 @@ public interface GenericResources extends SupportsListingByGroup, SupportsListingInGroupByTag, SupportsGettingById, - SupportsCreating { + SupportsCreating, + SupportsDeleting { /** * Checks if a resource exists in a resource group. * @@ -44,6 +46,8 @@ boolean checkExistence( String resourceName, String apiVersion); + boolean checkExistenceById(String id); + /** * Returns a resource belonging to a resource group. * 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 0c54ed0a63c3..48ae7d30b3ce 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 @@ -9,6 +9,7 @@ import com.microsoft.azure.management.apigeneration.Fluent; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByName; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsBatchCreation; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsBeginDeleting; 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; @@ -24,6 +25,7 @@ public interface ResourceGroups extends SupportsGettingByName, SupportsCreating, SupportsDeleting, + SupportsBeginDeleting, SupportsBatchCreation { /** * Checks whether resource group exists. diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBeginDeleting.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBeginDeleting.java new file mode 100644 index 000000000000..bd74f4e6915e --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBeginDeleting.java @@ -0,0 +1,47 @@ +/** + * 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.collection; + +import com.microsoft.azure.management.apigeneration.LangDefinition; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; +import rx.Observable; + +/** + * Provides access to deleting a resource from Azure, identifying it by its resource ID. + *

+ * (Note: this interface is not intended to be implemented by user code) + */ +@LangDefinition(ContainerName = "CollectionActions", CreateAsyncMethods = true) +public interface SupportsBeginDeleting { + /** + * Begins deleting a resource from Azure, identifying it by its resource ID. The + * resource will stay until get() returns null. + * + * @param id the resource ID of the resource to delete + */ + void beginDelete(String id); + + /** + * Asynchronously begins deleting a resource from Azure, identifying it by its resource ID. + * The resource will stay until get() returns null. + * + * @param id the resource ID of the resource to delete + * @param callback the callback on success or failure + * @return a handle to cancel the request + */ + ServiceCall beginDeleteAsync(String id, ServiceCallback callback); + + /** + * Asynchronously begins deleting a resource from Azure, identifying it by its resource ID. + * The resource will stay until get() returns null. + * + * @param id the resource ID of the resource to delete + * @return an observable of the request + */ + Observable beginDeleteAsync(String 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 6fb022b8a7fc..876b70c1cb50 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.fluentcore.arm.collection.implementation.GroupableResourcesImpl; import com.microsoft.azure.management.resources.fluentcore.utils.Utils; import rx.Observable; +import rx.functions.Func1; import java.util.List; @@ -75,6 +76,12 @@ public boolean checkExistence(String resourceGroupName, String resourceProviderN apiVersion); } + @Override + public boolean checkExistenceById(String id) { + String apiVersion = getApiVersionFromId(id).toBlocking().single(); + return innerCollection.checkExistenceById(id, apiVersion); + } + @Override public GenericResource getById(String id) { Provider provider = myManager.providers().getByName(ResourceUtils.resourceProviderFromResourceId(id)); @@ -193,4 +200,25 @@ public Observable deleteAsync(String groupName, String name) { // Not needed, can't be supported, provided only to satisfy GroupableResourceImpl's requirements throw new UnsupportedOperationException("Delete just by resource group and name is not supported. Please use other overloads."); } + + @Override + public Observable deleteAsync(final String id) { + return getApiVersionFromId(id) + .flatMap(new Func1>() { + @Override + public Observable call(String apiVersion) { + return innerCollection.deleteByIdAsync(id, apiVersion); + } + }); + } + + private Observable getApiVersionFromId(final String id) { + return myManager.providers().getByNameAsync(ResourceUtils.resourceProviderFromResourceId(id)) + .map(new Func1() { + @Override + public String call(Provider provider) { + return ResourceUtils.defaultApiVersion(id, provider); + } + }); + } } 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 4bb0003bdb25..e3f592f3d18f 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 @@ -11,7 +11,11 @@ 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 com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; +import com.microsoft.rest.ServiceResponse; import rx.Observable; +import rx.functions.Func1; /** * The implementation for {@link ResourceGroups} and its parent interfaces. @@ -76,4 +80,25 @@ protected ResourceGroupImpl wrapModel(ResourceGroupInner inner) { } return new ResourceGroupImpl(inner, serviceClient); } + + @Override + public void beginDelete(String id) { + beginDeleteAsync(id).toBlocking().subscribe(); + } + + @Override + public ServiceCall beginDeleteAsync(String id, ServiceCallback callback) { + return ServiceCall.create(beginDeleteAsync(id) + .flatMap(new Func1>>() { + @Override + public Observable> call(Void aVoid) { + return null; + } + }), callback); + } + + @Override + public Observable beginDeleteAsync(String name) { + return client.beginDeleteAsync(name); + } } diff --git a/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/GenericResourcesTests.java b/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/GenericResourcesTests.java index 2253cd1cce9e..222d420b26b0 100644 --- a/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/GenericResourcesTests.java +++ b/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/GenericResourcesTests.java @@ -35,8 +35,8 @@ public static void setup() throws Exception { @AfterClass public static void cleanup() throws Exception { - resourceGroups.delete(newRgName); - resourceGroups.delete(rgName); + resourceGroups.beginDelete(newRgName); + resourceGroups.beginDelete(rgName); } @Test @@ -72,5 +72,8 @@ public void canCreateUpdateMoveResource() throws Exception { resource.update() .withProperties(new ObjectMapper().readTree("{\"SiteMode\":\"Limited\",\"ComputeMode\":\"Dynamic\"}")) .apply(); + // Delete + genericResources.delete(resource.id()); + Assert.assertFalse(genericResources.checkExistence(newRgName, resource.resourceProviderNamespace(), resource.parentResourcePath(), resource.resourceType(), resource.name(), resource.apiVersion())); } } From d2c9fdba355758cf6190d27c9be5c8bf2dfe0e10 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Mon, 24 Oct 2016 23:43:05 -0700 Subject: [PATCH 2/2] missing javadoc on generic resources --- .../azure/management/resources/GenericResources.java | 6 ++++++ 1 file changed, 6 insertions(+) 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 ac1ce938ac29..becc866dd9b3 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 @@ -46,6 +46,12 @@ boolean checkExistence( String resourceName, String apiVersion); + /** + * Checks if a resource exists. + * + * @param id the ID of the resource. + * @return true if the resource exists; false otherwise + */ boolean checkExistenceById(String id); /**