Issue 158: Started implementation of createNodeWithGroupEncodedIntoNameThenStoreCredentials

This commit is contained in:
Jason King 2011-09-28 17:09:53 +01:00
parent b77bd0e132
commit 5122e2de45
2 changed files with 79 additions and 12 deletions

View File

@ -19,6 +19,7 @@
package org.jclouds.softlayer.compute.strategy;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
@ -54,7 +55,6 @@ public class SoftLayerComputeServiceAdapter implements
ComputeServiceAdapter<VirtualGuest, Set<ProductItem>, ProductItem, Datacenter> {
public static final String SAN_DESCRIPTION_REGEX=".*GB \\(SAN\\).*";
//TODO: Better to pass this in as a property like virtualGuestPackageName?
private static final Float BOOT_VOLUME_CAPACITY = 100F;
private final SoftLayerClient client;
@ -68,18 +68,82 @@ public class SoftLayerComputeServiceAdapter implements
}
@Override
public VirtualGuest createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name,
public VirtualGuest createNodeWithGroupEncodedIntoNameThenStoreCredentials(String group, String name,
Template template, Map<String, Credentials> credentialStore) {
VirtualGuest from = null; // from testCancelAndPlaceOrder()
// use name for hostname as possible
// you can ignore group, unless it is valid to set as domain
// get the cpu, mem, datacenter from the template ids
// make sure you store the credentials so that later functions can use them
// credentialStore.put("node#"+ from.getId() + "", new Credentials(from.loginUser (might be root),
// from.password));
return from;
Iterable<VirtualGuest> existing = findVirtualGuests(name,group);
if(!Iterables.isEmpty(existing)) {
throw new IllegalStateException(
"VirtualGuest(s) already exist with hostname:"+name+", group:"+group+". Existing:"+existing);
}
VirtualGuest newGuest = VirtualGuest.builder()
.domain(group)
.hostname(name)
.build();
ProductOrder order = ProductOrder.builder()
.packageId(getProductPackage().getId())
.location(template.getLocation().getId())
.quantity(1)
.useHourlyPricing(true)
.prices(getPrices(template))
.virtualGuest(newGuest)
.build();
client.getVirtualGuestClient().orderVirtualGuest(order);
VirtualGuest result = Iterables.getOnlyElement(findVirtualGuests(name, group));
Credentials credentials = new Credentials(null,null);
// This information is not always available.
OperatingSystem os = result.getOperatingSystem();
if(os!=null) {
Set<Password> passwords = os.getPasswords();
if(passwords.size()>0) {
Password pw = Iterables.get(passwords,0);
credentials = new Credentials(pw.getUsername(),pw.getPassword());
}
}
credentialStore.put("node#"+result.getId(),credentials);
return result;
}
private Iterable<VirtualGuest> findVirtualGuests(String hostname,String domain) {
checkNotNull(hostname,"hostname");
checkNotNull(domain,"domain");
Set<VirtualGuest> result = Sets.newLinkedHashSet();
for( VirtualGuest guest : client.getVirtualGuestClient().listVirtualGuests()) {
if ( guest.getHostname().equals(hostname) && guest.getDomain().equals(domain)) {
result.add(guest);
}
}
return result;
}
private Iterable<ProductItemPrice> getPrices(Template template) {
Set<ProductItemPrice> result = Sets.newLinkedHashSet();
int imageId = Integer.parseInt(template.getImage().getId());
result.add(ProductItemPrice.builder().id(imageId).build());
Iterable<String> hardwareIds = Splitter.on(",").split(template.getHardware().getId());
for(String hardwareId: hardwareIds) {
int id = Integer.parseInt(hardwareId);
result.add(ProductItemPrice.builder().id(id).build());
}
result.addAll(SoftLayerConstants.DEFAULT_VIRTUAL_GUEST_PRICES);
return result;
}
@Override
public Iterable<Set<ProductItem>> listHardwareProfiles() {
ProductPackage productPackage = getProductPackage();

View File

@ -64,9 +64,12 @@ public class SoftLayerComputeServiceAdapterLiveTest extends BaseSoftLayerClientL
@Test
public void testCreateNodeWithGroupEncodedIntoNameThenStoreCredentials() {
String group = "foo";
String group = "jclouds.org";
String name = "foo-ef4";
Template template = computeContext.getComputeService().templateBuilder().build();
Template template = computeContext.getComputeService().templateBuilder()
.locationId("3") // the default (singapore) doesn't work.
.build();
Map<String, Credentials> credentialStore = Maps.newLinkedHashMap();
guest = adapter.createNodeWithGroupEncodedIntoNameThenStoreCredentials(group, name, template, credentialStore);
assertEquals(guest.getHostname(), name);