com.amazonaws.services.ec2.model.EbsBlockDevice Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.EbsBlockDevice. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EncryptedImageCopyServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteResourcesWhenBothAMIAndSnapshotExist() {
    String encryptedImageId = "ami-87654321";
    String encryptedSnapshotId = "snap-12345678";
    Image image = new Image()
            .withImageId(encryptedImageId)
            .withBlockDeviceMappings(
                    new BlockDeviceMapping().withDeviceName("/dev/sdb").withVirtualName("ephemeral0"),
                    new BlockDeviceMapping().withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(encryptedSnapshotId)));
    when(ec2Client.describeImages(any()))
            .thenReturn(new DescribeImagesResult().withImages(image));

    List<CloudResource> resources = List.of(new CloudResource.Builder()
            .name(encryptedImageId)
            .type(ResourceType.AWS_ENCRYPTED_AMI)
            .build());

    underTest.deleteResources(DEFAULT_REGION, ec2Client, resources);

    verify(ec2Client, times(1)).deleteSnapshot(any());
    verify(ec2Client, times(1)).deregisterImage(any());
}
 
Example #2
Source File: EncryptedImageCopyServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteResourcesWhenOnlyAMIExistsAndSnapshotNot() {
    String encryptedImageId = "ami-87654321";
    Image image = new Image()
            .withImageId(encryptedImageId)
            .withBlockDeviceMappings(new BlockDeviceMapping()
                    .withEbs(new EbsBlockDevice().withEncrypted(true)));
    when(ec2Client.describeImages(any()))
            .thenReturn(new DescribeImagesResult().withImages(image));
    List<CloudResource> resources = List.of(new CloudResource.Builder()
            .name(encryptedImageId)
            .type(ResourceType.AWS_ENCRYPTED_AMI)
            .build());

    underTest.deleteResources(DEFAULT_REGION, ec2Client, resources);

    verify(ec2Client, times(0)).deleteSnapshot(any());
    verify(ec2Client, times(1)).deregisterImage(any());
}
 
Example #3
Source File: EncryptedImageCopyServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteResourcesWhenAMIDoesNotExistAtDeregisterShouldNotThrowException() {
    String encryptedImageId = "ami-87654321";
    String encryptedSnapshotId = "snap-12345678";
    String eMsg = String.format("An error occurred (%s) when calling the DeregisterImage operation: The image id '[%s]' does not exist",
            AMI_NOT_FOUND_MSG_CODE, encryptedImageId);

    Image image = new Image()
            .withImageId(encryptedImageId)
            .withBlockDeviceMappings(new BlockDeviceMapping()
                    .withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(encryptedSnapshotId)));
    when(ec2Client.describeImages(any()))
            .thenReturn(new DescribeImagesResult().withImages(image));
    when(ec2Client.deregisterImage(any()))
            .thenThrow(new AmazonServiceException(eMsg));
    List<CloudResource> resources = List.of(new CloudResource.Builder()
            .name(encryptedImageId)
            .type(ResourceType.AWS_ENCRYPTED_AMI)
            .build());

    underTest.deleteResources(DEFAULT_REGION, ec2Client, resources);

    verify(ec2Client, times(1)).deregisterImage(any());
    verify(ec2Client, times(0)).deleteSnapshot(any());
}
 
Example #4
Source File: EncryptedImageCopyServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteResourcesWhenSnapshotDoesNotExistShouldNotThrowException() {
    String encryptedImageId = "ami-87654321";
    String encryptedSnapshotId = "snap-12345678";
    String eMsg = String.format("An error occurred (%s) when calling the DeleteSnapshot operation: None", SNAPSHOT_NOT_FOUND_MSG_CODE);
    Image image = new Image()
            .withImageId(encryptedImageId)
            .withBlockDeviceMappings(new BlockDeviceMapping()
                    .withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(encryptedSnapshotId)));
    when(ec2Client.describeImages(any()))
            .thenReturn(new DescribeImagesResult().withImages(image));
    when(ec2Client.deleteSnapshot(any()))
            .thenThrow(new AmazonServiceException(eMsg));
    List<CloudResource> resources = List.of(new CloudResource.Builder()
            .name(encryptedImageId)
            .type(ResourceType.AWS_ENCRYPTED_AMI)
            .build());

    underTest.deleteResources(DEFAULT_REGION, ec2Client, resources);

    verify(ec2Client, times(1)).deregisterImage(any());
    verify(ec2Client, times(1)).deleteSnapshot(any());
}
 
