updated slicehost to current style of credential management

This commit is contained in:
Adrian Cole 2011-12-21 03:12:27 -08:00
parent 2aadb21b03
commit 83ca5dd931
7 changed files with 101 additions and 100 deletions

View File

@ -36,9 +36,7 @@ import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.logging.Logger;
import org.jclouds.slicehost.domain.Slice;
@ -56,7 +54,6 @@ public class SliceToNodeMetadata implements Function<Slice, NodeMetadata> {
protected final Map<Slice.Status, NodeState> sliceToNodeState;
protected final Supplier<Set<? extends Image>> images;
protected final Supplier<Set<? extends Hardware>> hardwares;
protected final Map<String, Credentials> credentialStore;
@Resource
protected Logger logger = Logger.NULL;
@ -88,11 +85,10 @@ public class SliceToNodeMetadata implements Function<Slice, NodeMetadata> {
}
@Inject
SliceToNodeMetadata(Map<Slice.Status, NodeState> sliceStateToNodeState, Map<String, Credentials> credentialStore,
SliceToNodeMetadata(Map<Slice.Status, NodeState> sliceStateToNodeState,
@Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location,
@Memoized Supplier<Set<? extends Hardware>> hardwares) {
this.sliceToNodeState = checkNotNull(sliceStateToNodeState, "sliceStateToNodeState");
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.images = checkNotNull(images, "images");
this.location = checkNotNull(location, "location");
this.hardwares = checkNotNull(hardwares, "hardwares");
@ -126,7 +122,6 @@ public class SliceToNodeMetadata implements Function<Slice, NodeMetadata> {
}
}));
builder.credentials(LoginCredentials.fromCredentials(credentialStore.get("node#" + from.getId())));
return builder.build();
}

View File

@ -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;
@ -47,7 +46,6 @@ public class SlicehostImageToImage implements Function<org.jclouds.slicehost.dom
builder.name(from.getName());
builder.description(from.getName());
builder.operatingSystem(imageToOs.apply(from));
builder.defaultCredentials(LoginCredentials.builder().user("root").build());
Image image = builder.build();
return image;
}

View File

