public Volume getVolume(String id) { try { Set<Volume> volumes = blockStoreApi.describeVolumesInRegion(null, id); if (!volumes.isEmpty()) { return volumes.iterator().next(); } else { return null; } } catch (ResourceNotFoundException e) { return null; } }
/** * Create a snapshot of the volume attached to the VM * @param vmi a VMInstance */ public String createSnapshot(VMInstance vmi){ NodeMetadata nm=getVMById(vmi.getId()); ElasticBlockStoreApi ebsClient = ec2api.getElasticBlockStoreApi().get(); journal.log(Level.INFO, ">> Creating snapshot of VM: "+vmi.getName()); Snapshot snapshot = ebsClient.createSnapshotInRegion("eu-west-1", nm.getHardware().getVolumes().get(0).getId()); String status=""; while (!status.toLowerCase().equals("completed")){ for(Snapshot s:ebsClient.describeSnapshotsInRegion("eu-west-1")) if(s.getId().equals(snapshot.getId())){ status=s.getStatus().name(); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } journal.log(Level.INFO, ">> Snapshot created with ID: "+snapshot.getId()); return snapshot.getId(); }
@Test(dependsOnMethods = "testGetCreateVolumePermissionForSnapshot") void testDeleteVolumeInRegion() { client.deleteVolumeInRegion(defaultRegion, volumeId); Set<Volume> volumes = client.describeVolumesInRegionWithFilter(defaultRegion, ImmutableMultimap .<String, String> builder().put("volume-id", volumeId).build()); // The volume may not exist or remain in "deleting" state for a while Volume volume = getOnlyElement(volumes, null); assertTrue(volume == null || Status.DELETING == volume.getStatus()); }
@Test(expectedExceptions = AWSResponseException.class) void testDescribeVolumesWithInvalidFilter() { String region = defaultRegion; SortedSet<Volume> allResults = Sets.newTreeSet(client.describeVolumesInRegion(region)); assertNotNull(allResults); assertFalse(allResults.isEmpty()); Volume volume = allResults.last(); client.describeVolumesInRegionWithFilter(region, ImmutableMultimap.<String, String>builder() .put("invalid-filter", volume.getId()).build()); }
@Test(expectedExceptions = AWSResponseException.class) void testDescribeSnapshotsWithFilterInvalid() { String region = defaultRegion; SortedSet<Snapshot> allResults = Sets.newTreeSet(client.describeSnapshotsInRegion(region)); assertNotNull(allResults); if (!allResults.isEmpty()) { Snapshot snapshot = allResults.last(); client.describeSnapshotsInRegionWithFilter(region, ImmutableMultimap.<String, String>builder() .put("invalid-filter", snapshot.getId()).build()); } }
Volume volume = ebsClient.createVolumeInAvailabilityZone(zone.getId(), 4); Snapshot snapshot = ebsClient.createSnapshotInRegion(volume.getRegion(), volume.getId()); ebsClient.deleteVolumeInRegion(volume.getRegion(), volume.getId()); volume = Iterables.getOnlyElement(ebsClient.describeVolumesInRegion(node.getLocation().getParent().getId(), device.getVolumeId())); volume = Iterables.getOnlyElement(ebsClient.describeVolumesInRegion(node.getLocation().getParent().getId(), device.getVolumeId())); ebsClient.deleteSnapshotInRegion(snapshot.getRegion(), snapshot.getId());
@Test void testCreateVolumeInAvailabilityZoneWithVolumeType() { Volume expected = client.createVolumeInAvailabilityZone(defaultZone, volumeType("gp2"), withSize(1)); assertNotNull(expected); assertEquals(expected.getAvailabilityZone(), defaultZone); assertEquals(expected.getVolumeType(), "gp2"); Set<Volume> result = Sets.newLinkedHashSet(client.describeVolumesInRegion(defaultRegion, expected.getId())); assertNotNull(result); assertEquals(result.size(), 1); Volume volume = result.iterator().next(); assertEquals(volume.getId(), expected.getId()); assertEquals(volume.getVolumeType(), expected.getVolumeType()); client.deleteVolumeInRegion(volume.getRegion(), volume.getId()); }
@Test(dependsOnMethods = "testCreateSnapshotInRegion") void testCreateVolumeFromSnapshotInAvailabilityZone() { Volume volume = client.createVolumeFromSnapshotInAvailabilityZone(defaultZone, snapshot.getId()); assertNotNull(volume); Predicate<Volume> availabile = retry(new VolumeAvailable(client), 600, 10, SECONDS); assert availabile.apply(volume); Volume result = Iterables.getOnlyElement(client.describeVolumesInRegion(defaultRegion, volume.getId())); assertEquals(volume.getId(), result.getId()); assertEquals(volume.getSnapshotId(), snapshot.getId()); assertEquals(volume.getAvailabilityZone(), defaultZone); assertEquals(result.getStatus(), Volume.Status.AVAILABLE); client.deleteVolumeInRegion(defaultRegion, result.getId()); }
@Test(enabled = false, dependsOnMethods = "testBundleInstance") void testAMIFromBundle() { volume = Iterables.getOnlyElement(client.getElasticBlockStoreApi().get().describeVolumesInRegion( volume.getRegion(), volume.getId())); if (!volume.getAttachments().isEmpty()) { client.getElasticBlockStoreApi().get().detachVolumeInRegion(instance.getRegion(), volume.getId(), false); System.out.printf("%d: %s awaiting detachment to complete%n", System.currentTimeMillis(), volume.getId()); assert volumeTester.apply(volume); snapshot = client.getElasticBlockStoreApi().get().createSnapshotInRegion(volume.getRegion(), volume.getId(), withDescription("EBS Ubuntu Hardy"));
BlockDevice gp2Device = instance.getEbsBlockDevices().get("/dev/sdb"); assertNotNull(gp2Device, "device /dev/sdb not present on " + instance); Volume gp2Volume = Iterables.getOnlyElement(ec2Api.getElasticBlockStoreApi().get().describeVolumesInRegion(regionId, gp2Device.getVolumeId())); assertNotNull(gp2Volume, "/dev/sdb volume is null"); assertEquals(gp2Volume.getVolumeType(), "gp2"); BlockDevice device = instance.getEbsBlockDevices().get("/dev/sda1"); assertNotNull(device, "device: /dev/sda1 not present on: " + instance); Snapshot snapshot = ec2Api.getElasticBlockStoreApi().get().createSnapshotInRegion(regionId, device.getVolumeId()); snapshotsToDelete.add(snapshot.getId());
@Test void testCreateVolumeInAvailabilityZone() { Volume expected = client.createVolumeInAvailabilityZone(defaultZone, withSize(1)); assertNotNull(expected); assertEquals(expected.getAvailabilityZone(), defaultZone); this.volumeId = expected.getId(); Set<Volume> result = Sets.newLinkedHashSet(client.describeVolumesInRegion(defaultRegion, expected.getId())); assertNotNull(result); assertEquals(result.size(), 1); Volume volume = result.iterator().next(); assertEquals(volume.getId(), expected.getId()); assertEquals(volume.getVolumeType(), expected.getVolumeType()); }
@Test void testDescribeSnapshots() { String region = defaultRegion; SortedSet<Snapshot> allResults = Sets.newTreeSet(client.describeSnapshotsInRegion(region)); assertNotNull(allResults); if (!allResults.isEmpty()) { Snapshot snapshot = allResults.last(); Snapshot result = Iterables.getOnlyElement(client.describeSnapshotsInRegion(region, snapshotIds(snapshot.getId()))); assertNotNull(result); assertEquals(result, snapshot); } }
@Test(dependsOnMethods = "testDeleteVolumeInRegion") void testDeleteSnapshotInRegion() { client.deleteSnapshotInRegion(defaultRegion, snapshot.getId()); assert client.describeSnapshotsInRegion(defaultRegion, snapshotIds(snapshot.getId())).size() == 0; }
client.getElasticBlockStoreApi().get().deleteSnapshotInRegion(snapshot.getRegion(), snapshot.getId()); } catch (Exception e) { e.printStackTrace(); client.getElasticBlockStoreApi().get().detachVolumeInRegion(volume.getRegion(), volume.getId(), true); assert volumeTester.apply(volume); } catch (Exception e) { client.getElasticBlockStoreApi().get().deleteVolumeInRegion(volume.getRegion(), volume.getId()); } catch (Exception e) { e.printStackTrace();
@AfterClass(groups = { "integration", "live" }) @Override protected void tearDownContext() { try { client.deleteSnapshotInRegion(defaultRegion, snapshot.getId()); client.deleteVolumeInRegion(defaultRegion, volumeId); } catch (Exception e) { // we don't really care about any exception here, so just delete away. } super.tearDownContext(); }
public void deleteVolume(String id) { blockStoreApi.deleteVolumeInRegion(null, id); }
@Test(enabled = false, dependsOnMethods = "testCreateRunningInstance") void testCreateAndAttachVolume() { volume = client.getElasticBlockStoreApi().get().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.getElasticBlockStoreApi().get().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()); }
protected Snapshot createSnapshot() throws RunNodesException { String instanceId = null; try { RunningInstance instance = getOnlyElement(concat(ec2Api.getInstanceApi().get().runInstancesInRegion( regionId, null, imageId, 1, 1))); instanceId = instance.getId(); assertTrue(runningTester.apply(instance), instanceId + "didn't achieve the state running!"); instance = getOnlyElement(concat(ec2Api.getInstanceApi().get().describeInstancesInRegion(regionId, instanceId))); BlockDevice device = instance.getEbsBlockDevices().get("/dev/sda1"); assertNotNull(device, "device: /dev/sda1 not present on: " + instance); Snapshot snapshot = ec2Api.getElasticBlockStoreApi().get().createSnapshotInRegion(regionId, device.getVolumeId()); snapshotsToDelete.add(snapshot.getId()); Predicate<Snapshot> snapshotted = retry(new SnapshotCompleted(ec2Api.getElasticBlockStoreApi().get()), 600, 10, SECONDS); assert snapshotted.apply(snapshot); return snapshot; } finally { if (instanceId != null) ec2Api.getInstanceApi().get().terminateInstancesInRegion(regionId, instanceId); } }
public void testCreateVolumeInAvailabilityZone() { Builder<HttpRequest, HttpResponse> builder = ImmutableMap.<HttpRequest, HttpResponse>builder(); builder.put(describeRegionsRequest, describeRegionsResponse); builder.putAll(describeAvailabilityZonesRequestResponse); builder.put( HttpRequest.builder() .method("POST") .endpoint("https://ec2.us-east-1.amazonaws.com/") .addHeader("Host", "ec2.us-east-1.amazonaws.com") .payload(payloadFromStringWithContentType("Action=CreateVolume" + "&AvailabilityZone=us-east-1a" + "&Signature=NCu8HU8u0A385rTgj%2BN5lq606jkc1eu88jof9yAxb6s%3D" + "&SignatureMethod=HmacSHA256" + "&SignatureVersion=2" + "&Size=4" + "&Timestamp=2012-04-16T15%3A54%3A08.897Z" + "&Version=2010-08-31" + "&AWSAccessKeyId=identity", "application/x-www-form-urlencoded")).build(), HttpResponse.builder() .statusCode(200) .payload(payloadFromResource("/created_volume.xml")).build()); ElasticBlockStoreApi client = requestsSendResponses(builder.build()).getElasticBlockStoreApi().get(); assertEquals(client.createVolumeInAvailabilityZone("us-east-1a", 4), creating); }
@Test(dependsOnMethods = "testCreateVolumeFromSnapshotInAvailabilityZone") void testCreateVolumeFromSnapshotInAvailabilityZoneWithOptions() { Volume volume = client.createVolumeInAvailabilityZone(defaultZone, fromSnapshotId(snapshot.getId())); assertNotNull(volume); Predicate<Volume> availabile = retry(new VolumeAvailable(client), 600, 10, SECONDS); assert availabile.apply(volume); Volume result = Iterables.getOnlyElement(client.describeVolumesInRegion(defaultRegion, volume.getId())); assertEquals(volume.getId(), result.getId()); assertEquals(volume.getSnapshotId(), snapshot.getId()); assertEquals(volume.getAvailabilityZone(), defaultZone); assertEquals(result.getStatus(), Volume.Status.AVAILABLE); client.deleteVolumeInRegion(defaultRegion, result.getId()); }