From 8cbf312c329acd042469d5984c11436286712969 Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Wed, 21 Sep 2016 17:00:26 +0200 Subject: [PATCH] Use the existing region suppliers to filter --- .../compute/AzureComputeServiceAdapter.java | 59 ++++++------------- .../predicates/IsDeploymentInRegions.java | 46 +++++++++++++++ 2 files changed, 65 insertions(+), 40 deletions(-) create mode 100644 providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/predicates/IsDeploymentInRegions.java diff --git a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java index 3941ebcfc8..61807a9589 100644 --- a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java @@ -16,6 +16,11 @@ */ package org.jclouds.azurecompute.arm.compute; +import static com.google.common.base.Preconditions.checkState; +import static java.lang.String.format; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.jclouds.util.Predicates2.retry; + import java.util.Collection; import java.util.List; import java.util.Set; @@ -29,6 +34,7 @@ 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.compute.predicates.IsDeploymentInRegions; import org.jclouds.azurecompute.arm.domain.Deployment; import org.jclouds.azurecompute.arm.domain.DeploymentBody; import org.jclouds.azurecompute.arm.domain.DeploymentProperties; @@ -38,11 +44,11 @@ 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.VMHardware; import org.jclouds.azurecompute.arm.domain.VMImage; +import org.jclouds.azurecompute.arm.domain.VMSize; import org.jclouds.azurecompute.arm.domain.Value; +import org.jclouds.azurecompute.arm.domain.Version; import org.jclouds.azurecompute.arm.features.DeploymentApi; import org.jclouds.azurecompute.arm.features.OSImageApi; import org.jclouds.azurecompute.arm.functions.CleanupResources; @@ -52,14 +58,13 @@ 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.location.Region; import org.jclouds.logging.Logger; -import org.jclouds.providers.ProviderMetadata; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Splitter; +import com.google.common.base.Supplier; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.FluentIterable; import com.google.common.collect.Iterables; @@ -68,11 +73,6 @@ import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import com.google.common.net.UrlEscapers; -import static com.google.common.base.Preconditions.checkState; -import static java.lang.String.format; -import static java.util.concurrent.TimeUnit.SECONDS; -import static org.jclouds.util.Predicates2.retry; - /** * Defines the connection between the {@link AzureComputeApi} implementation and the jclouds * {@link org.jclouds.compute.ComputeService}. @@ -86,16 +86,16 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter> regionIds; + private final IsDeploymentInRegions isDeploymentInRegions; private final DeploymentToVMDeployment deploymentToVMDeployment; @Inject AzureComputeServiceAdapter(final AzureComputeApi api, final AzureComputeConstants azureComputeConstants, - CleanupResources cleanupResources, Json json, ProviderMetadata providerMetadata, DeploymentToVMDeployment deploymentToVMDeployment) { - this.json = json; + CleanupResources cleanupResources, @Region Supplier> regionIds, + IsDeploymentInRegions isDeploymentInRegions, DeploymentToVMDeployment deploymentToVMDeployment) { this.api = api; this.azureComputeConstants = azureComputeConstants; this.azureGroup = azureComputeConstants.azureResourceGroup(); @@ -103,7 +103,8 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter listLocations() { - final Iterable whiteListedRegionNames = findWhiteListOfRegions(); - final Iterable vmLocations = FluentIterable.from(api.getResourceProviderApi().get("Microsoft.Compute")) .filter(new Predicate() { @Override @@ -305,7 +304,7 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter() { @Override public boolean apply(Location location) { - return whiteListedRegionNames == null ? true : Iterables.contains(whiteListedRegionNames, location.name()); + return regionIds.get().isEmpty() ? true : regionIds.get().contains(location.name()); } }) .toList(); @@ -317,7 +316,7 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter listNodes() { return FluentIterable.from(api.getDeploymentApi(azureGroup).list()) - .filter(new IsDeploymentInRegions(findWhiteListOfRegions())) + .filter(isDeploymentInRegions) .filter(new Predicate() { @Override public boolean apply(Deployment deployment) { @@ -369,24 +368,4 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter 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/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/predicates/IsDeploymentInRegions.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/predicates/IsDeploymentInRegions.java new file mode 100644 index 0000000000..57a0678b82 --- /dev/null +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/predicates/IsDeploymentInRegions.java @@ -0,0 +1,46 @@ +/* + * 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.predicates; + +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.azurecompute.arm.domain.Deployment; +import org.jclouds.azurecompute.arm.domain.Value; +import org.jclouds.location.Region; + +import com.google.common.base.Predicate; +import com.google.common.base.Supplier; + +@Singleton +public class IsDeploymentInRegions implements Predicate { + + private final Supplier> regionIds; + + @Inject + IsDeploymentInRegions(@Region Supplier> regionIds) { + this.regionIds = regionIds; + } + + @Override + public boolean apply(Deployment deployment) { + Value locationValue = deployment.properties().parameters().get("location"); + return regionIds.get().contains(locationValue.value()); + } +}