From 5be9ca1c52ce485eb6bd0ea96275db797d88e202 Mon Sep 17 00:00:00 2001 From: anuchan Date: Thu, 15 Sep 2016 19:47:51 -0700 Subject: [PATCH 1/8] Rework to simplify the create + update logic --- .../implementation/BatchAccountImpl.java | 2 +- .../implementation/AvailabilitySetImpl.java | 7 +- .../implementation/VirtualMachineImpl.java | 7 +- .../implementation/ServicePrincipalImpl.java | 7 +- .../graphrbac/implementation/UserImpl.java | 7 +- .../keyvault/implementation/VaultImpl.java | 5 -- .../implementation/LoadBalancerImpl.java | 5 -- .../network/implementation/NetworkImpl.java | 5 -- .../implementation/NetworkInterfaceImpl.java | 5 -- .../NetworkSecurityGroupImpl.java | 5 -- .../implementation/PublicIpAddressImpl.java | 7 +- .../CreatableResourcesImpl.java | 61 +++++++------ .../models/implementation/ResourceImpl.java | 45 +++------- .../model/implementation/CreatableImpl.java | 70 +++------------ .../CreatableUpdatableImpl.java | 87 +++++++++++++++++-- .../implementation/CreateUpdateTask.java | 53 +++++++++++ ...kGroup.java => CreateUpdateTaskGroup.java} | 60 ++++++++----- .../model/implementation/CreatorTaskItem.java | 42 --------- .../implementation/DeploymentImpl.java | 31 +++---- .../implementation/GenericResourceImpl.java | 18 +--- .../implementation/ResourceGroupImpl.java | 27 +++--- .../implementation/StorageAccountImpl.java | 4 +- .../java/com/microsoft/azure/DAGraph.java | 10 +++ 23 files changed, 285 insertions(+), 285 deletions(-) create mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTask.java rename azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/{CreatorTaskGroup.java => CreateUpdateTaskGroup.java} (52%) delete mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskItem.java diff --git a/azure-mgmt-batch/src/main/java/com/microsoft/azure/management/batch/implementation/BatchAccountImpl.java b/azure-mgmt-batch/src/main/java/com/microsoft/azure/management/batch/implementation/BatchAccountImpl.java index 8dd6da9db9edd..c4eb9e1ea37f6 100644 --- a/azure-mgmt-batch/src/main/java/com/microsoft/azure/management/batch/implementation/BatchAccountImpl.java +++ b/azure-mgmt-batch/src/main/java/com/microsoft/azure/management/batch/implementation/BatchAccountImpl.java @@ -80,7 +80,7 @@ public BatchAccount call(AccountResourceInner accountResourceInner) { } @Override - public Observable applyUpdateAsync() { + public Observable updateResourceAsync() { // TODO - ans - remove call to createResourceAsync and uncomment code below, after PATCH start sending the nulls. return createResourceAsync(); /* 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 04b42a5f1294d..fc7eb88a76a3d 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 @@ -92,12 +92,7 @@ public AvailabilitySetImpl withFaultDomainCount(int faultDomainCount) { return this; } - @Override - public Observable applyUpdateAsync() { - return this.createResourceAsync(); - } - - // CreatorTaskGroup.ResourceCreator implementation + // CreateUpdateTaskGroup.ResourceCreator.createResourceAsync implementation @Override public Observable createResourceAsync() { 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 852d301c0abc2..5d3431b1b0778 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 @@ -155,11 +155,6 @@ public VirtualMachine refresh() { return this; } - @Override - public Observable applyUpdateAsync() { - return this.createResourceAsync(); - } - @Override public void deallocate() { this.client.deallocate(this.resourceGroupName(), this.name()); @@ -876,7 +871,7 @@ public PowerState powerState() { return null; } - // CreatorTaskGroup.ResourceCreator implementation + // CreateUpdateTaskGroup.ResourceCreator.createResourceAsync implementation @Override public Observable createResourceAsync() { if (isInCreateMode()) { diff --git a/azure-mgmt-graph-rbac/src/main/java/com/microsoft/azure/management/graphrbac/implementation/ServicePrincipalImpl.java b/azure-mgmt-graph-rbac/src/main/java/com/microsoft/azure/management/graphrbac/implementation/ServicePrincipalImpl.java index 0dbe0d5258fd6..cbcf40ef029a5 100644 --- a/azure-mgmt-graph-rbac/src/main/java/com/microsoft/azure/management/graphrbac/implementation/ServicePrincipalImpl.java +++ b/azure-mgmt-graph-rbac/src/main/java/com/microsoft/azure/management/graphrbac/implementation/ServicePrincipalImpl.java @@ -78,7 +78,10 @@ public Observable createResourceAsync() { } @Override - public Observable applyAsync() { - return null; + public Observable updateResourceAsync() { return null; } + + @Override + public boolean isInCreateMode() { + return false; } } diff --git a/azure-mgmt-graph-rbac/src/main/java/com/microsoft/azure/management/graphrbac/implementation/UserImpl.java b/azure-mgmt-graph-rbac/src/main/java/com/microsoft/azure/management/graphrbac/implementation/UserImpl.java index 9779e5c7fd706..0f995880c029f 100644 --- a/azure-mgmt-graph-rbac/src/main/java/com/microsoft/azure/management/graphrbac/implementation/UserImpl.java +++ b/azure-mgmt-graph-rbac/src/main/java/com/microsoft/azure/management/graphrbac/implementation/UserImpl.java @@ -111,7 +111,12 @@ public Observable createResourceAsync() { } @Override - public Observable applyAsync() { + public Observable updateResourceAsync() { return null; } + + @Override + public boolean isInCreateMode() { + return false; + } } diff --git a/azure-mgmt-keyvault/src/main/java/com/microsoft/azure/management/keyvault/implementation/VaultImpl.java b/azure-mgmt-keyvault/src/main/java/com/microsoft/azure/management/keyvault/implementation/VaultImpl.java index af5e02a134629..d7efebaefd21e 100644 --- a/azure-mgmt-keyvault/src/main/java/com/microsoft/azure/management/keyvault/implementation/VaultImpl.java +++ b/azure-mgmt-keyvault/src/main/java/com/microsoft/azure/management/keyvault/implementation/VaultImpl.java @@ -261,9 +261,4 @@ public VaultImpl refresh() { setInner(client.get(resourceGroupName(), name())); return this; } - - @Override - public Observable applyUpdateAsync() { - return createAsync(); - } } diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/LoadBalancerImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/LoadBalancerImpl.java index b01dcb6330f27..72cab0f153f21 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/LoadBalancerImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/LoadBalancerImpl.java @@ -83,11 +83,6 @@ public LoadBalancerImpl refresh() { return this; } - @Override - public Observable applyUpdateAsync() { - return createResourceAsync(); - } - // Helpers @Override 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 a83c4174195b1..c63f38accba9f 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 @@ -65,11 +65,6 @@ public NetworkImpl refresh() { return this; } - @Override - public Observable applyUpdateAsync() { - return createResourceAsync(); - } - // Helpers NetworkImpl withSubnet(SubnetImpl subnet) { 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 29b1b2bda90d1..294f7d5fe575c 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 @@ -86,11 +86,6 @@ public NetworkInterface refresh() { return this; } - @Override - public Observable applyUpdateAsync() { - return createResourceAsync(); - } - // Setters (fluent) @Override 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 c12a3656792de..9d6b6239c7a0d 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 @@ -87,11 +87,6 @@ public NetworkSecurityGroupImpl refresh() { return this; } - @Override - public Observable applyUpdateAsync() { - return createResourceAsync(); - } - // Setters (fluent) @Override 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 5e6d6b60f236f..5c6162482a651 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 @@ -39,11 +39,6 @@ class PublicIpAddressImpl // Verbs - @Override - public Observable applyUpdateAsync() { - return this.createResourceAsync(); - } - @Override public PublicIpAddress refresh() { PublicIPAddressInner response = this.client.get(this.resourceGroupName(), this.name()); @@ -130,7 +125,7 @@ public String leafDomainLabel() { } } - // CreatorTaskGroup.ResourceCreator implementation + // CreateUpdateTaskGroup.ResourceCreator implementation @Override public Observable createResourceAsync() { // Clean up empty DNS settings 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 56f64cece1ae5..6ef863c198987 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 @@ -5,7 +5,7 @@ import com.microsoft.azure.management.resources.fluentcore.collection.SupportsBatchCreation; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import com.microsoft.azure.management.resources.fluentcore.model.CreatedResources; -import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableImpl; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableUpdatableImpl; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; @@ -51,28 +51,28 @@ public final CreatedResources create(List> creatables) { @Override @SafeVarargs public final Observable> createAsync(Creatable ... creatables) { - CreatableResourcesRootImpl rootResource = new CreatableResourcesRootImpl<>(); + CreatableUpdatableResourcesRootImpl rootResource = new CreatableUpdatableResourcesRootImpl<>(); rootResource.addCreatableDependencies(creatables); return rootResource.createAsync() - .map(new Func1, CreatedResources>() { + .map(new Func1, CreatedResources>() { @Override - public CreatedResources call(CreatableResourcesRoot tCreatableResourcesRoot) { - return new CreatedResourcesImpl(tCreatableResourcesRoot); + public CreatedResources call(CreatableUpdatableResourcesRoot tCreatableUpdatableResourcesRoot) { + return new CreatedResourcesImpl(tCreatableUpdatableResourcesRoot); } }); } @Override public final Observable> createAsync(List> creatables) { - CreatableResourcesRootImpl rootResource = new CreatableResourcesRootImpl<>(); + CreatableUpdatableResourcesRootImpl rootResource = new CreatableUpdatableResourcesRootImpl<>(); rootResource.addCreatableDependencies(creatables); return rootResource.createAsync() - .map(new Func1, CreatedResources>() { + .map(new Func1, CreatedResources>() { @Override - public CreatedResources call(CreatableResourcesRoot tCreatableResourcesRoot) { - return new CreatedResourcesImpl(tCreatableResourcesRoot); + public CreatedResources call(CreatableUpdatableResourcesRoot tCreatableUpdatableResourcesRoot) { + return new CreatedResourcesImpl(tCreatableUpdatableResourcesRoot); } }); } @@ -105,17 +105,17 @@ public ServiceResponse> call(CreatedResources ts) { * @param the type of the resources in the batch. */ private class CreatedResourcesImpl implements CreatedResources { - private CreatableResourcesRoot creatableResourcesRoot; + private CreatableUpdatableResourcesRoot creatableUpdatableResourcesRoot; private final List list; - CreatedResourcesImpl(CreatableResourcesRoot creatableResourcesRoot) { - this.creatableResourcesRoot = creatableResourcesRoot; - this.list = this.creatableResourcesRoot.createdTopLevelResources(); + CreatedResourcesImpl(CreatableUpdatableResourcesRoot creatableUpdatableResourcesRoot) { + this.creatableUpdatableResourcesRoot = creatableUpdatableResourcesRoot; + this.list = this.creatableUpdatableResourcesRoot.createdTopLevelResources(); } @Override public Resource createdRelatedResource(String key) { - return this.creatableResourcesRoot.createdRelatedResource(key); + return this.creatableUpdatableResourcesRoot.createdRelatedResource(key); } @Override @@ -240,26 +240,26 @@ public List subList(int fromIndex, int toIndex) { * * @param the type of the resources in the batch. */ - interface CreatableResourcesRoot extends Resource { + interface CreatableUpdatableResourcesRoot extends Resource { List createdTopLevelResources(); Resource createdRelatedResource(String key); } /** - * Implementation of {@link CreatableResourcesRoot}. + * Implementation of {@link CreatableUpdatableResourcesRoot}. * * @param the type of the resources in the batch. */ - private class CreatableResourcesRootImpl - extends CreatableImpl, Object, CreatableResourcesRootImpl> - implements CreatableResourcesRoot { + private class CreatableUpdatableResourcesRootImpl + extends CreatableUpdatableImpl, Object, CreatableUpdatableResourcesRootImpl> + implements CreatableUpdatableResourcesRoot { /** * Collection of keys of top level resources in this batch. */ private List keys; - CreatableResourcesRootImpl() { - super("CreatableResourcesRoot", null); + CreatableUpdatableResourcesRootImpl() { + super("CreatableUpdatableResourcesRoot", null); this.keys = new ArrayList<>(); } @@ -267,14 +267,14 @@ private class CreatableResourcesRootImpl public List createdTopLevelResources() { List resources = new ArrayList<>(); for (String resourceKey : keys) { - resources.add((ResourceT) creatorTaskGroup().createdResource(resourceKey)); + resources.add((ResourceT) creatorUpdatorTaskGroup().createdResource(resourceKey)); } return Collections.unmodifiableList(resources); } @Override public Resource createdRelatedResource(String key) { - return creatorTaskGroup().createdResource(key); + return creatorUpdatorTaskGroup().createdResource(key); } void addCreatableDependencies(Creatable ... creatables) { @@ -292,13 +292,18 @@ void addCreatableDependencies(List> creatables) { } @Override - public Observable> createResourceAsync() { - return Observable.just((CreatableResourcesRoot) this); + public Observable> createResourceAsync() { + return Observable.just((CreatableUpdatableResourcesRoot) this); } @Override - public CreatableResourcesRoot createResource() { - return this; + public Observable> updateResourceAsync() { + return createResourceAsync(); + } + + @Override + public boolean isInCreateMode() { + return true; } // Below overrides returns null as this is not a real resource in Azure @@ -306,7 +311,7 @@ public CreatableResourcesRoot createResource() { // resources. @Override - public CreatableResourcesRoot refresh() { + public CreatableUpdatableResourcesRoot refresh() { return null; } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/ResourceImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/ResourceImpl.java index fc3a0ba6a72af..ba588ad764b3f 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/ResourceImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/ResourceImpl.java @@ -39,7 +39,6 @@ public abstract class ResourceImpl< Resource { protected ResourceImpl(String name, InnerModelT innerObject) { super(name, innerObject); - // Initialize tags if (innerObject.getTags() == null) { innerObject.withTags(new TreeMap()); } @@ -149,47 +148,31 @@ public final FluentModelImplT withRegion(Region region) { return this.withRegion(region.toString()); } - - @Override - public Observable applyAsync() { - if (super.creatorTaskGroup.isPreparer()) { - super.creatorTaskGroup.prepare(); - return super.creatorTaskGroup.executeAsync().last(); - } - throw new IllegalStateException("Internal Error: createAsync can be called only on preparer"); - } - /** - * Execute the update request asynchronously. - * - * @return the handle to the REST call + * @return true if currently in define..create mode */ - public abstract Observable applyUpdateAsync(); - @Override - public Observable executeCreateOrUpdateAsync() { - if (this.isInCreateMode()) { - return createResourceAsync(); - } - else { - return applyUpdateAsync(); - } + public boolean isInCreateMode() { + return this.inner().id() == null; } /** - * @return true if currently in define..create mode + * This is the default implementation of updateResourceAsync, it simply calls createResourceAsync() since + * for most of the resource both create and update are handled by the same API call (CreateOrUpdate). + * A resource that uses different API call for update should override updateResourceAsync. + * + * @return the updated resource. */ - public boolean isInCreateMode() { - return this.inner().id() == null; + @Override + public Observable updateResourceAsync() { + return this.createResourceAsync(); } - protected List innersFromWrappers( - Collection> wrappers) { + protected List innersFromWrappers(Collection> wrappers) { return innersFromWrappers(wrappers, null); } - protected List innersFromWrappers( - Collection> wrappers, + protected List innersFromWrappers(Collection> wrappers, List inners) { if (wrappers == null || wrappers.size() == 0) { return inners; @@ -197,11 +180,9 @@ protected List innersFromWrappers( if (inners == null) { inners = new ArrayList<>(); } - for (Wrapper wrapper : wrappers) { inners.add(wrapper.inner()); } - return inners; } } 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 f3a509cde54a9..46e56c82dd898 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 @@ -6,8 +6,6 @@ package com.microsoft.azure.management.resources.fluentcore.model.implementation; -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; import com.microsoft.rest.ServiceResponse; @@ -22,38 +20,15 @@ * @param the fluent model implementation type */ public abstract class CreatableImpl> - extends IndexableRefreshableWrapperImpl - implements CreatorTaskGroup.ResourceCreator { + extends IndexableRefreshableWrapperImpl { /** * The name of the creatable resource. */ private String name; - /** - * The group of tasks to create this resource and it's dependencies. - */ - protected CreatorTaskGroup creatorTaskGroup; - protected CreatableImpl(String name, InnerModelT innerObject) { super(innerObject); this.name = name; - creatorTaskGroup = new CreatorTaskGroup<>(this.key(), this); - } - - /** - * Add a creatable resource dependency for this resource. - * - * @param creatableResource the creatable dependency. - */ - @SuppressWarnings("unchecked") - protected void addCreatableDependency(Creatable creatableResource) { - CreatorTaskGroup childGroup = - ((CreatorTaskGroup.ResourceCreator) creatableResource).creatorTaskGroup(); - childGroup.merge(this.creatorTaskGroup); - } - - protected Resource createdResource(String key) { - return (Resource) this.creatorTaskGroup.createdResource(key); } /** @@ -64,18 +39,15 @@ public String name() { } /** - * Default implementation of create(). + * Creates the resource asynchronously and return a observable to track the + * asynchronous operation. * - * @return the created resource + * @return an observable stream that emits the resource when it is created */ - @SuppressWarnings("unchecked") - public FluentModelT create() { - return createAsync().toBlocking().single(); - } + abstract Observable createAsync(); /** - * Puts the request into the queue and allow the HTTP client to execute - * it when system resources are available. + * Default implementation to create the resource asynchronously. * * @param callback the callback to handle success and failure * @return a handle to cancel the request @@ -85,35 +57,13 @@ public ServiceCall createAsync(final ServiceCallback } /** - * Default implementation of createAsync(). + * Default implementation of create(). * - * @return the handle to the create REST call + * @return the created resource */ @SuppressWarnings("unchecked") - public Observable createAsync() { - if (creatorTaskGroup.isPreparer()) { - creatorTaskGroup.prepare(); - return creatorTaskGroup.executeAsync().last(); - } - throw new IllegalStateException("Internal Error: createAsync can be called only on preparer"); - } - - /** - * @return the task group associated with this creatable. - */ - @Override - public CreatorTaskGroup creatorTaskGroup() { - return this.creatorTaskGroup; - } - - @Override - public FluentModelT createResource() { - return this.createResourceAsync().toBlocking().last(); - } - - @Override - public Observable executeCreateOrUpdateAsync() { - return this.createResourceAsync(); + public FluentModelT create() { + return createAsync().toBlocking().single(); } @SuppressWarnings("unchecked") diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java index adcc5b95b8ddd..07d78e276cd7a 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java @@ -6,9 +6,12 @@ package com.microsoft.azure.management.resources.fluentcore.model.implementation; +import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.model.Appliable; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; +import rx.Observable; /** * The base class for all updatable resource. @@ -21,14 +24,55 @@ public abstract class CreatableUpdatableImpl< FluentModelT, InnerModelT, FluentModelImplT extends IndexableRefreshableWrapperImpl> - extends CreatableImpl< - FluentModelT, - InnerModelT, - FluentModelImplT> - implements Appliable { + extends CreatableImpl + implements + Appliable, + CreateUpdateTaskGroup.ResourceCreatorUpdator { + /** + * The group of tasks to create or update this resource and it's dependencies. + */ + protected CreateUpdateTaskGroup createUpdateTaskGroup; + /** + * Creates a new instance of CreatableUpdatableImpl. + * + * @param name the name of the resource + * @param innerObject the inner object + */ protected CreatableUpdatableImpl(String name, InnerModelT innerObject) { super(name, innerObject); + createUpdateTaskGroup = new CreateUpdateTaskGroup<>(this.key(), this); + } + + /** + * @return the task group containing the tasks that creates or updates resources that this + * resource depends on. + */ + @Override + public CreateUpdateTaskGroup creatorUpdatorTaskGroup() { + return this.createUpdateTaskGroup; + } + + /** + * Gets a resource created by the task group belongs to this instance. + * + * @param key the key of the resource + * @return the created resource. + */ + protected Resource createdResource(String key) { + return (Resource) this.createUpdateTaskGroup.createdResource(key); + } + + /** + * Add a creatable resource dependency for this resource. + * + * @param creatableResource the creatable dependency. + */ + @SuppressWarnings("unchecked") + protected void addCreatableDependency(Creatable creatableResource) { + CreateUpdateTaskGroup childGroup = + ((CreateUpdateTaskGroup.ResourceCreatorUpdator) creatableResource).creatorUpdatorTaskGroup(); + childGroup.merge(this.createUpdateTaskGroup); } /** @@ -45,13 +89,42 @@ public FluentModelImplT update() { return (FluentModelImplT) this; } + /** + * Default implementation of createAsync(). + * + * @return the observable that emit the created resource. + */ @Override - public FluentModelT apply() { - return applyAsync().toBlocking().last(); + public Observable createAsync() { + return this.executeTaskGroupAsync(); + } + + /** + * Default implementation of applyAsync(). + * + * @return the observable that emit the updated resource. + */ + @Override + public Observable applyAsync() { + return this.executeTaskGroupAsync(); } @Override public ServiceCall applyAsync(ServiceCallback callback) { return observableToFuture(applyAsync(), callback); } + + @Override + public FluentModelT apply() { + return applyAsync().toBlocking().last(); + } + + @SuppressWarnings("unchecked") + protected Observable executeTaskGroupAsync() { + if (createUpdateTaskGroup.isPreparer()) { + createUpdateTaskGroup.prepare(); + return createUpdateTaskGroup.executeAsync().last(); + } + throw new IllegalStateException("Internal Error: executeTaskGroupAsync can be called only on preparer"); + } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTask.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTask.java new file mode 100644 index 0000000000000..cf0fc1e39595f --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTask.java @@ -0,0 +1,53 @@ +package com.microsoft.azure.management.resources.fluentcore.model.implementation; + +import com.microsoft.azure.TaskItem; +import rx.Observable; +import rx.functions.Action1; +import rx.schedulers.Schedulers; + +/** + * Represents a task that creates or updates a resource when executed. + * + * @param the type of the resource that this task creates or update + */ +public class CreateUpdateTask implements TaskItem { + private CreateUpdateTaskGroup.ResourceCreatorUpdator resourceCreatorUpdator; + private ResourceT resource; + + /** + * Creates CreateUpdateTask. + * + * @param resourceCreatorUpdator the resource creator and updator + */ + public CreateUpdateTask(CreateUpdateTaskGroup.ResourceCreatorUpdator resourceCreatorUpdator) { + this.resourceCreatorUpdator = resourceCreatorUpdator; + } + + @Override + public ResourceT result() { + return resource; + } + + @Override + public Observable executeAsync() { + if (this.resourceCreatorUpdator.isInCreateMode()) { + return this.resourceCreatorUpdator.createResourceAsync() + .subscribeOn(Schedulers.io()) + .doOnNext(new Action1() { + @Override + public void call(ResourceT resourceT) { + resource = resourceT; + } + }); + } else { + return this.resourceCreatorUpdator.updateResourceAsync() + .subscribeOn(Schedulers.io()) + .doOnNext(new Action1() { + @Override + public void call(ResourceT resourceT) { + resource = resourceT; + } + }); + } + } +} diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskGroup.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTaskGroup.java similarity index 52% rename from azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskGroup.java rename to azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTaskGroup.java index 5831a5b505cfe..63cdf6f6157fa 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskGroup.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTaskGroup.java @@ -8,62 +8,74 @@ * * @param the type of the resource this group creates */ -public class CreatorTaskGroup extends TaskGroupBase> { +public class CreateUpdateTaskGroup extends TaskGroupBase> { /** - * Represents a type that know how to create resource. + * Represents a type that know how to create or update resource. * - * @param the type of the resource that this creator creates + * @param the type of the resource that this creatorUpdator can create or update */ - interface ResourceCreator { + interface ResourceCreatorUpdator + extends ResourceCreator, ResourceUpdator { /** - * Creates the resource asynchronously. - * - * @return the observable reference + * @return true if this creatorUpdator is in create mode. */ - Observable createResourceAsync(); - + boolean isInCreateMode(); /** - * Creates the resource synchronously. - * - * @return the created resource + * @return Gets the task group. */ - T createResource(); + CreateUpdateTaskGroup creatorUpdatorTaskGroup(); + } + /** + * Represents a type that know how to update resource. + * + * @param the type of the resource that this updator updates + */ + interface ResourceUpdator { /** - * @return Gets the task group. + * Update the resource asynchronously. + * + * @return the observable reference */ - CreatorTaskGroup creatorTaskGroup(); + Observable updateResourceAsync(); + } + /** + * Represents a type that know how to create resource. + * + * @param the type of the resource that this creator creates + */ + interface ResourceCreator { /** - * Creates or updates the resource asynchronously. + * Creates the resource asynchronously. * * @return the observable reference */ - Observable executeCreateOrUpdateAsync(); + Observable createResourceAsync(); } /** - * Creates CreatorTaskGroup. + * Creates CreateUpdateTaskGroup. * * @param key the key of the root task - * @param resourceCreator represents the resource creator that this group want to create ultimately + * @param resourceCreatorUpdator represents the resource creator that this group want to create or update ultimately */ - public CreatorTaskGroup(String key, ResourceCreator resourceCreator) { - this(key, new CreatorTaskItem<>(resourceCreator)); + public CreateUpdateTaskGroup(String key, ResourceCreatorUpdator resourceCreatorUpdator) { + this(key, new CreateUpdateTask<>(resourceCreatorUpdator)); } /** - * Creates CreatorTaskGroup. + * Creates CreateUpdateTaskGroup. * * @param key the key of the root task * @param rootTask represents the root task that this group want to executes ultimately */ - public CreatorTaskGroup(String key, CreatorTaskItem rootTask) { + public CreateUpdateTaskGroup(String key, CreateUpdateTask rootTask) { super(key, rootTask); } /** - * Gets a resource created by a creator task in this group. + * Gets a resource created or updated by a create-update task in this group. *

* This method can return null if the resource has not yet created that happens if the responsible task * is not yet selected for execution or it's it progress or provided key is invalid. diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskItem.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskItem.java deleted file mode 100644 index 25a7c441b6b3e..0000000000000 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskItem.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.microsoft.azure.management.resources.fluentcore.model.implementation; - -import com.microsoft.azure.TaskItem; -import rx.Observable; -import rx.functions.Action1; -import rx.schedulers.Schedulers; - -/** - * Represents a task that creates a resource when executed. - * - * @param the type of the resource that this task creates - */ -public class CreatorTaskItem implements TaskItem { - private CreatorTaskGroup.ResourceCreator resourceCreator; - private ResourceT created; - - /** - * Creates CreatorTaskItem. - * - * @param resourceCreator the resource creator - */ - public CreatorTaskItem(CreatorTaskGroup.ResourceCreator resourceCreator) { - this.resourceCreator = resourceCreator; - } - - @Override - public ResourceT result() { - return created; - } - - @Override - public Observable executeAsync() { - return this.resourceCreator.executeCreateOrUpdateAsync() - .subscribeOn(Schedulers.io()) - .doOnNext(new Action1() { - @Override - public void call(ResourceT resourceT) { - created = resourceT; - } - }); - } -} 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 5bee4c82c1159..6664650aba359 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 @@ -279,15 +279,6 @@ public DeploymentImpl beginCreate() { return this; } - @Override - public DeploymentImpl create() { - if (this.creatableResourceGroup != null) { - this.creatableResourceGroup.create(); - } - createResource(); - return this; - } - @Override public Observable createAsync() { Observable observable; @@ -306,11 +297,6 @@ public Observable call(ResourceGroup resourceGroup) { return observable; } - @Override - public Deployment refresh() { - return null; - } - @Override public Observable createResourceAsync() { DeploymentInner inner = new DeploymentInner() @@ -326,6 +312,11 @@ public Observable createResourceAsync() { @Override public Observable applyAsync() { + return updateResourceAsync(); + } + + @Override + public Observable updateResourceAsync() { try { if (this.templateLink() != null && this.template() != null) { this.withTemplate(null); @@ -336,6 +327,16 @@ public Observable applyAsync() { } catch (IOException e) { return Observable.error(e); } - return this.createAsync(); + return createResourceAsync(); + } + + @Override + public Deployment refresh() { + return null; + } + + @Override + public boolean isInCreateMode() { + return this.inner().id() == null; } } 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 f0466e8cabbfc..fd82e163887c2 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 @@ -116,23 +116,7 @@ public GenericResourceImpl withApiVersion(String apiVersion) { return this; } - @Override - public GenericResourceImpl create() { - createResource(); - return this; - } - - @Override - public Observable createAsync() { - return createResourceAsync(); - } - - @Override - public Observable applyUpdateAsync() { - return createResourceAsync(); - } - - // CreatorTaskGroup.ResourceCreator implementation + // CreateUpdateTaskGroup.ResourceCreator implementation @Override public Observable createResourceAsync() { return client.createOrUpdateAsync( 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 f4539bd31e681..615771c6e140e 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 @@ -112,17 +112,6 @@ public ResourceGroupImpl withoutTag(String key) { return this; } - @Override - public Observable applyAsync() { - return createAsync(); - } - - @Override - public ResourceGroupImpl refresh() { - this.setInner(client.get(this.key)); - return this; - } - @Override public Observable createResourceAsync() { ResourceGroupInner params = new ResourceGroupInner(); @@ -131,4 +120,20 @@ public Observable createResourceAsync() { return client.createOrUpdateAsync(this.name(), params) .map(innerToFluentMap(this)); } + + @Override + public Observable updateResourceAsync() { + return createResourceAsync(); + } + + @Override + public boolean isInCreateMode() { + return this.inner().id() == null; + } + + @Override + public ResourceGroupImpl refresh() { + this.setInner(client.get(this.key)); + return this; + } } 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 50210e5b4dc26..b3aff56792328 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 @@ -188,7 +188,7 @@ public StorageAccountImpl update() { } @Override - public Observable applyUpdateAsync() { + public Observable updateResourceAsync() { return client.updateAsync(resourceGroupName(), name(), updateParameters) .map(innerToFluentMap(this)); } @@ -231,7 +231,7 @@ public StorageAccountImpl withAccessTier(AccessTier accessTier) { return this; } - // CreatorTaskGroup.ResourceCreator implementation + // CreateUpdateTaskGroup.ResourceCreator implementation @Override public Observable createResourceAsync() { createParameters.withLocation(this.regionName()); diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java index 153c38f2592a3..627adbe6b634d 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java @@ -122,6 +122,16 @@ public T getNodeData(String key) { return graph.get(key).data(); } + /** + * Checks whether graph contains a node with the given key. + * + * @param key the key of the node + * @return true if there is a node with given key false otherwise. + */ + public boolean containsNodeWithKey(String key) { + return graph.containsKey(key); + } + /** * Reports that a node is resolved hence other nodes depends on it can consume it. * From 1056ee16236ea0e6b09aa49cef2870a54c2a6434 Mon Sep 17 00:00:00 2001 From: anuchan Date: Fri, 16 Sep 2016 15:05:39 -0700 Subject: [PATCH 2/8] Introducing ApplicableImpl and making Applicable and CreatableImpl derive from CreatableUpdatableImpl --- .../models/implementation/ResourceImpl.java | 13 --- .../model/implementation/CreatableImpl.java | 84 +++-------------- .../CreatableUpdatableImpl.java | 94 +++++++++++++++++-- .../implementation/CreateUpdateTaskGroup.java | 38 +++----- .../model/implementation/UpdatableImpl.java | 31 ++++++ 5 files changed, 144 insertions(+), 116 deletions(-) create mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/UpdatableImpl.java diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/ResourceImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/ResourceImpl.java index ba588ad764b3f..d431a376444bb 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/ResourceImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/ResourceImpl.java @@ -10,7 +10,6 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.model.Wrapper; import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableUpdatableImpl; -import rx.Observable; import java.util.ArrayList; import java.util.Collection; @@ -156,18 +155,6 @@ public boolean isInCreateMode() { return this.inner().id() == null; } - /** - * This is the default implementation of updateResourceAsync, it simply calls createResourceAsync() since - * for most of the resource both create and update are handled by the same API call (CreateOrUpdate). - * A resource that uses different API call for update should override updateResourceAsync. - * - * @return the updated resource. - */ - @Override - public Observable updateResourceAsync() { - return this.createResourceAsync(); - } - protected List innersFromWrappers(Collection> wrappers) { return innersFromWrappers(wrappers, null); } 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 46e56c82dd898..bbb2d65a2ef3e 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 @@ -6,11 +6,8 @@ package com.microsoft.azure.management.resources.fluentcore.model.implementation; -import com.microsoft.rest.ServiceCall; -import com.microsoft.rest.ServiceCallback; -import com.microsoft.rest.ServiceResponse; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import rx.Observable; -import rx.functions.Func1; /** * The base class for all creatable resource. @@ -19,73 +16,20 @@ * @param the model inner type that the fluent model type wraps * @param the fluent model implementation type */ -public abstract class CreatableImpl> - extends IndexableRefreshableWrapperImpl { - /** - * The name of the creatable resource. - */ - private String name; - - protected CreatableImpl(String name, InnerModelT innerObject) { - super(innerObject); - this.name = name; - } - - /** - * @return the name of the creatable resource. - */ - public String name() { - return this.name; - } - - /** - * Creates the resource asynchronously and return a observable to track the - * asynchronous operation. - * - * @return an observable stream that emits the resource when it is created - */ - abstract Observable createAsync(); - - /** - * Default implementation to create the resource asynchronously. - * - * @param callback the callback to handle success and failure - * @return a handle to cancel the request - */ - public ServiceCall createAsync(final ServiceCallback callback) { - return observableToFuture(createAsync(), callback); - } - - /** - * Default implementation of create(). - * - * @return the created resource - */ - @SuppressWarnings("unchecked") - public FluentModelT create() { - return createAsync().toBlocking().single(); - } - - @SuppressWarnings("unchecked") - protected Func1 innerToFluentMap(final FluentModelImplT fluentModelImplT) { - return new Func1() { - @Override - public FluentModelT call(InnerModelT innerModelT) { - fluentModelImplT.setInner(innerModelT); - return (FluentModelT) fluentModelImplT; - } - }; +abstract public class CreatableImpl< + FluentModelT, + InnerModelT, + FluentModelImplT extends IndexableRefreshableWrapperImpl> + extends + CreatableUpdatableImpl + implements + Creatable { + public CreatableImpl(String name, InnerModelT innerObject) { + super(name, innerObject); } - protected ServiceCall observableToFuture(Observable observable, final ServiceCallback callback) { - return ServiceCall.create( - observable.map(new Func1>() { - @Override - public ServiceResponse call(FluentModelT fluentModelT) { - // TODO: When https://github.com/Azure/azure-sdk-for-java/issues/1029 is done, this map (and this method) can be removed - return new ServiceResponse<>(fluentModelT, null); - } - }), callback - ); + @Override + public final Observable applyAsync() { + throw new IllegalStateException("Internal Error: updateAsync cannot be called from CreatableImpl"); } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java index 07d78e276cd7a..c891f6412e729 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java @@ -11,10 +11,12 @@ import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; +import com.microsoft.rest.ServiceResponse; import rx.Observable; +import rx.functions.Func1; /** - * The base class for all updatable resource. + * The base class for all creatable and updatable resource. * * @param the fluent model type representing the resource * @param the model inner type that the fluent model type wraps @@ -24,10 +26,16 @@ public abstract class CreatableUpdatableImpl< FluentModelT, InnerModelT, FluentModelImplT extends IndexableRefreshableWrapperImpl> - extends CreatableImpl + extends IndexableRefreshableWrapperImpl implements Appliable, + Creatable, CreateUpdateTaskGroup.ResourceCreatorUpdator { + /** + * The name of the creatable, updatable resource. + */ + private final String name; + /** * The group of tasks to create or update this resource and it's dependencies. */ @@ -40,10 +48,18 @@ public abstract class CreatableUpdatableImpl< * @param innerObject the inner object */ protected CreatableUpdatableImpl(String name, InnerModelT innerObject) { - super(name, innerObject); + super(innerObject); + this.name = name; createUpdateTaskGroup = new CreateUpdateTaskGroup<>(this.key(), this); } + /** + * @return the name of the creatable updatable resource. + */ + public String name() { + return this.name; + } + /** * @return the task group containing the tasks that creates or updates resources that this * resource depends on. @@ -66,12 +82,12 @@ protected Resource createdResource(String key) { /** * Add a creatable resource dependency for this resource. * - * @param creatableResource the creatable dependency. + * @param creatable the creatable dependency. */ @SuppressWarnings("unchecked") - protected void addCreatableDependency(Creatable creatableResource) { + protected void addCreatableDependency(Creatable creatable) { CreateUpdateTaskGroup childGroup = - ((CreateUpdateTaskGroup.ResourceCreatorUpdator) creatableResource).creatorUpdatorTaskGroup(); + ((CreateUpdateTaskGroup.ResourceCreatorUpdator) creatable).creatorUpdatorTaskGroup(); childGroup.merge(this.createUpdateTaskGroup); } @@ -99,6 +115,26 @@ public Observable createAsync() { return this.executeTaskGroupAsync(); } + /** + * Default implementation to create the resource asynchronously. + * + * @param callback the callback to handle success and failure + * @return a handle to cancel the request + */ + public ServiceCall createAsync(final ServiceCallback callback) { + return observableToFuture(createAsync(), callback); + } + + /** + * Default implementation of create(). + * + * @return the created resource + */ + @SuppressWarnings("unchecked") + public FluentModelT create() { + return createAsync().toBlocking().single(); + } + /** * Default implementation of applyAsync(). * @@ -109,15 +145,37 @@ public Observable applyAsync() { return this.executeTaskGroupAsync(); } + /** + * Default implementation to update the resource asynchronously. + * + * @param callback the callback to handle success and failure + * @return a handle to cancel the request + */ @Override public ServiceCall applyAsync(ServiceCallback callback) { return observableToFuture(applyAsync(), callback); } + /** + * Default implementation of apply(). + * + * @return the created resource + */ @Override public FluentModelT apply() { return applyAsync().toBlocking().last(); } + /** + * This is the default implementation of 'updateResourceAsync', it simply calls createResourceAsync() + * since for most of the resource both create and update are handled by the same API call + * (CreateOrUpdate). A resource that uses different API call for update should override updateResourceAsync. + * + * @return the updated resource. + */ + @Override + public Observable updateResourceAsync() { + return this.createResourceAsync(); + } @SuppressWarnings("unchecked") protected Observable executeTaskGroupAsync() { @@ -127,4 +185,28 @@ protected Observable executeTaskGroupAsync() { } throw new IllegalStateException("Internal Error: executeTaskGroupAsync can be called only on preparer"); } + + @SuppressWarnings("unchecked") + protected Func1 innerToFluentMap(final FluentModelImplT fluentModelImplT) { + return new Func1() { + @Override + public FluentModelT call(InnerModelT innerModelT) { + fluentModelImplT.setInner(innerModelT); + return (FluentModelT) fluentModelImplT; + } + }; + } + + protected ServiceCall observableToFuture(Observable observable, + final ServiceCallback callback) { + return ServiceCall.create( + observable.map(new Func1>() { + @Override + public ServiceResponse call(FluentModelT fluentModelT) { + // TODO: When https://github.com/Azure/azure-sdk-for-java/issues/1029 is done, this map (and this method) can be removed + return new ServiceResponse<>(fluentModelT, null); + } + }), callback + ); + } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTaskGroup.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTaskGroup.java index 63cdf6f6157fa..a02882a8ed06c 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTaskGroup.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreateUpdateTaskGroup.java @@ -14,44 +14,28 @@ public class CreateUpdateTaskGroup extends TaskGroupBase the type of the resource that this creatorUpdator can create or update */ - interface ResourceCreatorUpdator - extends ResourceCreator, ResourceUpdator { + interface ResourceCreatorUpdator { /** - * @return true if this creatorUpdator is in create mode. - */ - boolean isInCreateMode(); - /** - * @return Gets the task group. + * Creates the resource asynchronously. + * + * @return the observable reference */ - CreateUpdateTaskGroup creatorUpdatorTaskGroup(); - } + Observable createResourceAsync(); - /** - * Represents a type that know how to update resource. - * - * @param the type of the resource that this updator updates - */ - interface ResourceUpdator { /** * Update the resource asynchronously. * * @return the observable reference */ Observable updateResourceAsync(); - } - - /** - * Represents a type that know how to create resource. - * - * @param the type of the resource that this creator creates - */ - interface ResourceCreator { /** - * Creates the resource asynchronously. - * - * @return the observable reference + * @return true if this creatorUpdator is in create mode. */ - Observable createResourceAsync(); + boolean isInCreateMode(); + /** + * @return Gets the task group. + */ + CreateUpdateTaskGroup creatorUpdatorTaskGroup(); } /** diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/UpdatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/UpdatableImpl.java new file mode 100644 index 0000000000000..2d27cb58e1fd1 --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/UpdatableImpl.java @@ -0,0 +1,31 @@ +package com.microsoft.azure.management.resources.fluentcore.model.implementation; + +import com.microsoft.azure.management.resources.fluentcore.model.Appliable; +import com.microsoft.azure.management.resources.fluentcore.model.Updatable; +import rx.Observable; + +/** + * The base class for all updatable resource. + * + * @param the fluent model type representing the creatable resource + * @param the model inner type that the fluent model type wraps + * @param the fluent model implementation type + */ +abstract public class UpdatableImpl< + FluentModelT, + InnerModelT, + FluentModelImplT extends IndexableRefreshableWrapperImpl> + extends + CreatableUpdatableImpl + implements + Updatable, + Appliable { + public UpdatableImpl(String name, InnerModelT innerObject) { + super(name, innerObject); + } + + @Override + public final Observable createAsync() { + throw new IllegalStateException("Internal Error: createAsync cannot be called from UpdatableImpl"); + } +} From ad657d507d2655ac01ef46418135236c5fa3c03a Mon Sep 17 00:00:00 2001 From: anuchan Date: Fri, 16 Sep 2016 15:22:05 -0700 Subject: [PATCH 3/8] removed unused method --- .../src/main/java/com/microsoft/azure/DAGraph.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java index 627adbe6b634d..153c38f2592a3 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java @@ -122,16 +122,6 @@ public T getNodeData(String key) { return graph.get(key).data(); } - /** - * Checks whether graph contains a node with the given key. - * - * @param key the key of the node - * @return true if there is a node with given key false otherwise. - */ - public boolean containsNodeWithKey(String key) { - return graph.containsKey(key); - } - /** * Reports that a node is resolved hence other nodes depends on it can consume it. * From dd0745361802cbbe37b56d4b255633771bf52cab Mon Sep 17 00:00:00 2001 From: anuchan Date: Fri, 16 Sep 2016 16:09:17 -0700 Subject: [PATCH 4/8] Fixing some checkstyle issues --- .../{UpdatableImpl.java => AppliableImpl.java} | 10 ++++++++-- .../fluentcore/model/implementation/CreatableImpl.java | 8 +++++++- 2 files changed, 15 insertions(+), 3 deletions(-) rename azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/{UpdatableImpl.java => AppliableImpl.java} (81%) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/UpdatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java similarity index 81% rename from azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/UpdatableImpl.java rename to azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java index 2d27cb58e1fd1..c03500db1b503 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/UpdatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java @@ -11,7 +11,7 @@ * @param the model inner type that the fluent model type wraps * @param the fluent model implementation type */ -abstract public class UpdatableImpl< +abstract public class AppliableImpl< FluentModelT, InnerModelT, FluentModelImplT extends IndexableRefreshableWrapperImpl> @@ -20,7 +20,13 @@ abstract public class UpdatableImpl< implements Updatable, Appliable { - public UpdatableImpl(String name, InnerModelT innerObject) { + /** + * Creates an AppliableImpl. + * + * @param name the appliable name + * @param innerObject the inner object + */ + protected AppliableImpl(String name, InnerModelT innerObject) { super(name, innerObject); } 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 bbb2d65a2ef3e..6e40bf6ab5135 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 @@ -24,7 +24,13 @@ abstract public class CreatableImpl< CreatableUpdatableImpl implements Creatable { - public CreatableImpl(String name, InnerModelT innerObject) { + /** + * Creates a CreatableImpl. + * + * @param name the creatable name + * @param innerObject the inner object + */ + protected CreatableImpl(String name, InnerModelT innerObject) { super(name, innerObject); } From 9960f4c9254907ab577c07716cb03bd477084409 Mon Sep 17 00:00:00 2001 From: anuchan Date: Fri, 16 Sep 2016 16:16:27 -0700 Subject: [PATCH 5/8] Exposing addApplicableDependency --- .../model/implementation/CreatableUpdatableImpl.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java index c891f6412e729..84b4efc4dc3df 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java @@ -91,6 +91,18 @@ protected void addCreatableDependency(Creatable creatable) { childGroup.merge(this.createUpdateTaskGroup); } + /** + * Add an updatable resource dependency for this resource. + * + * @param appliable the applicable dependency. + */ + @SuppressWarnings("unchecked") + protected void addAppliableDependency(Appliable appliable) { + CreateUpdateTaskGroup childGroup = + ((CreateUpdateTaskGroup.ResourceCreatorUpdator) appliable).creatorUpdatorTaskGroup(); + childGroup.merge(this.createUpdateTaskGroup); + } + /** * Begins an update for a new resource. *

From e4ac487c2253cd57430e5d7bfd332b85818759d4 Mon Sep 17 00:00:00 2001 From: anuchan Date: Fri, 16 Sep 2016 17:04:36 -0700 Subject: [PATCH 6/8] checkstyle fix --- .../fluentcore/model/implementation/AppliableImpl.java | 2 +- .../fluentcore/model/implementation/CreatableImpl.java | 2 +- .../fluentcore/model/implementation/CreatableUpdatableImpl.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java index c03500db1b503..7b1d305c7fa83 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java @@ -11,7 +11,7 @@ * @param the model inner type that the fluent model type wraps * @param the fluent model implementation type */ -abstract public class AppliableImpl< +public abstract class AppliableImpl< FluentModelT, InnerModelT, FluentModelImplT extends IndexableRefreshableWrapperImpl> 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 6e40bf6ab5135..cfc180ef14c98 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 @@ -16,7 +16,7 @@ * @param the model inner type that the fluent model type wraps * @param the fluent model implementation type */ -abstract public class CreatableImpl< +public abstract class CreatableImpl< FluentModelT, InnerModelT, FluentModelImplT extends IndexableRefreshableWrapperImpl> diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java index 84b4efc4dc3df..718d7ff14e75d 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableUpdatableImpl.java @@ -94,7 +94,7 @@ protected void addCreatableDependency(Creatable creatable) { /** * Add an updatable resource dependency for this resource. * - * @param appliable the applicable dependency. + * @param appliable the appliable dependency. */ @SuppressWarnings("unchecked") protected void addAppliableDependency(Appliable appliable) { From 7e9d86ec484536b0fd24928c149590c2a0866a59 Mon Sep 17 00:00:00 2001 From: anuchan Date: Wed, 21 Sep 2016 18:18:43 -0700 Subject: [PATCH 7/8] addressing rc --- .../fluentcore/model/implementation/AppliableImpl.java | 8 ++++++++ .../fluentcore/model/implementation/CreatableImpl.java | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java index 7b1d305c7fa83..b12486b6b5b11 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java @@ -34,4 +34,12 @@ protected AppliableImpl(String name, InnerModelT innerObject) { public final Observable createAsync() { throw new IllegalStateException("Internal Error: createAsync cannot be called from UpdatableImpl"); } + + @Override + public final Observable createResourceAsync() { + throw new IllegalStateException("Internal Error: createResourceAsync cannot be called from UpdatableImpl"); + } + + @Override + public abstract Observable updateResourceAsync(); } 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 cfc180ef14c98..09e90ecdc4762 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 @@ -38,4 +38,12 @@ protected CreatableImpl(String name, InnerModelT innerObject) { public final Observable applyAsync() { throw new IllegalStateException("Internal Error: updateAsync cannot be called from CreatableImpl"); } + + @Override + public final Observable updateResourceAsync() { + throw new IllegalStateException("Internal Error: updateResourceAsync cannot be called from CreatableImpl"); + } + + @Override + public abstract Observable createResourceAsync(); } From 2c2a8d77060a7ba7c993b3da9b98d9ceab3e2c88 Mon Sep 17 00:00:00 2001 From: anuchan Date: Wed, 21 Sep 2016 20:52:38 -0700 Subject: [PATCH 8/8] changing redis and vmss create-update to use new pattern --- .../implementation/VirtualMachineScaleSetImpl.java | 10 ---------- .../redis/implementation/RedisCacheImpl.java | 2 +- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java index fd1672da35d13..8c5929ce63e39 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java @@ -837,16 +837,6 @@ protected void afterCreating() { this.initializeChildrenFromInner(); } - @Override - public Observable applyAsync() { - return this.createAsync(); - } - - @Override - public Observable applyUpdateAsync() { - return this.createResourceAsync(); - } - @Override public VirtualMachineScaleSetImpl refresh() { VirtualMachineScaleSetInner inner = this.client.get(this.resourceGroupName(), this.name()); diff --git a/azure-mgmt-redis/src/main/java/com/microsoft/azure/management/redis/implementation/RedisCacheImpl.java b/azure-mgmt-redis/src/main/java/com/microsoft/azure/management/redis/implementation/RedisCacheImpl.java index 6d9961d404905..218151d942d0d 100644 --- a/azure-mgmt-redis/src/main/java/com/microsoft/azure/management/redis/implementation/RedisCacheImpl.java +++ b/azure-mgmt-redis/src/main/java/com/microsoft/azure/management/redis/implementation/RedisCacheImpl.java @@ -458,7 +458,7 @@ public RedisCacheImpl update() { } @Override - public Observable applyUpdateAsync() { + public Observable updateResourceAsync() { return client.updateAsync(resourceGroupName(), name(), updateParameters) .map(innerToFluentMap(this)) .doOnNext(new Action1() {