From 81fd582117a4a6378faeab9998ca801e0838a9ac Mon Sep 17 00:00:00 2001 From: anuchan Date: Mon, 8 Aug 2016 15:16:46 -0700 Subject: [PATCH 1/6] Exposing Create and CreateAsync in collection that takes variable number of Creatables --- .../management/compute/AvailabilitySets.java | 4 +- .../management/compute/VirtualMachines.java | 4 +- azure-mgmt-network/pom.xml | 6 + .../management/network/LoadBalancers.java | 4 +- .../management/network/NetworkInterfaces.java | 5 +- .../network/NetworkSecurityGroups.java | 4 +- .../azure/management/network/Networks.java | 4 +- .../management/network/PublicIpAddresses.java | 4 +- .../NetworkInterfaceOperationsTests.java | 105 +++++++++++ .../NetworkManagementTestBase.java | 32 ++++ .../management/resources/ResourceGroups.java | 4 +- .../CreatableResourcesImpl.java | 167 ++++++++++++++++++ .../GroupableResourcesImpl.java | 2 +- .../collection/SupportsBatchCreation.java | 36 ++++ .../model/BatchCreateOperationResult.java | 19 ++ .../implementation/ResourceGroupsImpl.java | 3 +- .../management/storage/StorageAccounts.java | 4 +- .../com/microsoft/azure/TaskGroupBase.java | 4 +- 18 files changed, 399 insertions(+), 12 deletions(-) create mode 100644 azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkInterfaceOperationsTests.java create mode 100644 azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkManagementTestBase.java create mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java create mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBatchCreation.java create mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/BatchCreateOperationResult.java diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/AvailabilitySets.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/AvailabilitySets.java index 9212e926d0f2..0b1ea8485e0f 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/AvailabilitySets.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/AvailabilitySets.java @@ -4,6 +4,7 @@ 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.SupportsBatchCreation; 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; @@ -18,5 +19,6 @@ public interface AvailabilitySets extends SupportsListing, SupportsCreating, SupportsDeleting, - SupportsDeletingByGroup { + SupportsDeletingByGroup, + SupportsBatchCreation { } diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachines.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachines.java index 0ebcb5d045e8..c6aed475629e 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachines.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachines.java @@ -5,6 +5,7 @@ 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.SupportsBatchCreation; 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; @@ -21,7 +22,8 @@ public interface VirtualMachines extends SupportsGettingById, SupportsCreating, SupportsDeleting, - SupportsDeletingByGroup { + SupportsDeletingByGroup, + SupportsBatchCreation { /** * @return entry point to virtual machine sizes diff --git a/azure-mgmt-network/pom.xml b/azure-mgmt-network/pom.xml index c18033a48316..0ea3ecac32d6 100644 --- a/azure-mgmt-network/pom.xml +++ b/azure-mgmt-network/pom.xml @@ -61,6 +61,12 @@ junit test + + com.microsoft.azure + azure-client-authentication + 1.0.0-SNAPSHOT + test + diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/LoadBalancers.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/LoadBalancers.java index 0518ad617d31..9d4f3ef831a9 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/LoadBalancers.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/LoadBalancers.java @@ -9,6 +9,7 @@ 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.SupportsBatchCreation; 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,5 +25,6 @@ public interface LoadBalancers extends SupportsGettingByGroup, SupportsGettingById, SupportsDeleting, - SupportsDeletingByGroup { + SupportsDeletingByGroup, + SupportsBatchCreation { } diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterfaces.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterfaces.java index 4e6f5c84b71c..3b67cbd2f806 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterfaces.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterfaces.java @@ -1,9 +1,11 @@ package com.microsoft.azure.management.network; +import com.microsoft.azure.management.resources.ResourceGroup; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsDeletingByGroup; 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.SupportsBatchCreation; 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; @@ -18,5 +20,6 @@ public interface NetworkInterfaces extends SupportsGettingByGroup, SupportsGettingById, SupportsDeleting, - SupportsDeletingByGroup { + SupportsDeletingByGroup, + SupportsBatchCreation { } diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkSecurityGroups.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkSecurityGroups.java index 2e5be3680c3d..dd6e6bde8998 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkSecurityGroups.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkSecurityGroups.java @@ -9,6 +9,7 @@ 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.SupportsBatchCreation; 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,5 +25,6 @@ public interface NetworkSecurityGroups extends SupportsGettingByGroup, SupportsGettingById, SupportsDeleting, - SupportsDeletingByGroup { + SupportsDeletingByGroup, + SupportsBatchCreation { } diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/Networks.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/Networks.java index 0d98140c9931..6795405c8b5b 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/Networks.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/Networks.java @@ -9,6 +9,7 @@ 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.SupportsBatchCreation; 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,5 +25,6 @@ public interface Networks extends SupportsGettingByGroup, SupportsGettingById, SupportsDeleting, - SupportsDeletingByGroup { + SupportsDeletingByGroup, + SupportsBatchCreation { } diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddresses.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddresses.java index df988c66cf6b..ae434425751c 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddresses.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddresses.java @@ -9,6 +9,7 @@ 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.SupportsBatchCreation; 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,5 +25,6 @@ public interface PublicIpAddresses extends SupportsListingByGroup, SupportsGettingByGroup, SupportsGettingById, - SupportsDeletingByGroup { + SupportsDeletingByGroup, + SupportsBatchCreation { } diff --git a/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkInterfaceOperationsTests.java b/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkInterfaceOperationsTests.java new file mode 100644 index 000000000000..54c66efa143e --- /dev/null +++ b/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkInterfaceOperationsTests.java @@ -0,0 +1,105 @@ +package com.microsoft.azure.management.network; + +import com.microsoft.azure.management.resources.ResourceGroup; +import com.microsoft.azure.management.resources.ResourceGroups; +import com.microsoft.azure.management.resources.fluentcore.arm.Region; +import com.microsoft.azure.management.resources.fluentcore.model.BatchCreateOperationResult; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import java.util.HashMap; +import java.util.LinkedHashMap; + +public class NetworkInterfaceOperationsTests extends NetworkManagementTestBase { + private static String RG_NAME = "javanwmrg"; + private static String resourceGroupId = null; + + @BeforeClass + public static void setup() throws Exception { + createClients(); + } + + @AfterClass + public static void cleanup() throws Exception { + if (resourceGroupId != null) { + resourceManager.resourceGroups().delete(resourceGroupId); + } + } + + @Test + public void canCreateBatchOfNetworkInterfaces() throws Exception { + ResourceGroups resourceGroups = resourceManager.resourceGroups(); + Networks networks = networkManager.networks(); + NetworkInterfaces networkInterfaces = networkManager.networkInterfaces(); + + Creatable resourceGroupCreatable = resourceGroups + .define(RG_NAME) + .withRegion(Region.US_EAST); + + final String vnetName = "vnet1212"; + Creatable networkCreatable = networks + .define(vnetName) + .withRegion(Region.US_EAST) + .withNewResourceGroup(resourceGroupCreatable) + .withAddressSpace("10.0.0.0/28"); + + // Prepare a batch of nics + // + final String nic1Name = "nic1"; + Creatable networkInterface1Creatable = networkInterfaces + .define(nic1Name) + .withRegion(Region.US_EAST) + .withNewResourceGroup(resourceGroupCreatable) + .withNewPrimaryNetwork(networkCreatable) + .withPrimaryPrivateIpAddressStatic("10.0.0.5"); + + final String nic2Name = "nic2"; + Creatable networkInterface2Creatable = networkInterfaces + .define(nic2Name) + .withRegion(Region.US_EAST) + .withNewResourceGroup(resourceGroupCreatable) + .withNewPrimaryNetwork(networkCreatable) + .withPrimaryPrivateIpAddressStatic("10.0.0.6"); + + final String nic3Name = "nic3"; + Creatable networkInterface3Creatable = networkInterfaces + .define(nic3Name) + .withRegion(Region.US_EAST) + .withNewResourceGroup(resourceGroupCreatable) + .withNewPrimaryNetwork(networkCreatable) + .withPrimaryPrivateIpAddressStatic("10.0.0.7"); + + final String nic4Name = "nic4"; + Creatable networkInterface4Creatable = networkInterfaces + .define(nic4Name) + .withRegion(Region.US_EAST) + .withNewResourceGroup(resourceGroupCreatable) + .withNewPrimaryNetwork(networkCreatable) + .withPrimaryPrivateIpAddressStatic("10.0.0.8"); + + BatchCreateOperationResult batchNics = networkInterfaces.create(networkInterface1Creatable, + networkInterface2Creatable, + networkInterface3Creatable, + networkInterface4Creatable); + + Assert.assertTrue(batchNics.resources().size() == 4); + HashMap found = new LinkedHashMap<>(); + for(NetworkInterface nic : batchNics.resources()) { + if (nic.name().equalsIgnoreCase(nic1Name)) { + found.put(nic1Name, true); + } + if (nic.name().equalsIgnoreCase(nic2Name)) { + found.put(nic2Name, true); + } + if (nic.name().equalsIgnoreCase(nic3Name)) { + found.put(nic3Name, true); + } + if (nic.name().equalsIgnoreCase(nic4Name)) { + found.put(nic4Name, true); + } + } + Assert.assertTrue(found.size() == 4); + } +} diff --git a/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkManagementTestBase.java b/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkManagementTestBase.java new file mode 100644 index 000000000000..b9995111b0de --- /dev/null +++ b/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkManagementTestBase.java @@ -0,0 +1,32 @@ +package com.microsoft.azure.management.network; + +import com.microsoft.azure.AzureEnvironment; +import com.microsoft.azure.credentials.ApplicationTokenCredentials; +import com.microsoft.azure.management.network.implementation.NetworkManager; +import com.microsoft.azure.management.resources.implementation.ResourceManager; +import com.microsoft.azure.RestClient; + +public abstract class NetworkManagementTestBase { + protected static ResourceManager resourceManager; + protected static NetworkManager networkManager; + + public static void createClients() { + ApplicationTokenCredentials credentials = new ApplicationTokenCredentials( + System.getenv("client-id"), + System.getenv("domain"), + System.getenv("secret"), + null); + + RestClient restClient = AzureEnvironment.AZURE.newRestClientBuilder() + .withCredentials(credentials) + //.withLogLevel(HttpLoggingInterceptor.Level.BASIC) + .build(); + + resourceManager = ResourceManager + .authenticate(restClient) + .withSubscription(System.getenv("arm.subscription-id")); + + networkManager = NetworkManager + .authenticate(restClient, System.getenv("arm.subscription-id")); + } +} \ No newline at end of file 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 c56712079edb..a39a0253329b 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 @@ -8,6 +8,7 @@ import com.microsoft.azure.CloudException; 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.SupportsCreating; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing; @@ -21,7 +22,8 @@ public interface ResourceGroups extends SupportsListing, SupportsGettingByName, SupportsCreating, - SupportsDeleting { + SupportsDeleting, + SupportsBatchCreation { /** * Checks whether resource group exists. * diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java new file mode 100644 index 000000000000..537f5091a835 --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java @@ -0,0 +1,167 @@ +package com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation; + +import com.microsoft.azure.management.resources.fluentcore.arm.Region; +import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsBatchCreation; +import com.microsoft.azure.management.resources.fluentcore.model.BatchCreateOperationResult; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableImpl; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; +import com.microsoft.rest.ServiceResponse; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * Base class for creatable resource collection, i.e. those where the member of the collection is of Resource + * type {@link com.microsoft.azure.Resource } and are creatable. + * (Internal use only) + * + * @param the individual resource type returned + * @param the individual resource implementation + * @param the wrapper inner type + */ +public abstract class CreatableResourcesImpl + extends CreatableWrappersImpl + implements SupportsBatchCreation { + + /** + * the root resource that groups batch of creatable resources in the collection. + */ + private BatchRootResourceImpl rootResource; + + protected CreatableResourcesImpl() { + } + + @Override + public BatchCreateOperationResult create(Creatable creatable, Creatable ... creatables) throws Exception { + rootResource = new BatchRootResourceImpl<>(); // Creates a new batchResource each batch + rootResource.addCreatableDependencies(creatable, creatables); + rootResource.create(); + return new BatchCreateOperationResultImpl<>(rootResource); + } + + @Override + public ServiceCall createAsync(final ServiceCallback> callback, Creatable creatable, Creatable ... creatables) { + rootResource = new BatchRootResourceImpl<>(); // Creates a new batchResource each batch + rootResource.addCreatableDependencies(creatable, creatables); + return rootResource.createAsync(new ServiceCallback>() { + @Override + public void failure(Throwable t) { + callback.failure((t)); + } + + @Override + public void success(ServiceResponse> result) { + callback.success(new ServiceResponse>(new BatchCreateOperationResultImpl<>(result.getBody()), null)); + } + }); + } + + /** + * Implements {@link BatchCreateOperationResult}. + * @param the type of the resources in the batch. + */ + private class BatchCreateOperationResultImpl implements BatchCreateOperationResult { + private BatchRootResource batchRootResource; + + BatchCreateOperationResultImpl(BatchRootResource localResource) { + this.batchRootResource = localResource; + } + + @Override + public List resources() { + return batchRootResource.resources(); + } + } + + /** + * The local dummy root resource for the batch of creatable resources in a batch. + * + * @param the type of the resources in the batch. + */ + interface BatchRootResource extends Resource { + List resources(); + } + + /** + * Implementation of {@link BatchRootResource}. + * + * @param the type of the resources in the batch. + */ + private class BatchRootResourceImpl + extends CreatableImpl, Object, BatchRootResourceImpl, Resource> + implements BatchRootResource { + /** + * Collection of keys of top level resources in this batch. + */ + private List keys; + + BatchRootResourceImpl() { + super("BatchRootResource", null); + this.keys = new ArrayList<>(); + } + + @Override + public List resources() { + List resources = new ArrayList<>(); + for (String resourceKey : keys) { + resources.add((ResourceT) creatorTaskGroup().createdResource(resourceKey)); + } + return Collections.unmodifiableList(resources); + } + + void addCreatableDependencies(Creatable creatable, Creatable ... creatables) { + this.keys.add(creatable.key()); + this.addCreatableDependency((creatable)); + for (Creatable item : creatables) { + this.keys.add(item.key()); + this.addCreatableDependency((item)); + } + } + + @Override + public ServiceCall createResourceAsync(ServiceCallback serviceCallback) { + serviceCallback.success(new ServiceResponse(this, null)); + return null; + } + + @Override + public Resource createResource() throws Exception { + return this; + } + + @Override + public BatchRootResource refresh() throws Exception { + return null; + } + + @Override + public String id() { + return null; + } + + @Override + public String type() { + return null; + } + + @Override + public String regionName() { + return null; + } + + @Override + public Region region() { + return null; + } + + @Override + public Map tags() { + return null; + } + } +} \ No newline at end of file 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 3e1abb830986..3cb20c9a11e6 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 @@ -30,7 +30,7 @@ public abstract class GroupableResourcesImpl< InnerT extends Resource, InnerCollectionT, ManagerT extends ManagerBase> - extends CreatableWrappersImpl + extends CreatableResourcesImpl implements SupportsGettingById, SupportsGettingByGroup { diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBatchCreation.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBatchCreation.java new file mode 100644 index 000000000000..0eac9c2ff0f8 --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBatchCreation.java @@ -0,0 +1,36 @@ +package com.microsoft.azure.management.resources.fluentcore.collection; + + +import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; +import com.microsoft.azure.management.resources.fluentcore.model.BatchCreateOperationResult; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; + +/** + * Providing access to creating a batch of Azure top level resources of same type. + *

+ * (Note: this interface is not intended to be implemented by user code) + * @param the top level Azure resource type + */ +public interface SupportsBatchCreation { + /** + * Executes the create requests on a collection of resources. + * + * @param creatable the first creatable in the batch + * @param creatables the rest of the creatables in the batch + * @return the batch resource from which created resources in this batch can be accessed. + * @throws Exception exceptions from Azure + */ + BatchCreateOperationResult create(Creatable creatable, Creatable... creatables) throws Exception; + + /** + * Puts the request into the queue and allow the HTTP client to execute it when system resources are available. + * + * @param callback the callback to handle success and failure + * @param creatable the first creatable in the batch + * @param creatables the rest of the creatables in the batch + * @return a handle to cancel the request + */ + ServiceCall createAsync(ServiceCallback> callback, Creatable creatable, Creatable... creatables); +} \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/BatchCreateOperationResult.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/BatchCreateOperationResult.java new file mode 100644 index 000000000000..b37ea28b483f --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/BatchCreateOperationResult.java @@ -0,0 +1,19 @@ +package com.microsoft.azure.management.resources.fluentcore.model; + + +import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; + +import java.util.List; + +/** + * Represents result of batch of create operations. + *

+ * (Note: this interface is not intended to be implemented by user code) + * @param the type of the resource in this batch. + */ +public interface BatchCreateOperationResult { + /** + * @return the collection of created resources in this batch. + */ + List resources(); +} 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 a93dfe05db87..79b3e2457599 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 @@ -9,6 +9,7 @@ import com.microsoft.azure.CloudException; import com.microsoft.azure.PagedList; 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.arm.collection.implementation.CreatableWrappersImpl; import com.microsoft.azure.management.resources.ResourceGroup; @@ -18,7 +19,7 @@ * The implementation for {@link ResourceGroups} and its parent interfaces. */ final class ResourceGroupsImpl - extends CreatableWrappersImpl + extends CreatableResourcesImpl implements ResourceGroups { private final ResourceGroupsInner client; private final ResourceManagementClientImpl serviceClient; diff --git a/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/StorageAccounts.java b/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/StorageAccounts.java index 7c3314b54c51..0e8804e86927 100644 --- a/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/StorageAccounts.java +++ b/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/StorageAccounts.java @@ -11,6 +11,7 @@ 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.SupportsBatchCreation; 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; @@ -27,7 +28,8 @@ public interface StorageAccounts extends SupportsListingByGroup, SupportsGettingByGroup, SupportsGettingById, - SupportsDeletingByGroup { + SupportsDeletingByGroup, + SupportsBatchCreation { /** * Checks that account name is valid and is not in use. * diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroupBase.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroupBase.java index c7b982a8f098..8a5547cac373 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroupBase.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroupBase.java @@ -162,7 +162,9 @@ public boolean isCancelled() { * @param call the call */ private void addCall(ServiceCall call) { - this.serviceCalls.add(call); + if (call != null) { + this.serviceCalls.add(call); + } } } } From e136d7036ce68bae27bf1dc2aab9f7a3a37bdb6e Mon Sep 17 00:00:00 2001 From: anuchan Date: Mon, 8 Aug 2016 15:59:28 -0700 Subject: [PATCH 2/6] Removing unused import --- .../management/resources/implementation/ResourceGroupsImpl.java | 1 - 1 file changed, 1 deletion(-) 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 79b3e2457599..bb3202f8177e 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 @@ -10,7 +10,6 @@ import com.microsoft.azure.PagedList; 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.arm.collection.implementation.CreatableWrappersImpl; import com.microsoft.azure.management.resources.ResourceGroup; import java.io.IOException; From a8c3e1326beed1f2aaea041e3f19e7d87c058ae9 Mon Sep 17 00:00:00 2001 From: anuchan Date: Mon, 8 Aug 2016 16:13:08 -0700 Subject: [PATCH 3/6] Removing more unused imports --- .../microsoft/azure/management/network/NetworkInterfaces.java | 1 - 1 file changed, 1 deletion(-) diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterfaces.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterfaces.java index 3b67cbd2f806..96e726f9f406 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterfaces.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterfaces.java @@ -1,6 +1,5 @@ package com.microsoft.azure.management.network; -import com.microsoft.azure.management.resources.ResourceGroup; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsDeletingByGroup; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByGroup; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingById; From 874b875d0df70ebf9a32248e457f35180f3a8a14 Mon Sep 17 00:00:00 2001 From: anuchan Date: Mon, 8 Aug 2016 18:21:16 -0700 Subject: [PATCH 4/6] Use CreateAsync in Create and couple of renames based on internal discussions --- .../NetworkInterfaceOperationsTests.java | 8 +- .../CreatableResourcesImpl.java | 236 +++++++++++++++--- .../collection/SupportsBatchCreation.java | 19 +- ...ationResult.java => CreatedResources.java} | 9 +- 4 files changed, 225 insertions(+), 47 deletions(-) rename azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/{BatchCreateOperationResult.java => CreatedResources.java} (59%) diff --git a/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkInterfaceOperationsTests.java b/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkInterfaceOperationsTests.java index 54c66efa143e..08f965e201f7 100644 --- a/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkInterfaceOperationsTests.java +++ b/azure-mgmt-network/src/test/java/com.microsoft.azure.management.network/NetworkInterfaceOperationsTests.java @@ -3,7 +3,7 @@ import com.microsoft.azure.management.resources.ResourceGroup; import com.microsoft.azure.management.resources.ResourceGroups; import com.microsoft.azure.management.resources.fluentcore.arm.Region; -import com.microsoft.azure.management.resources.fluentcore.model.BatchCreateOperationResult; +import com.microsoft.azure.management.resources.fluentcore.model.CreatedResources; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import org.junit.AfterClass; import org.junit.Assert; @@ -79,14 +79,14 @@ public void canCreateBatchOfNetworkInterfaces() throws Exception { .withNewPrimaryNetwork(networkCreatable) .withPrimaryPrivateIpAddressStatic("10.0.0.8"); - BatchCreateOperationResult batchNics = networkInterfaces.create(networkInterface1Creatable, + CreatedResources batchNics = networkInterfaces.create(networkInterface1Creatable, networkInterface2Creatable, networkInterface3Creatable, networkInterface4Creatable); - Assert.assertTrue(batchNics.resources().size() == 4); + Assert.assertTrue(batchNics.size() == 4); HashMap found = new LinkedHashMap<>(); - for(NetworkInterface nic : batchNics.resources()) { + for(NetworkInterface nic : batchNics) { if (nic.name().equalsIgnoreCase(nic1Name)) { found.put(nic1Name, true); } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java index 537f5091a835..f0d4372663ca 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java @@ -3,7 +3,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.Region; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsBatchCreation; -import com.microsoft.azure.management.resources.fluentcore.model.BatchCreateOperationResult; +import com.microsoft.azure.management.resources.fluentcore.model.CreatedResources; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableImpl; import com.microsoft.rest.ServiceCall; @@ -11,8 +11,11 @@ import com.microsoft.rest.ServiceResponse; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.Map; /** @@ -37,44 +40,155 @@ protected CreatableResourcesImpl() { } @Override - public BatchCreateOperationResult create(Creatable creatable, Creatable ... creatables) throws Exception { - rootResource = new BatchRootResourceImpl<>(); // Creates a new batchResource each batch - rootResource.addCreatableDependencies(creatable, creatables); - rootResource.create(); - return new BatchCreateOperationResultImpl<>(rootResource); + public CreatedResources create(Creatable ... creatables) throws Exception { + ServiceCall> serviceCall = createAsync(null, creatables); + serviceCall.wait(); + return serviceCall.get().getBody(); } @Override - public ServiceCall createAsync(final ServiceCallback> callback, Creatable creatable, Creatable ... creatables) { - rootResource = new BatchRootResourceImpl<>(); // Creates a new batchResource each batch - rootResource.addCreatableDependencies(creatable, creatables); - return rootResource.createAsync(new ServiceCallback>() { - @Override - public void failure(Throwable t) { - callback.failure((t)); - } + public ServiceCall> createAsync(final ServiceCallback> callback, Creatable ... creatables) { + rootResource = new BatchRootResourceImpl<>(); // Imp: Creates a new BatchRootResourceImpl for each batch + rootResource.addCreatableDependencies(creatables); - @Override - public void success(ServiceResponse> result) { - callback.success(new ServiceResponse>(new BatchCreateOperationResultImpl<>(result.getBody()), null)); - } - }); + final BatchServiceCall batchServiceCall = new BatchServiceCall(); + ServiceCall> serviceCall = rootResource.createAsync(batchServiceCall.wrapCallback(callback)); + batchServiceCall.setInnerServiceCall(serviceCall); + return batchServiceCall; } /** - * Implements {@link BatchCreateOperationResult}. + * Implements {@link CreatedResources}. * @param the type of the resources in the batch. */ - private class BatchCreateOperationResultImpl implements BatchCreateOperationResult { + private class CreatedResourcesImpl implements CreatedResources { private BatchRootResource batchRootResource; - BatchCreateOperationResultImpl(BatchRootResource localResource) { - this.batchRootResource = localResource; + private final List list; + + CreatedResourcesImpl(BatchRootResource batchRootResource) { + this.batchRootResource = batchRootResource; + this.list = this.batchRootResource.createdTopLevelResources(); + } + + @Override + public Resource createdRelatedResource(String key) { + return this.batchRootResource.createdRelatedResource(key); + } + + @Override + public int size() { + return list.size(); + } + + @Override + public boolean isEmpty() { + return list.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return list.contains(o); + } + + @Override + public Iterator iterator() { + return list.iterator(); + } + + @Override + public Object[] toArray() { + return list.toArray(); + } + + @Override + public T[] toArray(T[] a) { + return list.toArray(a); + } + + @Override + public boolean add(ResourceT resourceT) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean containsAll(Collection c) { + return list.containsAll(c); + } + + @Override + public boolean addAll(Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(int index, Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + + @Override + public ResourceT get(int index) { + return this.list.get(index); } @Override - public List resources() { - return batchRootResource.resources(); + public ResourceT set(int index, ResourceT element) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, ResourceT element) { + throw new UnsupportedOperationException(); + } + + @Override + public ResourceT remove(int index) { + throw new UnsupportedOperationException(); + } + + @Override + public int indexOf(Object o) { + return this.list.indexOf(o); + } + + @Override + public int lastIndexOf(Object o) { + return this.list.lastIndexOf(o); + } + + @Override + public ListIterator listIterator() { + return this.list.listIterator(); + } + + @Override + public ListIterator listIterator(int index) { + return this.list.listIterator(index); + } + + @Override + public List subList(int fromIndex, int toIndex) { + return this.list.subList(fromIndex, toIndex); } } @@ -84,7 +198,8 @@ public List resources() { * @param the type of the resources in the batch. */ interface BatchRootResource extends Resource { - List resources(); + List createdTopLevelResources(); + Resource createdRelatedResource(String key); } /** @@ -106,7 +221,7 @@ private class BatchRootResourceImpl } @Override - public List resources() { + public List createdTopLevelResources() { List resources = new ArrayList<>(); for (String resourceKey : keys) { resources.add((ResourceT) creatorTaskGroup().createdResource(resourceKey)); @@ -114,9 +229,12 @@ public List resources() { return Collections.unmodifiableList(resources); } - void addCreatableDependencies(Creatable creatable, Creatable ... creatables) { - this.keys.add(creatable.key()); - this.addCreatableDependency((creatable)); + @Override + public Resource createdRelatedResource(String key) { + return (Resource) creatorTaskGroup().createdResource(key); + } + + void addCreatableDependencies(Creatable ... creatables) { for (Creatable item : creatables) { this.keys.add(item.key()); this.addCreatableDependency((item)); @@ -164,4 +282,62 @@ public Map tags() { return null; } } + + /** + * Represents a collection of in-progress service calls in a batch. + */ + private class BatchServiceCall extends ServiceCall> { + private ServiceCall> innerServiceCall; + + /** + * Creates BatchServiceCall. + */ + BatchServiceCall() { + super(null); + } + + /** + * Sets the inner service call. + * + * @param inner the service call to wrap + */ + public void setInnerServiceCall(ServiceCall> inner) { + this.innerServiceCall = inner; + } + + /** + * Cancels all the service calls currently executing. + */ + public void cancel() { + this.innerServiceCall.cancel(); + } + + /** + * @return true if the call has been canceled; false otherwise. + */ + public boolean isCancelled() { + return this.innerServiceCall.isCancelled(); + } + + ServiceCallback> wrapCallback(final ServiceCallback> innerCallback) { + final BatchServiceCall self = this; + return new ServiceCallback>() { + @Override + public void failure(Throwable t) { + self.failure(t); + if (innerCallback != null) { + innerCallback.failure((t)); + } + } + + @Override + public void success(ServiceResponse> result) { + self.success(new ServiceResponse>(new CreatedResourcesImpl<>(result.getBody()), null)); + if (innerCallback != null) { + innerCallback.success(new ServiceResponse>(new CreatedResourcesImpl<>(result.getBody()), null)); + } + } + }; + } + } } \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBatchCreation.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBatchCreation.java index 0eac9c2ff0f8..336d1bdac1db 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBatchCreation.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/collection/SupportsBatchCreation.java @@ -2,7 +2,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; -import com.microsoft.azure.management.resources.fluentcore.model.BatchCreateOperationResult; +import com.microsoft.azure.management.resources.fluentcore.model.CreatedResources; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; @@ -15,22 +15,21 @@ */ public interface SupportsBatchCreation { /** - * Executes the create requests on a collection of resources. + * Executes the create requests on a collection (batch) of resources. * - * @param creatable the first creatable in the batch - * @param creatables the rest of the creatables in the batch - * @return the batch resource from which created resources in this batch can be accessed. + * @param creatables the creatables in the batch + * @return the batch operation result from which created resources in this batch can be accessed. * @throws Exception exceptions from Azure */ - BatchCreateOperationResult create(Creatable creatable, Creatable... creatables) throws Exception; + CreatedResources create(Creatable... creatables) throws Exception; /** - * Puts the request into the queue and allow the HTTP client to execute it when system resources are available. + * Puts the requests to create a batch of resources into the queue and allow the HTTP client to execute it when + * system resources are available. * * @param callback the callback to handle success and failure - * @param creatable the first creatable in the batch - * @param creatables the rest of the creatables in the batch + * @param creatables the creatables in the batch * @return a handle to cancel the request */ - ServiceCall createAsync(ServiceCallback> callback, Creatable creatable, Creatable... creatables); + ServiceCall> createAsync(ServiceCallback> callback, Creatable... creatables); } \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/BatchCreateOperationResult.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/CreatedResources.java similarity index 59% rename from azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/BatchCreateOperationResult.java rename to azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/CreatedResources.java index b37ea28b483f..3e9487098a6d 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/BatchCreateOperationResult.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/CreatedResources.java @@ -11,9 +11,12 @@ * (Note: this interface is not intended to be implemented by user code) * @param the type of the resource in this batch. */ -public interface BatchCreateOperationResult { +public interface CreatedResources extends List { /** - * @return the collection of created resources in this batch. + * Gets a created resource with the given key. + * + * @param key the key of the resource + * @return the created resource */ - List resources(); + Resource createdRelatedResource(String key); } From 9eef694d0029611d29b73cd69aab3aaa2dfd0b99 Mon Sep 17 00:00:00 2001 From: anuchan Date: Mon, 8 Aug 2016 21:27:05 -0700 Subject: [PATCH 5/6] Using better names for inner types used in CreatableResourcesImpl --- .../CreatableResourcesImpl.java | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java index f0d4372663ca..adf0b751c3a5 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java @@ -31,11 +31,6 @@ public abstract class CreatableResourcesImpl implements SupportsBatchCreation { - /** - * the root resource that groups batch of creatable resources in the collection. - */ - private BatchRootResourceImpl rootResource; - protected CreatableResourcesImpl() { } @@ -47,14 +42,15 @@ public CreatedResources create(Creatable ... creatables) throws Exception } @Override - public ServiceCall> createAsync(final ServiceCallback> callback, Creatable ... creatables) { - rootResource = new BatchRootResourceImpl<>(); // Imp: Creates a new BatchRootResourceImpl for each batch + public ServiceCall> createAsync(final ServiceCallback> callback, + Creatable ... creatables) { + CreatableResourcesRootImpl rootResource = new CreatableResourcesRootImpl<>(); rootResource.addCreatableDependencies(creatables); - final BatchServiceCall batchServiceCall = new BatchServiceCall(); - ServiceCall> serviceCall = rootResource.createAsync(batchServiceCall.wrapCallback(callback)); - batchServiceCall.setInnerServiceCall(serviceCall); - return batchServiceCall; + final CreateResourcesServiceCall createResourcesServiceCall = new CreateResourcesServiceCall(); + ServiceCall> serviceCall = rootResource.createAsync(createResourcesServiceCall.wrapCallback(callback)); + createResourcesServiceCall.setInnerServiceCall(serviceCall); + return createResourcesServiceCall; } /** @@ -62,18 +58,17 @@ public ServiceCall> createAsync(final ServiceCallback the type of the resources in the batch. */ private class CreatedResourcesImpl implements CreatedResources { - private BatchRootResource batchRootResource; - + private CreatableResourcesRoot creatableResourcesRoot; private final List list; - CreatedResourcesImpl(BatchRootResource batchRootResource) { - this.batchRootResource = batchRootResource; - this.list = this.batchRootResource.createdTopLevelResources(); + CreatedResourcesImpl(CreatableResourcesRoot creatableResourcesRoot) { + this.creatableResourcesRoot = creatableResourcesRoot; + this.list = this.creatableResourcesRoot.createdTopLevelResources(); } @Override public Resource createdRelatedResource(String key) { - return this.batchRootResource.createdRelatedResource(key); + return this.creatableResourcesRoot.createdRelatedResource(key); } @Override @@ -192,31 +187,32 @@ public List subList(int fromIndex, int toIndex) { } } - /** - * The local dummy root resource for the batch of creatable resources in a batch. + /** + * The local root resource that is used as dummy parent resource for the batch creatable resources + * added via {@link CreatableResourcesImpl#create} or {@link CreatableResourcesImpl#createAsync}. * * @param the type of the resources in the batch. */ - interface BatchRootResource extends Resource { + interface CreatableResourcesRoot extends Resource { List createdTopLevelResources(); Resource createdRelatedResource(String key); } /** - * Implementation of {@link BatchRootResource}. + * Implementation of {@link CreatableResourcesRoot}. * * @param the type of the resources in the batch. */ - private class BatchRootResourceImpl - extends CreatableImpl, Object, BatchRootResourceImpl, Resource> - implements BatchRootResource { + private class CreatableResourcesRootImpl + extends CreatableImpl, Object, CreatableResourcesRootImpl, Resource> + implements CreatableResourcesRoot { /** * Collection of keys of top level resources in this batch. */ private List keys; - BatchRootResourceImpl() { - super("BatchRootResource", null); + CreatableResourcesRootImpl() { + super("CreatableResourcesRoot", null); this.keys = new ArrayList<>(); } @@ -252,8 +248,12 @@ public Resource createResource() throws Exception { return this; } + // Below overrides returns null as this is not a real resource in Azure + // but a dummy resource representing parent of a batch of creatable Azure + // resources. + @Override - public BatchRootResource refresh() throws Exception { + public CreatableResourcesRoot refresh() throws Exception { return null; } @@ -284,15 +284,15 @@ public Map tags() { } /** - * Represents a collection of in-progress service calls in a batch. + * Represents a collection of in-progress Create service calls in the batch. */ - private class BatchServiceCall extends ServiceCall> { - private ServiceCall> innerServiceCall; + private class CreateResourcesServiceCall extends ServiceCall> { + private ServiceCall> innerServiceCall; /** - * Creates BatchServiceCall. + * Creates CreateResourcesServiceCall. */ - BatchServiceCall() { + CreateResourcesServiceCall() { super(null); } @@ -301,7 +301,7 @@ private class BatchServiceCall extends ServiceCall> { * * @param inner the service call to wrap */ - public void setInnerServiceCall(ServiceCall> inner) { + public void setInnerServiceCall(ServiceCall> inner) { this.innerServiceCall = inner; } @@ -319,9 +319,9 @@ public boolean isCancelled() { return this.innerServiceCall.isCancelled(); } - ServiceCallback> wrapCallback(final ServiceCallback> innerCallback) { - final BatchServiceCall self = this; - return new ServiceCallback>() { + ServiceCallback> wrapCallback(final ServiceCallback> innerCallback) { + final CreateResourcesServiceCall self = this; + return new ServiceCallback>() { @Override public void failure(Throwable t) { self.failure(t); @@ -331,7 +331,7 @@ public void failure(Throwable t) { } @Override - public void success(ServiceResponse> result) { + public void success(ServiceResponse> result) { self.success(new ServiceResponse>(new CreatedResourcesImpl<>(result.getBody()), null)); if (innerCallback != null) { innerCallback.success(new ServiceResponse>(new CreatedResourcesImpl<>(result.getBody()), null)); From d25b4a0c08e46c6e1df82bbe79d6d66c1cb562a7 Mon Sep 17 00:00:00 2001 From: anuchan Date: Tue, 9 Aug 2016 08:11:18 -0700 Subject: [PATCH 6/6] Removing trailing space --- .../arm/collection/implementation/CreatableResourcesImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java index adf0b751c3a5..70685b4ba576 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java @@ -251,7 +251,7 @@ public Resource createResource() throws Exception { // Below overrides returns null as this is not a real resource in Azure // but a dummy resource representing parent of a batch of creatable Azure // resources. - + @Override public CreatableResourcesRoot refresh() throws Exception { return null;