updated cloudservers to current style of credential management

This commit is contained in:
Adrian Cole 2011-12-21 03:12:47 -08:00
parent 83ca5dd931
commit e2bb762cb1
7 changed files with 80 additions and 78 deletions

View File

@ -24,7 +24,6 @@ import javax.inject.Singleton;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.domain.LoginCredentials;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -48,7 +47,6 @@ public class CloudServersImageToImage implements Function<org.jclouds.cloudserve
builder.description(from.getName()); builder.description(from.getName());
builder.version(from.getUpdated().getTime() + ""); builder.version(from.getUpdated().getTime() + "");
builder.operatingSystem(imageToOs.apply(from)); builder.operatingSystem(imageToOs.apply(from));
builder.defaultCredentials(LoginCredentials.builder().user("root").build());
Image image = builder.build(); Image image = builder.build();
return image; return image;
} }

View File

@ -40,11 +40,9 @@ import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeState; import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder; import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope; import org.jclouds.domain.LocationScope;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -62,7 +60,6 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
protected final Supplier<Location> location; protected final Supplier<Location> location;
protected final Map<String, Credentials> credentialStore;
protected final Map<ServerStatus, NodeState> serverToNodeState; protected final Map<ServerStatus, NodeState> serverToNodeState;
protected final Supplier<Set<? extends Image>> images; protected final Supplier<Set<? extends Image>> images;
protected final Supplier<Set<? extends Hardware>> hardwares; protected final Supplier<Set<? extends Hardware>> hardwares;
@ -94,11 +91,10 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
} }
@Inject @Inject
ServerToNodeMetadata(Map<ServerStatus, NodeState> serverStateToNodeState, Map<String, Credentials> credentialStore, ServerToNodeMetadata(Map<ServerStatus, NodeState> serverStateToNodeState,
@Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location, @Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location,
@Memoized Supplier<Set<? extends Hardware>> hardwares) { @Memoized Supplier<Set<? extends Hardware>> hardwares) {
this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState"); this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState");
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.images = checkNotNull(images, "images"); this.images = checkNotNull(images, "images");
this.location = checkNotNull(location, "location"); this.location = checkNotNull(location, "location");
this.hardwares = checkNotNull(hardwares, "hardwares"); this.hardwares = checkNotNull(hardwares, "hardwares");
@ -120,7 +116,6 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
builder.state(serverToNodeState.get(from.getStatus())); builder.state(serverToNodeState.get(from.getStatus()));
builder.publicAddresses(from.getAddresses().getPublicAddresses()); builder.publicAddresses(from.getAddresses().getPublicAddresses());
builder.privateAddresses(from.getAddresses().getPrivateAddresses()); builder.privateAddresses(from.getAddresses().getPrivateAddresses());
builder.credentials(LoginCredentials.fromCredentials(credentialStore.get("node#" + from.getId())));
return builder.build(); return builder.build();
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudservers.compute.strategy; package org.jclouds.cloudservers.compute.strategy;
import static com.google.common.base.Preconditions.checkNotNull; 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 static org.jclouds.cloudservers.options.CreateServerOptions.Builder.withMetadata;
import java.util.Map; import java.util.Map;
@ -28,10 +29,13 @@ import javax.inject.Singleton;
import org.jclouds.cloudservers.CloudServersClient; import org.jclouds.cloudservers.CloudServersClient;
import org.jclouds.cloudservers.domain.Server; import org.jclouds.cloudservers.domain.Server;
import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName;
import org.jclouds.compute.strategy.PrioritizeCredentialsFromTemplate;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.domain.LoginCredentials;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -42,23 +46,40 @@ import com.google.common.base.Function;
public class CloudServersCreateNodeWithGroupEncodedIntoName implements CreateNodeWithGroupEncodedIntoName { public class CloudServersCreateNodeWithGroupEncodedIntoName implements CreateNodeWithGroupEncodedIntoName {
protected final CloudServersClient client; protected final CloudServersClient client;
protected final Map<String, Credentials> credentialStore; protected final Map<String, Credentials> credentialStore;
protected final PrioritizeCredentialsFromTemplate prioritizeCredentialsFromTemplate;
protected final Function<Server, NodeMetadata> serverToNodeMetadata; protected final Function<Server, NodeMetadata> serverToNodeMetadata;
@Inject @Inject
protected CloudServersCreateNodeWithGroupEncodedIntoName(CloudServersClient client, Map<String, Credentials> credentialStore, protected CloudServersCreateNodeWithGroupEncodedIntoName(CloudServersClient client,
Map<String, Credentials> credentialStore,
PrioritizeCredentialsFromTemplate prioritizeCredentialsFromTemplate,
Function<Server, NodeMetadata> serverToNodeMetadata) { Function<Server, NodeMetadata> serverToNodeMetadata) {
this.client = checkNotNull(client, "client"); this.client = checkNotNull(client, "client");
this.credentialStore = checkNotNull(credentialStore, "credentialStore"); this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.serverToNodeMetadata = checkNotNull(serverToNodeMetadata, "serverToNodeMetadata"); this.serverToNodeMetadata = checkNotNull(serverToNodeMetadata, "serverToNodeMetadata");
this.prioritizeCredentialsFromTemplate = checkNotNull(prioritizeCredentialsFromTemplate,
"prioritizeCredentialsFromTemplate");
} }
@Override @Override
public NodeMetadata createNodeWithGroupEncodedIntoName(String group, String name, Template template) { public NodeMetadata createNodeWithGroupEncodedIntoName(String group, String name, Template template) {
Server from = client.createServer(name, Integer.parseInt(template.getImage().getProviderId()), checkState(group != null, "group (that which groups identical nodes together) must be specified");
Integer.parseInt(template.getHardware().getProviderId()), checkState(name != null && name.indexOf(group) != -1, "name should have %s encoded into it", group);
withMetadata(template.getOptions().getUserMetadata())); checkState(template != null, "template must be specified");
credentialStore.put("node#" + from.getId(), new Credentials("root", from.getAdminPass()));
return serverToNodeMetadata.apply(from); Server server = client
.createServer(name, Integer.parseInt(template.getImage().getProviderId()), Integer.parseInt(template
.getHardware().getProviderId()), withMetadata(template.getOptions().getUserMetadata()));
NodeAndInitialCredentials<Server> from = new NodeAndInitialCredentials<Server>(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;
} }
} }

View File

@ -18,15 +18,23 @@
*/ */
package org.jclouds.cloudservers.compute.strategy; 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.Inject;
import javax.inject.Singleton; 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.CloudServersClient;
import org.jclouds.cloudservers.domain.Server; 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.Function;
import com.google.common.base.Functions;
/** /**
* @author Adrian Cole * @author Adrian Cole
@ -35,19 +43,37 @@ import com.google.common.base.Function;
public class CloudServersGetNodeMetadataStrategy implements GetNodeMetadataStrategy { public class CloudServersGetNodeMetadataStrategy implements GetNodeMetadataStrategy {
private final CloudServersClient client; private final CloudServersClient client;
private final Function<Server, NodeMetadata> serverToNodeMetadata; private final Map<String, Credentials> credentialStore;
private final Function<Server, NodeMetadata> nodeMetadataAdapter;
@Inject @Inject
protected CloudServersGetNodeMetadataStrategy(CloudServersClient client, protected CloudServersGetNodeMetadataStrategy(CloudServersClient client, Map<String, Credentials> credentialStore,
Function<Server, NodeMetadata> serverToNodeMetadata) { Function<Server, NodeMetadata> nodeMetadataAdapter) {
this.client = client; this.client = client;
this.serverToNodeMetadata = serverToNodeMetadata; this.credentialStore = credentialStore;
this.nodeMetadataAdapter = Functions.compose(addLoginCredentials, checkNotNull(nodeMetadataAdapter,
"nodeMetadataAdapter"));
} }
private final Function<NodeMetadata, NodeMetadata> addLoginCredentials = new Function<NodeMetadata, NodeMetadata>() {
@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 @Override
public NodeMetadata getNode(String id) { public NodeMetadata getNode(String id) {
int serverId = Integer.parseInt(id); int serverId = Integer.parseInt(id);
Server server = client.getServer(serverId); Server server = client.getServer(serverId);
return server == null ? null : serverToNodeMetadata.apply(server); return server == null ? null : nodeMetadataAdapter.apply(server);
} }
} }

View File

@ -27,12 +27,14 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; 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.domain.Image;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.cloudservers.CloudServersClient;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -50,20 +52,23 @@ public class CloudServersImageSupplier implements Supplier<Set<? extends Image>>
protected final CloudServersClient sync; protected final CloudServersClient sync;
protected final Function<org.jclouds.cloudservers.domain.Image, Image> cloudServersImageToImage; protected final Function<org.jclouds.cloudservers.domain.Image, Image> cloudServersImageToImage;
protected final AddDefaultCredentialsToImage addDefaultCredentialsToImage;
@Inject @Inject
CloudServersImageSupplier(CloudServersClient sync, CloudServersImageSupplier(CloudServersClient sync,
Function<org.jclouds.cloudservers.domain.Image, Image> cloudServersImageToImage) { Function<org.jclouds.cloudservers.domain.Image, Image> cloudServersImageToImage,
AddDefaultCredentialsToImage addDefaultCredentialsToImage) {
this.sync = sync; this.sync = sync;
this.cloudServersImageToImage = cloudServersImageToImage; this.cloudServersImageToImage = cloudServersImageToImage;
this.addDefaultCredentialsToImage = addDefaultCredentialsToImage;
} }
@Override @Override
public Set<? extends Image> get() { public Set<? extends Image> get() {
Set<Image> images; Set<Image> images;
logger.debug(">> providing images"); logger.debug(">> providing images");
images = Sets.<Image> newLinkedHashSet(Iterables.transform(sync.listImages(withDetails()), images = Sets.<Image> newLinkedHashSet(Iterables.transform(sync.listImages(withDetails()), Functions.compose(
cloudServersImageToImage)); addDefaultCredentialsToImage, cloudServersImageToImage)));
logger.debug("<< images(%d)", images.size()); logger.debug("<< images(%d)", images.size());
return images; return images;
} }

View File

@ -29,7 +29,6 @@ import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.OsFamily; import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.json.Json; import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule; import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -44,22 +43,18 @@ public class CloudServersImageToImageTest {
@Test @Test
public void testApplyWhereImageNotFound() throws UnknownHostException { public void testApplyWhereImageNotFound() throws UnknownHostException {
assertEquals( assertEquals(convertImage(), new ImageBuilder().name("CentOS 5.2").operatingSystem(
convertImage(), new OperatingSystem.Builder().family(OsFamily.CENTOS).version("5.2").description("CentOS 5.2").is64Bit(
new ImageBuilder() true).build()).description("CentOS 5.2").ids("2").version("1286712000000").build());
.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());
} }
public static Image convertImage() { public static Image convertImage() {
org.jclouds.cloudservers.domain.Image image = ParseImageFromJsonResponseTest.parseImage(); org.jclouds.cloudservers.domain.Image image = ParseImageFromJsonResponseTest.parseImage();
CloudServersImageToImage parser = new CloudServersImageToImage(new CloudServersImageToOperatingSystem(new BaseComputeServiceContextModule() { CloudServersImageToImage parser = new CloudServersImageToImage(new CloudServersImageToOperatingSystem(
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule()) new BaseComputeServiceContextModule() {
.getInstance(Json.class)))); }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice
.createInjector(new GsonModule()).getInstance(Json.class))));
return parser.apply(image); return parser.apply(image);
} }

View File

@ -39,11 +39,9 @@ import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Processor; import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume; import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.VolumeBuilder; import org.jclouds.compute.domain.VolumeBuilder;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder; import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope; import org.jclouds.domain.LocationScope;
import org.jclouds.domain.LoginCredentials;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
@ -58,39 +56,6 @@ import com.google.common.collect.ImmutableSet;
public class ServerToNodeMetadataTest { public class ServerToNodeMetadataTest {
Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build(); 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<ServerStatus, NodeState> serverStateToNodeState = CloudServersComputeServiceDependenciesModule.serverToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of();
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Server server = ParseServerFromJsonResponseTest.parseServer();
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials> of("node#1234", creds), Suppliers.<Set<? extends Image>> ofInstance(images),
Suppliers.ofInstance(provider), Suppliers.<Set<? extends Hardware>> 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 @Test
public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException { public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException {
Map<ServerStatus, NodeState> serverStateToNodeState = CloudServersComputeServiceDependenciesModule.serverToNodeState; Map<ServerStatus, NodeState> serverStateToNodeState = CloudServersComputeServiceDependenciesModule.serverToNodeState;
@ -98,8 +63,7 @@ public class ServerToNodeMetadataTest {
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(); Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Server server = ParseServerFromJsonResponseTest.parseServer(); Server server = ParseServerFromJsonResponseTest.parseServer();
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares)); .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
NodeMetadata metadata = parser.apply(server); NodeMetadata metadata = parser.apply(server);
@ -130,8 +94,7 @@ public class ServerToNodeMetadataTest {
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(); Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Server server = ParseServerFromJsonResponseTest.parseServer(); Server server = ParseServerFromJsonResponseTest.parseServer();
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares)); .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
NodeMetadata metadata = parser.apply(server); NodeMetadata metadata = parser.apply(server);
@ -164,8 +127,7 @@ public class ServerToNodeMetadataTest {
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor()); Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor());
Server server = ParseServerFromJsonResponseTest.parseServer(); Server server = ParseServerFromJsonResponseTest.parseServer();
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares)); .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
NodeMetadata metadata = parser.apply(server); NodeMetadata metadata = parser.apply(server);