private void setInstanceInitiatedShutdownBehaviorForInstanceInRegion() {
    try {

      client
          .getInstanceServices()
          .setInstanceInitiatedShutdownBehaviorForInstanceInRegion(
              null, ebsInstance.getId(), InstanceInitiatedShutdownBehavior.STOP);

      assertEquals(
          InstanceInitiatedShutdownBehavior.STOP,
          client
              .getInstanceServices()
              .getInstanceInitiatedShutdownBehaviorForInstanceInRegion(null, ebsInstance.getId()));
      client
          .getInstanceServices()
          .setInstanceInitiatedShutdownBehaviorForInstanceInRegion(
              null, ebsInstance.getId(), InstanceInitiatedShutdownBehavior.TERMINATE);

      assertEquals(
          InstanceInitiatedShutdownBehavior.TERMINATE,
          client
              .getInstanceServices()
              .getInstanceInitiatedShutdownBehaviorForInstanceInRegion(null, ebsInstance.getId()));
      System.out.println("OK: setInstanceInitiatedShutdownBehaviorForInstanceInRegion");
    } catch (Exception e) {
      System.err.println("setInstanceInitiatedShutdownBehaviorForInstanceInRegion");
      e.printStackTrace();
    }
  }
  private void setBlockDeviceMappingForInstanceInRegion() {
    String volumeId = ebsInstance.getEbsBlockDevices().get("/dev/sda1").getVolumeId();

    Map<String, BlockDevice> mapping = Maps.newLinkedHashMap();
    mapping.put("/dev/sda1", new BlockDevice(volumeId, false));
    try {
      client
          .getInstanceServices()
          .setBlockDeviceMappingForInstanceInRegion(null, ebsInstance.getId(), mapping);

      Map<String, BlockDevice> devices =
          client
              .getInstanceServices()
              .getBlockDeviceMappingForInstanceInRegion(null, ebsInstance.getId());
      assertEquals(devices.size(), 1);
      String deviceName = Iterables.getOnlyElement(devices.keySet());
      BlockDevice device = Iterables.getOnlyElement(devices.values());

      assertEquals(device.getVolumeId(), volumeId);
      assertEquals(deviceName, "/dev/sda1");
      assertEquals(device.isDeleteOnTermination(), false);

      System.out.println("OK: setBlockDeviceMappingForInstanceInRegion");
    } catch (Exception e) {
      System.err.println("setBlockDeviceMappingForInstanceInRegion");

      e.printStackTrace();
    }
  }
  // Fires up an instance, finds its root volume ID, takes a snapshot, then
  // terminates the instance.
  private Snapshot createSnapshot() throws RunNodesException {

    String instanceId = null;
    try {
      RunningInstance instance =
          getOnlyElement(
              concat(
                  ec2Client
                      .getInstanceServices()
                      .runInstancesInRegion(regionId, null, imageId, 1, 1)));
      instanceId = instance.getId();

      assertTrue(runningTester.apply(instance), instanceId + "didn't achieve the state running!");

      instance =
          getOnlyElement(
              concat(
                  ec2Client.getInstanceServices().describeInstancesInRegion(regionId, instanceId)));
      BlockDevice device = instance.getEbsBlockDevices().get("/dev/sda1");
      assertNotNull(device, "device: /dev/sda1 not present on: " + instance);
      Snapshot snapshot =
          ec2Client
              .getElasticBlockStoreServices()
              .createSnapshotInRegion(regionId, device.getVolumeId());
      snapshotsToDelete.add(snapshot.getId());
      return snapshot;
    } finally {
      if (instanceId != null)
        ec2Client.getInstanceServices().terminateInstancesInRegion(regionId, instanceId);
    }
  }
  @VisibleForTesting
  static List<Volume> addEBS(final RunningInstance instance, Iterable<? extends Volume> volumes) {
    Iterable<Volume> ebsVolumes =
        Iterables.transform(
            instance.getEbsBlockDevices().entrySet(),
            new Function<Entry<String, BlockDevice>, Volume>() {

              @Override
              public Volume apply(Entry<String, BlockDevice> from) {
                return new VolumeImpl(
                    from.getValue().getVolumeId(),
                    Volume.Type.SAN,
                    null,
                    from.getKey(),
                    instance.getRootDeviceName() != null
                        && instance.getRootDeviceName().equals(from.getKey()),
                    true);
              }
            });

    if (instance.getRootDeviceType() == RootDeviceType.EBS) {
      volumes =
          Iterables.filter(
              volumes,
              new Predicate<Volume>() {

                @Override
                public boolean apply(Volume input) {
                  return !input.isBootDevice();
                }
              });
    }
    return Lists.newArrayList(Iterables.concat(volumes, ebsVolumes));
  }
 private void setKernelForInstanceInRegion() {
   String oldKernel =
       client.getInstanceServices().getKernelForInstanceInRegion(null, ebsInstance.getId());
   client.getInstanceServices().setKernelForInstanceInRegion(null, ebsInstance.getId(), oldKernel);
   assertEquals(
       oldKernel,
       client.getInstanceServices().getKernelForInstanceInRegion(null, ebsInstance.getId()));
 }
 private void setRamdiskForInstanceInRegion() {
   String ramdisk =
       client.getInstanceServices().getRamdiskForInstanceInRegion(null, ebsInstance.getId());
   client.getInstanceServices().setRamdiskForInstanceInRegion(null, ebsInstance.getId(), ramdisk);
   assertEquals(
       ramdisk,
       client.getInstanceServices().getRamdiskForInstanceInRegion(null, ebsInstance.getId()));
 }
 private void setUserDataForInstanceInRegion() {
   client
       .getInstanceServices()
       .setUserDataForInstanceInRegion(null, ebsInstance.getId(), "test".getBytes());
   assertEquals(
       "test",
       client.getInstanceServices().getUserDataForInstanceInRegion(null, ebsInstance.getId()));
 }
 @VisibleForTesting
 String getGroupForInstance(final RunningInstance instance) {
   String group = parseGroupFrom(instance, instance.getGroupNames());
   if (group == null && instance.getKeyName() != null) {
     // when not using a generated security group, e.g. in VPC, try from key:
     group = parseGroupFrom(instance, Sets.newHashSet(instance.getKeyName()));
   }
   return group;
 }
 private void setInstanceTypeForInstanceInRegion() {
   client
       .getInstanceServices()
       .setInstanceTypeForInstanceInRegion(null, ebsInstance.getId(), InstanceType.C1_MEDIUM);
   assertEquals(
       InstanceType.C1_MEDIUM,
       client.getInstanceServices().getInstanceTypeForInstanceInRegion(null, ebsInstance.getId()));
   client
       .getInstanceServices()
       .setInstanceTypeForInstanceInRegion(null, ebsInstance.getId(), InstanceType.M1_SMALL);
   assertEquals(
       InstanceType.M1_SMALL,
       client.getInstanceServices().getInstanceTypeForInstanceInRegion(null, ebsInstance.getId()));
 }
 private String parseGroupFrom(final RunningInstance instance, final Set<String> data) {
   String group = null;
   try {
     Predicate<String> containsAnyGroup = namingConvention.create().containsAnyGroup();
     String encodedGroup = Iterables.getOnlyElement(Iterables.filter(data, containsAnyGroup));
     group = namingConvention.create().extractGroup(encodedGroup);
   } catch (NoSuchElementException e) {
     logger.debug("no group parsed from %s's data: %s", instance.getId(), data);
   } catch (IllegalArgumentException e) {
     logger.debug(
         "too many groups match naming convention; %s's data: %s", instance.getId(), data);
   }
   return group;
 }
 @Override
 protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
   LoginCredentials creds =
       LoginCredentials.fromCredentials(
           credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId()));
   String spotRequestId = AWSRunningInstance.class.cast(instance).getSpotInstanceRequestId();
   if (creds == null && spotRequestId != null) {
     creds =
         LoginCredentials.fromCredentials(
             credentialStore.get("node#" + instance.getRegion() + "/" + spotRequestId));
     if (creds != null)
       credentialStore.put("node#" + instance.getRegion() + "/" + instance.getId(), creds);
   }
   if (creds != null) builder.credentials(creds);
 }
 @Override
 public NodeMetadata apply(RunningInstance instance) {
   if (instance == null || instance.getId() == null) return null;
   NodeMetadataBuilder builder = new NodeMetadataBuilder();
   builder = buildInstance(instance, builder);
   return builder.build();
 }
  private RunningInstance blockUntilWeCanSshIntoInstance(RunningInstance instance)
      throws UnknownHostException {
    System.out.printf(
        "%d: %s awaiting instance to run %n", System.currentTimeMillis(), instance.getId());
    assert runningTester.apply(instance);

    // search my identity for the instance I just created
    Set<? extends Reservation<? extends RunningInstance>> reservations =
        client
            .getInstanceServices()
            .describeInstancesInRegion(instance.getRegion(), instance.getId()); // last
    // parameter
    // (ids)
    // narrows
    // the
    // search

    instance = Iterables.getOnlyElement(Iterables.getOnlyElement(reservations));

    System.out.printf(
        "%d: %s awaiting ssh service to start%n",
        System.currentTimeMillis(), instance.getIpAddress());
    assert socketTester.apply(new IPSocket(instance.getIpAddress(), 22));
    System.out.printf(
        "%d: %s ssh service started%n", System.currentTimeMillis(), instance.getDnsName());
    sshPing(instance);
    System.out.printf("%d: %s ssh connection made%n", System.currentTimeMillis(), instance.getId());
    return instance;
  }
  @Test(
      enabled = false,
      dependsOnMethods = {"testAMIFromBundle"})
  public void testInstanceFromEBS() throws Exception {
    System.out.printf(
        "%d: %s creating instance from ebs-backed ami%n",
        System.currentTimeMillis(), ebsImage.getId());

    ebsInstance = createInstance(ebsImage.getId());

    client
        .getInstanceServices()
        .stopInstancesInRegion(ebsInstance.getRegion(), true, ebsInstance.getId());

    System.out.printf(
        "%d: %s awaiting instance to stop %n", System.currentTimeMillis(), ebsInstance.getId());
    stoppedTester.apply(ebsInstance);
    tryToChangeStuff();
    System.out.printf(
        "%d: %s awaiting instance to start %n", System.currentTimeMillis(), ebsInstance.getId());
    client
        .getInstanceServices()
        .startInstancesInRegion(ebsInstance.getRegion(), ebsInstance.getId());
    ebsInstance = blockUntilWeCanSshIntoInstance(ebsInstance);
  }
  @Test(enabled = false, dependsOnMethods = "testCreateAndAttachVolume")
  void testBundleInstance() {
    SshClient ssh =
        sshFactory.create(
            new IPSocket(instance.getIpAddress(), 22),
            new Credentials("ubuntu", keyPair.getKeyMaterial()));
    try {
      ssh.connect();
    } catch (SshException e) { // try twice in case there is a network timeout
      try {
        Thread.sleep(10 * 1000);
      } catch (InterruptedException e1) {
      }
      ssh.connect();
    }
    try {
      System.out.printf(
          "%d: %s writing ebs script%n", System.currentTimeMillis(), instance.getId());
      String script = "/tmp/mkebsboot-init.sh";
      ssh.put(script, Payloads.newStringPayload(mkEbsBoot));

      System.out.printf(
          "%d: %s launching ebs script%n", System.currentTimeMillis(), instance.getId());
      ssh.exec("chmod 755 " + script);
      ssh.exec(script + " init");
      ExecResponse output = ssh.exec("sudo " + script + " start");
      System.out.println(output);
      output = ssh.exec(script + " status");

      assert !output.getOutput().trim().equals("") : output;

      RetryablePredicate<String> scriptTester =
          new RetryablePredicate<String>(
              new ScriptTester(ssh, SCRIPT_END), 600, 10, TimeUnit.SECONDS);
      scriptTester.apply(script);
    } finally {
      if (ssh != null) ssh.disconnect();
    }
  }
  public void testEBS() throws UnknownHostException {

    Set<Reservation<RunningInstance>> contents =
        ImmutableSet.of(
            new Reservation<RunningInstance>(
                defaultRegion,
                ImmutableSet.of("adriancole.ec2ebsingress"),
                ImmutableSet.of(
                    RunningInstance.builder()
                        .region(defaultRegion)
                        .groupName("adriancole.ec2ebsingress")
                        .amiLaunchIndex("0")
                        .dnsName("ec2-75-101-203-146.compute-1.amazonaws.com")
                        .imageId("ami-849875ed")
                        .instanceId("i-e564438d")
                        .instanceState(InstanceState.RUNNING)
                        .rawState("running")
                        .instanceType(InstanceType.M1_SMALL)
                        .ipAddress("75.101.203.146")
                        .kernelId("aki-a71cf9ce")
                        .keyName("adriancole.ec2ebs1")
                        .launchTime(dateService.iso8601DateParse("2009-12-30T04:06:23.000Z"))
                        // MonitoringState.DISABLED
                        .availabilityZone("us-east-1b")
                        // "placement"
                        .virtualizationType("hvm")
                        .privateDnsName("domU-12-31-39-09-CE-53.compute-1.internal")
                        .privateIpAddress("10.210.209.157")
                        .ramdiskId("ari-a51cf9cc")
                        .rootDeviceType(RootDeviceType.EBS)
                        .rootDeviceName("/dev/sda1")
                        .device(
                            "/dev/sda1",
                            new BlockDevice(
                                "vol-dc6ca8b5",
                                Attachment.Status.ATTACHED,
                                dateService.iso8601DateParse("2009-12-30T04:06:29.000Z"),
                                true))
                        .build()),
                "993194456877",
                null,
                "r-596dd731"));

    Set<Reservation<? extends RunningInstance>> result =
        parseRunningInstances("/describe_instances_ebs.xml");

    assertEquals(result.toString(), contents.toString());
    assertEquals(get(get(result, 0), 0).getInstanceState(), InstanceState.RUNNING);
    assertEquals(get(get(result, 0), 0).getRawState(), "running");
  }
 private RunningInstance createInstance(String imageId) throws UnknownHostException {
   RunningInstance instance = null;
   while (instance == null) {
     try {
       System.out.printf("%d: running instance%n", System.currentTimeMillis());
       Reservation<? extends RunningInstance> reservation =
           client
               .getInstanceServices()
               .runInstancesInRegion(
                   null,
                   null, // allow
                   // ec2
                   // to
                   // chose
                   // an
                   // availability
                   // zone
                   imageId,
                   1, // minimum instances
                   1, // maximum instances
                   withKeyName(keyPair.getKeyName()) // key I created above
                       .asType(InstanceType.M1_SMALL) // smallest instance
                       // size
                       .withSecurityGroup(securityGroupName)); // group I
       // created
       // above
       instance = Iterables.getOnlyElement(reservation);
     } catch (HttpResponseException htpe) {
       if (htpe.getResponse().getStatusCode() == 400) continue;
       throw htpe;
     }
   }
   assertNotNull(instance.getId());
   assertEquals(instance.getInstanceState(), InstanceState.PENDING);
   instance = blockUntilWeCanSshIntoInstance(instance);
   return instance;
 }
  @Test(enabled = false, dependsOnMethods = "testCreateRunningInstance")
  void testCreateAndAttachVolume() {
    volume =
        client
            .getElasticBlockStoreServices()
            .createVolumeInAvailabilityZone(instance.getAvailabilityZone(), VOLUME_SIZE);
    System.out.printf(
        "%d: %s awaiting volume to become available%n", System.currentTimeMillis(), volume.getId());

    assert volumeTester.apply(volume);

    Attachment attachment =
        client
            .getElasticBlockStoreServices()
            .attachVolumeInRegion(
                instance.getRegion(), volume.getId(), instance.getId(), "/dev/sdh");

    System.out.printf(
        "%d: %s awaiting attachment to complete%n", System.currentTimeMillis(), attachment.getId());

    assert attachTester.apply(attachment);
    System.out.printf(
        "%d: %s attachment complete%n", System.currentTimeMillis(), attachment.getId());
  }
  @VisibleForTesting
  Hardware getHardwareForInstance(final RunningInstance instance) {
    try {
      return Iterables.find(
          hardware.get(),
          new Predicate<Hardware>() {

            @Override
            public boolean apply(Hardware input) {
              return input.getId().equals(instance.getInstanceType());
            }
          });
    } catch (NoSuchElementException e) {
      logger.debug(
          "couldn't match instance type %s in: %s", instance.getInstanceType(), hardware.get());
      return null;
    }
  }
  public void testWhenRunning() throws UnknownHostException {

    Set<Reservation<RunningInstance>> contents =
        ImmutableSet.of(
            new Reservation<RunningInstance>(
                defaultRegion,
                ImmutableSet.of("adriancole.ec2ingress"),
                ImmutableSet.of(
                    RunningInstance.builder()
                        .region(defaultRegion)
                        .groupName("adriancole.ec2ingress")
                        .amiLaunchIndex("0")
                        .dnsName("ec2-174-129-81-68.compute-1.amazonaws.com")
                        .imageId("ami-82e4b5c7")
                        .instanceId("i-0799056f")
                        .instanceState(InstanceState.RUNNING)
                        .rawState("running")
                        .instanceType(InstanceType.M1_SMALL)
                        .ipAddress("174.129.81.68")
                        .kernelId("aki-a71cf9ce")
                        .keyName("adriancole.ec21")
                        .launchTime(dateService.iso8601DateParse("2009-11-09T03:00:34.000Z"))
                        // MonitoringState.DISABLED,
                        .availabilityZone("us-east-1c")
                        .virtualizationType("paravirtual")
                        .privateDnsName("ip-10-243-42-70.ec2.internal")
                        .privateIpAddress("10.243.42.70")
                        .ramdiskId("ari-a51cf9cc")
                        .rootDeviceType(RootDeviceType.INSTANCE_STORE)
                        .build()),
                "993194456877",
                null,
                "r-a3c508cb"));

    Set<Reservation<? extends RunningInstance>> result =
        parseRunningInstances("/describe_instances_running.xml");

    assertEquals(result.toString(), contents.toString());
    assertEquals(get(get(result, 0), 0).getInstanceState(), InstanceState.RUNNING);
    assertEquals(get(get(result, 0), 0).getRawState(), "running");
  }
  @AfterTest
  void cleanup() {
    if (ebsInstance != null) {
      try {
        client
            .getInstanceServices()
            .terminateInstancesInRegion(ebsInstance.getRegion(), ebsInstance.getId());
        terminatedTester.apply(ebsInstance);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (ebsImage != null) {
      try {
        client.getAMIServices().deregisterImageInRegion(ebsImage.getRegion(), ebsImage.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (snapshot != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .deleteSnapshotInRegion(snapshot.getRegion(), snapshot.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (attachment != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .detachVolumeInRegion(volume.getRegion(), volume.getId(), true);
        assert volumeTester.apply(volume);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (instance != null) {
      try {
        client
            .getInstanceServices()
            .terminateInstancesInRegion(instance.getRegion(), instance.getId());
        terminatedTester.apply(instance);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (volume != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .deleteVolumeInRegion(volume.getRegion(), volume.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (keyPair != null) {
      try {
        client
            .getKeyPairServices()
            .deleteKeyPairInRegion(keyPair.getRegion(), keyPair.getKeyName());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (securityGroupName != null) {
      try {
        client.getSecurityGroupServices().deleteSecurityGroupInRegion(null, securityGroupName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
 private void doCheckKey(RunningInstance newDetails) throws UnknownHostException {
   doCheckKey(newDetails.getIpAddress());
 }
  protected NodeMetadataBuilder buildInstance(
      final RunningInstance instance, NodeMetadataBuilder builder) {
    builder.providerId(instance.getId());
    builder.id(instance.getRegion() + "/" + instance.getId());
    String group = getGroupForInstance(instance);
    builder.group(group);
    // standard convention from aws-ec2, which might not be re-used outside.
    if (instance.getPrivateDnsName() != null)
      builder.hostname(instance.getPrivateDnsName().replaceAll("\\..*", ""));
    addCredentialsForInstance(builder, instance);
    builder.status(instanceToNodeStatus.get(instance.getInstanceState()));
    builder.backendStatus(instance.getRawState());

    // collect all ip addresses into one bundle in case the api mistakenly put a private address
    // into the public address field
    Builder<String> addressesBuilder = ImmutableSet.builder();
    if (Strings.emptyToNull(instance.getIpAddress()) != null)
      addressesBuilder.add(instance.getIpAddress());
    // Add dnsName (if available) to addresses, when the IPAddress is null
    // happens on Eucalyptus sometimes.
    else if (Strings.emptyToNull(instance.getDnsName()) != null)
      addressesBuilder.add(instance.getDnsName());
    if (Strings.emptyToNull(instance.getPrivateIpAddress()) != null)
      addressesBuilder.add(instance.getPrivateIpAddress());

    Set<String> addresses = addressesBuilder.build();

    builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE)));
    builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
    builder.hardware(parseHardware(instance));
    Location location = getLocationForAvailabilityZoneOrRegion(instance);
    builder.location(location);
    builder.imageId(instance.getRegion() + "/" + instance.getImageId());

    // extract the operating system from the image
    RegionAndName regionAndName = new RegionAndName(instance.getRegion(), instance.getImageId());
    try {
      Image image = imageMap.get().getUnchecked(regionAndName);
      if (image != null) builder.operatingSystem(image.getOperatingSystem());
    } catch (CacheLoader.InvalidCacheLoadException e) {
      logger.debug("image not found for %s: %s", regionAndName, e);
    } catch (UncheckedExecutionException e) {
      logger.debug("error getting image for %s: %s", regionAndName, e);
    }
    return builder;
  }
 protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
   builder.credentials(
       LoginCredentials.fromCredentials(
           credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId())));
 }
 private Location getLocationForAvailabilityZoneOrRegion(final RunningInstance instance) {
   Location location = findLocationWithId(instance.getAvailabilityZone());
   if (location == null) location = findLocationWithId(instance.getRegion());
   return location;
 }
  public void testApplyInputStream() {
    Set<Reservation<RunningInstance>> contents =
        ImmutableSet.of(
            new Reservation<RunningInstance>(
                defaultRegion,
                ImmutableSet.of("default"),
                ImmutableSet.of(
                    RunningInstance.builder()
                        .region(defaultRegion)
                        .groupName("default")
                        .amiLaunchIndex("23")
                        .dnsName("ec2-72-44-33-4.compute-1.amazonaws.com")
                        .imageId("ami-6ea54007")
                        .instanceId("i-28a64341")
                        .instanceState(InstanceState.RUNNING)
                        .rawState("running")
                        .instanceType(InstanceType.M1_LARGE)
                        .kernelId("aki-ba3adfd3")
                        .keyName("example-key-name")
                        .launchTime(dateService.iso8601DateParse("2007-08-07T11:54:42.000Z"))
                        // MonitoringState.DISABLED,
                        .availabilityZone("us-east-1b")
                        .virtualizationType("paravirtual")
                        .privateDnsName("10-251-50-132.ec2.internal") // product codes
                        // ImmutableSet.of("774F4FF8")
                        .tags(ImmutableMap.of("Name", "ec2-o", "Empty", ""))
                        .ramdiskId("ari-badbad00")
                        .rootDeviceType(RootDeviceType.INSTANCE_STORE)
                        .build(),
                    RunningInstance.builder()
                        .region(defaultRegion)
                        .groupName("default")
                        .amiLaunchIndex("23")
                        .dnsName("ec2-72-44-33-6.compute-1.amazonaws.com")
                        .imageId("ami-6ea54007")
                        .instanceId("i-28a64435")
                        .instanceState(InstanceState.RUNNING)
                        .rawState("running")
                        .instanceType(InstanceType.M1_LARGE)
                        .kernelId("aki-ba3adfd3")
                        .keyName("example-key-name")
                        .launchTime(dateService.iso8601DateParse("2007-08-07T11:54:42.000Z"))
                        // MonitoringState.DISABLED,
                        .availabilityZone("us-east-1b")
                        .virtualizationType("paravirtual")
                        .privateDnsName("10-251-50-134.ec2.internal") // product codes
                        // ImmutableSet.of("774F4FF8")
                        .ramdiskId("ari-badbad00")
                        .rootDeviceType(RootDeviceType.INSTANCE_STORE)
                        .build()),
                "UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM",
                null,
                "r-44a5402d"));

    Set<Reservation<? extends RunningInstance>> result =
        parseRunningInstances("/describe_instances.xml");

    assertEquals(result.toString(), contents.toString());
    assertEquals(get(get(result, 0), 0).getInstanceState(), InstanceState.RUNNING);
    assertEquals(get(get(result, 0), 0).getRawState(), "running");
  }
  @Test(enabled = false, dependsOnMethods = "testBundleInstance")
  void testAMIFromBundle() {
    volume =
        Iterables.getOnlyElement(
            client
                .getElasticBlockStoreServices()
                .describeVolumesInRegion(volume.getRegion(), volume.getId()));
    if (volume.getAttachments().size() > 0) {
      // should be cleanly unmounted, so force is not necessary.
      client
          .getElasticBlockStoreServices()
          .detachVolumeInRegion(instance.getRegion(), volume.getId(), false);
      System.out.printf(
          "%d: %s awaiting detachment to complete%n", System.currentTimeMillis(), volume.getId());
      assert volumeTester.apply(volume);
    } else {
      attachment = null; // protect test closer so that it doesn't try to
      // detach
    }
    snapshot =
        client
            .getElasticBlockStoreServices()
            .createSnapshotInRegion(
                volume.getRegion(), volume.getId(), withDescription("EBS Ubuntu Hardy"));

    System.out.printf(
        "%d: %s awaiting snapshot to complete%n", System.currentTimeMillis(), snapshot.getId());

    assert snapshotTester.apply(snapshot);
    Image image =
        Iterables.getOnlyElement(
            client
                .getAMIServices()
                .describeImagesInRegion(snapshot.getRegion(), imageIds(IMAGE_ID)));
    String description = image.getDescription() == null ? "jclouds" : image.getDescription();

    System.out.printf(
        "%d: %s creating ami from snapshot%n", System.currentTimeMillis(), snapshot.getId());

    String amiId =
        client
            .getAMIServices()
            .registerUnixImageBackedByEbsInRegion(
                snapshot.getRegion(),
                "ebsboot-" + image.getId(),
                snapshot.getId(),
                withKernelId(image.getKernelId())
                    .withRamdisk(image.getRamdiskId())
                    .withDescription(description)
                    .asArchitecture(Architecture.I386));
    try {
      ebsImage =
          Iterables.getOnlyElement(
              client
                  .getAMIServices()
                  .describeImagesInRegion(snapshot.getRegion(), imageIds(amiId)));
    } catch (AWSResponseException e) {
      // TODO add a retry handler for this HTTP code 400 and the below error
      if (e.getError().getClass().equals("InvalidAMIID.NotFound"))
        ebsImage =
            Iterables.getOnlyElement(
                client
                    .getAMIServices()
                    .describeImagesInRegion(snapshot.getRegion(), imageIds(amiId)));
      else throw e;
    }
    verifyImage();
  }
 @Override
 public RegionAndName apply(RunningInstance from) {
   return new RegionAndName(from.getRegion(), from.getId());
 }
  @SuppressWarnings("unchecked")
  private void assertRegionAndZoneForLocation(Location location, String region, String zone) {
    String imageId = "ami1";
    String instanceCreatedId = "instance1";
    // setup mocks
    TemplateBuilder templateBuilder = createMock(TemplateBuilder.class);
    EC2CreateNodesInGroupThenAddToSet strategy = setupStrategy(templateBuilder);
    InputParams input = new InputParams(location);
    InstanceClient instanceClient = createMock(InstanceClient.class);
    RunInstancesOptions ec2Options = createMock(RunInstancesOptions.class);
    RunningInstance instance = createMock(RunningInstance.class);
    Reservation<? extends RunningInstance> reservation =
        new Reservation<RunningInstance>(
            region,
            ImmutableSet.<String>of(),
            ImmutableSet.<RunningInstance>of(instance),
            "ownerId",
            "requesterId",
            "reservationId");
    NodeMetadata nodeMetadata = createMock(NodeMetadata.class);

    // setup expectations
    expect(templateBuilder.fromTemplate(input.template)).andReturn(templateBuilder);
    expect(templateBuilder.build()).andReturn(input.template);
    expect(strategy.client.getInstanceServices()).andReturn(instanceClient).atLeastOnce();
    expect(
            strategy.createKeyPairAndSecurityGroupsAsNeededAndReturncustomize.execute(
                region, input.tag, input.template))
        .andReturn(ec2Options);
    expect(input.template.getLocation()).andReturn(input.location).atLeastOnce();
    expect(input.template.getImage()).andReturn(input.image).atLeastOnce();
    expect(input.image.getProviderId()).andReturn(imageId).atLeastOnce();
    expect(instanceClient.runInstancesInRegion(region, zone, imageId, 1, input.count, ec2Options))
        .andReturn(Reservation.class.cast(reservation));
    expect(instance.getId()).andReturn(instanceCreatedId).atLeastOnce();
    // simulate a lazy credentials fetch
    Credentials creds = new Credentials("foo", "bar");
    expect(strategy.instanceToCredentials.apply(instance)).andReturn(creds);
    expect(instance.getRegion()).andReturn(region).atLeastOnce();
    expect(strategy.credentialStore.put("node#" + region + "/" + instanceCreatedId, creds))
        .andReturn(null);

    expect(strategy.instancePresent.apply(new RegionAndName(region, instanceCreatedId)))
        .andReturn(true);
    expect(input.template.getOptions()).andReturn(input.options).atLeastOnce();

    expect(strategy.runningInstanceToNodeMetadata.apply(instance)).andReturn(nodeMetadata);
    expect(
            strategy.utils.customizeNodesAndAddToGoodMapOrPutExceptionIntoBadMap(
                eq(input.options),
                containsNodeMetadata(nodeMetadata),
                eq(input.nodes),
                eq(input.badNodes),
                eq(input.customization)))
        .andReturn(null);

    // replay mocks
    replay(templateBuilder);
    replay(instanceClient);
    replay(ec2Options);
    replay(instance);
    replay(nodeMetadata);
    input.replayMe();
    replayStrategy(strategy);

    // run
    strategy.execute(
        input.tag, input.count, input.template, input.nodes, input.badNodes, input.customization);

    // verify mocks
    verify(templateBuilder);
    verify(instanceClient);
    verify(ec2Options);
    verify(instance);
    verify(nodeMetadata);
    input.verifyMe();
    verifyStrategy(strategy);
  }