Create a LoadingCache that maps from zone ID to Zone object, and use it in createNodeWithGroupEncodedIntoName(). (Review feedback)

This commit is contained in:
Richard Downer 2012-01-09 13:56:41 +02:00
parent f9c86860ab
commit abe77fd25a
4 changed files with 118 additions and 2 deletions

View File

@ -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<CacheLoader<Long, Set<IPForwardingRule>>>() {
}).to(GetIPForwardingRulesByVirtualMachine.class);
bind(new TypeLiteral<CacheLoader<Long, Zone>>() {
}).to(ZoneIdToZone.class);
bind(new TypeLiteral<Supplier<LoadingCache<Long, Zone>>>() {
}).to(ZoneIdToZoneSupplier.class);
}
@Provides

View File

@ -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<Long, Set<IPForwardingRule>> vmToRules;
private final Map<String, Credentials> credentialStore;
private final Map<NetworkType, ? extends OptionsConverter> optionsConverters;
private final Supplier<LoadingCache<Long, Zone>> zoneIdToZone;
@Inject
public CloudStackComputeServiceAdapter(CloudStackClient client, Predicate<Long> jobComplete,
@ -100,7 +103,8 @@ public class CloudStackComputeServiceAdapter implements
BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult,
StaticNATVirtualMachineInNetwork.Factory staticNATVMInNetwork,
CreatePortForwardingRulesForIP setupPortForwardingRulesForIP, LoadingCache<Long, Set<IPForwardingRule>> vmToRules,
Map<String, Credentials> credentialStore, Map<NetworkType, ? extends OptionsConverter> optionsConverters) {
Map<String, Credentials> credentialStore, Map<NetworkType, ? extends OptionsConverter> optionsConverters,
Supplier<LoadingCache<Long, Zone>> 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<Long, Network> 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);

View File

@ -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<Long, Zone> {
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);
}
}

View File

@ -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<LoadingCache<Long, Zone>> {
private final LoadingCache<Long, Zone> cache;
@Inject
public ZoneIdToZoneSupplier(CacheLoader<Long, Zone> zoneIdToZone, @Named(PROPERTY_SESSION_INTERVAL) long expirationSecs) {
cache = CacheBuilder.newBuilder().expireAfterWrite(expirationSecs, TimeUnit.SECONDS).build(zoneIdToZone);
}
@Override
public LoadingCache<Long, Zone> get() {
return cache;
}
}