From 6e09985659951c3d446ad02f8d1283f5fcc45b46 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 16 Aug 2010 12:25:22 -0700 Subject: [PATCH] normalized tag parsing code --- .../compute/util/ComputeServiceUtils.java | 57 ++++++++------- .../jclouds/compute/util/ComputeUtils.java | 4 +- .../functions/ServerToNodeMetadata.java | 19 +++-- .../functions/InstanceToNodeMetadata.java | 24 +++---- .../functions/ServerToNodeMetadata.java | 16 ++--- ...imuHostingComputeServiceContextModule.java | 70 ++++++++++--------- .../functions/SliceToNodeMetadata.java | 13 ++-- .../functions/VCloudGetNodeMetadata.java | 37 ++++------ 8 files changed, 109 insertions(+), 131 deletions(-) diff --git a/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java b/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java index bc7db4b2d9..9d0de65a67 100644 --- a/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java +++ b/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java @@ -24,6 +24,8 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Map.Entry; import java.util.concurrent.Callable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jclouds.compute.ComputeServiceContextBuilder; import org.jclouds.compute.domain.Architecture; @@ -45,22 +47,24 @@ import com.google.common.collect.Iterables; * @author Adrian Cole */ public class ComputeServiceUtils { + public static final Pattern DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+"); + + public static String parseTagFromName(String from) { + Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from); + return matcher.find() ? matcher.group(1) : "NOTAG-" + from; + } public static final Map> NAME_VERSION_MAP = ImmutableMap - .> of( - org.jclouds.compute.domain.OsFamily.CENTOS, ImmutableMap - . builder().put("5.3", "5.3").put("5.4", "5.4").put( - "5.5", "5.5").build(), - org.jclouds.compute.domain.OsFamily.RHEL, ImmutableMap - . builder().put("5.3", "5.3").put("5.4", "5.4").put( - "5.5", "5.5").build(), - org.jclouds.compute.domain.OsFamily.UBUNTU, ImmutableMap - . builder().put("hardy", "8.04").put("intrepid", - "8.10").put("jaunty", "9.04").put("karmic", "9.10").put( - "lucid", "10.04").put("maverick", "10.10").build()); + .> of(org.jclouds.compute.domain.OsFamily.CENTOS, + ImmutableMap. builder().put("5.3", "5.3").put("5.4", "5.4").put("5.5", "5.5") + .build(), org.jclouds.compute.domain.OsFamily.RHEL, + ImmutableMap. builder().put("5.3", "5.3").put("5.4", "5.4").put("5.5", "5.5") + .build(), org.jclouds.compute.domain.OsFamily.UBUNTU, ImmutableMap + . builder().put("hardy", "8.04").put("intrepid", "8.10").put("jaunty", + "9.04").put("karmic", "9.10").put("lucid", "10.04").put("maverick", "10.10") + .build()); - public static String parseVersionOrReturnEmptyString(org.jclouds.compute.domain.OsFamily family, - final String in) { + public static String parseVersionOrReturnEmptyString(org.jclouds.compute.domain.OsFamily family, final String in) { if (NAME_VERSION_MAP.containsKey(family)) { CONTAINS_SUBSTRING contains = new CONTAINS_SUBSTRING(in.replace('-', '.')); try { @@ -94,27 +98,24 @@ public class ComputeServiceUtils { Formatter fmt = new Formatter().format("Execution failures:%n%n"); int index = 1; for (Entry errorMessage : executionExceptions.entrySet()) { - fmt.format("%s) %s on %s:%n%s%n%n", index++, errorMessage.getValue().getClass() - .getSimpleName(), errorMessage.getKey(), Throwables - .getStackTraceAsString(errorMessage.getValue())); + fmt.format("%s) %s on %s:%n%s%n%n", index++, errorMessage.getValue().getClass().getSimpleName(), errorMessage + .getKey(), Throwables.getStackTraceAsString(errorMessage.getValue())); } return fmt.format("%s error[s]", executionExceptions.size()).toString(); } - public static String createNodeErrorMessage( - Map failedNodes) { + public static String createNodeErrorMessage(Map failedNodes) { Formatter fmt = new Formatter().format("Node failures:%n%n"); int index = 1; for (Entry errorMessage : failedNodes.entrySet()) { - fmt.format("%s) %s on node %s:%n%s%n%n", index++, errorMessage.getValue().getClass() - .getSimpleName(), errorMessage.getKey().getId(), Throwables - .getStackTraceAsString(errorMessage.getValue())); + fmt.format("%s) %s on node %s:%n%s%n%n", index++, errorMessage.getValue().getClass().getSimpleName(), + errorMessage.getKey().getId(), Throwables.getStackTraceAsString(errorMessage.getValue())); } return fmt.format("%s error[s]", failedNodes.size()).toString(); } - public static Iterable filterByName( - Iterable nodes, final String name) { + public static Iterable filterByName(Iterable nodes, + final String name) { return Iterables.filter(nodes, new Predicate() { @Override public boolean apply(ComputeMetadata input) { @@ -144,8 +145,7 @@ public class ComputeServiceUtils { public static boolean isKeyAuth(NodeMetadata createdNode) { return createdNode.getCredentials().credential != null - && createdNode.getCredentials().credential - .startsWith("-----BEGIN RSA PRIVATE KEY-----"); + && createdNode.getCredentials().credential.startsWith("-----BEGIN RSA PRIVATE KEY-----"); } /** @@ -153,10 +153,9 @@ public class ComputeServiceUtils { * returns a new instance of {@link NodeMetadata} that has new credentials */ public static NodeMetadata installNewCredentials(NodeMetadata node, Credentials newCredentials) { - return new NodeMetadataImpl(node.getProviderId(), node.getName(), node.getId(), node - .getLocation(), node.getUri(), node.getUserMetadata(), node.getTag(), node - .getImage(), node.getState(), node.getPublicAddresses(), node.getPrivateAddresses(), - node.getExtra(), newCredentials); + return new NodeMetadataImpl(node.getProviderId(), node.getName(), node.getId(), node.getLocation(), + node.getUri(), node.getUserMetadata(), node.getTag(), node.getImage(), node.getState(), node + .getPublicAddresses(), node.getPrivateAddresses(), node.getExtra(), newCredentials); } public static Iterable getSupportedProviders() { diff --git a/compute/src/main/java/org/jclouds/compute/util/ComputeUtils.java b/compute/src/main/java/org/jclouds/compute/util/ComputeUtils.java index 68561adc07..a87adaa868 100644 --- a/compute/src/main/java/org/jclouds/compute/util/ComputeUtils.java +++ b/compute/src/main/java/org/jclouds/compute/util/ComputeUtils.java @@ -195,8 +195,8 @@ public class ComputeUtils { @Nullable SshCallable last) { checkState(this.sshFactory != null, "runScript requested, but no SshModule configured"); checkNodeHasPublicIps(node); - checkNotNull(node.getCredentials(), "credentials for node " + node.getId()); - checkNotNull(node.getCredentials().credential, "credentials.credential for node " + node.getId()); + checkNotNull(node.getCredentials(), "credentials for node " + node.getName()); + checkNotNull(node.getCredentials().credential, "credentials.credential for node " + node.getName()); SshClient ssh = createSshClientOncePortIsListeningOnNode(node); try { ssh.connect(); diff --git a/gogrid/src/main/java/org/jclouds/gogrid/compute/functions/ServerToNodeMetadata.java b/gogrid/src/main/java/org/jclouds/gogrid/compute/functions/ServerToNodeMetadata.java index 4eab53e7b9..5ffd324378 100644 --- a/gogrid/src/main/java/org/jclouds/gogrid/compute/functions/ServerToNodeMetadata.java +++ b/gogrid/src/main/java/org/jclouds/gogrid/compute/functions/ServerToNodeMetadata.java @@ -20,12 +20,11 @@ package org.jclouds.gogrid.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.annotation.Resource; import javax.inject.Inject; @@ -54,7 +53,6 @@ import com.google.common.collect.Iterables; */ @Singleton public class ServerToNodeMetadata implements Function { - public static final Pattern ALL_BEFORE_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+"); @Resource protected Logger logger = Logger.NULL; @@ -74,14 +72,14 @@ public class ServerToNodeMetadata implements Function { @Override public boolean apply(Image input) { return input.getProviderId().equals(instance.getImage().getId() + "") - && (input.getLocation() == null || input.getLocation().getId().equals( - instance.getDatacenter().getId() + "")); + && (input.getLocation() == null || input.getLocation().getId().equals( + instance.getDatacenter().getId() + "")); } } @Inject ServerToNodeMetadata(Map serverStateToNodeState, GoGridClient client, - Set images, Map locations) { + Set images, Map locations) { this.serverStateToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState"); this.client = checkNotNull(client, "client"); this.images = checkNotNull(images, "images"); @@ -90,8 +88,7 @@ public class ServerToNodeMetadata implements Function { @Override public NodeMetadata apply(Server from) { - Matcher matcher = ALL_BEFORE_HYPHEN_HEX.matcher(from.getName()); - final String tag = matcher.find() ? matcher.group(1) : null; + String tag = parseTagFromName(from.getName()); Set ipSet = ImmutableSet.of(from.getIp().getIp()); NodeState state = serverStateToNodeState.get(from.getState()); Credentials creds = client.getServerServices().getServerCredentialsList().get(from.getName()); @@ -102,8 +99,8 @@ public class ServerToNodeMetadata implements Function { logger.warn("could not find a matching image for server %s", from); } return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", locations.get(from - .getDatacenter().getId() - + ""), null, ImmutableMap. of(), tag, image, state, ipSet, ImmutableList. of(), - ImmutableMap. of(), creds); + .getDatacenter().getId() + + ""), null, ImmutableMap. of(), tag, image, state, ipSet, ImmutableList. of(), + ImmutableMap. of(), creds); } } \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/compute/functions/InstanceToNodeMetadata.java b/ibmdev/src/main/java/org/jclouds/ibmdev/compute/functions/InstanceToNodeMetadata.java index b0dbfc16ea..2c12bc93d6 100644 --- a/ibmdev/src/main/java/org/jclouds/ibmdev/compute/functions/InstanceToNodeMetadata.java +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/compute/functions/InstanceToNodeMetadata.java @@ -20,11 +20,10 @@ package org.jclouds.ibmdev.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName; import java.util.Map; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.annotation.Resource; import javax.inject.Inject; @@ -50,7 +49,6 @@ import com.google.common.collect.ImmutableSet; */ @Singleton public class InstanceToNodeMetadata implements Function { - public static final Pattern ALL_BEFORE_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+"); @Resource protected Logger logger = Logger.NULL; @@ -61,11 +59,9 @@ public class InstanceToNodeMetadata implements Function @Inject InstanceToNodeMetadata(Map instanceStateToNodeState, - Map images, - @Named("CREDENTIALS") Map credentialsMap, + Map images, @Named("CREDENTIALS") Map credentialsMap, Map locations) { - this.instanceStateToNodeState = checkNotNull(instanceStateToNodeState, - "instanceStateToNodeState"); + this.instanceStateToNodeState = checkNotNull(instanceStateToNodeState, "instanceStateToNodeState"); this.images = checkNotNull(images, "images"); this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap"); this.locations = checkNotNull(locations, "locations"); @@ -73,16 +69,14 @@ public class InstanceToNodeMetadata implements Function @Override public NodeMetadata apply(Instance from) { - Matcher matcher = ALL_BEFORE_HYPHEN_HEX.matcher(from.getName()); - final String tag = matcher.find() ? matcher.group(1) : null; - Set ipSet = from.getIp() != null ? ImmutableSet.of(from.getIp()) : ImmutableSet - . of(); + String tag = parseTagFromName(from.getName()); + Set ipSet = from.getIp() != null ? ImmutableSet.of(from.getIp()) : ImmutableSet. of(); NodeState state = instanceStateToNodeState.get(from.getStatus()); Image image = images.get(from.getImageId()); String key = tag != null ? credentialsMap.get(tag) : null; - return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", locations - .get(image.getLocation()), null, ImmutableMap. of(), tag, image, - state, ipSet, ImmutableList. of(), ImmutableMap. of(), - new Credentials(image.getDefaultCredentials().identity, key)); + return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", locations.get(image + .getLocation()), null, ImmutableMap. of(), tag, image, state, ipSet, ImmutableList + . of(), ImmutableMap. of(), new Credentials( + image.getDefaultCredentials().identity, key)); } } \ No newline at end of file diff --git a/rackspace/src/main/java/org/jclouds/rackspace/cloudservers/compute/functions/ServerToNodeMetadata.java b/rackspace/src/main/java/org/jclouds/rackspace/cloudservers/compute/functions/ServerToNodeMetadata.java index a72e79a810..80df86598e 100644 --- a/rackspace/src/main/java/org/jclouds/rackspace/cloudservers/compute/functions/ServerToNodeMetadata.java +++ b/rackspace/src/main/java/org/jclouds/rackspace/cloudservers/compute/functions/ServerToNodeMetadata.java @@ -20,12 +20,11 @@ package org.jclouds.rackspace.cloudservers.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.annotation.Resource; import javax.inject.Inject; @@ -50,7 +49,6 @@ import com.google.common.collect.Iterables; * @author Adrian Cole */ public class ServerToNodeMetadata implements Function { - public static final Pattern DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+"); private final Location location; private final Map serverToNodeState; private final Set images; @@ -70,13 +68,13 @@ public class ServerToNodeMetadata implements Function { @Override public boolean apply(Image input) { return input.getProviderId().equals(instance.getImageId() + "") - && (input.getLocation() == null || input.getLocation().equals(location.getParent())); + && (input.getLocation() == null || input.getLocation().equals(location.getParent())); } } @Inject ServerToNodeMetadata(Map serverStateToNodeState, Set images, - Location location) { + Location location) { this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState"); this.images = checkNotNull(images, "images"); this.location = checkNotNull(location, "location"); @@ -84,8 +82,7 @@ public class ServerToNodeMetadata implements Function { @Override public NodeMetadata apply(Server from) { - Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from.getName()); - final String tag = matcher.find() ? matcher.group(1) : null; + String tag = parseTagFromName(from.getName()); Location host = new LocationImpl(LocationScope.HOST, from.getHostId(), from.getHostId(), location); Image image = null; try { @@ -94,7 +91,8 @@ public class ServerToNodeMetadata implements Function { logger.warn("could not find a matching image for server %s in location %s", from, location); } return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", host, null, from.getMetadata(), - tag, image, serverToNodeState.get(from.getStatus()), from.getAddresses().getPublicAddresses(), from - .getAddresses().getPrivateAddresses(), ImmutableMap. of(), null); + tag, image, serverToNodeState.get(from.getStatus()), from.getAddresses().getPublicAddresses(), from + .getAddresses().getPrivateAddresses(), ImmutableMap. of(), null); } + } \ No newline at end of file diff --git a/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/compute/config/RimuHostingComputeServiceContextModule.java b/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/compute/config/RimuHostingComputeServiceContextModule.java index 19aaadb3b8..3241b9909d 100755 --- a/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/compute/config/RimuHostingComputeServiceContextModule.java +++ b/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/compute/config/RimuHostingComputeServiceContextModule.java @@ -21,6 +21,7 @@ package org.jclouds.rimuhosting.miro.compute.config; import static com.google.common.base.Preconditions.checkNotNull; import static org.jclouds.compute.domain.OsFamily.UBUNTU; +import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName; import static org.jclouds.rimuhosting.miro.reference.RimuHostingConstants.PROPERTY_RIMUHOSTING_DEFAULT_DC; import java.util.Map; @@ -124,7 +125,7 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Named("DEFAULT") protected TemplateBuilder provideTemplate(TemplateBuilder template) { return template.sizeId("MIRO1B").osFamily(UBUNTU).architecture(Architecture.X86_32).imageNameMatches( - ".*10\\.?04.*"); + ".*10\\.?04.*"); } @Provides @@ -183,7 +184,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Inject protected RimuHostingAddNodeWithTagStrategy(RimuHostingClient client, - Function> getPublicAddresses, Map runningStateToNodeState) { + Function> getPublicAddresses, + Map runningStateToNodeState) { this.client = client; this.getPublicAddresses = getPublicAddresses; this.runningStateToNodeState = runningStateToNodeState; @@ -192,13 +194,13 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Override public NodeMetadata execute(String tag, String name, Template template) { NewServerResponse serverResponse = client.createServer(name, checkNotNull(template.getImage().getProviderId(), - "imageId"), checkNotNull(template.getSize().getProviderId(), "sizeId")); + "imageId"), checkNotNull(template.getSize().getProviderId(), "sizeId")); Server server = client.getServer(serverResponse.getServer().getId()); NodeMetadata node = new NodeMetadataImpl(server.getId().toString(), name, server.getId().toString(), template - .getLocation(), null, ImmutableMap. of(), tag, template.getImage(), - runningStateToNodeState.get(server.getState()), getPublicAddresses.apply(server), ImmutableList - . of(), ImmutableMap. of(), new Credentials("root", serverResponse - .getNewInstanceRequest().getCreateOptions().getPassword())); + .getLocation(), null, ImmutableMap. of(), tag, template.getImage(), + runningStateToNodeState.get(server.getState()), getPublicAddresses.apply(server), ImmutableList + . of(), ImmutableMap. of(), new Credentials("root", serverResponse + .getNewInstanceRequest().getCreateOptions().getPassword())); return node; } @@ -211,7 +213,7 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Inject protected RimuHostingListNodesStrategy(RimuHostingClient client, - Function serverToNodeMetadata) { + Function serverToNodeMetadata) { this.client = client; this.serverToNodeMetadata = serverToNodeMetadata; } @@ -236,7 +238,7 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Inject protected RimuHostingGetNodeMetadataStrategy(RimuHostingClient client, - Function serverToNodeMetadata) { + Function serverToNodeMetadata) { this.client = client; this.serverToNodeMetadata = serverToNodeMetadata; } @@ -251,11 +253,11 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @VisibleForTesting static final Map runningStateToNodeState = ImmutableMap. builder() - .put(RunningState.RUNNING, NodeState.RUNNING)// - .put(RunningState.NOTRUNNING, NodeState.SUSPENDED)// - .put(RunningState.POWERCYCLING, NodeState.PENDING)// - .put(RunningState.RESTARTING, NodeState.PENDING)// - .build(); + .put(RunningState.RUNNING, NodeState.RUNNING)// + .put(RunningState.NOTRUNNING, NodeState.SUSPENDED)// + .put(RunningState.POWERCYCLING, NodeState.PENDING)// + .put(RunningState.RESTARTING, NodeState.PENDING)// + .build(); @Singleton @Provides @@ -286,16 +288,16 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Override public boolean apply(Image input) { return input.getProviderId().equals(instance.getImageId()) - && (input.getLocation() == null || input.getLocation().equals(location) || input.getLocation() - .equals(location.getParent())); + && (input.getLocation() == null || input.getLocation().equals(location) || input.getLocation() + .equals(location.getParent())); } } @SuppressWarnings("unused") @Inject ServerToNodeMetadata(Function> getPublicAddresses, - Map runningStateToNodeState, Set images, - Set locations) { + Map runningStateToNodeState, Set images, + Set locations) { this.getPublicAddresses = checkNotNull(getPublicAddresses, "serverStateToNodeState"); this.runningStateToNodeState = checkNotNull(runningStateToNodeState, "serverStateToNodeState"); this.images = checkNotNull(images, "images"); @@ -306,8 +308,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { public NodeMetadata apply(Server from) { Location location = new LocationImpl(LocationScope.ZONE, from.getLocation().getId(), from.getLocation() - .getName(), null); - String tag = from.getName().replaceAll("-[0-9]+", ""); + .getName(), null); + String tag = parseTagFromName(from.getName()); Credentials creds = null; Image image = null; @@ -318,8 +320,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { } NodeState state = runningStateToNodeState.get(from.getState()); return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", location, null, ImmutableMap - . of(), tag, image, state, getPublicAddresses.apply(from), ImmutableList. of(), - ImmutableMap. of(), creds); + . of(), tag, image, state, getPublicAddresses.apply(from), ImmutableList + . of(), ImmutableMap. of(), creds); } } @@ -329,14 +331,14 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Override public Iterable apply(Server server) { return server.getIpAddresses() == null ? ImmutableSet. of() : Iterables.concat(ImmutableList.of(server - .getIpAddresses().getPrimaryIp()), server.getIpAddresses().getSecondaryIps()); + .getIpAddresses().getPrimaryIp()), server.getIpAddresses().getSecondaryIps()); } } @Provides @Singleton Location getDefaultLocation(@Named(PROPERTY_RIMUHOSTING_DEFAULT_DC) final String defaultDC, - Set locations) { + Set locations) { return Iterables.find(locations, new Predicate() { @Override @@ -350,14 +352,14 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Provides @Singleton Set getDefaultLocations(RimuHostingClient sync, LogHolder holder, - Function indexer, @Provider String providerName) { + Function indexer, @Provider String providerName) { final Set locations = Sets.newHashSet(); holder.logger.debug(">> providing locations"); Location provider = new LocationImpl(LocationScope.PROVIDER, providerName, providerName, null); for (final PricingPlan from : sync.getPricingPlanList()) { try { locations.add(new LocationImpl(LocationScope.ZONE, from.getDataCenter().getId(), from.getDataCenter() - .getName(), provider)); + .getName(), provider)); } catch (NullPointerException e) { holder.logger.warn("datacenter not present in " + from.getId()); } @@ -380,9 +382,10 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Provides @Singleton protected Set provideSizes(RimuHostingClient sync, Set images, - Set locations, LogHolder holder, - @Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, Function indexer) - throws InterruptedException, TimeoutException, ExecutionException { + Set locations, LogHolder holder, + @Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, + Function indexer) throws InterruptedException, TimeoutException, + ExecutionException { final Set sizes = Sets.newHashSet(); holder.logger.debug(">> providing sizes"); for (final PricingPlan from : sync.getPricingPlanList()) { @@ -397,7 +400,7 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { }); sizes.add(new SizeImpl(from.getId(), from.getId(), from.getId(), location, null, ImmutableMap - . of(), 1, from.getRam(), from.getDiskSize(), ImagePredicates.any())); + . of(), 1, from.getRam(), from.getDiskSize(), ImagePredicates.any())); } catch (NullPointerException e) { holder.logger.warn("datacenter not present in " + from.getId()); } @@ -417,7 +420,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { @Provides @Singleton protected Set provideImages(final RimuHostingClient sync, LogHolder holder, - Function indexer) throws InterruptedException, ExecutionException, TimeoutException { + Function indexer) throws InterruptedException, ExecutionException, + TimeoutException { final Set images = Sets.newHashSet(); holder.logger.debug(">> providing images"); for (final org.jclouds.rimuhosting.miro.domain.Image from : sync.getImageList()) { @@ -438,8 +442,8 @@ public class RimuHostingComputeServiceContextModule extends AbstractModule { } images.add(new ImageImpl(from.getId(), from.getDescription(), from.getId(), null, null, ImmutableMap - . of(), from.getDescription(), version, os, osDescription, arch, new Credentials("root", - null))); + . of(), from.getDescription(), version, os, osDescription, arch, new Credentials( + "root", null))); } holder.logger.debug("<< images(%d)", images.size()); return images; diff --git a/slicehost/src/main/java/org/jclouds/slicehost/compute/functions/SliceToNodeMetadata.java b/slicehost/src/main/java/org/jclouds/slicehost/compute/functions/SliceToNodeMetadata.java index f6b2b12de9..84e66c63c3 100644 --- a/slicehost/src/main/java/org/jclouds/slicehost/compute/functions/SliceToNodeMetadata.java +++ b/slicehost/src/main/java/org/jclouds/slicehost/compute/functions/SliceToNodeMetadata.java @@ -20,12 +20,11 @@ package org.jclouds.slicehost.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.annotation.Resource; import javax.inject.Inject; @@ -47,7 +46,6 @@ import com.google.common.collect.Iterables; * @author Adrian Cole */ public class SliceToNodeMetadata implements Function { - public static final Pattern DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX = Pattern.compile("([^-]+)-[0-9a-f]+"); private final Location location; private final Map sliceToNodeState; private final Set images; @@ -70,7 +68,7 @@ public class SliceToNodeMetadata implements Function { @Inject SliceToNodeMetadata(Map sliceStateToNodeState, Set images, - Location location) { + Location location) { this.sliceToNodeState = checkNotNull(sliceStateToNodeState, "sliceStateToNodeState"); this.images = checkNotNull(images, "images"); this.location = checkNotNull(location, "location"); @@ -78,8 +76,7 @@ public class SliceToNodeMetadata implements Function { @Override public NodeMetadata apply(Slice from) { - Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from.getName()); - final String tag = matcher.find() ? matcher.group(1) : null; + String tag = parseTagFromName(from.getName()); Image image = null; try { image = Iterables.find(images, new FindImageForSlice(from)); @@ -88,8 +85,8 @@ public class SliceToNodeMetadata implements Function { } return new NodeMetadataImpl(from.getId() + "", from.getName(), from.getId() + "", location, null, ImmutableMap - . of(), tag, image, sliceToNodeState.get(from.getStatus()), Iterables.filter(from - .getAddresses(), new Predicate() { + . of(), tag, image, sliceToNodeState.get(from.getStatus()), Iterables.filter(from + .getAddresses(), new Predicate() { @Override public boolean apply(String input) { diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/compute/functions/VCloudGetNodeMetadata.java b/vcloud/core/src/main/java/org/jclouds/vcloud/compute/functions/VCloudGetNodeMetadata.java index fbffced35d..351223b70c 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/compute/functions/VCloudGetNodeMetadata.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/compute/functions/VCloudGetNodeMetadata.java @@ -20,12 +20,11 @@ package org.jclouds.vcloud.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName; import java.net.URI; import java.util.Map; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.annotation.Resource; import javax.inject.Inject; @@ -49,8 +48,8 @@ import org.jclouds.vcloud.domain.VAppStatus; import com.google.common.collect.ImmutableMap; /** - * Configures the {@link VCloudComputeServiceContext}; requires - * {@link BaseVCloudComputeClient} bound. + * Configures the {@link VCloudComputeServiceContext}; requires {@link BaseVCloudComputeClient} + * bound. * * @author Adrian Cole */ @@ -67,12 +66,10 @@ public class VCloudGetNodeMetadata { protected final GetExtra getExtra; protected final Map vAppStatusToNodeState; - public static final Pattern TAG_PATTERN_WITHOUT_TEMPLATE = Pattern.compile("([^-]+)-[0-9a-f]+"); - @Inject VCloudGetNodeMetadata(VCloudClient client, VCloudComputeClient computeClient, - Map vAppStatusToNodeState, GetExtra getExtra, - FindLocationForResource findLocationForResourceInVDC, Provider> images) { + Map vAppStatusToNodeState, GetExtra getExtra, + FindLocationForResource findLocationForResourceInVDC, Provider> images) { this.client = checkNotNull(client, "client"); this.images = checkNotNull(images, "images"); this.getExtra = checkNotNull(getExtra, "getExtra"); @@ -83,22 +80,14 @@ public class VCloudGetNodeMetadata { public NodeMetadata execute(String in) { URI id = URI.create(in); - VApp vApp = client.getVApp(id); - if (vApp == null) + VApp from = client.getVApp(id); + if (from == null) return null; - - String tag = null; - Image image = null; - Matcher matcher = TAG_PATTERN_WITHOUT_TEMPLATE.matcher(vApp.getName()); - if (matcher.find()) { - tag = matcher.group(1); - } else { - tag = "NOTAG-" + vApp.getName(); - } - Location location = findLocationForResourceInVDC.apply(vApp.getVDC()); - return new NodeMetadataImpl(vApp.getId().toASCIIString(), vApp.getName(), vApp.getId() - .toASCIIString(), location, vApp.getId(), ImmutableMap. of(), tag, image, - vAppStatusToNodeState.get(vApp.getStatus()), computeClient.getPublicAddresses(id), computeClient - .getPrivateAddresses(id), getExtra.apply(vApp), null); + String tag = parseTagFromName(from.getName()); + Location location = findLocationForResourceInVDC.apply(from.getVDC()); + return new NodeMetadataImpl(in, from.getName(), in, location, from.getId(), ImmutableMap. of(), + tag, null, vAppStatusToNodeState.get(from.getStatus()), computeClient.getPublicAddresses(id), + computeClient.getPrivateAddresses(id), getExtra.apply(from), null); } + } \ No newline at end of file