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 ca751ed202611..57fdb91833af0 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 @@ -82,7 +82,7 @@ public BatchAccount call(BatchAccountInner batchAccountInner) { } @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-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-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 6fbcacecc3839..aa63d4fa2f9a4 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 @@ -79,7 +79,7 @@ public Observable createResourceAsync() { } @Override - public Observable applyAsync() { - throw new UnsupportedOperationException("not implemented yet"); + 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 1514858b8dad5..ba1c22281d589 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 @@ -112,7 +112,7 @@ public Observable createResourceAsync() { } @Override - public Observable applyAsync() { - throw new UnsupportedOperationException("not implemented yet"); + 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 f96daad74c7c0..3d3a516f0b8a1 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 14833e8d68c6e..1ac0b6f569476 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 @@ -91,11 +91,6 @@ public NetworkSecurityGroupImpl refresh() { return this; } - @Override - public Observable applyUpdateAsync() { - return createResourceAsync(); - } - @Override public List listAssociatedSubnets() { final List subnetRefs = this.inner().subnets(); 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 17588ee11c255..8f4c7a99f6930 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,11 +45,6 @@ class PublicIpAddressImpl // Verbs - @Override - public Observable applyUpdateAsync() { - return this.createResourceAsync(); - } - @Override public PublicIpAddress refresh() { PublicIPAddressInner response = this.client.get(this.resourceGroupName(), this.name()); @@ -149,7 +144,7 @@ public String leafDomainLabel() { } } - // CreatorTaskGroup.ResourceCreator implementation + // CreateUpdateTaskGroup.ResourceCreator implementation @Override public Observable createResourceAsync() { // Clean up empty DNS settings 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() { 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..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; @@ -39,7 +38,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 +147,19 @@ 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 - */ - public abstract Observable applyUpdateAsync(); - - @Override - public Observable executeCreateOrUpdateAsync() { - if (this.isInCreateMode()) { - return createResourceAsync(); - } - else { - return applyUpdateAsync(); - } - } - /** * @return true if currently in define..create mode */ + @Override public boolean isInCreateMode() { return this.inner().id() == null; } - 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 +167,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/AppliableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java new file mode 100644 index 0000000000000..b12486b6b5b11 --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/AppliableImpl.java @@ -0,0 +1,45 @@ +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 + */ +public abstract class AppliableImpl< + FluentModelT, + InnerModelT, + FluentModelImplT extends IndexableRefreshableWrapperImpl> + extends + CreatableUpdatableImpl + implements + Updatable, + Appliable { + /** + * Creates an AppliableImpl. + * + * @param name the appliable name + * @param innerObject the inner object + */ + protected AppliableImpl(String name, InnerModelT innerObject) { + super(name, innerObject); + } + + @Override + 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 f3a509cde54a9..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 @@ -6,13 +6,8 @@ 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; import rx.Observable; -import rx.functions.Func1; /** * The base class for all creatable resource. @@ -21,121 +16,34 @@ * @param the model inner type that the fluent model type wraps * @param the fluent model implementation type */ -public abstract class CreatableImpl> - extends IndexableRefreshableWrapperImpl - implements CreatorTaskGroup.ResourceCreator { - /** - * 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. +public abstract class CreatableImpl< + FluentModelT, + InnerModelT, + FluentModelImplT extends IndexableRefreshableWrapperImpl> + extends + CreatableUpdatableImpl + implements + Creatable { + /** + * Creates a CreatableImpl. * - * @param creatableResource the creatable dependency. + * @param name the creatable name + * @param innerObject the inner object */ - @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); - } - - /** - * @return the name of the creatable resource. - */ - public String name() { - return this.name; - } - - /** - * Default implementation of create(). - * - * @return the created resource - */ - @SuppressWarnings("unchecked") - public FluentModelT create() { - return createAsync().toBlocking().single(); - } - - /** - * 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 - */ - public ServiceCall createAsync(final ServiceCallback callback) { - return observableToFuture(createAsync(), callback); - } - - /** - * Default implementation of createAsync(). - * - * @return the handle to the create REST call - */ - @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"); + protected CreatableImpl(String name, InnerModelT innerObject) { + super(name, innerObject); } - /** - * @return the task group associated with this creatable. - */ @Override - public CreatorTaskGroup creatorTaskGroup() { - return this.creatorTaskGroup; + public final Observable applyAsync() { + throw new IllegalStateException("Internal Error: updateAsync cannot be called from CreatableImpl"); } @Override - public FluentModelT createResource() { - return this.createResourceAsync().toBlocking().last(); + public final Observable updateResourceAsync() { + throw new IllegalStateException("Internal Error: updateResourceAsync cannot be called from CreatableImpl"); } @Override - public Observable executeCreateOrUpdateAsync() { - return this.createResourceAsync(); - } - - @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 - ); - } + public abstract Observable createResourceAsync(); } 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..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 @@ -6,12 +6,17 @@ 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 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 @@ -21,14 +26,81 @@ public abstract class CreatableUpdatableImpl< FluentModelT, InnerModelT, FluentModelImplT extends IndexableRefreshableWrapperImpl> - extends CreatableImpl< - FluentModelT, - InnerModelT, - FluentModelImplT> - implements Appliable { + 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. + */ + 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); + 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. + */ + @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 creatable the creatable dependency. + */ + @SuppressWarnings("unchecked") + protected void addCreatableDependency(Creatable creatable) { + CreateUpdateTaskGroup childGroup = + ((CreateUpdateTaskGroup.ResourceCreatorUpdator) creatable).creatorUpdatorTaskGroup(); + childGroup.merge(this.createUpdateTaskGroup); + } + + /** + * Add an updatable resource dependency for this resource. + * + * @param appliable the appliable dependency. + */ + @SuppressWarnings("unchecked") + protected void addAppliableDependency(Appliable appliable) { + CreateUpdateTaskGroup childGroup = + ((CreateUpdateTaskGroup.ResourceCreatorUpdator) appliable).creatorUpdatorTaskGroup(); + childGroup.merge(this.createUpdateTaskGroup); } /** @@ -45,13 +117,108 @@ 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 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(). + * + * @return the observable that emit the updated resource. + */ + @Override + 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() { + if (createUpdateTaskGroup.isPreparer()) { + createUpdateTaskGroup.prepare(); + return createUpdateTaskGroup.executeAsync().last(); + } + 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/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 55% 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..a02882a8ed06c 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,13 +8,13 @@ * * @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 { /** * Creates the resource asynchronously. * @@ -23,47 +23,43 @@ interface ResourceCreator { Observable createResourceAsync(); /** - * Creates the resource synchronously. + * Update the resource asynchronously. * - * @return the created resource + * @return the observable reference */ - T createResource(); - + Observable updateResourceAsync(); /** - * @return Gets the task group. + * @return true if this creatorUpdator is in create mode. */ - CreatorTaskGroup creatorTaskGroup(); - + boolean isInCreateMode(); /** - * Creates or updates the resource asynchronously. - * - * @return the observable reference + * @return Gets the task group. */ - Observable executeCreateOrUpdateAsync(); + CreateUpdateTaskGroup creatorUpdatorTaskGroup(); } /** - * 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());