use of com.amazonaws.services.ec2.model.Image in project cloudbreak by hortonworks.
the class AwsResourceConnector method getRootDeviceName.
private String getRootDeviceName(AuthenticatedContext ac, CloudStack cloudStack) {
AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
DescribeImagesResult images = ec2Client.describeImages(new DescribeImagesRequest().withImageIds(cloudStack.getImage().getImageName()));
if (images.getImages().isEmpty()) {
throw new CloudConnectorException(String.format("AMI is not available: '%s'.", cloudStack.getImage().getImageName()));
}
Image image = images.getImages().get(0);
if (image == null) {
throw new CloudConnectorException(String.format("Couldn't describe AMI '%s'.", cloudStack.getImage().getImageName()));
}
return image.getRootDeviceName();
}
use of com.amazonaws.services.ec2.model.Image in project TOSCAna by StuPro-TOSCAna.
the class CapabilityMapper method mapOsCapabilityToImageId.
/**
* Requests the AWS server for ImageIds with filters which are filled based on the values of the {@link OsCapability}.
* <br>
* The image with the latest creation date is picked and its imageId returned.
*
* @param osCapability the OsCapability to map
* @return a {@link String} that contains a valid ImageId that can be added to the properties of an EC2
*/
public String mapOsCapabilityToImageId(OsCapability osCapability) throws SdkClientException, ParseException, IllegalArgumentException {
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(awsRegion).build();
// need to set these
DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withFilters(new Filter("virtualization-type").withValues("hvm"), new Filter("root-device-type").withValues("ebs")).withOwners(// this is the ownerId of amazon itself
"099720109477");
if (osCapability.getType().isPresent() && osCapability.getType().get().equals(OsCapability.Type.WINDOWS)) {
describeImagesRequest.withFilters(new Filter("platform").withValues("windows"));
}
if (osCapability.getDistribution().isPresent()) {
if (osCapability.getDistribution().get().equals(OsCapability.Distribution.UBUNTU)) {
// */ubuntu/images/* gets better results than plain *ubuntu*
describeImagesRequest.withFilters(new Filter("name").withValues("*ubuntu/images/*"));
} else {
// just search for the string
describeImagesRequest.withFilters(new Filter("name").withValues("*" + osCapability.getDistribution().toString() + "*"));
}
}
if (osCapability.getVersion().isPresent()) {
describeImagesRequest.withFilters(new Filter("name").withValues("*" + osCapability.getVersion().get() + "*"));
}
if (osCapability.getArchitecture().isPresent()) {
if (osCapability.getArchitecture().get().equals(OsCapability.Architecture.x86_64)) {
describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_64));
} else if (osCapability.getArchitecture().get().equals(OsCapability.Architecture.x86_32)) {
describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_32));
} else {
throw new UnsupportedOperationException("This architecture is not supported " + osCapability.getArchitecture());
}
} else {
// defaulting to 64 bit architecture
describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_64));
}
try {
DescribeImagesResult describeImagesResult = ec2.describeImages(describeImagesRequest);
String imageId = processResult(describeImagesResult);
logger.debug("ImageId is: '{}'", imageId);
return imageId;
} catch (SdkClientException se) {
logger.error("Cannot connect to AWS to request image Ids");
throw se;
} catch (ParseException pe) {
logger.error("Error parsing date format of image creation dates");
throw pe;
} catch (IllegalArgumentException ie) {
logger.error("With the filters created from the OsCapability there are no valid images received");
throw ie;
}
}
use of com.amazonaws.services.ec2.model.Image in project TOSCAna by StuPro-TOSCAna.
the class CapabilityMapper method processResult.
/**
* Processes the result of an {@link DescribeImagesRequest} and returns the imageId of the latest image.
*
* @param describeImagesResult the result received from aws
* @return returns the latest imageId
*/
private String processResult(DescribeImagesResult describeImagesResult) throws ParseException, IllegalArgumentException {
Integer numReceivedImages = describeImagesResult.getImages().size();
logger.debug("Got '{}' images from aws", numReceivedImages);
if (numReceivedImages > 0) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Map<Date, Image> creationDateMap = new HashMap<>();
for (Image image : describeImagesResult.getImages()) {
Date date = dateFormat.parse(image.getCreationDate());
creationDateMap.put(date, image);
}
Image latest = creationDateMap.get(Collections.max(creationDateMap.keySet()));
logger.debug("Latest image received: '{}'", latest);
return latest.getImageId();
} else {
throw new IllegalArgumentException("No images received");
}
}
Aggregations