@ -19,16 +19,20 @@
package org.jclouds.slicehost.compute.strategy;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
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 org.jclouds.slicehost.SlicehostClient;
import org.jclouds.slicehost.domain.Slice;
@ -40,23 +44,41 @@ import com.google.common.base.Function;
*/
@Singleton
public class SlicehostCreateNodeWithGroupEncodedIntoName implements CreateNodeWithGroupEncodedIntoName {
protected final SlicehostClient client;
protected final Map<String, Credentials> credentialStore;
protected final PrioritizeCredentialsFromTemplate prioritizeCredentialsFromTemplate;
protected final Function<Slice, NodeMetadata> sliceToNodeMetadata;
@Inject
protected SlicehostCreateNodeWithGroupEncodedIntoName(SlicehostClient client, Map<String, Credentials> credentialStore,
Function<Slice, NodeMetadata> sliceToNodeMetadata) {
protected SlicehostCreateNodeWithGroupEncodedIntoName(SlicehostClient client,
Map<String, Credentials> credentialStore,
PrioritizeCredentialsFromTemplate prioritizeCredentialsFromTemplate,
Function<Slice, NodeMetadata> sliceToNodeMetadata) {
this.client = checkNotNull(client, "client");
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.sliceToNodeMetadata = checkNotNull(sliceToNodeMetadata, "sliceToNodeMetadata");
this.prioritizeCredentialsFromTemplate = checkNotNull(prioritizeCredentialsFromTemplate,
"prioritizeCredentialsFromTemplate");
}
@Override
public NodeMetadata createNodeWithGroupEncodedIntoName(String group, String name, Template template) {
Slice from = client.createSlice(name, Integer.parseInt(template.getImage().getProviderId()),
Integer.parseInt(template.getHardware().getProviderId()));
credentialStore.put("node#" + from.getId(), new Credentials("root", from.getRootPassword()));
return sliceToNodeMetadata.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");
Slice slice = client.createSlice(name, Integer.parseInt(template.getImage().getProviderId()), Integer
.parseInt(template.getHardware().getProviderId()));
NodeAndInitialCredentials<Slice> from = new NodeAndInitialCredentials<Slice>(slice, slice.getId() + "",
LoginCredentials.builder().password(slice.getRootPassword()).build());
LoginCredentials fromNode = from.getCredentials();
LoginCredentials creds = prioritizeCredentialsFromTemplate.apply(template, fromNode);
if (creds != null)
credentialStore.put("node#" + from.getNodeId(), creds);
NodeMetadata node = sliceToNodeMetadata.apply(from.getNode());
return node;
}
}

View File

@ -18,15 +18,23 @@
*/
package org.jclouds.slicehost.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.domain.NodeMetadataBuilder;
import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.slicehost.SlicehostClient;
import org.jclouds.slicehost.domain.Slice;
import com.google.common.base.Function;
import com.google.common.base.Functions;
/**
*
@ -36,18 +44,37 @@ import com.google.common.base.Function;
public class SlicehostGetNodeMetadataStrategy implements GetNodeMetadataStrategy {
private final SlicehostClient client;
private final Function<Slice, NodeMetadata> sliceToNodeMetadata;
private final Map<String, Credentials> credentialStore;
private final Function<Slice, NodeMetadata> nodeMetadataAdapter;
@Inject
protected SlicehostGetNodeMetadataStrategy(SlicehostClient client, Function<Slice, NodeMetadata> sliceToNodeMetadata) {
protected SlicehostGetNodeMetadataStrategy(SlicehostClient client, Map<String, Credentials> credentialStore,
Function<Slice, NodeMetadata> nodeMetadataAdapter) {
this.client = client;
this.sliceToNodeMetadata = sliceToNodeMetadata;
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
public NodeMetadata getNode(String id) {
int sliceId = Integer.parseInt(id);
Slice slice = client.getSlice(sliceId);
return slice == null ? null : sliceToNodeMetadata.apply(slice);
return slice == null ? null : nodeMetadataAdapter.apply(slice);
}
}

View File

@ -25,12 +25,14 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
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.slicehost.SlicehostClient;
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;
@ -47,22 +49,25 @@ public class SlicehostImageSupplier implements Supplier<Set<? extends Image>> {
protected Logger logger = Logger.NULL;
protected final SlicehostClient sync;
protected final Function<org.jclouds.slicehost.domain.Image, Image> slicehostImageToImage;
protected final Function<org.jclouds.slicehost.domain.Image, Image> cloudServersImageToImage;
protected final AddDefaultCredentialsToImage addDefaultCredentialsToImage;
@Inject
SlicehostImageSupplier(SlicehostClient sync,
Function<org.jclouds.slicehost.domain.Image, Image> slicehostImageToImage) {
Function<org.jclouds.slicehost.domain.Image, Image> cloudServersImageToImage,
AddDefaultCredentialsToImage addDefaultCredentialsToImage) {
this.sync = sync;
this.slicehostImageToImage = slicehostImageToImage;
this.cloudServersImageToImage = cloudServersImageToImage;
this.addDefaultCredentialsToImage = addDefaultCredentialsToImage;
}
@Override
public Set<? extends Image> get() {
Set<Image> images;
logger.debug(">> providing images");
images = Sets.newLinkedHashSet(Iterables.transform(sync.listImages(), slicehostImageToImage));
images = Sets.<Image> newLinkedHashSet(Iterables.transform(sync.listImages(), Functions.compose(
addDefaultCredentialsToImage, cloudServersImageToImage)));
logger.debug("<< images(%d)", images.size());
return images;
}
}

View File

@ -35,11 +35,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.jclouds.slicehost.compute.config.SlicehostComputeServiceDependenciesModule;
import org.jclouds.slicehost.domain.Slice;
import org.jclouds.slicehost.xml.SliceHandlerTest;
@ -57,28 +55,6 @@ import com.google.common.collect.ImmutableSet;
public class SliceToNodeMetadataTest {
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("abdce").build();
Map<Slice.Status, NodeState> sliceStateToNodeState = SlicehostComputeServiceDependenciesModule.sliceStatusToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of();
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Slice slice = SliceHandlerTest.parseSlice();
SliceToNodeMetadata parser = new SliceToNodeMetadata(sliceStateToNodeState, ImmutableMap
.<String, Credentials> of("node#1", creds), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
NodeMetadata metadata = parser.apply(slice);
assertEquals(
metadata,
new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(ImmutableSet.of("174.143.212.229"))
.privateAddresses(ImmutableSet.of("10.176.164.199")).group("jclouds").imageId("2").id("1")
.providerId("1").name("jclouds-foo").hostname("jclouds-foo").location(provider).credentials(creds)
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
@Test
public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException {
Map<Slice.Status, NodeState> sliceStateToNodeState = SlicehostComputeServiceDependenciesModule.sliceStatusToNodeState;
@ -86,18 +62,16 @@ public class SliceToNodeMetadataTest {
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Slice slice = SliceHandlerTest.parseSlice();
SliceToNodeMetadata parser = new SliceToNodeMetadata(sliceStateToNodeState, ImmutableMap
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
SliceToNodeMetadata parser = new SliceToNodeMetadata(sliceStateToNodeState, Suppliers
.<Set<? extends Image>> ofInstance(images), Suppliers.ofInstance(provider), Suppliers
.<Set<? extends Hardware>> ofInstance(hardwares));
NodeMetadata metadata = parser.apply(slice);
assertEquals(
metadata,
new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(ImmutableSet.of("174.143.212.229"))
.privateAddresses(ImmutableSet.of("10.176.164.199")).group("jclouds").imageId("2").id("1")
.providerId("1").name("jclouds-foo").hostname("jclouds-foo").location(provider)
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("174.143.212.229")).privateAddresses(ImmutableSet.of("10.176.164.199")).group("jclouds")
.imageId("2").id("1").providerId("1").name("jclouds-foo").hostname("jclouds-foo").location(provider)
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
@Test
@ -108,24 +82,18 @@ public class SliceToNodeMetadataTest {
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Slice slice = SliceHandlerTest.parseSlice();
SliceToNodeMetadata parser = new SliceToNodeMetadata(sliceStateToNodeState, ImmutableMap
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
SliceToNodeMetadata parser = new SliceToNodeMetadata(sliceStateToNodeState, Suppliers
.<Set<? extends Image>> ofInstance(images), Suppliers.ofInstance(provider), Suppliers
.<Set<? extends Hardware>> ofInstance(hardwares));
NodeMetadata metadata = parser.apply(slice);
assertEquals(
metadata,
new NodeMetadataBuilder()
.state(NodeState.PENDING)
.publicAddresses(ImmutableSet.of("174.143.212.229"))
.privateAddresses(ImmutableSet.of("10.176.164.199"))
.group("jclouds")
.imageId("2")
.operatingSystem(
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("174.143.212.229")).privateAddresses(ImmutableSet.of("10.176.164.199")).group("jclouds")
.imageId("2").operatingSystem(
new OperatingSystem.Builder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2")
.is64Bit(true).build()).id("1").providerId("1").name("jclouds-foo")
.hostname("jclouds-foo").location(provider)
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
.is64Bit(true).build()).id("1").providerId("1").name("jclouds-foo").hostname(
"jclouds-foo").location(provider).userMetadata(
ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
@Test
@ -135,32 +103,21 @@ public class SliceToNodeMetadataTest {
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor());
Slice slice = SliceHandlerTest.parseSlice();
SliceToNodeMetadata parser = new SliceToNodeMetadata(sliceStateToNodeState, ImmutableMap
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
SliceToNodeMetadata parser = new SliceToNodeMetadata(sliceStateToNodeState, Suppliers
.<Set<? extends Image>> ofInstance(images), Suppliers.ofInstance(provider), Suppliers
.<Set<? extends Hardware>> ofInstance(hardwares));
NodeMetadata metadata = parser.apply(slice);
assertEquals(
metadata,
new NodeMetadataBuilder()
.state(NodeState.PENDING)
.publicAddresses(ImmutableSet.of("174.143.212.229"))
.privateAddresses(ImmutableSet.of("10.176.164.199"))
.group("jclouds")
.imageId("2")
.hardware(
new HardwareBuilder()
.ids("1")
.name("256 slice")
.processors(ImmutableList.of(new Processor(0.25, 1.0)))
.ram(256)
.volumes(
ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(1.0f)
.durable(true).bootDevice(true).build())).build())
.operatingSystem(
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("174.143.212.229")).privateAddresses(ImmutableSet.of("10.176.164.199")).group("jclouds")
.imageId("2").hardware(
new HardwareBuilder().ids("1").name("256 slice").processors(
ImmutableList.of(new Processor(0.25, 1.0))).ram(256).volumes(
ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(1.0f).durable(true)
.bootDevice(true).build())).build()).operatingSystem(
new OperatingSystem.Builder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2")
.is64Bit(true).build()).id("1").providerId("1").name("jclouds-foo")
.hostname("jclouds-foo").location(provider)
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
.is64Bit(true).build()).id("1").providerId("1").name("jclouds-foo").hostname(
"jclouds-foo").location(provider).userMetadata(
ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
}

View File

@ -31,7 +31,6 @@ import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.slicehost.xml.ImageHandlerTest;
@ -54,8 +53,7 @@ public class SlicehostImageToImageTest {
.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").build());
.is64Bit(true).build()).description("CentOS 5.2").ids("2").build());
}
@Test
@ -67,8 +65,7 @@ public class SlicehostImageToImageTest {
.operatingSystem(
new OperatingSystem.Builder().family(OsFamily.UBUNTU).version("10.10")
.description("Ubuntu 10.10 (maverick) 32-bit").build())
.description("Ubuntu 10.10 (maverick) 32-bit")
.defaultCredentials(LoginCredentials.builder().user("root").build()).ids("70").build());
.description("Ubuntu 10.10 (maverick) 32-bit").ids("70").build());
}
public static Image convertImage() {