From e2bb762cb1d4ffae94ed692e8b36370cfc7eb61e Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 21 Dec 2011 03:12:47 -0800 Subject: [PATCH] updated cloudservers to current style of credential management --- .../functions/CloudServersImageToImage.java | 2 - .../functions/ServerToNodeMetadata.java | 7 +-- ...ersCreateNodeWithGroupEncodedIntoName.java | 33 +++++++++++--- .../CloudServersGetNodeMetadataStrategy.java | 40 ++++++++++++++--- .../suppliers/CloudServersImageSupplier.java | 13 ++++-- .../CloudServersImageToImageTest.java | 19 +++----- .../functions/ServerToNodeMetadataTest.java | 44 ++----------------- 7 files changed, 80 insertions(+), 78 deletions(-) diff --git a/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/functions/CloudServersImageToImage.java b/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/functions/CloudServersImageToImage.java index 40fd333d4a..bedecffe6b 100644 --- a/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/functions/CloudServersImageToImage.java +++ b/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/functions/CloudServersImageToImage.java @@ -24,7 +24,6 @@ import javax.inject.Singleton; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.OperatingSystem; -import org.jclouds.domain.LoginCredentials; import com.google.common.base.Function; @@ -48,7 +47,6 @@ public class CloudServersImageToImage implements Function { protected Logger logger = Logger.NULL; protected final Supplier location; - protected final Map credentialStore; protected final Map serverToNodeState; protected final Supplier> images; protected final Supplier> hardwares; @@ -94,11 +91,10 @@ public class ServerToNodeMetadata implements Function { } @Inject - ServerToNodeMetadata(Map serverStateToNodeState, Map credentialStore, + ServerToNodeMetadata(Map serverStateToNodeState, @Memoized Supplier> images, Supplier location, @Memoized Supplier> hardwares) { this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState"); - this.credentialStore = checkNotNull(credentialStore, "credentialStore"); this.images = checkNotNull(images, "images"); this.location = checkNotNull(location, "location"); this.hardwares = checkNotNull(hardwares, "hardwares"); @@ -120,7 +116,6 @@ public class ServerToNodeMetadata implements Function { builder.state(serverToNodeState.get(from.getStatus())); builder.publicAddresses(from.getAddresses().getPublicAddresses()); builder.privateAddresses(from.getAddresses().getPrivateAddresses()); - builder.credentials(LoginCredentials.fromCredentials(credentialStore.get("node#" + from.getId()))); return builder.build(); } diff --git a/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/strategy/CloudServersCreateNodeWithGroupEncodedIntoName.java b/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/strategy/CloudServersCreateNodeWithGroupEncodedIntoName.java index 1f7d682e3b..5a68faff3a 100644 --- a/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/strategy/CloudServersCreateNodeWithGroupEncodedIntoName.java +++ b/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/strategy/CloudServersCreateNodeWithGroupEncodedIntoName.java @@ -19,6 +19,7 @@ package org.jclouds.cloudservers.compute.strategy; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; import static org.jclouds.cloudservers.options.CreateServerOptions.Builder.withMetadata; import java.util.Map; @@ -28,10 +29,13 @@ import javax.inject.Singleton; import org.jclouds.cloudservers.CloudServersClient; import org.jclouds.cloudservers.domain.Server; +import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.Template; import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; +import org.jclouds.compute.strategy.PrioritizeCredentialsFromTemplate; import org.jclouds.domain.Credentials; +import org.jclouds.domain.LoginCredentials; import com.google.common.base.Function; @@ -42,23 +46,40 @@ import com.google.common.base.Function; public class CloudServersCreateNodeWithGroupEncodedIntoName implements CreateNodeWithGroupEncodedIntoName { protected final CloudServersClient client; protected final Map credentialStore; + protected final PrioritizeCredentialsFromTemplate prioritizeCredentialsFromTemplate; protected final Function serverToNodeMetadata; @Inject - protected CloudServersCreateNodeWithGroupEncodedIntoName(CloudServersClient client, Map credentialStore, + protected CloudServersCreateNodeWithGroupEncodedIntoName(CloudServersClient client, + Map credentialStore, + PrioritizeCredentialsFromTemplate prioritizeCredentialsFromTemplate, Function serverToNodeMetadata) { this.client = checkNotNull(client, "client"); this.credentialStore = checkNotNull(credentialStore, "credentialStore"); this.serverToNodeMetadata = checkNotNull(serverToNodeMetadata, "serverToNodeMetadata"); + this.prioritizeCredentialsFromTemplate = checkNotNull(prioritizeCredentialsFromTemplate, + "prioritizeCredentialsFromTemplate"); } @Override public NodeMetadata createNodeWithGroupEncodedIntoName(String group, String name, Template template) { - Server from = client.createServer(name, Integer.parseInt(template.getImage().getProviderId()), - Integer.parseInt(template.getHardware().getProviderId()), - withMetadata(template.getOptions().getUserMetadata())); - credentialStore.put("node#" + from.getId(), new Credentials("root", from.getAdminPass())); - return serverToNodeMetadata.apply(from); + checkState(group != null, "group (that which groups identical nodes together) must be specified"); + checkState(name != null && name.indexOf(group) != -1, "name should have %s encoded into it", group); + checkState(template != null, "template must be specified"); + + Server server = client + .createServer(name, Integer.parseInt(template.getImage().getProviderId()), Integer.parseInt(template + .getHardware().getProviderId()), withMetadata(template.getOptions().getUserMetadata())); + + NodeAndInitialCredentials from = new NodeAndInitialCredentials(server, server.getId() + "", + LoginCredentials.builder().password(server.getAdminPass()).build()); + + LoginCredentials fromNode = from.getCredentials(); + LoginCredentials creds = prioritizeCredentialsFromTemplate.apply(template, fromNode); + if (creds != null) + credentialStore.put("node#" + from.getNodeId(), creds); + NodeMetadata node = serverToNodeMetadata.apply(from.getNode()); + return node; } } diff --git a/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/strategy/CloudServersGetNodeMetadataStrategy.java b/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/strategy/CloudServersGetNodeMetadataStrategy.java index da438407b0..1347285ae4 100644 --- a/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/strategy/CloudServersGetNodeMetadataStrategy.java +++ b/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/strategy/CloudServersGetNodeMetadataStrategy.java @@ -18,15 +18,23 @@ */ package org.jclouds.cloudservers.compute.strategy; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + import javax.inject.Inject; import javax.inject.Singleton; -import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.strategy.GetNodeMetadataStrategy; import org.jclouds.cloudservers.CloudServersClient; import org.jclouds.cloudservers.domain.Server; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.NodeMetadataBuilder; +import org.jclouds.compute.strategy.GetNodeMetadataStrategy; +import org.jclouds.domain.Credentials; +import org.jclouds.domain.LoginCredentials; import com.google.common.base.Function; +import com.google.common.base.Functions; /** * @author Adrian Cole @@ -35,19 +43,37 @@ import com.google.common.base.Function; public class CloudServersGetNodeMetadataStrategy implements GetNodeMetadataStrategy { private final CloudServersClient client; - private final Function serverToNodeMetadata; + private final Map credentialStore; + private final Function nodeMetadataAdapter; @Inject - protected CloudServersGetNodeMetadataStrategy(CloudServersClient client, - Function serverToNodeMetadata) { + protected CloudServersGetNodeMetadataStrategy(CloudServersClient client, Map credentialStore, + Function nodeMetadataAdapter) { this.client = client; - this.serverToNodeMetadata = serverToNodeMetadata; + this.credentialStore = credentialStore; + this.nodeMetadataAdapter = Functions.compose(addLoginCredentials, checkNotNull(nodeMetadataAdapter, + "nodeMetadataAdapter")); } + private final Function addLoginCredentials = new Function() { + + @Override + public NodeMetadata apply(NodeMetadata arg0) { + return credentialStore.containsKey("node#" + arg0.getId()) ? NodeMetadataBuilder.fromNodeMetadata(arg0) + .credentials(LoginCredentials.fromCredentials(credentialStore.get("node#" + arg0.getId()))).build() + : arg0; + } + + @Override + public String toString() { + return "addLoginCredentialsFromCredentialStore()"; + } + }; + @Override public NodeMetadata getNode(String id) { int serverId = Integer.parseInt(id); Server server = client.getServer(serverId); - return server == null ? null : serverToNodeMetadata.apply(server); + return server == null ? null : nodeMetadataAdapter.apply(server); } } diff --git a/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/suppliers/CloudServersImageSupplier.java b/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/suppliers/CloudServersImageSupplier.java index 54bf93b3b2..220e1ec49f 100644 --- a/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/suppliers/CloudServersImageSupplier.java +++ b/apis/cloudservers/src/main/java/org/jclouds/cloudservers/compute/suppliers/CloudServersImageSupplier.java @@ -27,12 +27,14 @@ import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; +import org.jclouds.cloudservers.CloudServersClient; +import org.jclouds.compute.config.ComputeServiceAdapterContextModule.AddDefaultCredentialsToImage; import org.jclouds.compute.domain.Image; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.logging.Logger; -import org.jclouds.cloudservers.CloudServersClient; import com.google.common.base.Function; +import com.google.common.base.Functions; import com.google.common.base.Supplier; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; @@ -50,20 +52,23 @@ public class CloudServersImageSupplier implements Supplier> protected final CloudServersClient sync; protected final Function cloudServersImageToImage; + protected final AddDefaultCredentialsToImage addDefaultCredentialsToImage; @Inject CloudServersImageSupplier(CloudServersClient sync, - Function cloudServersImageToImage) { + Function cloudServersImageToImage, + AddDefaultCredentialsToImage addDefaultCredentialsToImage) { this.sync = sync; this.cloudServersImageToImage = cloudServersImageToImage; + this.addDefaultCredentialsToImage = addDefaultCredentialsToImage; } @Override public Set get() { Set images; logger.debug(">> providing images"); - images = Sets. newLinkedHashSet(Iterables.transform(sync.listImages(withDetails()), - cloudServersImageToImage)); + images = Sets. newLinkedHashSet(Iterables.transform(sync.listImages(withDetails()), Functions.compose( + addDefaultCredentialsToImage, cloudServersImageToImage))); logger.debug("<< images(%d)", images.size()); return images; } diff --git a/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/CloudServersImageToImageTest.java b/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/CloudServersImageToImageTest.java index 990055628b..adda82530a 100644 --- a/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/CloudServersImageToImageTest.java +++ b/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/CloudServersImageToImageTest.java @@ -29,7 +29,6 @@ import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OsFamily; import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.domain.LoginCredentials; import org.jclouds.json.Json; import org.jclouds.json.config.GsonModule; import org.testng.annotations.Test; @@ -44,22 +43,18 @@ public class CloudServersImageToImageTest { @Test public void testApplyWhereImageNotFound() throws UnknownHostException { - assertEquals( - convertImage(), - new ImageBuilder() - .name("CentOS 5.2") - .operatingSystem( - new OperatingSystem.Builder().family(OsFamily.CENTOS).version("5.2").description("CentOS 5.2").is64Bit(true) - .build()).description("CentOS 5.2").defaultCredentials(LoginCredentials.builder().user("root").build()) - .ids("2").version("1286712000000").build()); + assertEquals(convertImage(), new ImageBuilder().name("CentOS 5.2").operatingSystem( + new OperatingSystem.Builder().family(OsFamily.CENTOS).version("5.2").description("CentOS 5.2").is64Bit( + true).build()).description("CentOS 5.2").ids("2").version("1286712000000").build()); } public static Image convertImage() { org.jclouds.cloudservers.domain.Image image = ParseImageFromJsonResponseTest.parseImage(); - CloudServersImageToImage parser = new CloudServersImageToImage(new CloudServersImageToOperatingSystem(new BaseComputeServiceContextModule() { - }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule()) - .getInstance(Json.class)))); + CloudServersImageToImage parser = new CloudServersImageToImage(new CloudServersImageToOperatingSystem( + new BaseComputeServiceContextModule() { + }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice + .createInjector(new GsonModule()).getInstance(Json.class)))); return parser.apply(image); } diff --git a/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/ServerToNodeMetadataTest.java b/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/ServerToNodeMetadataTest.java index f30bfd94a5..eaa4b574a1 100644 --- a/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/ServerToNodeMetadataTest.java +++ b/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/ServerToNodeMetadataTest.java @@ -39,11 +39,9 @@ import org.jclouds.compute.domain.OsFamily; import org.jclouds.compute.domain.Processor; import org.jclouds.compute.domain.Volume; import org.jclouds.compute.domain.VolumeBuilder; -import org.jclouds.domain.Credentials; import org.jclouds.domain.Location; import org.jclouds.domain.LocationBuilder; import org.jclouds.domain.LocationScope; -import org.jclouds.domain.LoginCredentials; import org.testng.annotations.Test; import com.google.common.base.Suppliers; @@ -58,39 +56,6 @@ import com.google.common.collect.ImmutableSet; public class ServerToNodeMetadataTest { Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build(); - @Test - public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException { - LoginCredentials creds = LoginCredentials.builder().user("root").password("abcde").build(); - - Map serverStateToNodeState = CloudServersComputeServiceDependenciesModule.serverToNodeState; - Set images = ImmutableSet.of(); - Set hardwares = ImmutableSet.of(); - Server server = ParseServerFromJsonResponseTest.parseServer(); - - ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap - . of("node#1234", creds), Suppliers.> ofInstance(images), - Suppliers.ofInstance(provider), Suppliers.> ofInstance(hardwares)); - - NodeMetadata metadata = parser.apply(server); - - assertEquals( - metadata, - new NodeMetadataBuilder() - .state(NodeState.PENDING) - .publicAddresses(ImmutableSet.of("67.23.10.132", "67.23.10.131")) - .privateAddresses(ImmutableSet.of("10.176.42.16")) - .imageId("2") - .id("1234") - .providerId("1234") - .name("sample-server") - .hostname("sample-server") - .credentials(creds) - .location( - new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") - .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) - .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); - } - @Test public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException { Map serverStateToNodeState = CloudServersComputeServiceDependenciesModule.serverToNodeState; @@ -98,8 +63,7 @@ public class ServerToNodeMetadataTest { Set hardwares = ImmutableSet.of(); Server server = ParseServerFromJsonResponseTest.parseServer(); - ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap - . of(), Suppliers.> ofInstance(images), Suppliers + ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.> ofInstance(images), Suppliers .ofInstance(provider), Suppliers.> ofInstance(hardwares)); NodeMetadata metadata = parser.apply(server); @@ -130,8 +94,7 @@ public class ServerToNodeMetadataTest { Set hardwares = ImmutableSet.of(); Server server = ParseServerFromJsonResponseTest.parseServer(); - ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap - . of(), Suppliers.> ofInstance(images), Suppliers + ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.> ofInstance(images), Suppliers .ofInstance(provider), Suppliers.> ofInstance(hardwares)); NodeMetadata metadata = parser.apply(server); @@ -164,8 +127,7 @@ public class ServerToNodeMetadataTest { Set hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor()); Server server = ParseServerFromJsonResponseTest.parseServer(); - ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap - . of(), Suppliers.> ofInstance(images), Suppliers + ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.> ofInstance(images), Suppliers .ofInstance(provider), Suppliers.> ofInstance(hardwares)); NodeMetadata metadata = parser.apply(server);