Skip to content

Commit

Permalink
Merge pull request #679 from anuchandy/master
Browse files Browse the repository at this point in the history
Applying fluent model design patterns in existing models and collections.
  • Loading branch information
jianghaolu committed May 5, 2016
2 parents 0f8a407 + 4713ef4 commit 86eaef1
Show file tree
Hide file tree
Showing 59 changed files with 882 additions and 796 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public class AvailabilitySetImpl
// The client to make AvailabilitySet Management API calls
private final AvailabilitySetsInner client;

public AvailabilitySetImpl(String name, AvailabilitySetInner inner, AvailabilitySetsInner innerCollection,
ResourceGroups resourceGroups,
VirtualMachines virtualMachines) {
super(inner.id(), inner, resourceGroups);
public AvailabilitySetImpl(String name, AvailabilitySetInner innerModel,
final AvailabilitySetsInner client,
final ResourceGroups resourceGroups,
final VirtualMachines virtualMachines) {
super(innerModel.id(), innerModel, resourceGroups);
this.name = name;
this.client = innerCollection;
this.client = client;
this.virtualMachines = virtualMachines;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public class AvailabilitySetsImpl implements AvailabilitySets {
private final VirtualMachines virtualMachines;
private final PagedListConverter<AvailabilitySetInner, AvailabilitySet> converter;

public AvailabilitySetsImpl(AvailabilitySetsInner client, ResourceGroups resourceGroups, VirtualMachines virtualMachines) {
public AvailabilitySetsImpl(final AvailabilitySetsInner client,
final ResourceGroups resourceGroups,
final VirtualMachines virtualMachines) {
this.client = client;
this.resourceGroups = resourceGroups;
this.virtualMachines = virtualMachines;
Expand All @@ -38,14 +40,21 @@ public AvailabilitySet typeConvert(AvailabilitySetInner availabilitySetInner) {
}

@Override
public void delete(String groupName, String name) throws Exception {
this.client.delete(groupName, name);
}

@Override
public AvailabilitySet get(String groupName, String name) throws Exception {
ServiceResponse<AvailabilitySetInner> response = this.client.get(groupName, name);
return createFluentModel(response.getBody());
public PagedList<AvailabilitySet> list() throws CloudException, IOException {
return new GroupPagedList<AvailabilitySet>(resourceGroups.list()) {
@Override
public List<AvailabilitySet> listNextGroup(String resourceGroupName) throws RestException, IOException {
PageImpl<AvailabilitySetInner> page = new PageImpl<>();
page.setItems(client.list(resourceGroupName).getBody());
page.setNextPageLink(null);
return converter.convert(new PagedList<AvailabilitySetInner>(page) {
@Override
public Page<AvailabilitySetInner> nextPage(String nextPageLink) throws RestException, IOException {
return null;
}
});
}
};
}

@Override
Expand All @@ -61,6 +70,12 @@ public Page<AvailabilitySetInner> nextPage(String nextPageLink) throws RestExcep
});
}

@Override
public AvailabilitySet get(String groupName, String name) throws CloudException, IOException {
ServiceResponse<AvailabilitySetInner> response = this.client.get(groupName, name);
return createFluentModel(response.getBody());
}

@Override
public AvailabilitySet.DefinitionBlank define(String name) throws Exception {
return createFluentModel(name);
Expand All @@ -72,21 +87,8 @@ public void delete(String id) throws Exception {
}

@Override
public PagedList<AvailabilitySet> list() throws CloudException, IOException {
return new GroupPagedList<AvailabilitySet>(resourceGroups.list()) {
@Override
public List<AvailabilitySet> listNextGroup(String resourceGroupName) throws RestException, IOException {
PageImpl<AvailabilitySetInner> page = new PageImpl<>();
page.setItems(client.list(resourceGroupName).getBody());
page.setNextPageLink(null);
return converter.convert(new PagedList<AvailabilitySetInner>(page) {
@Override
public Page<AvailabilitySetInner> nextPage(String nextPageLink) throws RestException, IOException {
return null;
}
});
}
};
public void delete(String groupName, String name) throws Exception {
this.client.delete(groupName, name);
}

/** Fluent model create helpers **/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@
import com.microsoft.azure.management.resources.ResourceGroup;

import java.io.IOException;
import java.util.List;

public class AvailabilitySetsInGroup implements AvailabilitySets.InGroup {
private final ResourceGroup resourceGroup;
private final AvailabilitySets availabilitySetsCore;
private final AvailabilitySets availabilitySets;

public AvailabilitySetsInGroup(AvailabilitySets availabilitySetsCore, ResourceGroup resourceGroup) {
public AvailabilitySetsInGroup(AvailabilitySets availabilitySets, ResourceGroup resourceGroup) {
this.resourceGroup = resourceGroup;
this.availabilitySetsCore = availabilitySetsCore;
this.availabilitySets = availabilitySets;
}

@Override
public AvailabilitySet.DefinitionProvisionable define(String name) throws Exception {
return this.availabilitySetsCore
return this.availabilitySets
.define(name)
.withRegion(this.resourceGroup.location())
.withExistingGroup(this.resourceGroup.name());
}

@Override
public void delete(String name) throws Exception {
this.availabilitySetsCore.delete(this.resourceGroup.name(), name);
this.availabilitySets.delete(this.resourceGroup.name(), name);
}

@Override
public PagedList<AvailabilitySet> list() throws CloudException, IOException {
return this.availabilitySetsCore.list(resourceGroup.name());
return this.availabilitySets.list(resourceGroup.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.microsoft.azure.management.compute.implementation;

import com.microsoft.azure.management.compute.AvailabilitySets;
import com.microsoft.azure.management.compute.VirtualMachines;
import com.microsoft.azure.management.compute.implementation.api.ComputeManagementClientImpl;
import com.microsoft.azure.management.resources.fluentcore.arm.AzureConfigurable;
import com.microsoft.azure.management.resources.fluentcore.arm.implementation.AzureConfigurableImpl;
import com.microsoft.azure.management.resources.implementation.ResourceManager;
import com.microsoft.rest.RestClient;
import com.microsoft.rest.credentials.ServiceClientCredentials;

public final class ComputeManager {
private RestClient restClient;
private String subscriptionId;
// The service managers
private ResourceManager resourceClient;
// The sdk clients
private ComputeManagementClientImpl computeManagementClient;
// The collections
private AvailabilitySets availabilitySets;
private VirtualMachines virtualMachines;

public static Configurable configurable() {
return new ComputeManager().new ConfigurableImpl();
}

public static ComputeManager authenticate(ServiceClientCredentials credentials, String subscriptionId) {
return new ComputeManager(credentials, subscriptionId);
}

public static ComputeManager authenticate(RestClient restClient, String subscriptionId) {
return new ComputeManager(restClient, subscriptionId);
}

public interface Configurable extends AzureConfigurable<Configurable> {
ComputeManager authenticate(ServiceClientCredentials credentials, String subscriptionId);
}

final class ConfigurableImpl extends AzureConfigurableImpl<Configurable> implements Configurable {
public ComputeManager authenticate(ServiceClientCredentials credentials, String subscriptionId) {
buildRestClient(credentials);
return ComputeManager.authenticate(restClient, subscriptionId);
}
}

private ComputeManager(ServiceClientCredentials credentials, String subscriptionId) {
this.restClient = new RestClient
.Builder("https://management.azure.com")
.withCredentials(credentials)
.build();
this.subscriptionId = subscriptionId;
}

private ComputeManager(RestClient restClient, String subscriptionId) {
this.restClient = restClient;
this.subscriptionId = subscriptionId;
}

private ComputeManager() {}

public AvailabilitySets availabilitySets() {
if (availabilitySets == null) {
availabilitySets = new AvailabilitySetsImpl(computeManagementClient().availabilitySets(),
resourceClient().resourceGroups(), virtualMachines());
}
return availabilitySets;
}

public VirtualMachines virtualMachines() {
virtualMachines = null;
return virtualMachines;
}

private ComputeManagementClientImpl computeManagementClient() {
if (computeManagementClient == null) {
computeManagementClient = new ComputeManagementClientImpl(restClient);
computeManagementClient.setSubscriptionId(subscriptionId);
}
return computeManagementClient;
}

private ResourceManager resourceClient() {
if (restClient == null) {
resourceClient = ResourceManager
.authenticate(restClient)
.useSubscription(subscriptionId);
}
return resourceClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,47 @@

import com.microsoft.azure.management.compute.AvailabilitySets;
import com.microsoft.azure.management.compute.VirtualMachines;
import com.microsoft.azure.management.compute.implementation.api.ComputeManagementClientImpl;
import com.microsoft.azure.management.resources.ResourceConnector;
import com.microsoft.azure.management.resources.ResourceGroup;
import com.microsoft.azure.management.resources.implementation.ResourceConnectorBase;
import com.microsoft.rest.credentials.ServiceClientCredentials;
import com.microsoft.rest.RestClient;

public class ComputeResourceConnector extends ResourceConnectorBase<ComputeResourceConnector> {
private ComputeManagementClientImpl client;
private ComputeManager computeClient;
private AvailabilitySets.InGroup availabilitySets;
private VirtualMachines.InGroup virtualMachines;

private ComputeResourceConnector(ServiceClientCredentials credentials, String subscriptionId, ResourceGroup resourceGroup) {
super(credentials, subscriptionId, resourceGroup);
constructClient();
private ComputeResourceConnector(RestClient restClient, String subscriptionId, ResourceGroup resourceGroup) {
super(restClient, subscriptionId, resourceGroup);
}

private static ComputeResourceConnector create(ServiceClientCredentials credentials, String subscriptionId, ResourceGroup resourceGroup) {
return new ComputeResourceConnector(credentials, subscriptionId, resourceGroup);
}

private void constructClient() {
client = new ComputeManagementClientImpl(credentials);
client.setSubscriptionId(subscriptionId);
private static ComputeResourceConnector create(RestClient restClient, String subscriptionId, ResourceGroup resourceGroup) {
return new ComputeResourceConnector(restClient, subscriptionId, resourceGroup);
}

public static class Builder implements ResourceConnector.Builder<ComputeResourceConnector> {
public ComputeResourceConnector create(ServiceClientCredentials credentials, String subscriptionId, ResourceGroup resourceGroup) {
return ComputeResourceConnector.create(credentials, subscriptionId, resourceGroup);
public ComputeResourceConnector create(RestClient restClient, String subscriptionId, ResourceGroup resourceGroup) {
return ComputeResourceConnector.create(restClient, subscriptionId, resourceGroup);
}
}

public AvailabilitySets.InGroup availabilitySets() {
if (availabilitySets == null) {
availabilitySets = new AvailabilitySetsInGroup(availabilitySetsCore(), resourceGroup);
availabilitySets = new AvailabilitySetsInGroup(computeClient().availabilitySets(), resourceGroup);
}
return availabilitySets;
}

public VirtualMachines VirtualMachines() {
public VirtualMachines.InGroup VirtualMachines() {
// TODO
return null;
}

private AvailabilitySets availabilitySetsCore() {
AvailabilitySets availabilitySetsCore = new AvailabilitySetsImpl(client.availabilitySets(), resourceGroups(), VirtualMachines());
return availabilitySetsCore;
private ComputeManager computeClient() {
if (computeClient == null) {
computeClient = ComputeManager
.authenticate(restClient, subscriptionId);
}
return computeClient;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.microsoft.azure.management.network;


public interface PublicIP {
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.microsoft.azure.management.resources;

import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByGroup;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByGroup;
import com.microsoft.azure.management.resources.fluentcore.collection.SupportsGetting;
import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing;

public interface DeploymentOperations extends
SupportsListing<DeploymentOperation>,
SupportsGetting<DeploymentOperation> {
InGroup resourceGroup(String resourceGroupName);
SupportsListingByGroup<DeploymentOperation>,
SupportsGetting<DeploymentOperation>,
SupportsGettingByGroup<DeploymentOperation> {
InGroup resourceGroup(ResourceGroup resourceGroup);

interface InGroup extends
SupportsListing<DeploymentOperation>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.microsoft.azure.management.resources;

import com.microsoft.azure.CloudException;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsDeletingByGroup;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByGroup;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByGroup;
import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating;
import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting;
import com.microsoft.azure.management.resources.fluentcore.collection.SupportsGetting;
Expand All @@ -9,13 +12,17 @@
import java.io.IOException;

public interface Deployments extends
SupportsCreating<Deployment.DefinitionBlank>,
SupportsListing<Deployment>,
SupportsListingByGroup<Deployment>,
SupportsGetting<Deployment>,
SupportsCreating<Deployment.DefinitionBlank>,
SupportsDeleting {
SupportsGettingByGroup<Deployment>,
SupportsDeleting,
SupportsDeletingByGroup {
boolean checkExistence(String deploymentName) throws IOException, CloudException;
boolean checkExistence(String groupName, String deploymentName) throws IOException, CloudException;

InGroup resourceGroup(String resourceGroupName);
InGroup resourceGroup(ResourceGroup resourceGroup);

interface InGroup extends
SupportsListing<Deployment>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.microsoft.azure.management.resources;

import com.microsoft.azure.CloudException;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsDeletingByGroup;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByGroup;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByGroup;
import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating;
import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting;
import com.microsoft.azure.management.resources.fluentcore.collection.SupportsGetting;
Expand All @@ -10,8 +13,16 @@

public interface GenericResources extends
SupportsListing<GenericResource>,
SupportsListingByGroup<GenericResource>,
SupportsGetting<GenericResource>,
SupportsGettingByGroup<GenericResource>,
SupportsCreating<GenericResource.DefinitionBlank>,
SupportsDeleting {
SupportsDeleting,
SupportsDeletingByGroup {
boolean checkExistence(String resourceGroupName, String resourceProviderNamespace, String parentResourcePath, String resourceType, String resourceName, String apiVersion) throws IOException, CloudException;

interface InGroup extends
SupportsListing<GenericResource>,
SupportsGetting<GenericResource> {
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.microsoft.azure.management.resources;

import com.microsoft.rest.credentials.ServiceClientCredentials;
import com.microsoft.rest.RestClient;

public interface ResourceConnector<T extends ResourceConnector> {
interface Builder<T> {
T create(ServiceClientCredentials credentials, String subscriptionId, ResourceGroup resourceGroup);
T create(RestClient restClient, String subscriptionId, ResourceGroup resourceGroup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import okhttp3.Interceptor;
import okhttp3.logging.HttpLoggingInterceptor;

public interface AzureConfigureBase<T extends AzureConfigureBase<T>> {
public interface AzureConfigurable<T extends AzureConfigurable<T>> {
T withLogLevel(HttpLoggingInterceptor.Level level);
T withInterceptor(Interceptor interceptor);
T withUserAgent(String userAgent);
Expand Down
Loading

0 comments on commit 86eaef1

Please sign in to comment.