From 905e11f44201176794b097861f379588ccb9aad0 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Tue, 14 Jun 2016 17:03:29 -0700 Subject: [PATCH 1/5] Basic prototyping of async in fluent --- .../implementation/AvailabilitySetImpl.java | 26 +++++ .../implementation/VirtualMachineImpl.java | 99 +++++++++++++++++++ .../network/implementation/NetworkImpl.java | 49 +++++++-- .../implementation/NetworkInterfaceImpl.java | 27 +++++ .../NetworkSecurityGroupImpl.java | 33 ++++++- .../implementation/PublicIpAddressImpl.java | 24 +++++ .../resources/fluentcore/model/Creatable.java | 12 +++ .../model/implementation/CreatableImpl.java | 21 +++- .../implementation/CreatableTaskGroup.java | 19 +++- .../implementation/CreatableTaskItem.java | 34 ++++++- .../IndexableRefreshableWrapperImpl.java | 8 +- .../resources/fluentcore/utils/Utils.java | 55 +++++++++++ .../implementation/DeploymentImpl.java | 65 ++++++++++++ .../implementation/GenericResourceImpl.java | 21 ++++ .../implementation/ResourceGroupImpl.java | 18 +++- .../implementation/StorageAccountImpl.java | 29 ++++++ .../com/microsoft/azure/TestTemplate.java | 9 +- .../microsoft/azure/TestVirtualMachine.java | 49 ++++++++- .../java/com/microsoft/azure/TaskGroup.java | 16 +++ .../com/microsoft/azure/TaskGroupBase.java | 71 +++++++++---- .../java/com/microsoft/azure/TaskItem.java | 9 +- 21 files changed, 644 insertions(+), 50 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java index 3f475f65d74f..0a3f893515aa 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java @@ -16,6 +16,9 @@ import com.microsoft.azure.management.resources.ResourceGroups; import com.microsoft.azure.management.resources.fluentcore.arm.ResourceLazyList; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +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 java.io.IOException; @@ -118,6 +121,11 @@ public AvailabilitySetImpl create() throws Exception { return this; } + @Override + public ServiceCall createAsync(ServiceCallback callback) { + return super.creatablesCreateAsync(Utils.toVoidCallback(this, callback)); + } + @Override public AvailabilitySetImpl update() throws Exception { return this; @@ -136,4 +144,22 @@ protected void createResource() throws Exception { this.idOfVMsInSet = null; this.vmsInSet = null; } + + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + return this.client.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), + Utils.fromVoidCallback(this, new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + idOfVMsInSet = null; + vmsInSet = null; + callback.success(result); + } + })); + } } diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java index e6ed84771491..f495de8b3c8a 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java @@ -49,6 +49,8 @@ import com.microsoft.azure.management.storage.StorageAccount; import com.microsoft.azure.management.storage.implementation.StorageManager; import com.microsoft.rest.RestException; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; import java.io.IOException; @@ -153,6 +155,11 @@ public VirtualMachine create() throws Exception { return this; } + @Override + public ServiceCall createAsync(ServiceCallback callback) { + return super.creatablesCreateAsync(Utils.toVoidCallback(this, callback)); + } + @Override public VirtualMachineImpl update() throws Exception { return this; @@ -824,6 +831,43 @@ protected void createResource() throws Exception { initializeDataDisks(); } + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + if (isInCreateMode()) { + setOSDiskAndOSProfileDefaults(); + setHardwareProfileDefaults(); + } + DataDiskImpl.setDataDisksDefaults(this.dataDisks, this.vmName); + final VirtualMachineImpl self = this; + final ServiceCall call = new ServiceCall(null); + handleStorageSettingsAsync(new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + handleNetworkSettings(); + handleAvailabilitySettings(); + call.newCall(client.createOrUpdateAsync(resourceGroupName(), vmName, inner(), + Utils.fromVoidCallback(self, new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + initializeDataDisks(); + callback.success(result); + } + })).getCall()); + } + }); + return call; + } + /**************************************************. * Helper methods **************************************************/ @@ -910,6 +954,61 @@ private void handleStorageSettings() throws Exception { } } + private void handleStorageSettingsAsync(final ServiceCallback callback) { + final ServiceCallback storageAccountServiceCallback = new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + if (isInCreateMode()) { + if (isOSDiskFromImage(inner().storageProfile().osDisk())) { + String uri = inner() + .storageProfile() + .osDisk().vhd().uri() + .replaceFirst("\\{storage-base-url}", result.getBody().endPoints().primary().blob()); + inner().storageProfile().osDisk().vhd().withUri(uri); + } + DataDiskImpl.ensureDisksVhdUri(dataDisks, result.getBody(), vmName); + } else { + if (result.getBody() != null) { + DataDiskImpl.ensureDisksVhdUri(dataDisks, result.getBody(), vmName); + } else { + DataDiskImpl.ensureDisksVhdUri(dataDisks, vmName); + } + } + callback.success(new ServiceResponse(result.getHeadResponse())); + } + }; + if (this.creatableStorageAccountKey != null) { + storageAccountServiceCallback.success(new ServiceResponse<>( + (StorageAccount) this.createdResource(this.creatableStorageAccountKey), null)); + } else if (this.existingStorageAccountToAssociate != null) { + storageAccountServiceCallback.success(new ServiceResponse<>( + this.existingStorageAccountToAssociate, null)); + } else if (osDiskRequiresImplicitStorageAccountCreation() + || dataDisksRequiresImplicitStorageAccountCreation()) { + this.storageManager.storageAccounts() + .define(nameWithPrefix("stg", null)) + .withRegion(this.region()) + .withExistingGroup(this.resourceGroupName()) + .createAsync(new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + storageAccountServiceCallback.success(result); + } + }); + } + + } + private void handleNetworkSettings() { if (isInCreateMode()) { NetworkInterface primaryNetworkInterface = null; diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java index ad18d1c3da66..6d78f516c44c 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java @@ -5,12 +5,6 @@ */ package com.microsoft.azure.management.network.implementation; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.network.Subnet; import com.microsoft.azure.management.network.implementation.api.SubnetInner; @@ -18,8 +12,17 @@ import com.microsoft.azure.management.network.implementation.api.VirtualNetworksInner; import com.microsoft.azure.management.resources.ResourceGroups; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +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 java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + /** * Internal virtual network implementation of the fluent interface. * (Internal use only) @@ -68,6 +71,11 @@ public NetworkImpl create() throws Exception { return this; } + @Override + public ServiceCall createAsync(ServiceCallback callback) { + return super.creatablesCreateAsync(Utils.toVoidCallback(this, callback)); + } + @Override public NetworkImpl update() throws Exception { return this; @@ -176,4 +184,33 @@ protected void createResource() throws Exception { this.setInner(response.getBody()); initializeSubnetsFromInner(); } + + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + // Ensure address spaces + if (this.addressSpaces().size() == 0) { + this.withAddressSpace("10.0.0.0/16"); + } + + if (isInCreateMode()) { + // Create a subnet as needed, covering the entire first address space + if (this.inner().subnets().size() == 0) { + this.withSubnet("subnet1", this.addressSpaces().get(0)); + } + } + + return this.client.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), + Utils.fromVoidCallback(this, new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + initializeSubnetsFromInner(); + callback.success(result); + } + })); + } } \ No newline at end of file diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java index fffddc417f89..90707231bac2 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java @@ -22,6 +22,8 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; 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 java.io.IOException; @@ -88,6 +90,11 @@ public NetworkInterfaceImpl create() throws Exception { return this; } + @Override + public ServiceCall createAsync(ServiceCallback callback) { + return this.createResourceAsync(Utils.toVoidCallback(this, callback)); + } + @Override public NetworkInterfaceImpl update() throws Exception { return this; @@ -309,6 +316,26 @@ protected void createResource() throws Exception { initializeNicIpConfigurations(); } + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + NicIpConfigurationImpl.ensureConfigurations(this.nicIpConfigurations); + return this.client.createOrUpdateAsync(this.resourceGroupName(), + this.nicName, + this.inner(), + Utils.fromVoidCallback(this, new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + initializeNicIpConfigurations(); + callback.success(result); + } + })); + } + /**************************************************. * Helper methods **************************************************/ diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java index 9902f4b0d665..8912d7a7b795 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java @@ -5,10 +5,6 @@ */ package com.microsoft.azure.management.network.implementation; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import com.microsoft.azure.management.network.NetworkSecurityGroup; import com.microsoft.azure.management.network.NetworkSecurityRule; import com.microsoft.azure.management.network.implementation.api.NetworkInterfaceInner; @@ -17,8 +13,15 @@ import com.microsoft.azure.management.network.implementation.api.SecurityRuleInner; import com.microsoft.azure.management.resources.ResourceGroups; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +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 java.util.ArrayList; +import java.util.Collections; +import java.util.List; + /** * Implementation of the NetworkSecurityGroup interface. * (Internal use only) @@ -76,6 +79,11 @@ public NetworkSecurityGroupImpl create() throws Exception { return this; } + @Override + public ServiceCall createAsync(ServiceCallback callback) { + return super.creatablesCreateAsync(Utils.toVoidCallback(this, callback)); + } + @Override public NetworkSecurityGroupImpl update() throws Exception { return this; @@ -94,6 +102,23 @@ protected void createResource() throws Exception { initializeRulesFromInner(); } + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + return this.client.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), + Utils.fromVoidCallback(this, new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + initializeRulesFromInner(); + callback.success(result); + } + })); + } + // Setters (fluent) diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java index 9f219af29ccb..2a6938b7c9ea 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java @@ -12,6 +12,9 @@ import com.microsoft.azure.management.network.implementation.api.PublicIPAddressesInner; import com.microsoft.azure.management.resources.ResourceGroups; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.utils.Utils; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; /** @@ -62,6 +65,11 @@ public PublicIpAddressImpl create() throws Exception { return this; } + @Override + public ServiceCall createAsync(ServiceCallback callback) { + return super.creatablesCreateAsync(Utils.toVoidCallback(this, callback)); + } + // Setters (fluent) @Override @@ -157,4 +165,20 @@ protected void createResource() throws Exception { this.client.createOrUpdate(this.resourceGroupName(), this.name(), this.inner()); this.setInner(response.getBody()); } + + @Override + protected ServiceCall createResourceAsync(ServiceCallback callback) { + // Clean up empty DNS settings + final PublicIPAddressDnsSettings dnsSettings = this.inner().dnsSettings(); + if (dnsSettings != null) { + if ((dnsSettings.domainNameLabel() == null || dnsSettings.domainNameLabel().isEmpty()) + && (dnsSettings.fqdn() == null || dnsSettings.fqdn().isEmpty()) + && (dnsSettings.reverseFqdn() == null || dnsSettings.reverseFqdn().isEmpty())) { + this.inner().withDnsSettings(null); + } + } + + return this.client.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), + Utils.fromVoidCallback(this, callback)); + } } \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Creatable.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Creatable.java index e73bf420f1de..b690ed3c1d58 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Creatable.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Creatable.java @@ -6,6 +6,9 @@ package com.microsoft.azure.management.resources.fluentcore.model; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; + /** * The final stage of the resource definition, at which it can be create, using {@link #create()}. * @@ -19,4 +22,13 @@ public interface Creatable extends Indexable { * @throws Exception exceptions from Azure */ T create() 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 + * @return a handle to cancel the request + */ + ServiceCall createAsync(ServiceCallback callback); } \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java index e747a7e3a12a..30743a4e0afe 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java @@ -8,6 +8,8 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; /** * The base class for all creatable resource. @@ -25,7 +27,7 @@ public abstract class CreatableImpl protected CreatableImpl(String name, InnerModelT innerObject) { super(name, innerObject); - creatableTaskGroup = new CreatableTaskGroup(name, (Creatable) this, this); + creatableTaskGroup = new CreatableTaskGroup(name, (Creatable) this, this); } /** @@ -38,12 +40,22 @@ protected CreatableImpl(String name, InnerModelT innerObject) { */ protected void creatablesCreate() throws Exception { if (creatableTaskGroup.isRoot()) { + creatableTaskGroup.prepare(); creatableTaskGroup.execute(); } else { createResource(); } } + protected ServiceCall creatablesCreateAsync(ServiceCallback callback) { + if (creatableTaskGroup.isRoot()) { + creatableTaskGroup.prepare(); + return creatableTaskGroup.executeAsync(callback); + } else { + return createResourceAsync(callback); + } + } + /** * add a creatable resource dependency for this resource. * @@ -59,6 +71,11 @@ public void createRootResource() throws Exception { this.createResource(); } + @Override + public ServiceCall createRootResourceAsync(ServiceCallback callback) { + return this.createResourceAsync(callback); + } + protected Resource createdResource(String key) { return this.creatableTaskGroup.taskResult(key); } @@ -69,4 +86,6 @@ protected Resource createdResource(String key) { * @throws Exception the exception */ protected abstract void createResource() throws Exception; + + protected abstract ServiceCall createResourceAsync(ServiceCallback callback); } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableTaskGroup.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableTaskGroup.java index 74a561d54538..bf40e4fefda0 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableTaskGroup.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableTaskGroup.java @@ -1,13 +1,17 @@ package com.microsoft.azure.management.resources.fluentcore.model.implementation; -import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.TaskGroupBase; +import com.microsoft.azure.TaskItem; +import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; /** * Type representing a group of creatable tasks and the dependency between them. */ -public class CreatableTaskGroup extends TaskGroupBase { +public class CreatableTaskGroup extends TaskGroupBase { + /** * Represents a type that know how to create the root resource in a CreatableTaskGroup. */ @@ -16,6 +20,8 @@ interface RootResourceCreator { * Creates the root resource. */ void createRootResource() throws Exception; + + ServiceCall createRootResourceAsync(ServiceCallback serviceCallback); } private final RootResourceCreator rootCreate; @@ -28,7 +34,7 @@ interface RootResourceCreator { * @param rootCreate {@link RootResourceCreator} that know how to create the rootCreatable once all the * dependencies are available */ - public CreatableTaskGroup(String rootCreatableId, Creatable rootCreatable, RootResourceCreator rootCreate) { + public CreatableTaskGroup(String rootCreatableId, Creatable rootCreatable, RootResourceCreator rootCreate) { this(rootCreatableId, new CreatableTaskItem(rootCreatable), rootCreate); } @@ -59,7 +65,12 @@ public Resource createdResource(String key) { } @Override - public void executeRootTask(CreatableTaskItem rootTaskItem) throws Exception { + public void executeRootTask(TaskItem task) throws Exception { this.rootCreate.createRootResource(); } + + @Override + public ServiceCall executeRootTaskAsync(TaskItem task, ServiceCallback callback) { + return this.rootCreate.createRootResourceAsync(callback); + } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableTaskItem.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableTaskItem.java index e86d9ff4326b..584974bf1358 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableTaskItem.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableTaskItem.java @@ -1,14 +1,20 @@ package com.microsoft.azure.management.resources.fluentcore.model.implementation; +import com.microsoft.azure.DAGNode; +import com.microsoft.azure.TaskGroup; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.TaskItem; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; +import com.microsoft.rest.ServiceResponse; /** * Represents a task that creates a resource when executed. */ public class CreatableTaskItem implements TaskItem { - private Creatable creatable; + private Creatable creatable; + private DAGNode> node; private Resource created; /** @@ -16,7 +22,7 @@ public class CreatableTaskItem implements TaskItem { * * @param creatable the creatable */ - public CreatableTaskItem(Creatable creatable) { + public CreatableTaskItem(Creatable creatable) { this.creatable = creatable; } @@ -26,7 +32,27 @@ public Resource result() { } @Override - public void execute() throws Exception { - this.created = (Resource) this.creatable.create(); + public void execute(TaskGroup> taskGroup, DAGNode> node) throws Exception { + this.created = this.creatable.create(); + taskGroup.dag().reportedCompleted(node); + taskGroup.execute(); + } + + @Override + public ServiceCall executeAsync(final TaskGroup> taskGroup, final DAGNode> node, final ServiceCallback callback) { + final CreatableTaskItem self = this; + return ((Creatable) this.creatable).createAsync(new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + self.created = result.getBody(); + taskGroup.dag().reportedCompleted(node); + taskGroup.executeAsync(callback); + } + }); } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/IndexableRefreshableWrapperImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/IndexableRefreshableWrapperImpl.java index 801f26ea8ba3..127b83325be6 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/IndexableRefreshableWrapperImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/IndexableRefreshableWrapperImpl.java @@ -31,7 +31,13 @@ public InnerModelT inner() { return this.innerObject; } - protected void setInner(InnerModelT inner) { + /** + * Set the wrapped inner model. + * (For internal use only) + * + * @param inner the new inner model + */ + public void setInner(InnerModelT inner) { this.innerObject = inner; } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java index 49f0683f82a5..5c507fb8bacd 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/utils/Utils.java @@ -6,6 +6,10 @@ package com.microsoft.azure.management.resources.fluentcore.utils; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableImpl; +import com.microsoft.rest.ServiceCallback; +import com.microsoft.rest.ServiceResponse; + /** * Defines a few utilities. */ @@ -33,6 +37,57 @@ public static boolean toPrimitiveBoolean(Boolean value) { return value; } + /** + * Creates a void callback from a callback that returns another type of + * instance. This is useful for internal async handoffs where returned + * resource is stored elsewhere. + * + * @param model the fluent model + * @param callback the callback to return the fluent model + * @param the fluent model type + * @return the void callback + */ + public static ServiceCallback toVoidCallback(final T model, final ServiceCallback callback) { + return new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + callback.success(new ServiceResponse<>(model, result.getResponse())); + } + }; + } + + /** + * Creates a callback returning the inner resource from a fluent model + * and a void callback, and set the inner on the fluent model. + * + * @param modelImpl the implementation instance of the fluent resource + * @param callback the void callback + * @param the inner resource type + * @param the fluent resource type + * @param the implementation for the fuent resource type + * @return the inner callback + */ + public static > ServiceCallback + fromVoidCallback(final FluentImplT modelImpl, final ServiceCallback callback) { + return new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + modelImpl.setInner(result.getBody()); + callback.success(new ServiceResponse(result.getHeadResponse())); + } + }; + } + private Utils() { } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java index 46bba3a7f35b..248ec63e2ad2 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java @@ -30,6 +30,9 @@ import com.microsoft.azure.management.resources.implementation.api.ParametersLink; import com.microsoft.azure.management.resources.implementation.api.ProviderInner; import com.microsoft.azure.management.resources.implementation.api.TemplateLink; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; +import com.microsoft.rest.ServiceResponse; import org.joda.time.DateTime; import java.io.IOException; @@ -281,6 +284,46 @@ public Deployment create() throws Exception { // FLUENT: implementation return this; } + @Override + public ServiceCall createAsync(final ServiceCallback callback) { + final Deployment self = this; + if (this.creatableResourceGroup != null) { + return this.creatableResourceGroup.createAsync(new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + createResourceAsync(new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + callback.success(new ServiceResponse<>(self, result.getResponse())); + } + }); + } + }); + } else { + return createResourceAsync(new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + callback.success(new ServiceResponse<>(self, result.getResponse())); + } + }); + } + } + @Override public Deployment refresh() throws Exception { return null; @@ -298,6 +341,28 @@ protected void createResource() throws Exception { client.createOrUpdate(resourceGroupName(), name(), inner); } + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + DeploymentInner inner = new DeploymentInner() + .withProperties(new DeploymentProperties()); + inner.properties().withMode(mode()); + inner.properties().withTemplate(template()); + inner.properties().withTemplateLink(templateLink()); + inner.properties().withParameters(parameters()); + inner.properties().withParametersLink(parametersLink()); + return client.createOrUpdateAsync(resourceGroupName(), name(), inner, new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + callback.success(new ServiceResponse(result.getHeadResponse())); + } + }); + } + @Override public Update update() throws Exception { return this; diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java index e5ef8dbc7bd2..047e975aea87 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java @@ -8,10 +8,13 @@ import com.microsoft.azure.management.resources.GenericResource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.utils.Utils; import com.microsoft.azure.management.resources.implementation.api.GenericResourceInner; import com.microsoft.azure.management.resources.implementation.api.Plan; import com.microsoft.azure.management.resources.implementation.api.ResourceManagementClientImpl; import com.microsoft.azure.management.resources.implementation.api.ResourcesInner; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; /** * The implementation for GenericResource and its nested interfaces. @@ -127,6 +130,11 @@ public GenericResource create() throws Exception { return this; } + @Override + public ServiceCall createAsync(final ServiceCallback callback) { + return createResourceAsync(Utils.toVoidCallback(this, callback)); + } + @Override protected void createResource() throws Exception { GenericResourceInner inner = client.createOrUpdate( @@ -141,6 +149,19 @@ protected void createResource() throws Exception { this.setInner(inner); } + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + return client.createOrUpdateAsync( + resourceGroupName(), + resourceProviderNamespace, + parentResourceId, + resourceType, + key(), + apiVersion, + inner(), + Utils.fromVoidCallback(this, callback)); + } + @Override public UpdateWithApiVersion update() throws Exception { return this; diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java index 434f0c6bf9b4..b915400c42b5 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java @@ -13,11 +13,14 @@ import com.microsoft.azure.management.resources.ResourceGroupExportTemplateOptions; import com.microsoft.azure.management.resources.fluentcore.arm.Region; import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableImpl; +import com.microsoft.azure.management.resources.fluentcore.utils.Utils; import com.microsoft.azure.management.resources.implementation.api.ExportTemplateRequestInner; import com.microsoft.azure.management.resources.implementation.api.ResourceGroupExportResultInner; import com.microsoft.azure.management.resources.implementation.api.ResourceGroupInner; import com.microsoft.azure.management.resources.implementation.api.ResourceGroupsInner; import com.microsoft.azure.management.resources.implementation.api.ResourceManagementClientImpl; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; import java.io.IOException; import java.util.Arrays; @@ -129,7 +132,12 @@ public ResourceGroupImpl create() throws Exception { // FLUENT: implem } @Override - public ResourceGroupImpl refresh() throws Exception { // FLUENT: implementation of ResourceGroup.Refreshable + public ServiceCall createAsync(final ServiceCallback callback) { + return super.creatablesCreateAsync(Utils.toVoidCallback(this, callback)); + } + + @Override + public ResourceGroupImpl refresh() throws Exception { this.setInner(client.get(this.key).getBody()); return this; } @@ -151,4 +159,12 @@ protected void createResource() throws Exception { params.withTags(this.inner().tags()); client.createOrUpdate(this.name(), params); } + + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + ResourceGroupInner params = new ResourceGroupInner(); + params.withLocation(this.inner().location()); + params.withTags(this.inner().tags()); + return client.createOrUpdateAsync(this.name(), params, Utils.fromVoidCallback(this, callback)); + } } diff --git a/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java b/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java index 91fb973c186f..14a56a76f766 100644 --- a/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java +++ b/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java @@ -9,6 +9,7 @@ import com.microsoft.azure.CloudException; import com.microsoft.azure.management.resources.ResourceGroups; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.utils.Utils; import com.microsoft.azure.management.storage.KeyType; import com.microsoft.azure.management.storage.StorageAccount; import com.microsoft.azure.management.storage.implementation.api.AccessTier; @@ -24,6 +25,8 @@ import com.microsoft.azure.management.storage.implementation.api.StorageAccountListKeysResultInner; import com.microsoft.azure.management.storage.implementation.api.StorageAccountUpdateParametersInner; import com.microsoft.azure.management.storage.implementation.api.StorageAccountsInner; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; import org.joda.time.DateTime; @@ -151,6 +154,11 @@ public StorageAccountImpl create() throws Exception { return this; } + @Override + public ServiceCall createAsync(ServiceCallback callback) { + return super.creatablesCreateAsync(Utils.toVoidCallback(this, callback)); + } + @Override public StorageAccountImpl withSku(SkuName skuName) { if (isInCreateMode()) { @@ -201,6 +209,27 @@ protected void createResource() throws Exception { clearWrapperProperties(); } + @Override + protected ServiceCall createResourceAsync(final ServiceCallback callback) { + createParameters.withLocation(this.region()); + createParameters.withTags(this.inner().getTags()); + final StorageAccountImpl self = this; + return this.client.createAsync(this.resourceGroupName(), this.name(), createParameters, + new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + client.getPropertiesAsync(resourceGroupName(), name(), + Utils.fromVoidCallback(self, callback)); + clearWrapperProperties(); + } + }); + } + @Override public StorageAccountImpl update() throws Exception { updateParameters = new StorageAccountUpdateParametersInner(); diff --git a/azure/src/test/java/com/microsoft/azure/TestTemplate.java b/azure/src/test/java/com/microsoft/azure/TestTemplate.java index 10c22c50a57f..aae728b4c195 100644 --- a/azure/src/test/java/com/microsoft/azure/TestTemplate.java +++ b/azure/src/test/java/com/microsoft/azure/TestTemplate.java @@ -5,16 +5,14 @@ */ package com.microsoft.azure; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Test; - import com.microsoft.azure.management.resources.ResourceGroups; import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByGroup; import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting; import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing; +import org.junit.Assert; + +import java.io.IOException; /** * Base class for CRUD test cases for top level Azure resource models. @@ -89,7 +87,6 @@ public void verifyDeleting() throws Exception { * @param resourceGroups the resource groups collection * @throws Exception if anything goes wrong */ - @Test public void runTest(C collection, ResourceGroups resourceGroups) throws Exception { this.collection = collection; this.resourceGroups = resourceGroups; diff --git a/azure/src/test/java/com/microsoft/azure/TestVirtualMachine.java b/azure/src/test/java/com/microsoft/azure/TestVirtualMachine.java index 89f8d0c3273a..198e1ab181cf 100644 --- a/azure/src/test/java/com/microsoft/azure/TestVirtualMachine.java +++ b/azure/src/test/java/com/microsoft/azure/TestVirtualMachine.java @@ -1,15 +1,28 @@ package com.microsoft.azure; +import com.microsoft.azure.credentials.ApplicationTokenCredentials; import com.microsoft.azure.management.compute.VirtualMachine; import com.microsoft.azure.management.compute.VirtualMachines; import com.microsoft.azure.management.compute.implementation.KnownVirtualMachineImage; import com.microsoft.azure.management.resources.fluentcore.arm.Region; +import com.microsoft.rest.RestClient; +import com.microsoft.rest.ServiceCallback; +import com.microsoft.rest.ServiceResponse; +import okhttp3.logging.HttpLoggingInterceptor; +import org.junit.Test; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.fail; public class TestVirtualMachine extends TestTemplate { @Override public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exception { final String vmName = "vm" + this.testId; - VirtualMachine vm = virtualMachines.define(vmName) + final CountDownLatch latch = new CountDownLatch(1); + final VirtualMachine[] vms = new VirtualMachine[1]; + virtualMachines.define(vmName) .withRegion(Region.US_EAST) .withNewGroup() .withNewPrimaryNetwork("10.0.0.0/28") @@ -20,8 +33,20 @@ public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exc .withWindowsOS() .withAdminUserName("testuser") .withPassword("12NewPA$$w0rd!") - .create(); - return vm; + .createAsync(new ServiceCallback() { + @Override + public void failure(Throwable t) { + fail(); + } + + @Override + public void success(ServiceResponse result) { + vms[0] = result.getBody(); + latch.countDown(); + } + }); + latch.await(12, TimeUnit.MINUTES); + return vms[0]; } @Override @@ -33,4 +58,22 @@ public VirtualMachine updateResource(VirtualMachine resource) throws Exception { public void print(VirtualMachine virtualMachine) { TestUtils.print(virtualMachine); } + + @Test + public void run() throws Exception { + ApplicationTokenCredentials credentials = new ApplicationTokenCredentials( + System.getenv("client-id"), + System.getenv("domain"), + System.getenv("secret"), + null); + + RestClient.Builder restBuilder = AzureEnvironment.AZURE.newRestClientBuilder() + .withCredentials(credentials) + .withLogLevel(HttpLoggingInterceptor.Level.BODY); + + RestClient restClient = restBuilder.build(); + + Azure azure = Azure.authenticate(restClient).withDefaultSubscription(); + runTest(azure.virtualMachines(), azure.resourceGroups()); + } } diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroup.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroup.java index 94b4628ff70a..29507425d443 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroup.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroup.java @@ -7,6 +7,9 @@ package com.microsoft.azure; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; + /** * Represents a group of related tasks. *

@@ -39,6 +42,11 @@ public interface TaskGroup> { */ void merge(TaskGroup parentTaskGroup); + /** + * Prepare the graph for execution. + */ + void prepare(); + /** * Executes the tasks in the group. *

@@ -48,6 +56,14 @@ public interface TaskGroup> { */ void execute() throws Exception; + /** + * Executes the tasks in the group asynchronously. + * + * @param callback the callback to call on failure or success + * @return the handle to the REST call + */ + ServiceCall executeAsync(ServiceCallback callback); + /** * Gets the result of execution of a task in the group. *

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 d0dc430edc3a..a4a93ae1f281 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 @@ -7,15 +7,17 @@ package com.microsoft.azure; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; + /** * The base implementation of TaskGroup interface. * * @param the result type of the tasks in the group - * @param type representing task in the group */ -public abstract class TaskGroupBase> - implements TaskGroup { - private DAGraph> dag; +public abstract class TaskGroupBase + implements TaskGroup> { + private DAGraph, DAGNode>> dag; /** * Creates TaskGroupBase. @@ -23,12 +25,12 @@ public abstract class TaskGroupBase> * @param rootTaskItemId the id of the root task in this task group * @param rootTaskItem the root task */ - public TaskGroupBase(String rootTaskItemId, U rootTaskItem) { + public TaskGroupBase(String rootTaskItemId, TaskItem rootTaskItem) { this.dag = new DAGraph<>(new DAGNode<>(rootTaskItemId, rootTaskItem)); } @Override - public DAGraph> dag() { + public DAGraph, DAGNode>> dag() { return dag; } @@ -38,24 +40,42 @@ public boolean isRoot() { } @Override - public void merge(TaskGroup parentTaskGroup) { + public void merge(TaskGroup> parentTaskGroup) { dag.merge(parentTaskGroup.dag()); } @Override - public void execute() throws Exception { + public void prepare() { if (isRoot()) { dag.prepare(); - DAGNode nextNode = dag.getNext(); - while (nextNode != null) { - if (dag.isRootNode(nextNode)) { - executeRootTask(nextNode.data()); - } else { - nextNode.data().execute(); - } - dag.reportedCompleted(nextNode); - nextNode = dag.getNext(); - } + } + } + + @Override + public void execute() throws Exception { + DAGNode> nextNode = dag.getNext(); + if (nextNode == null) { + return; + } + + if (dag.isRootNode(nextNode)) { + executeRootTask(nextNode.data()); + } else { + nextNode.data().execute(this, nextNode); + } + } + + @Override + public ServiceCall executeAsync(final ServiceCallback callback) { + final DAGNode> nextNode = dag.getNext(); + if (nextNode == null) { + return null; + } + + if (dag.isRootNode(nextNode)) { + return executeRootTaskAsync(nextNode.data(), callback); + } else { + return nextNode.data().executeAsync(this, nextNode, callback); } } @@ -74,5 +94,18 @@ public T taskResult(String taskId) { * @param task the root task in this group * @throws Exception the exception */ - public abstract void executeRootTask(U task) throws Exception; + public abstract void executeRootTask(TaskItem task) throws Exception; + + /** + * executes the root task in this group asynchronously. + *

+ * this method will be invoked when all the task dependencies of the root task are finished + * executing, at this point root task can be executed by consuming the result of tasks it + * depends on. + * + * @param task the root task in this group + * @param callback the callback when the task fails or succeeds + * @return the handle to the REST call + */ + public abstract ServiceCall executeRootTaskAsync(TaskItem task, ServiceCallback callback); } diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java index 7df830f69bb9..870105917457 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java @@ -7,6 +7,9 @@ package com.microsoft.azure; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; + /** * Type representing a task in a task group {@link TaskGroup}. * @@ -22,7 +25,11 @@ public interface TaskItem { * Executes the task. *

* once executed the result will be available through result getter + * + * @param taskGroup the task group dispatching tasks * @throws Exception exception */ - void execute() throws Exception; + void execute(TaskGroup> taskGroup, DAGNode> node) throws Exception; + + ServiceCall executeAsync(TaskGroup> taskGroup, DAGNode> node, ServiceCallback callback); } From 81c50fdc484a920bd0253c173fd5fd09fa86d1ff Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 15 Jun 2016 00:08:26 -0700 Subject: [PATCH 2/5] Add applyAsync() --- .../implementation/AvailabilitySetImpl.java | 5 +++++ .../compute/implementation/DataDiskImpl.java | 7 +++++++ .../implementation/VirtualMachineImpl.java | 5 +++++ .../network/implementation/NetworkImpl.java | 5 +++++ .../implementation/NetworkInterfaceImpl.java | 5 +++++ .../NetworkSecurityGroupImpl.java | 5 +++++ .../implementation/NicIpConfigurationImpl.java | 7 +++++++ .../implementation/PublicIpAddressImpl.java | 5 +++++ .../resources/fluentcore/model/Appliable.java | 11 +++++++++++ .../model/implementation/CreatableImpl.java | 1 + .../implementation/DeploymentImpl.java | 16 ++++++++++++++++ .../implementation/GenericResourceImpl.java | 5 +++++ .../implementation/ResourceGroupImpl.java | 5 +++++ .../implementation/StorageAccountImpl.java | 17 +++++++++++++++++ .../main/java/com/microsoft/azure/TaskItem.java | 11 +++++++++++ 15 files changed, 110 insertions(+) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java index 3420472135fe..917f6e60c4e4 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java @@ -93,6 +93,11 @@ public AvailabilitySetImpl apply() throws Exception { return this.create(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + return this.createAsync(callback); + } + @Override protected void createResource() throws Exception { ServiceResponse response = this.client.createOrUpdate(this.resourceGroupName(), this.name(), this.inner()); diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java index d76fac30cd0c..130fdab65c5d 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java @@ -7,6 +7,8 @@ import com.microsoft.azure.management.compute.implementation.api.VirtualHardDisk; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.ChildResourceImpl; import com.microsoft.azure.management.storage.StorageAccount; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; import java.util.ArrayList; import java.util.List; @@ -144,6 +146,11 @@ public VirtualMachine apply() { return this.parent(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + throw new UnsupportedOperationException("Apply doesn't run asynchronously on child resources!"); + } + protected static void setDataDisksDefaults(List dataDisks, String namePrefix) { List usedLuns = new ArrayList<>(); for (DataDisk dataDisk : dataDisks) { diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java index dfb853252778..d2771b778585 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java @@ -162,6 +162,11 @@ public VirtualMachineImpl apply() throws Exception { return this.create(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + return this.createAsync(callback); + } + @Override public void deallocate() throws CloudException, IOException, InterruptedException { this.client.deallocate(this.resourceGroupName(), this.name()); diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java index 73a3f8e05d46..eb365e67dc97 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java @@ -70,6 +70,11 @@ public NetworkImpl apply() throws Exception { return this.create(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + return createAsync(callback); + } + // Setters (fluent) @Override diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java index b9cad79b947b..2527c362f941 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java @@ -89,6 +89,11 @@ public NetworkInterfaceImpl apply() throws Exception { return this.create(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + return createAsync(callback); + } + /**************************************************. * Setters **************************************************/ diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java index e6792e46913a..0bda1c0301aa 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java @@ -78,6 +78,11 @@ public NetworkSecurityGroupImpl apply() throws Exception { return this.create(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + return createAsync(callback); + } + @Override protected void createResource() throws Exception { ServiceResponse response = diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NicIpConfigurationImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NicIpConfigurationImpl.java index 530456913a20..b665ae1df4db 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NicIpConfigurationImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NicIpConfigurationImpl.java @@ -13,6 +13,8 @@ import com.microsoft.azure.management.network.implementation.api.SubnetInner; import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.ChildResourceImpl; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; import java.io.IOException; import java.util.List; @@ -125,6 +127,11 @@ public NetworkInterface apply() { return parent(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + throw new UnsupportedOperationException("Apply doesn't run asynchronously on child resources!"); + } + @Override public NicIpConfigurationImpl withNewNetwork(Network.DefinitionCreatable creatable) { this.creatableVirtualNetworkKey = creatable.key(); diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java index 71ddb978d487..3d14beca6b7d 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java @@ -45,6 +45,11 @@ public PublicIpAddressImpl apply() throws Exception { return this.create(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + return this.createAsync(callback); + } + @Override public PublicIpAddress refresh() throws Exception { ServiceResponse response = diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Appliable.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Appliable.java index 8866b469e712..bc26b679f8df 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Appliable.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Appliable.java @@ -6,6 +6,9 @@ package com.microsoft.azure.management.resources.fluentcore.model; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; + /** * The base interface for all template interfaces that support update operations. * @@ -19,4 +22,12 @@ public interface Appliable extends Indexable { * @throws Exception exceptions from Azure */ T apply() throws Exception; + + /** + * Execute the update request asynchronously. + * + * @param callback the callback for success and failure + * @return the handle to the REST call + */ + ServiceCall applyAsync(ServiceCallback callback); } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java index 4b71414dd135..e8cca6299a8f 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java @@ -103,6 +103,7 @@ public FluentModelImplT create() throws Exception { * @param callback the callback to call on success or failure * @return the handle to the create REST call */ + @SuppressWarnings("unchecked") public ServiceCall createAsync(ServiceCallback callback) { if (creatableTaskGroup.isRoot()) { return creatableTaskGroup.executeAsync(Utils.toVoidCallback((FluentModelT) this, callback)); diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java index d7cd9b079ce6..ac1546db1697 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java @@ -363,6 +363,7 @@ public void success(ServiceResponse result) { }); } + @Override public DeploymentImpl apply() throws Exception { if (this.templateLink() != null && this.template() != null) { this.withTemplate(null); @@ -372,4 +373,19 @@ public DeploymentImpl apply() throws Exception { } return this.create(); } + + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + try { + if (this.templateLink() != null && this.template() != null) { + this.withTemplate(null); + } + if (this.parametersLink() != null && this.parameters() != null) { + this.withParameters(null); + } + } catch (IOException e) { + callback.failure(e); + } + return this.createAsync(callback); + } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java index 0d99f135a37b..c99db5faeae4 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java @@ -164,4 +164,9 @@ protected ServiceCall createResourceAsync(final ServiceCallback callback) public GenericResourceImpl apply() throws Exception { return create(); } + + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + return createAsync(callback); + } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java index 8c9b8b7572c5..f2eb517613c9 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java @@ -125,6 +125,11 @@ public ResourceGroupImpl apply() throws Exception { return this.create(); } + @Override + public ServiceCall applyAsync(ServiceCallback callback) { + return createAsync(callback); + } + @Override public ResourceGroupImpl refresh() throws Exception { this.setInner(client.get(this.key).getBody()); diff --git a/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java b/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java index 46f6ed37f812..191322f5ca13 100644 --- a/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java +++ b/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java @@ -242,6 +242,23 @@ public StorageAccountImpl apply() throws Exception { return this; } + @Override + public ServiceCall applyAsync(final ServiceCallback callback) { + final StorageAccountImpl self = this; + return client.updateAsync(resourceGroupName(), name(), updateParameters, new ServiceCallback() { + @Override + public void failure(Throwable t) { + callback.failure(t); + } + + @Override + public void success(ServiceResponse result) { + setInner(result.getBody()); + callback.success(new ServiceResponse(self, result.getResponse())); + } + }); + } + @Override public StorageAccountImpl withCustomDomain(CustomDomain customDomain) { if (isInCreateMode()) { diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java index 870105917457..fb74c23845c3 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java @@ -27,9 +27,20 @@ public interface TaskItem { * once executed the result will be available through result getter * * @param taskGroup the task group dispatching tasks + * @param node the node the task item is associated with * @throws Exception exception */ void execute(TaskGroup> taskGroup, DAGNode> node) throws Exception; + /** + * Executes the task asynchronously. + *

+ * once executed the result will be available through result getter + + * @param taskGroup the task group dispatching tasks + * @param node the node the task item is associated with + * @param callback callback to call on success or failure + * @return the handle of the REST call + */ ServiceCall executeAsync(TaskGroup> taskGroup, DAGNode> node, ServiceCallback callback); } From a7f22bdc0c2b4726ab5dcfb224da674c2f21c56e Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 15 Jun 2016 00:47:34 -0700 Subject: [PATCH 3/5] Add missing .prepare() calls --- .../fluentcore/model/implementation/CreatableImpl.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java index e8cca6299a8f..f897367d19d6 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java @@ -90,6 +90,7 @@ protected Resource createdResource(String key) { @SuppressWarnings("unchecked") public FluentModelImplT create() throws Exception { if (creatableTaskGroup.isRoot()) { + creatableTaskGroup.prepare(); creatableTaskGroup.execute(); } else { createResource(); @@ -106,6 +107,7 @@ public FluentModelImplT create() throws Exception { @SuppressWarnings("unchecked") public ServiceCall createAsync(ServiceCallback callback) { if (creatableTaskGroup.isRoot()) { + creatableTaskGroup.prepare(); return creatableTaskGroup.executeAsync(Utils.toVoidCallback((FluentModelT) this, callback)); } else { return createResourceAsync(Utils.toVoidCallback((FluentModelT) this, callback)); From c2d839ea9f05616b4d388167f49a011d2726b0c2 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Thu, 16 Jun 2016 17:37:39 -0700 Subject: [PATCH 4/5] Fix build failures --- .../compute/implementation/DataDiskImpl.java | 2 +- .../com/microsoft/azure/TestVirtualMachine.java | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java index 984d4829654c..d32ae74382ed 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java @@ -145,7 +145,7 @@ public VirtualMachine attach() { public VirtualMachine set() { return this.parent(); } - + protected static void setDataDisksDefaults(List dataDisks, String namePrefix) { List usedLuns = new ArrayList<>(); for (DataDisk dataDisk : dataDisks) { diff --git a/azure/src/test/java/com/microsoft/azure/TestVirtualMachine.java b/azure/src/test/java/com/microsoft/azure/TestVirtualMachine.java index 45cad81e0e6d..006851502dbe 100644 --- a/azure/src/test/java/com/microsoft/azure/TestVirtualMachine.java +++ b/azure/src/test/java/com/microsoft/azure/TestVirtualMachine.java @@ -4,8 +4,8 @@ import com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage; import com.microsoft.azure.management.compute.VirtualMachine; import com.microsoft.azure.management.compute.VirtualMachines; +import com.microsoft.azure.management.compute.implementation.api.VirtualMachineSizeTypes; import com.microsoft.azure.management.resources.fluentcore.arm.Region; -import com.microsoft.rest.RestClient; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; import okhttp3.logging.HttpLoggingInterceptor; @@ -31,6 +31,7 @@ public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exc .withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER) .withAdminUserName("testuser") .withPassword("12NewPA$$w0rd!") + .withSize(VirtualMachineSizeTypes.STANDARD_D1_V2) .createAsync(new ServiceCallback() { @Override public void failure(Throwable t) { @@ -65,13 +66,10 @@ public void run() throws Exception { System.getenv("secret"), null); - RestClient.Builder restBuilder = AzureEnvironment.AZURE.newRestClientBuilder() - .withCredentials(credentials) - .withLogLevel(HttpLoggingInterceptor.Level.BODY); - - RestClient restClient = restBuilder.build(); - - Azure azure = Azure.authenticate(restClient).withDefaultSubscription(); + Azure azure = Azure.configure() + .withLogLevel(HttpLoggingInterceptor.Level.BODY) + .authenticate(credentials) + .withDefaultSubscription(); runTest(azure.virtualMachines(), azure.resourceGroups()); } } From 6d8d289fcf0010ebd4c067bb0276c0b36eca666d Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Thu, 16 Jun 2016 17:42:42 -0700 Subject: [PATCH 5/5] Fix checkstyle: --- .../azure/management/compute/implementation/DataDiskImpl.java | 2 -- .../network/implementation/NicIpConfigurationImpl.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java index d32ae74382ed..aeadd4e2ad2c 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/DataDiskImpl.java @@ -7,8 +7,6 @@ import com.microsoft.azure.management.compute.implementation.api.VirtualHardDisk; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.ChildResourceImpl; import com.microsoft.azure.management.storage.StorageAccount; -import com.microsoft.rest.ServiceCall; -import com.microsoft.rest.ServiceCallback; import java.util.ArrayList; import java.util.List; diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NicIpConfigurationImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NicIpConfigurationImpl.java index 8dc6d9586d3c..c46c6b9d02f4 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NicIpConfigurationImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NicIpConfigurationImpl.java @@ -11,8 +11,6 @@ import com.microsoft.azure.management.network.implementation.api.SubnetInner; import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.ChildResourceImpl; -import com.microsoft.rest.ServiceCall; -import com.microsoft.rest.ServiceCallback; import java.io.IOException; import java.util.List;