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 82b792c7b..3941ebcfc 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 @@ -16,11 +16,8 @@ */ package org.jclouds.azurecompute.arm.compute; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Set; import javax.annotation.Resource; @@ -30,33 +27,32 @@ import org.jclouds.azurecompute.arm.AzureComputeApi; import org.jclouds.azurecompute.arm.compute.config.AzureComputeServiceContextModule.AzureComputeConstants; +import org.jclouds.azurecompute.arm.compute.functions.DeploymentToVMDeployment; 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.StorageService; -import org.jclouds.azurecompute.arm.domain.VMImage; -import org.jclouds.azurecompute.arm.domain.VMHardware; import org.jclouds.azurecompute.arm.domain.Location; import org.jclouds.azurecompute.arm.domain.Offer; -import org.jclouds.azurecompute.arm.domain.PublicIPAddress; +import org.jclouds.azurecompute.arm.domain.ResourceProviderMetaData; import org.jclouds.azurecompute.arm.domain.SKU; +import org.jclouds.azurecompute.arm.domain.StorageService; import org.jclouds.azurecompute.arm.domain.VMDeployment; import org.jclouds.azurecompute.arm.domain.VMSize; import org.jclouds.azurecompute.arm.domain.Version; -import org.jclouds.azurecompute.arm.domain.VirtualMachine; -import org.jclouds.azurecompute.arm.domain.VirtualMachineInstance; +import org.jclouds.azurecompute.arm.domain.VMHardware; +import org.jclouds.azurecompute.arm.domain.VMImage; +import org.jclouds.azurecompute.arm.domain.Value; import org.jclouds.azurecompute.arm.features.DeploymentApi; import org.jclouds.azurecompute.arm.features.OSImageApi; -import org.jclouds.azurecompute.arm.util.BlobHelper; import org.jclouds.azurecompute.arm.functions.CleanupResources; +import org.jclouds.azurecompute.arm.util.BlobHelper; import org.jclouds.azurecompute.arm.util.DeploymentTemplateBuilder; import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.domain.Template; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.domain.LoginCredentials; +import org.jclouds.json.Json; import org.jclouds.location.reference.LocationConstants; import org.jclouds.logging.Logger; import org.jclouds.providers.ProviderMetadata; @@ -90,17 +86,16 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter credential = null; - + NodeAndInitialCredentials credential; if (template.getOptions().getPublicKey() != null){ String privateKey = template.getOptions().getPrivateKey(); credential = new NodeAndInitialCredentials(deployment, name, @@ -167,7 +159,6 @@ public boolean apply(final String name) { credential = new NodeAndInitialCredentials(deployment, name, LoginCredentials.builder().user(loginUser).password(loginPassword).authenticateSudo(true).build()); } - return credential; } @@ -288,7 +279,7 @@ public VMImage getImage(final String id) { @Override public Iterable listLocations() { - final Iterable whiteListZoneName = findWhiteListOfRegions(); + final Iterable whiteListedRegionNames = findWhiteListOfRegions(); final Iterable vmLocations = FluentIterable.from(api.getResourceProviderApi().get("Microsoft.Compute")) .filter(new Predicate() { @@ -314,7 +305,7 @@ public boolean apply(Location location) { .filter(new Predicate() { @Override public boolean apply(Location location) { - return whiteListZoneName == null ? true : Iterables.contains(whiteListZoneName, location.name()); + return whiteListedRegionNames == null ? true : Iterables.contains(whiteListedRegionNames, location.name()); } }) .toList(); @@ -322,27 +313,14 @@ public boolean apply(Location location) { return locations; } - private Iterable findWhiteListOfRegions() { - if (providerMetadata.getDefaultProperties().get(LocationConstants.PROPERTY_REGIONS) == null) return null; - return Splitter.on(",").trimResults().split((CharSequence) providerMetadata.getDefaultProperties().get(LocationConstants.PROPERTY_REGIONS)); - } - - private String getResourceGroupFromId(String id) { - String searchStr = "/resourceGroups/"; - int indexStart = id.lastIndexOf(searchStr) + searchStr.length(); - searchStr = "/providers/"; - int indexEnd = id.lastIndexOf(searchStr); - - String resourceGroup = id.substring(indexStart, indexEnd); - return resourceGroup; - } - @Override public VMDeployment getNode(final String id) { Deployment deployment = api.getDeploymentApi(azureGroup).get(id); - if (deployment == null) - return null; - return convertDeploymentToVMDeployment(deployment); + if (deployment == null) return null; + if (new IsDeploymentInRegions(findWhiteListOfRegions()).apply(deployment)) { + return deploymentToVMDeployment.apply(deployment); + } + return null; } @Override @@ -365,84 +343,21 @@ public void suspendNode(final String id) { api.getVirtualMachineApi(azureGroup).stop(id); } - private List getIPAddresses(Deployment deployment) { - List list = new ArrayList(); - String resourceGroup = getResourceGroupFromId(deployment.id()); - - if (deployment.properties() != null && deployment.properties().dependencies() != null) { - List dependencies = deployment.properties().dependencies(); - for (int d = 0; d < dependencies.size(); d++) { - if (dependencies.get(d).resourceType().equals("Microsoft.Network/networkInterfaces")) { - List dependsOn = dependencies.get(d).dependsOn(); - for (int e = 0; e < dependsOn.size(); e++) { - if (dependsOn.get(e).resourceType().equals("Microsoft.Network/publicIPAddresses")) { - String resourceName = dependsOn.get(e).resourceName(); - PublicIPAddress ip = api.getPublicIPAddressApi(resourceGroup).get(resourceName); - list.add(ip); - break; - } - } - } - } - } - return list; - } - - private List getNetworkInterfaceCards(Deployment deployment) { - List result = new ArrayList(); - - 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 ipAddressList = getIPAddresses(deployment); - List networkInterfaceCards = getNetworkInterfaceCards(deployment); - VirtualMachine vm = api.getVirtualMachineApi(azureGroup).get(id); - VirtualMachineInstance vmInstanceDetails = api.getVirtualMachineApi(azureGroup).getInstanceDetails(id); - Map userMetaData = null; - Iterable 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 listNodes() { - List deployments = api.getDeploymentApi(azureGroup).list(); - - List vmDeployments = new ArrayList(); - for (Deployment d : deployments){ - // Check that this vm is not generalized and made to custom image - try { - String storageAccountName = d.name().replaceAll("[^A-Za-z0-9 ]", "") + "stor"; - String key = api.getStorageAccountApi(azureGroup).getKeys(storageAccountName).key1(); - if (!BlobHelper.customImageExists(storageAccountName, key)) - vmDeployments.add(convertDeploymentToVMDeployment(d)); - } - catch (Exception e) { - // This might happen if there is no custom images but vm is generalized. No need to list - } - } - - return vmDeployments; + return FluentIterable.from(api.getDeploymentApi(azureGroup).list()) + .filter(new IsDeploymentInRegions(findWhiteListOfRegions())) + .filter(new Predicate() { + @Override + public boolean apply(Deployment deployment) { + Value storageAccountNameValue = deployment.properties().parameters().get("storageAccountName"); + String storageAccountName = storageAccountNameValue.value(); + String key = api.getStorageAccountApi(azureGroup).getKeys(storageAccountName).key1(); + return !BlobHelper.customImageExists(storageAccountName, key); + } + }) + .transform(deploymentToVMDeployment) + .toList(); } @Override @@ -455,4 +370,23 @@ public boolean apply(final VMDeployment input) { }); } + private Iterable findWhiteListOfRegions() { + if (providerMetadata.getDefaultProperties().get(LocationConstants.PROPERTY_REGIONS) == null) return null; + return Splitter.on(",").trimResults().split((CharSequence) providerMetadata.getDefaultProperties().get(LocationConstants.PROPERTY_REGIONS)); + } + + private class IsDeploymentInRegions implements Predicate { + + private final Iterable whiteListOfRegions; + + public IsDeploymentInRegions(Iterable whiteListOfRegions) { + this.whiteListOfRegions = whiteListOfRegions; + } + + @Override + public boolean apply(Deployment deployment) { + Value locationValue = deployment.properties().parameters().get("location"); + return Iterables.contains(whiteListOfRegions, locationValue.value()); + } + } } diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/functions/DeploymentToVMDeployment.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/functions/DeploymentToVMDeployment.java new file mode 100644 index 000000000..31f1a5858 --- /dev/null +++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/functions/DeploymentToVMDeployment.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.azurecompute.arm.compute.functions; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.azurecompute.arm.AzureComputeApi; +import org.jclouds.azurecompute.arm.compute.config.AzureComputeServiceContextModule; +import org.jclouds.azurecompute.arm.domain.Deployment; +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.VirtualMachine; +import org.jclouds.azurecompute.arm.domain.VirtualMachineInstance; + +import com.google.common.base.Function; + +/** + * Converts an Deployment into a VMDeployment. + */ +@Singleton +public class DeploymentToVMDeployment implements Function { + + private final AzureComputeServiceContextModule.AzureComputeConstants azureComputeConstants; + + private final AzureComputeApi api; + + @Inject + DeploymentToVMDeployment(AzureComputeApi api, final AzureComputeServiceContextModule.AzureComputeConstants azureComputeConstants) { + this.api = api; + this.azureComputeConstants = azureComputeConstants; + } + + @Override + public VMDeployment apply(final Deployment deployment) { + String id = deployment.id(); + List ipAddressList = getIPAddresses(deployment); + List networkInterfaceCards = getNetworkInterfaceCards(deployment); + VirtualMachine vm = api.getVirtualMachineApi(azureComputeConstants.azureResourceGroup()).get(id); + VirtualMachineInstance vmInstanceDetails = api.getVirtualMachineApi(azureComputeConstants.azureResourceGroup()).getInstanceDetails(id); + Map userMetaData = null; + Iterable 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); + } + + private List getIPAddresses(Deployment deployment) { + List list = new ArrayList(); + String resourceGroup = getResourceGroupFromId(deployment.id()); + + if (deployment.properties() != null && deployment.properties().dependencies() != null) { + List dependencies = deployment.properties().dependencies(); + for (int d = 0; d < dependencies.size(); d++) { + if (dependencies.get(d).resourceType().equals("Microsoft.Network/networkInterfaces")) { + List dependsOn = dependencies.get(d).dependsOn(); + for (int e = 0; e < dependsOn.size(); e++) { + if (dependsOn.get(e).resourceType().equals("Microsoft.Network/publicIPAddresses")) { + String resourceName = dependsOn.get(e).resourceName(); + PublicIPAddress ip = api.getPublicIPAddressApi(resourceGroup).get(resourceName); + list.add(ip); + break; + } + } + } + } + } + return list; + } + + private String getResourceGroupFromId(String id) { + String searchStr = "/resourceGroups/"; + int indexStart = id.lastIndexOf(searchStr) + searchStr.length(); + searchStr = "/providers/"; + int indexEnd = id.lastIndexOf(searchStr); + + String resourceGroup = id.substring(indexStart, indexEnd); + return resourceGroup; + } + + private List getNetworkInterfaceCards(Deployment deployment) { + List result = new ArrayList(); + + 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; + } + +} diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Deployment.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Deployment.java index 2fc85bc36..be363ecd0 100644 --- a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Deployment.java +++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Deployment.java @@ -16,18 +16,18 @@ */ package org.jclouds.azurecompute.arm.domain; -import static com.google.common.collect.ImmutableList.copyOf; - import java.util.List; import java.util.Map; -import com.google.common.collect.ImmutableMap; import org.jclouds.azurecompute.arm.util.GetEnumValue; import org.jclouds.domain.JsonBall; import org.jclouds.javax.annotation.Nullable; +import org.jclouds.json.SerializedNames; import com.google.auto.value.AutoValue; -import org.jclouds.json.SerializedNames; +import com.google.common.collect.ImmutableMap; + +import static com.google.common.collect.ImmutableList.copyOf; @AutoValue public abstract class Deployment { @@ -186,7 +186,7 @@ public abstract static class DeploymentProperties { public abstract ContentLink templateLink(); @Nullable - public abstract Map parameters(); + public abstract Map parameters(); @Nullable public abstract ContentLink parametersLink(); @@ -209,7 +209,7 @@ public static DeploymentProperties create(final String provisioningState, final List dependencies, final Map template, final ContentLink templateLink, - final Map parameters, + final Map parameters, final ContentLink parametersLink, final String mode, final String duration, @@ -222,7 +222,7 @@ public static DeploymentProperties create(final String provisioningState, dependencies == null ? null : copyOf(dependencies), template == null ? ImmutableMap.builder().build() : ImmutableMap.copyOf(template), templateLink, - parameters == null ? ImmutableMap.builder().build() : ImmutableMap.copyOf(parameters), + parameters == null ? ImmutableMap.builder().build() : ImmutableMap.copyOf(parameters), parametersLink, mode, duration, diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Value.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Value.java new file mode 100644 index 000000000..4b0ce0c96 --- /dev/null +++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Value.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.azurecompute.arm.domain; + +import org.jclouds.json.SerializedNames; + +import com.google.auto.value.AutoValue; + +@AutoValue +public abstract class Value { + + /** + * The type of the Value + */ + public abstract String type(); + + /** + * The name of the Value + */ + public abstract String value(); + + @SerializedNames({"type", "value"}) + public static Value create(final String type, final String value) { + + return new AutoValue_Value(type, value); + } + +} diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/BlobHelper.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/BlobHelper.java index ee794ec08..f5bfc758a 100644 --- a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/BlobHelper.java +++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/BlobHelper.java @@ -16,6 +16,9 @@ */ package org.jclouds.azurecompute.arm.util; +import java.util.ArrayList; +import java.util.List; + import org.jclouds.ContextBuilder; import org.jclouds.azure.storage.domain.BoundedSet; import org.jclouds.azureblob.AzureBlobClient; @@ -25,9 +28,6 @@ import org.jclouds.azurecompute.arm.domain.VMImage; import org.jclouds.util.Closeables2; -import java.util.ArrayList; -import java.util.List; - public class BlobHelper { public static void deleteContainerIfExists(String storage, String key, String containerName) { diff --git a/azurecompute-arm/src/test/resources/logback-test.xml b/azurecompute-arm/src/test/resources/logback-test.xml index c823913f6..cb55d4982 100644 --- a/azurecompute-arm/src/test/resources/logback-test.xml +++ b/azurecompute-arm/src/test/resources/logback-test.xml @@ -1,34 +1,42 @@ - - - + + + target/test-data/jclouds.log + + %d %-5p [%c] [%thread] %m%n + + + + target/test-data/jclouds-wire.log + + %d %-5p [%c] [%thread] %m%n + + + + target/jclouds-compute.log - - %msg%n + %d %-5p [%c] [%thread] %m%n - - + + + + + + + + + + + + + + + + + + + + - - - - -