Repository: jclouds-labs
Updated Branches:
  refs/heads/master b975be5e8 -> 787af1dc8


[JCLOUDS-664] make getImage(id) only do a single API request

* Fill in the private IP addresses in NodeMetadata
* Unify how getNode and listNodes build the VMDeployment
* Change VMDeployment to use AutoValue


Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/913e4be9
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/913e4be9
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/913e4be9

Branch: refs/heads/master
Commit: 913e4be9d55ac3defbc430f0dc7f995ba690b2e9
Parents: b975be5
Author: Ladislav Thon <[email protected]>
Authored: Mon Jul 11 10:32:11 2016 +0200
Committer: Ignasi Barrera <[email protected]>
Committed: Sun Jul 17 23:26:26 2016 +0200

----------------------------------------------------------------------
 .../arm/compute/AzureComputeServiceAdapter.java | 94 +++++++++++---------
 .../functions/DeploymentToNodeMetadata.java     | 49 ++++++----
 .../azurecompute/arm/domain/VMDeployment.java   | 36 ++++++--
 3 files changed, 113 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/913e4be9/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java
----------------------------------------------------------------------
diff --git 
a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java
 
b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java
index 48c7672..53e49ee 100644
--- 
a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java
+++ 
b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java
@@ -26,6 +26,7 @@ import java.util.Arrays;
 import java.util.Collection;
 
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import javax.annotation.Resource;
@@ -42,6 +43,7 @@ import 
org.jclouds.azurecompute.arm.compute.functions.VMImageToImage;
 import org.jclouds.azurecompute.arm.domain.Deployment;
 import org.jclouds.azurecompute.arm.domain.DeploymentBody;
 import org.jclouds.azurecompute.arm.domain.DeploymentProperties;
+import org.jclouds.azurecompute.arm.domain.NetworkInterfaceCard;
 import org.jclouds.azurecompute.arm.domain.ResourceProviderMetaData;
 import org.jclouds.azurecompute.arm.domain.VMImage;
 import org.jclouds.azurecompute.arm.domain.VMHardware;
@@ -51,9 +53,9 @@ import org.jclouds.azurecompute.arm.domain.PublicIPAddress;
 import org.jclouds.azurecompute.arm.domain.SKU;
 import org.jclouds.azurecompute.arm.domain.VMDeployment;
 import org.jclouds.azurecompute.arm.domain.VirtualMachine;
+import org.jclouds.azurecompute.arm.domain.VirtualMachineInstance;
 import org.jclouds.azurecompute.arm.features.DeploymentApi;
 import org.jclouds.azurecompute.arm.features.OSImageApi;
-import org.jclouds.azurecompute.arm.features.VirtualMachineApi;
 import org.jclouds.azurecompute.arm.util.DeploymentTemplateBuilder;
 import org.jclouds.compute.ComputeServiceAdapter;
 import org.jclouds.compute.domain.Template;
@@ -131,8 +133,7 @@ public class AzureComputeServiceAdapter implements 
ComputeServiceAdapter<VMDeplo
             Deployment deployment = deploymentApi.create(name, 
deploymentTemplate);
 
             if (deployment != null) {
-               VMDeployment vmDeployment = new VMDeployment();
-               vmDeployment.deployment = deployment;
+               VMDeployment vmDeployment = VMDeployment.create(deployment);
                deployments.add(vmDeployment);
             } else {
                logger.debug("Failed to create deployment!");
@@ -272,13 +273,15 @@ public class AzureComputeServiceAdapter implements 
ComputeServiceAdapter<VMDeplo
          return ref;
       }
 
-      Iterable<VMImage> images = listImages();
+      String location = fields[0];
+      String publisher = fields[1];
+      String offer = fields[2];
+      String sku = fields[3];
 
-      for (VMImage image : images) {
-         String imageId = VMImageToImage.encodeFieldsToUniqueId(image);
-         if (id.equals(imageId)){
-            return image;
-         }
+      OSImageApi osImageApi = api.getOSImageApi(location);
+      List<Version> versions = osImageApi.listVersions(publisher, offer, sku);
+      if (!versions.isEmpty()) {
+         return VMImage.create(publisher, offer, sku, versions.get(0).name(), 
location, false);
       }
       return null;
    }
@@ -324,21 +327,7 @@ public class AzureComputeServiceAdapter implements 
ComputeServiceAdapter<VMDeplo
       Deployment deployment = api.getDeploymentApi(azureGroup).get(id);
       if (deployment == null)
          return null;
