From 573978615699a1c2bb2f4195e1ab47575c686215 Mon Sep 17 00:00:00 2001 From: anuchan Date: Tue, 2 Aug 2016 13:49:10 -0700 Subject: [PATCH 1/2] Extending DeploymentOperationsImpl to extends from ReadableWrappersImpl, fixing https://github.com/Azure/azure-sdk-for-java/issues/999 and adding DefinitionStages and UpdateStages interfaces to GenericResource --- .../management/resources/GenericResource.java | 272 ++++++++++-------- .../resources/GenericResources.java | 2 +- .../DeploymentOperationsImpl.java | 20 +- .../implementation/GenericResourceImpl.java | 11 +- .../implementation/GenericResourcesImpl.java | 2 +- 5 files changed, 159 insertions(+), 148 deletions(-) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResource.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResource.java index 603e78f4ac28..81f19543668f 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResource.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResource.java @@ -21,7 +21,7 @@ public interface GenericResource extends GroupableResource, Refreshable, - Updatable, + Updatable, Wrapper { /** * @return the namespace of the resource provider @@ -54,169 +54,193 @@ public interface GenericResource extends Object properties(); /** - * A generic resource definition allowing region to be specified. + * The entirety of the generic resource definition. */ - interface DefinitionBlank extends GroupableResource.DefinitionWithRegion { + interface Definition extends + DefinitionStages.Blank, + DefinitionStages.WithGroup, + DefinitionStages.WithResourceType, + DefinitionStages.WithProviderNamespace, + DefinitionStages.WithOrWithoutParentResource, + DefinitionStages.WithPlan, + DefinitionStages.WithApiVersion, + DefinitionStages.WithCreate { } /** - * A generic resource definition allowing resource group to be specified. + * Grouping of generic resource definition stages. */ - interface DefinitionWithGroup extends GroupableResource.DefinitionStages.WithGroup { - } - - /** - * A generic resource definition allowing resource type to be specified. - */ - interface DefinitionWithResourceType { + interface DefinitionStages { /** - * Specifies the resource's type. - * - * @param resourceType the type of the resources - * @return the next stage of generic resource definition + * A generic resource definition allowing region to be specified. */ - DefinitionWithProviderNamespace withResourceType(String resourceType); - } + interface Blank extends GroupableResource.DefinitionWithRegion { + } - /** - * A generic resource definition allowing provider namespace to be specified. - */ - interface DefinitionWithProviderNamespace { /** - * Specifies the resource provider's namespace. - * - * @param resourceProviderNamespace the namespace of the resource provider - * @return the next stage of the generic resource definition + * A generic resource definition allowing resource group to be specified. */ - DefinitionWithOrWithoutParentResource withProviderNamespace(String resourceProviderNamespace); - } + interface WithGroup extends GroupableResource.DefinitionStages.WithGroup { + } - /** - * A generic resource definition allowing parent resource to be specified. - */ - interface DefinitionWithOrWithoutParentResource extends DefinitionWithPlan { /** - * Specifies the parent resource. - * - * @param parentResourceId the parent resource id - * @return the next stage of the generic resource definition + * A generic resource definition allowing resource type to be specified. */ - DefinitionWithPlan withParentResource(String parentResourceId); // ParentResource is optional so user can navigate to DefinitionWithPlan with or without it. - } + interface WithResourceType { + /** + * Specifies the resource's type. + * + * @param resourceType the type of the resources + * @return the next stage of generic resource definition + */ + WithProviderNamespace withResourceType(String resourceType); + } - /** - * A generic resource definition allowing plan to be specified. - */ - interface DefinitionWithPlan { /** - * Specifies the plan of the resource. The plan can only be set for 3rd party resources. - * - * @param name the name of the plan - * @param publisher the publisher of the plan - * @param product the name of the product - * @param promotionCode the promotion code, if any - * @return the next stage of the generic resource definition + * A generic resource definition allowing provider namespace to be specified. */ - DefinitionWithApiVersion withPlan(String name, String publisher, String product, String promotionCode); + interface WithProviderNamespace { + /** + * Specifies the resource provider's namespace. + * + * @param resourceProviderNamespace the namespace of the resource provider + * @return the next stage of the generic resource definition + */ + WithOrWithoutParentResource withProviderNamespace(String resourceProviderNamespace); + } /** - * Specifies the plan of the resource. - * - * @return the next stage of the generic resource definition + * A generic resource definition allowing parent resource to be specified. */ - DefinitionWithApiVersion withoutPlan(); - } + interface WithOrWithoutParentResource extends WithPlan { + /** + * Specifies the parent resource. + * + * @param parentResourceId the parent resource id + * @return the next stage of the generic resource definition + */ + WithPlan withParentResource(String parentResourceId); // ParentResource is optional so user can navigate to WithPlan with or without it. + } - /** - * A generic resource definition allowing api version to be specified. - */ - interface DefinitionWithApiVersion { /** - * Specifies the api version. - * - * @param apiVersion the API version of the resource - * @return the next stage of the generic resource definition + * A generic resource definition allowing plan to be specified. */ - DefinitionCreatable withApiVersion(String apiVersion); - } + interface WithPlan { + /** + * Specifies the plan of the resource. The plan can only be set for 3rd party resources. + * + * @param name the name of the plan + * @param publisher the publisher of the plan + * @param product the name of the product + * @param promotionCode the promotion code, if any + * @return the next stage of the generic resource definition + */ + WithApiVersion withPlan(String name, String publisher, String product, String promotionCode); + + /** + * Specifies the plan of the resource. + * + * @return the next stage of the generic resource definition + */ + WithApiVersion withoutPlan(); + } - /** - * A deployment definition with sufficient inputs to create a new - * resource in the cloud, but exposing additional optional inputs to - * specify. - */ - interface DefinitionCreatable extends - Creatable, - Resource.DefinitionWithTags { /** - * Specifies other properties. - * - * @param properties the properties object - * @return the next stage of generic resource definition + * A generic resource definition allowing api version to be specified. */ - DefinitionCreatable withProperties(Object properties); - } + interface WithApiVersion { + /** + * Specifies the api version. + * + * @param apiVersion the API version of the resource + * @return the next stage of the generic resource definition + */ + WithCreate withApiVersion(String apiVersion); + } - /** - * A generic resource update allowing to change the resource properties. - */ - interface UpdateWithProperties { /** - * Specifies other properties of the resource. - * - * @param properties the properties object - * @return the next stage of generic resource update + * A deployment definition with sufficient inputs to create a new + * resource in the cloud, but exposing additional optional inputs to + * specify. */ - Update withProperties(Object properties); + interface WithCreate extends + Creatable, + Resource.DefinitionWithTags { + /** + * Specifies other properties. + * + * @param properties the properties object + * @return the next stage of generic resource definition + */ + WithCreate withProperties(Object properties); + } } /** - * A generic resource update allowing to change the parent resource. + * Grouping of generic resource update stages. */ - interface UpdateWithParentResource { + interface UpdateStages { /** - * Specifies the parent resource. - * - * @param parentResourceId the parent resource ID - * @return the next stage of the generic resource definition + * A generic resource update allowing to change the resource properties. */ - Update withParentResource(String parentResourceId); - } + interface WithProperties { + /** + * Specifies other properties of the resource. + * + * @param properties the properties object + * @return the next stage of generic resource update + */ + Update withProperties(Object properties); + } - /** - * A generic resource update allowing to change the resource plan. - */ - interface UpdateWithPlan { /** - * Specifies the plan of the resource. - * - * @param name the name of the plan - * @param publisher the publisher of the plan - * @param product the name of the product - * @param promotionCode the promotion code, if any - * @return the next stage of the generic resource update + * A generic resource update allowing to change the parent resource. */ - Update withPlan(String name, String publisher, String product, String promotionCode); + interface WithParentResource { + /** + * Specifies the parent resource. + * + * @param parentResourceId the parent resource ID + * @return the next stage of the generic resource definition + */ + Update withParentResource(String parentResourceId); + } /** - * Specifies the plan of the resource. - * - * @return the next stage of the generic resource update + * A generic resource update allowing to change the resource plan. */ - Update withoutPlan(); - } + interface WithPlan { + /** + * Specifies the plan of the resource. + * + * @param name the name of the plan + * @param publisher the publisher of the plan + * @param product the name of the product + * @param promotionCode the promotion code, if any + * @return the next stage of the generic resource update + */ + Update withPlan(String name, String publisher, String product, String promotionCode); + + /** + * Specifies the plan of the resource. + * + * @return the next stage of the generic resource update + */ + Update withoutPlan(); + } - /** - * The template for a generic resource update operation for specifying the resource provider API version. - */ - interface UpdateWithApiVersion { /** - * Specifies the API version of the resource provider. - * - * @param apiVersion the API version - * @return the next stage of the generic resource update + * The template for a generic resource update operation for specifying the resource provider API version. */ - Update withApiVersion(String apiVersion); + interface WithApiVersion { + /** + * Specifies the API version of the resource provider. + * + * @param apiVersion the API version + * @return the next stage of the generic resource update + */ + Update withApiVersion(String apiVersion); + } } /** @@ -224,9 +248,9 @@ interface UpdateWithApiVersion { */ interface Update extends Appliable, - UpdateWithPlan, - UpdateWithParentResource, - UpdateWithProperties, + UpdateStages.WithPlan, + UpdateStages.WithParentResource, + UpdateStages.WithProperties, Resource.UpdateWithTags { } } \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResources.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResources.java index d4ad3640329e..1a70853b83e4 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResources.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResources.java @@ -20,7 +20,7 @@ public interface GenericResources extends SupportsListingByGroup, SupportsGettingById, - SupportsCreating { + SupportsCreating { /** * Checks if a resource exists in a resource group. * diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentOperationsImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentOperationsImpl.java index 5a3ee01f0580..de76b5e66554 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentOperationsImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentOperationsImpl.java @@ -11,42 +11,36 @@ import com.microsoft.azure.management.resources.Deployment; import com.microsoft.azure.management.resources.DeploymentOperation; import com.microsoft.azure.management.resources.DeploymentOperations; -import com.microsoft.azure.management.resources.fluentcore.utils.PagedListConverter; - +import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.ReadableWrappersImpl; import java.io.IOException; /** * The implementation of {@link DeploymentOperations}. */ final class DeploymentOperationsImpl + extends ReadableWrappersImpl implements DeploymentOperations { private final DeploymentOperationsInner client; private final Deployment deployment; - private final PagedListConverter converter; DeploymentOperationsImpl(final DeploymentOperationsInner client, final Deployment deployment) { this.client = client; this.deployment = deployment; - converter = new PagedListConverter() { - @Override - public DeploymentOperation typeConvert(DeploymentOperationInner deploymentInner) { - return createFluentModel(deploymentInner); - } - }; } @Override public PagedList list() throws CloudException, IOException { - return converter.convert(client.list(deployment.resourceGroupName(), deployment.name()).getBody()); + return wrapList(client.list(deployment.resourceGroupName(), deployment.name()).getBody()); } @Override public DeploymentOperation getById(String operationId) throws CloudException, IllegalArgumentException, IOException { - return createFluentModel(client.get(deployment.resourceGroupName(), deployment.name(), operationId).getBody()); + return wrapModel(client.get(deployment.resourceGroupName(), deployment.name(), operationId).getBody()); } - private DeploymentOperationImpl createFluentModel(DeploymentOperationInner deploymentOperationInner) { - return new DeploymentOperationImpl(deploymentOperationInner, this.client); + @Override + protected DeploymentOperationImpl wrapModel(DeploymentOperationInner inner) { + return new DeploymentOperationImpl(inner, this.client); } } 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 857cb16d5716..e6778969f710 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 @@ -25,15 +25,8 @@ final class GenericResourceImpl ResourceManager> implements GenericResource, - GenericResource.DefinitionBlank, - GenericResource.DefinitionWithGroup, - GenericResource.DefinitionWithResourceType, - GenericResource.DefinitionWithProviderNamespace, - GenericResource.DefinitionWithOrWithoutParentResource, - GenericResource.DefinitionWithPlan, - GenericResource.DefinitionWithApiVersion, - GenericResource.DefinitionCreatable, - GenericResource.UpdateWithApiVersion, + GenericResource.Definition, + GenericResource.UpdateStages.WithApiVersion, GenericResource.Update { private final ResourcesInner client; private String resourceProviderNamespace; diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourcesImpl.java index 626ec3d84b7c..cc9f409d1efe 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourcesImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourcesImpl.java @@ -42,7 +42,7 @@ public PagedList listByGroup(String groupName) throws CloudExce } @Override - public GenericResource.DefinitionBlank define(String name) { + public GenericResource.DefinitionStages.Blank define(String name) { return new GenericResourceImpl( name, new GenericResourceInner(), From 654f089076223a268ab12d3f508d25edac5377c2 Mon Sep 17 00:00:00 2001 From: anuchan Date: Tue, 2 Aug 2016 16:40:07 -0700 Subject: [PATCH 2/2] Address review comment: simplify chain definition by defining withParentResource interface --- .../management/resources/GenericResource.java | 31 ++++++++++--------- .../resources/GenericResourcesTests.java | 2 +- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResource.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResource.java index 81f19543668f..3795d7dccae6 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResource.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/GenericResource.java @@ -61,7 +61,7 @@ interface Definition extends DefinitionStages.WithGroup, DefinitionStages.WithResourceType, DefinitionStages.WithProviderNamespace, - DefinitionStages.WithOrWithoutParentResource, + DefinitionStages.WithParentResource, DefinitionStages.WithPlan, DefinitionStages.WithApiVersion, DefinitionStages.WithCreate { @@ -106,20 +106,7 @@ interface WithProviderNamespace { * @param resourceProviderNamespace the namespace of the resource provider * @return the next stage of the generic resource definition */ - WithOrWithoutParentResource withProviderNamespace(String resourceProviderNamespace); - } - - /** - * A generic resource definition allowing parent resource to be specified. - */ - interface WithOrWithoutParentResource extends WithPlan { - /** - * Specifies the parent resource. - * - * @param parentResourceId the parent resource id - * @return the next stage of the generic resource definition - */ - WithPlan withParentResource(String parentResourceId); // ParentResource is optional so user can navigate to WithPlan with or without it. + WithPlan withProviderNamespace(String resourceProviderNamespace); } /** @@ -158,12 +145,26 @@ interface WithApiVersion { WithCreate withApiVersion(String apiVersion); } + /** + * A generic resource definition allowing parent resource to be specified. + */ + interface WithParentResource { + /** + * Specifies the parent resource. + * + * @param parentResourceId the parent resource id + * @return the next stage of the generic resource definition + */ + WithCreate withParentResource(String parentResourceId); + } + /** * A deployment definition with sufficient inputs to create a new * resource in the cloud, but exposing additional optional inputs to * specify. */ interface WithCreate extends + WithParentResource, Creatable, Resource.DefinitionWithTags { /** diff --git a/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/GenericResourcesTests.java b/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/GenericResourcesTests.java index ae73aecc023a..c47e13e92d06 100644 --- a/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/GenericResourcesTests.java +++ b/azure-mgmt-resources/src/test/java/com/microsoft/azure/management/resources/GenericResourcesTests.java @@ -47,9 +47,9 @@ public void canCreateUpdateMoveResource() throws Exception { .withExistingResourceGroup(rgName) .withResourceType("sites") .withProviderNamespace("Microsoft.Web") - .withParentResource("") .withoutPlan() .withApiVersion("2015-08-01") + .withParentResource("") .withProperties(new ObjectMapper().readTree("{\"SiteMode\":\"Limited\",\"ComputeMode\":\"Shared\"}")) .create(); //List