Example #5
Source File: EncryptedImageCopyServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteResourcesWhenAMIDeregisterFailsWithEC2Client() {
    thrown.expect(CloudConnectorException.class);

    String encryptedImageId = "ami-87654321";
    String encryptedSnapshotId = "snap-12345678";
    Image image = new Image()
            .withImageId(encryptedImageId)
            .withBlockDeviceMappings(
                    new BlockDeviceMapping().withDeviceName("/dev/sdb").withVirtualName("ephemeral0"),
                    new BlockDeviceMapping().withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(encryptedSnapshotId)));
    when(ec2Client.describeImages(any()))
            .thenReturn(new DescribeImagesResult().withImages(image));
    when(ec2Client.deregisterImage(any()))
            .thenThrow(new AmazonServiceException("Something went wrong or your credentials has been expired"));
    List<CloudResource> resources = List.of(new CloudResource.Builder()
            .name(encryptedImageId)
            .type(ResourceType.AWS_ENCRYPTED_AMI)
            .build());

    underTest.deleteResources(DEFAULT_REGION, ec2Client, resources);

    verify(ec2Client, times(0)).deleteSnapshot(any());
    verify(ec2Client, times(1)).deregisterImage(any());
}
 
Example #6
Source File: EncryptedImageCopyServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteResourcesWhenAMISnapshotDeletionFailsWithEC2Client() {
    thrown.expect(CloudConnectorException.class);

    String encryptedImageId = "ami-87654321";
    String encryptedSnapshotId = "snap-12345678";
    Image image = new Image()
            .withImageId(encryptedImageId)
            .withBlockDeviceMappings(new BlockDeviceMapping()
                    .withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(encryptedSnapshotId)));
    when(ec2Client.describeImages(any()))
            .thenReturn(new DescribeImagesResult().withImages(image));
    when(ec2Client.deleteSnapshot(any()))
            .thenThrow(new AmazonServiceException("Something went wrong or your credentials has been expired"));
    List<CloudResource> resources = List.of(new CloudResource.Builder()
            .name(encryptedImageId)
            .type(ResourceType.AWS_ENCRYPTED_AMI)
            .build());

    underTest.deleteResources(DEFAULT_REGION, ec2Client, resources);

    verify(ec2Client, times(1)).deregisterImage(any());
    verify(ec2Client, times(1)).deleteSnapshot(any());
}
 
Example #7
Source File: EncryptedImageCopyServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteResourcesShouldDeleteMultipleSnapshotsWhenMultipleSnapshotAreLinkedToTheAMI() {
    String encryptedImageId = "ami-87654321";
    String encryptedSnapshotId = "snap-12345678";
    String secondEncryptedSnapshotId = "snap-12345555";
    Image image = new Image()
            .withImageId(encryptedImageId)
            .withBlockDeviceMappings(
                    new BlockDeviceMapping().withDeviceName("/dev/sdb").withVirtualName("ephemeral0"),
                    new BlockDeviceMapping().withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(encryptedSnapshotId)),
                    new BlockDeviceMapping().withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(secondEncryptedSnapshotId)));
    when(ec2Client.describeImages(any()))
            .thenReturn(new DescribeImagesResult().withImages(image));

    List<CloudResource> resources = List.of(new CloudResource.Builder()
            .name(encryptedImageId)
            .type(ResourceType.AWS_ENCRYPTED_AMI)
            .build());

    underTest.deleteResources(DEFAULT_REGION, ec2Client, resources);

    verify(ec2Client, times(1)).deregisterImage(any());
    verify(ec2Client, times(2)).deleteSnapshot(any());
}
 
Example #8
Source File: EncryptedImageCopyServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteResourcesShouldDeleteOnlyEbsDeviceMappingsWithSnapshotWhenMultipleSnapshotAndEphemeralDevicesAreLinkedToTheAMI() {
    String encryptedImageId = "ami-87654321";
    String encryptedSnapshotId = "snap-12345678";
    String secondEncryptedSnapshotId = "snap-12345555";
    Image image = new Image()
            .withImageId(encryptedImageId)
            .withBlockDeviceMappings(
                    new BlockDeviceMapping().withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(encryptedSnapshotId)),
                    new BlockDeviceMapping().withEbs(new EbsBlockDevice().withEncrypted(true).withSnapshotId(secondEncryptedSnapshotId)),
                    new BlockDeviceMapping().withDeviceName("/dev/sdb").withVirtualName("ephemeral0"),
                    new BlockDeviceMapping().withDeviceName("/dev/sdc").withVirtualName("ephemeral1"));
    when(ec2Client.describeImages(any()))
            .thenReturn(new DescribeImagesResult().withImages(image));

    List<CloudResource> resources = List.of(new CloudResource.Builder()
            .name(encryptedImageId)
            .type(ResourceType.AWS_ENCRYPTED_AMI)
            .build());

    underTest.deleteResources(DEFAULT_REGION, ec2Client, resources);

    verify(ec2Client, times(1)).deregisterImage(any());
    verify(ec2Client, times(2)).deleteSnapshot(any());
}
 
