From 5122e2de453b585671cfcb12792c4986a0597976 Mon Sep 17 00:00:00 2001 From: Jason King Date: Wed, 28 Sep 2011 17:09:53 +0100 Subject: [PATCH] Issue 158: Started implementation of createNodeWithGroupEncodedIntoNameThenStoreCredentials --- .../SoftLayerComputeServiceAdapter.java | 84 ++++++++++++++++--- ...oftLayerComputeServiceAdapterLiveTest.java | 7 +- 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java index 37a1de5f0b..398f4befb3 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java @@ -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, 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 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 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 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 findVirtualGuests(String hostname,String domain) { + checkNotNull(hostname,"hostname"); + checkNotNull(domain,"domain"); + + Set 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 getPrices(Template template) { + Set result = Sets.newLinkedHashSet(); + + int imageId = Integer.parseInt(template.getImage().getId()); + result.add(ProductItemPrice.builder().id(imageId).build()); + + Iterable 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> listHardwareProfiles() { ProductPackage productPackage = getProductPackage(); diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/SoftLayerComputeServiceAdapterLiveTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/SoftLayerComputeServiceAdapterLiveTest.java index f13d1e0f18..1687a52ba5 100644 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/SoftLayerComputeServiceAdapterLiveTest.java +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/SoftLayerComputeServiceAdapterLiveTest.java @@ -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 credentialStore = Maps.newLinkedHashMap(); guest = adapter.createNodeWithGroupEncodedIntoNameThenStoreCredentials(group, name, template, credentialStore); assertEquals(guest.getHostname(), name);