From abe77fd25a25b98508e9f641f97ef92a25a4a1d8 Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Mon, 9 Jan 2012 13:56:41 +0200 Subject: [PATCH] Create a LoadingCache that maps from zone ID to Zone object, and use it in createNodeWithGroupEncodedIntoName(). (Review feedback) --- ...CloudStackComputeServiceContextModule.java | 6 +++ .../CloudStackComputeServiceAdapter.java | 14 +++++- .../cloudstack/functions/ZoneIdToZone.java | 50 +++++++++++++++++++ .../suppliers/ZoneIdToZoneSupplier.java | 50 +++++++++++++++++++ 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/ZoneIdToZone.java create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/ZoneIdToZoneSupplier.java diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/config/CloudStackComputeServiceContextModule.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/config/CloudStackComputeServiceContextModule.java index fd8336fe70..e1633a0117 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/config/CloudStackComputeServiceContextModule.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/config/CloudStackComputeServiceContextModule.java @@ -52,9 +52,11 @@ import org.jclouds.cloudstack.domain.VirtualMachine; import org.jclouds.cloudstack.domain.Zone; import org.jclouds.cloudstack.features.GuestOSClient; import org.jclouds.cloudstack.functions.StaticNATVirtualMachineInNetwork; +import org.jclouds.cloudstack.functions.ZoneIdToZone; import org.jclouds.cloudstack.predicates.JobComplete; import org.jclouds.cloudstack.suppliers.GetCurrentUser; import org.jclouds.cloudstack.suppliers.NetworksForCurrentUser; +import org.jclouds.cloudstack.suppliers.ZoneIdToZoneSupplier; import org.jclouds.collect.Memoized; import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.config.ComputeServiceAdapterContextModule; @@ -111,6 +113,10 @@ public class CloudStackComputeServiceContextModule install(new FactoryModuleBuilder().build(StaticNATVirtualMachineInNetwork.Factory.class)); bind(new TypeLiteral>>() { }).to(GetIPForwardingRulesByVirtualMachine.class); + bind(new TypeLiteral>() { + }).to(ZoneIdToZone.class); + bind(new TypeLiteral>>() { + }).to(ZoneIdToZoneSupplier.class); } @Provides diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java index 70313527a1..64f8325cae 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java @@ -32,12 +32,14 @@ import static org.jclouds.cloudstack.predicates.TemplatePredicates.isReady; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ExecutionException; import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; +import com.google.common.base.Throwables; import com.google.common.collect.Iterables; import org.jclouds.cloudstack.CloudStackClient; import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions; @@ -93,6 +95,7 @@ public class CloudStackComputeServiceAdapter implements private final LoadingCache> vmToRules; private final Map credentialStore; private final Map optionsConverters; + private final Supplier> zoneIdToZone; @Inject public CloudStackComputeServiceAdapter(CloudStackClient client, Predicate jobComplete, @@ -100,7 +103,8 @@ public class CloudStackComputeServiceAdapter implements BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult, StaticNATVirtualMachineInNetwork.Factory staticNATVMInNetwork, CreatePortForwardingRulesForIP setupPortForwardingRulesForIP, LoadingCache> vmToRules, - Map credentialStore, Map optionsConverters) { + Map credentialStore, Map optionsConverters, + Supplier> zoneIdToZone) { this.client = checkNotNull(client, "client"); this.jobComplete = checkNotNull(jobComplete, "jobComplete"); this.networkSupplier = checkNotNull(networkSupplier, "networkSupplier"); @@ -111,6 +115,7 @@ public class CloudStackComputeServiceAdapter implements this.vmToRules = checkNotNull(vmToRules, "vmToRules"); this.credentialStore = checkNotNull(credentialStore, "credentialStore"); this.optionsConverters = optionsConverters; + this.zoneIdToZone = zoneIdToZone; } @Override @@ -124,7 +129,12 @@ public class CloudStackComputeServiceAdapter implements Map networks = networkSupplier.get(); final long zoneId = Long.parseLong(template.getLocation().getId()); - Zone zone = client.getZoneClient().getZone(zoneId); + Zone zone = null; + try { + zone = zoneIdToZone.get().get(zoneId); + } catch (ExecutionException e) { + Throwables.propagate(e); + } CloudStackTemplateOptions templateOptions = template.getOptions().as(CloudStackTemplateOptions.class); diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/ZoneIdToZone.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/ZoneIdToZone.java new file mode 100644 index 0000000000..8ee174f15f --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/ZoneIdToZone.java @@ -0,0 +1,50 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.cloudstack.functions; + +import com.google.common.cache.CacheLoader; +import com.google.inject.Inject; +import org.jclouds.cloudstack.CloudStackClient; +import org.jclouds.cloudstack.domain.Zone; +import org.jclouds.cloudstack.features.ZoneClient; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Defines a cache that allows a zone to be looked up by its ID. + * + * @author Richard Downer + */ +public class ZoneIdToZone extends CacheLoader { + + private final ZoneClient zoneClient; + + @Inject + public ZoneIdToZone(CloudStackClient client) { + checkNotNull(client, "client"); + this.zoneClient = client.getZoneClient(); + } + + @Override + public Zone load(Long zoneId) throws Exception { + checkNotNull(zoneId, "zoneId"); + return zoneClient.getZone(zoneId); + } + +} diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/ZoneIdToZoneSupplier.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/ZoneIdToZoneSupplier.java new file mode 100644 index 0000000000..fd4540835b --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/ZoneIdToZoneSupplier.java @@ -0,0 +1,50 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.cloudstack.suppliers; + +import com.google.common.base.Supplier; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.inject.Inject; +import org.jclouds.cloudstack.domain.Zone; + +import javax.inject.Named; +import java.util.concurrent.TimeUnit; + +import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; + +/** + * Supplies a cache that maps from zone IDs to zones. + * + * @author Richard Downer + */ +public class ZoneIdToZoneSupplier implements Supplier> { + private final LoadingCache cache; + + @Inject + public ZoneIdToZoneSupplier(CacheLoader zoneIdToZone, @Named(PROPERTY_SESSION_INTERVAL) long expirationSecs) { + cache = CacheBuilder.newBuilder().expireAfterWrite(expirationSecs, TimeUnit.SECONDS).build(zoneIdToZone); + } + + @Override + public LoadingCache get() { + return cache; + } +}