Example #9
Source File: ImagesTableProviderTest.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
private Image makeImage(String id)
{
    Image image = new Image();
    image.withImageId(id)
            .withArchitecture("architecture")
            .withCreationDate("created")
            .withDescription("description")
            .withHypervisor("hypervisor")
            .withImageLocation("location")
            .withImageType("type")
            .withKernelId("kernel")
            .withName("name")
            .withOwnerId("owner")
            .withPlatform("platform")
            .withRamdiskId("ramdisk")
            .withRootDeviceName("root_device")
            .withRootDeviceType("root_type")
            .withSriovNetSupport("srvio_net")
            .withState("state")
            .withVirtualizationType("virt_type")
            .withPublic(true)
            .withTags(new Tag("key", "value"))
            .withBlockDeviceMappings(new BlockDeviceMapping()
                    .withDeviceName("dev_name")
                    .withNoDevice("no_device")
                    .withVirtualName("virt_name")
                    .withEbs(new EbsBlockDevice()
                            .withIops(100)
                            .withKmsKeyId("ebs_kms_key")
                            .withVolumeType("ebs_type")
                            .withVolumeSize(100)));

    return image;
}
 
Example #10
Source File: V1ItemFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static BlockDeviceMapping blockDeviceMapping() {
    return new BlockDeviceMapping()
        .withDeviceName(randomS(100))
        .withVirtualName(randomS(50))
        .withNoDevice(randomS(50))
        .withEbs(new EbsBlockDevice().withDeleteOnTermination(true)
                                     .withEncrypted(false)
                                     .withIops(50)
                                     .withKmsKeyId(randomS(50))
                                     .withSnapshotId(randomS(50))
                                     .withVolumeSize(50)
                                     .withVolumeType(VolumeType.Gp2));
}
 
Example #11
Source File: Ec2Util.java    From s3-bucket-loader with Apache License 2.0 4 votes vote down vote up
public List<Instance> launchEc2Instances(AmazonEC2Client ec2Client, Properties props) throws Exception {
	
	Integer totalExpectedWorkers = Integer.valueOf(props.getProperty("master.workers.total"));
	
	// disk size
	Collection<BlockDeviceMapping> blockDevices = new ArrayList<BlockDeviceMapping>();
	blockDevices.add(
			new BlockDeviceMapping()
				.withDeviceName(props.getProperty("master.workers.ec2.disk.deviceName"))
				.withEbs(new EbsBlockDevice()
						.withVolumeType(VolumeType.valueOf(props.getProperty("master.workers.ec2.disk.volumeType")))
						.withDeleteOnTermination(true)
					    .withVolumeSize(Integer.valueOf(props.getProperty("master.workers.ec2.disk.size.gigabytes")))));
	
	// create our run request for the total workers we expect
	RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
	runInstancesRequest.withImageId(props.getProperty("master.workers.ec2.ami.id"))
				        .withInstanceType(props.getProperty("master.workers.ec2.instanceType"))
				        .withMinCount(totalExpectedWorkers)
				        .withMaxCount(totalExpectedWorkers)
				        .withBlockDeviceMappings(blockDevices)
				        .withKeyName(props.getProperty("master.workers.ec2.keyName"))
				        .withSecurityGroupIds(props.getProperty("master.workers.ec2.securityGroupId"))
				        .withInstanceInitiatedShutdownBehavior(ShutdownBehavior.valueOf(props.getProperty("master.workers.ec2.shutdownBehavior")))
				        .withSubnetId(props.getProperty("master.workers.ec2.subnetId"))
				        .withUserData(Base64.encodeAsString(readFile(props.getProperty("master.workers.ec2.userDataFile")).getBytes()));
	
	// launch
	logger.debug("Launching " + totalExpectedWorkers + " EC2 instances, " +
						"it may take few minutes for workers to come up...: \n" +
						"\tamiId:" + runInstancesRequest.getImageId() +"\n"+
						"\tsecGrpId:" + runInstancesRequest.getSecurityGroupIds().get(0) +"\n"+
						"\tsubnetId:" + runInstancesRequest.getSubnetId() +"\n"+
						"\tinstanceType:" + runInstancesRequest.getInstanceType() +"\n"+
						"\tshutdownBehavior:" + runInstancesRequest.getInstanceInitiatedShutdownBehavior() +"\n"+
						"\tkeyName:" + runInstancesRequest.getKeyName() 
						);


	// as the instances come up, assuming the "userData" above launches the worker we will be good
	// they will auto register w/ us the master 
	RunInstancesResult result = ec2Client.runInstances(runInstancesRequest);
	Reservation reservation = result.getReservation();
	return reservation.getInstances();
}