-      String resourceGroup = getResourceGroupFromId(deployment.id());
-      VMDeployment vmDeployment = new VMDeployment();
-      vmDeployment.deployment = deployment;
-      List<PublicIPAddress> list = getIPAddresses(deployment);
-      vmDeployment.ipAddressList = list;
-      VirtualMachine vm = api.getVirtualMachineApi(azureGroup).get(id);
-      vmDeployment.virtualMachine = vm;
-      vmDeployment.vm = 
api.getVirtualMachineApi(azureGroup).getInstanceDetails(id);
-      if (vm != null && vm.tags() != null) {
-         vmDeployment.userMetaData = vm.tags();
-         String tagString = vmDeployment.userMetaData.get("tags");
-         List<String> tags = Arrays.asList(tagString.split(","));
-         vmDeployment.tags = tags;
-      }
-      return vmDeployment;
+      return convertDeploymentToVMDeployment(deployment);
    }
 
    @Override
@@ -384,30 +373,49 @@ public class AzureComputeServiceAdapter implements 
ComputeServiceAdapter<VMDeplo
       return list;
    }
 
+   private List<NetworkInterfaceCard> getNetworkInterfaceCards(Deployment 
deployment) {
+      List<NetworkInterfaceCard> result = new 
ArrayList<NetworkInterfaceCard>();
+
+      String resourceGroup = getResourceGroupFromId(deployment.id());
+
+      if (deployment.properties() != null && 
deployment.properties().dependencies() != null) {
+         for (Deployment.Dependency dependency : 
deployment.properties().dependencies()) {
+            if 
(dependency.resourceType().equals("Microsoft.Network/networkInterfaces")) {
+               String resourceName = dependency.resourceName();
+               NetworkInterfaceCard nic = 
api.getNetworkInterfaceCardApi(resourceGroup).get(resourceName);
+               result.add(nic);
+            }
+         }
+      }
+
+      return result;
+   }
+
+   private VMDeployment convertDeploymentToVMDeployment(Deployment deployment) 
{
+      String id = deployment.id();
+      String resourceGroup = getResourceGroupFromId(id);
+
+      List<PublicIPAddress> ipAddressList = getIPAddresses(deployment);
+      List<NetworkInterfaceCard> networkInterfaceCards = 
getNetworkInterfaceCards(deployment);
+      VirtualMachine vm = api.getVirtualMachineApi(azureGroup).get(id);
+      VirtualMachineInstance vmInstanceDetails = 
api.getVirtualMachineApi(azureGroup).getInstanceDetails(id);
+      Map<String, String> userMetaData = null;
+      Iterable<String> tags = null;
+      if (vm != null && vm.tags() != null) {
+         userMetaData = vm.tags();
+         String tagString = userMetaData.get("tags");
+         tags = Arrays.asList(tagString.split(","));
+      }
+      return VMDeployment.create(deployment, ipAddressList, vmInstanceDetails, 
vm, networkInterfaceCards, userMetaData, tags);
+   }
+
    @Override
    public Iterable<VMDeployment> listNodes() {
       List<Deployment> deployments = api.getDeploymentApi(azureGroup).list();
 
       List<VMDeployment> vmDeployments = new ArrayList<VMDeployment>();
-
       for (Deployment d : deployments){
-         VMDeployment vmDeployment = new VMDeployment();
-         vmDeployment.deployment = d;
-         VirtualMachineApi vmApi = api.getVirtualMachineApi(azureGroup);
-         vmDeployment.vm = vmApi.getInstanceDetails(d.name());
-         List<PublicIPAddress> list = getIPAddresses(d);
-         vmDeployment.ipAddressList = list;
-
-         VirtualMachine vm = vmApi.get(d.name());
-         vmDeployment.virtualMachine = vm;
-
-         if (vm != null && vm.tags() != null) {
-            vmDeployment.userMetaData = vm.tags();
-            String tagString = vmDeployment.userMetaData.get("tags");
-            List<String> tags = Arrays.asList(tagString.split(","));
-            vmDeployment.tags = tags;
-         }
-         vmDeployments.add(vmDeployment);
+         vmDeployments.add(convertDeploymentToVMDeployment(d));
       }
       return vmDeployments;
    }
@@ -417,7 +425,7 @@ public class AzureComputeServiceAdapter implements 
ComputeServiceAdapter<VMDeplo
       return Iterables.filter(listNodes(), new Predicate<VMDeployment>() {
          @Override
          public boolean apply(final VMDeployment input) {
-            return Iterables.contains(ids, input.deployment.name());
+            return Iterables.contains(ids, input.deployment().name());
          }
       });
    }

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/913e4be9/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/functions/DeploymentToNodeMetadata.java
----------------------------------------------------------------------
diff --git 
a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/functions/DeploymentToNodeMetadata.java
 
b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/functions/DeploymentToNodeMetadata.java
index 40e09b7..8ea8235 100644
--- 
a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/functions/DeploymentToNodeMetadata.java
+++ 
b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/functions/DeploymentToNodeMetadata.java
@@ -27,6 +27,8 @@ import org.jclouds.azurecompute.arm.AzureComputeApi;
 import org.jclouds.azurecompute.arm.domain.ComputeNode;
 import org.jclouds.azurecompute.arm.domain.Deployment;
 import org.jclouds.azurecompute.arm.domain.ImageReference;
+import org.jclouds.azurecompute.arm.domain.IpConfiguration;
+import org.jclouds.azurecompute.arm.domain.NetworkInterfaceCard;
 import org.jclouds.azurecompute.arm.domain.PublicIPAddress;
 import org.jclouds.azurecompute.arm.domain.VMDeployment;
 import org.jclouds.azurecompute.arm.domain.VMHardware;
@@ -111,20 +113,20 @@ public class DeploymentToNodeMetadata implements 
Function<VMDeployment, NodeMeta
    @Override
    public NodeMetadata apply(final VMDeployment from) {
       final NodeMetadataBuilder builder = new NodeMetadataBuilder();
-      final Deployment deployment = from.deployment;
+      final Deployment deployment = from.deployment();
       builder.id(deployment.name());
       builder.providerId(deployment.name());
       builder.name(deployment.name());
       String group =  
this.nodeNamingConvention.extractGroup(deployment.name());
       builder.group(group);
-      if (from.tags != null)
-         builder.tags(from.tags);
-      if (from.userMetaData != null)
-         builder.userMetadata(from.userMetaData);
+      if (from.tags() != null)
+         builder.tags(from.tags());
+      if (from.userMetaData() != null)
+         builder.userMetadata(from.userMetaData());
 
       NodeMetadata.Status status = 
STATUS_TO_NODESTATUS.get(provisioningStateFromString(deployment.properties().provisioningState()));
-      if (status == NodeMetadata.Status.RUNNING && from.vm != null && 
from.vm.statuses() != null) {
-         List<VirtualMachineInstance.VirtualMachineStatus> statuses = 
from.vm.statuses();
+      if (status == NodeMetadata.Status.RUNNING && from.vm() != null && 
from.vm().statuses() != null) {
+         List<VirtualMachineInstance.VirtualMachineStatus> statuses = 
from.vm().statuses();
          for (int c = 0; c < statuses.size(); c++) {
             if (statuses.get(c).code().substring(0, 10).equals("PowerState")) {
                if (statuses.get(c).displayStatus().equals("VM running")) {
@@ -139,11 +141,11 @@ public class DeploymentToNodeMetadata implements 
Function<VMDeployment, NodeMeta
 
       builder.status(status);
 
-      if (from.vm != null) {
+      if (from.vm() != null) {
          builder.hostname(deployment.name() + "pc");
       }
 
-      Credentials credentials = credentialStore.get("node#" + 
from.deployment.name());
+      Credentials credentials = credentialStore.get("node#" + 
from.deployment().name());
       if (credentials != null && 
credentials.identity.equals(JCLOUDS_DEFAULT_USERNAME)) {
          credentials = new Credentials(AZURE_LOGIN_USERNAME, 
credentials.credential);
       }
@@ -162,9 +164,9 @@ public class DeploymentToNodeMetadata implements 
Function<VMDeployment, NodeMeta
       builder.credentials(LoginCredentials.fromCredentials(credentials));
 
       final Set<String> publicIpAddresses = Sets.newLinkedHashSet();
-      if (from.ipAddressList != null) {
-         for (int c = 0; c < from.ipAddressList.size(); c++) {
-            PublicIPAddress ip = from.ipAddressList.get(c);
+      if (from.ipAddressList() != null) {
+         for (int c = 0; c < from.ipAddressList().size(); c++) {
+            PublicIPAddress ip = from.ipAddressList().get(c);
             if (ip != null && ip.properties() != null && 
ip.properties().ipAddress() != null)
             {
                publicIpAddresses.add(ip.properties().ipAddress());
@@ -174,10 +176,25 @@ public class DeploymentToNodeMetadata implements 
Function<VMDeployment, NodeMeta
          if (publicIpAddresses.size() > 0)
             builder.publicAddresses(publicIpAddresses);
       }
+      final Set<String> privateIpAddresses = Sets.newLinkedHashSet();
+      if (from.networkInterfaceCards() != null) {
+         for (NetworkInterfaceCard nic : from.networkInterfaceCards()) {
+            if (nic != null && nic.properties() != null && 
nic.properties().ipConfigurations() != null) {
+               for (IpConfiguration ip : nic.properties().ipConfigurations()) {
+                  if (ip != null && ip.properties() != null && 
ip.properties().privateIPAddress() != null) {
+                     
privateIpAddresses.add(ip.properties().privateIPAddress());
+                  }
+               }
+            }
+         }
+         if (!privateIpAddresses.isEmpty()) {
+            builder.privateAddresses(privateIpAddresses);
+         }
+      }
 
       org.jclouds.azurecompute.arm.domain.Location myLocation = null;
-      if (from.virtualMachine != null) {
-         String locationName = from.virtualMachine.location();
+      if (from.virtualMachine() != null) {
+         String locationName = from.virtualMachine().location();
          List<org.jclouds.azurecompute.arm.domain.Location> locations = 
api.getLocationApi().list();
 
          for (org.jclouds.azurecompute.arm.domain.Location location : 
locations) {
@@ -189,7 +206,7 @@ public class DeploymentToNodeMetadata implements 
Function<VMDeployment, NodeMeta
          Location jLocation = this.locationToLocation.apply(myLocation);
          builder.location(jLocation);
 
-         ImageReference imageReference = 
from.virtualMachine.properties().storageProfile().imageReference();
+         ImageReference imageReference = 
from.virtualMachine().properties().storageProfile().imageReference();
 
          if (imageReference != null) {
             VMImage vmImage = VMImage.create(imageReference.publisher(), 
imageReference.offer(), imageReference.sku(),
@@ -199,7 +216,7 @@ public class DeploymentToNodeMetadata implements 
Function<VMDeployment, NodeMeta
          }
 
          VMSize myVMSize = null;
-         String vmSizeName = 
from.virtualMachine.properties().hardwareProfile().vmSize();
+         String vmSizeName = 
from.virtualMachine().properties().hardwareProfile().vmSize();
          List<VMSize> vmSizes = api.getVMSizeApi(locationName).list();
          for (VMSize vmSize : vmSizes) {
             if (vmSize.name().equals(vmSizeName)) {

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/913e4be9/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/VMDeployment.java
----------------------------------------------------------------------
diff --git 
a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/VMDeployment.java
 
b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/VMDeployment.java
index c663944..948e69b 100644
--- 
a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/VMDeployment.java
+++ 
b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/VMDeployment.java
@@ -16,21 +16,43 @@
  */
 package org.jclouds.azurecompute.arm.domain;
 
+import com.google.auto.value.AutoValue;
+import org.jclouds.javax.annotation.Nullable;
 
 import java.util.List;
 import java.util.Map;
 
-public class VMDeployment {
+@AutoValue
+public abstract class VMDeployment {
 
-   public Deployment deployment;
+   public abstract Deployment deployment();
 
-   public List<PublicIPAddress> ipAddressList;
+   @Nullable
+   public abstract List<PublicIPAddress> ipAddressList();
 
-   public VirtualMachineInstance vm;
+   @Nullable
+   public abstract VirtualMachineInstance vm();
 
-   public VirtualMachine virtualMachine;
+   @Nullable
+   public abstract VirtualMachine virtualMachine();
 
-   public Map<String, String> userMetaData;
+   @Nullable
+   public abstract List<NetworkInterfaceCard> networkInterfaceCards();
 
-   public Iterable<String> tags;
+   @Nullable
+   public abstract Map<String, String> userMetaData();
+
+   @Nullable
+   public abstract Iterable<String> tags();
+
+   public static VMDeployment create(Deployment deployment) {
+      return create(deployment, null, null, null, null, null, null);
+   }
+
+   public static VMDeployment create(Deployment deployment, 
List<PublicIPAddress> ipAddressList,
+                                     VirtualMachineInstance vm, VirtualMachine 
virtualMachine,
+                                     List<NetworkInterfaceCard> 
networkInterfaceCards, Map<String, String> userMetaData,
+                                     Iterable<String> tags) {
+      return new AutoValue_VMDeployment(deployment, ipAddressList, vm, 
virtualMachine, networkInterfaceCards, userMetaData, tags);
+   }
 }

Reply via email to