/** * Checks whether image is present. * * @param amiID * @return <code>Image </code> if the matches one of the amiID * */ public Image resolveAMI(String amiID) throws APPlatformException { LOGGER.debug("resolveAMI('{}') entered", amiID); DescribeImagesRequest dir = new DescribeImagesRequest(); dir.withImageIds(amiID); DescribeImagesResult describeImagesResult = getEC2() .describeImages(dir); List<Image> images = describeImagesResult.getImages(); for (Image image : images) { LOGGER.debug(image.getImageId() + "==" + image.getImageLocation() + "==" + image.getName()); return image; } throw new APPlatformException(Messages.getAll("error_invalid_image") + amiID); }
@Test public void testResolveAMIFound() throws Exception { ec2mock.createDescribeImagesResult("image1"); Image result = ec2comm.resolveAMI("image1"); assertEquals("image1", result.getImageId()); ArgumentCaptor<DescribeImagesRequest> argCaptor = ArgumentCaptor .forClass(DescribeImagesRequest.class); verify(ec2).describeImages(argCaptor.capture()); DescribeImagesRequest dir = argCaptor.getValue(); for (Filter filter : dir.getFilters()) { if (filter.getName().equals("name")) { assertEquals("image1", filter.getValues().get(0)); } } }
List<String> getSnapshotIdsFromImageId(AmazonEC2Async client, ImageCreateRequest request, Context context) { // LambdaLogger logger = context.getLogger(); String imageId = request.getImageId(); DescribeImagesResult result = client.describeImages(new DescribeImagesRequest().withImageIds(imageId)); List<String> snapshotIds = new ArrayList<String>(); for (Image image : result.getImages()) { for (BlockDeviceMapping block : image.getBlockDeviceMappings()) { snapshotIds.add(block.getEbs().getSnapshotId()); } } return snapshotIds; }
/** * Validates if the given AMI has given tag and value. * * @return true if matches otherwise false */ public boolean isAmiWithTagExist(final String amiId, final String tagName, final String tagValue) { final DescribeImagesRequest request = new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)); try { final DescribeImagesResult result = ec2Client.describeImages(request); return result.getImages().size() > 0; } catch (final AmazonServiceException ase) { if (ase.getErrorCode() == "InvalidAMIID.NotFound") { return false; } throw ase; } }
@Test public void isAmiWithTagExistTrue() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client); String amiId = "ami-1234abcd"; String tagName = "sometag"; String tagValue = "someval"; when(ec2Client.describeImages( new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)) ) ).thenReturn( new DescribeImagesResult().withImages(new Image()) ); // invoke method under test assertTrue(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue)); }
@Test public void isAmiWithTagExistFalse() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client); String amiId = "ami-1234abcd"; String tagName = "sometag"; String tagValue = "someval"; when(ec2Client.describeImages( new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)) ) ).thenReturn( new DescribeImagesResult() ); // invoke method under test assertFalse(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue)); }
@Test public void isAmiWithTagExistNotFound() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client); String amiId = "ami-1234abcd"; String tagName = "sometag"; String tagValue = "someval"; AmazonServiceException ex = new AmazonServiceException("fake-exception"); ex.setErrorCode("InvalidAMIID.NotFound"); when(ec2Client.describeImages( new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)) ) ).thenThrow(ex); // invoke method under test assertFalse(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue)); }
@Test public void isAmiWithTagExistThrowException() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client); String amiId = "ami-1234abcd"; String tagName = "sometag"; String tagValue = "someval"; String unknownAwsExMessage = "Unknown AWS exception message"; when(ec2Client.describeImages( new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)) ) ).thenThrow(new AmazonServiceException(unknownAwsExMessage)); try { // invoke method under test amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue); fail("Expected exception message '" + unknownAwsExMessage + "'not received"); } catch (AmazonServiceException ex) { // pass assertEquals(unknownAwsExMessage, ex.getErrorMessage()); } }
@Override @Cacheable(cacheNames = "ami-details", cacheManager = "oneDayTTLCacheManager") public Map<String, String> getAmiDetails(final String accountId, final Region region, final String amiId) { final ImmutableMap.Builder<String, String> result = ImmutableMap.builder(); result.put("ami_id", amiId); final AmazonEC2Client ec2 = clientProvider.getClient(AmazonEC2Client.class, accountId, region); final Optional<Image> ami = Optional.ofNullable(new DescribeImagesRequest().withImageIds(amiId)) .map(ec2::describeImages) .map(DescribeImagesResult::getImages) .map(List::stream) .flatMap(Stream::findFirst); ami.map(Image::getName).ifPresent(name -> result.put("ami_name", name)); ami.map(Image::getOwnerId).ifPresent(owner -> result.put("ami_owner_id", owner)); return result.build(); }
@Test public void testApplyAmiFound() throws Exception { when(ec2InstanceContextMock.getAmiId()).thenReturn(Optional.of(AMI_ID)); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); final DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(AMI_ID); when(amazonEC2ClientMock.describeImages(eq(describeImagesRequest))) .thenReturn(new DescribeImagesResult() .withImages(newArrayList(new Image() .withImageId(AMI_ID) .withName(AMI_NAME)) ) ); final Optional<Image> result = amiProvider.apply(ec2InstanceContextMock); assertThat(result).isPresent(); verify(ec2InstanceContextMock).getAmiId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeImages(eq(describeImagesRequest)); }
@Test public void testApplyAmiNotFound() throws Exception { when(ec2InstanceContextMock.getAmiId()).thenReturn(Optional.of(AMI_ID)); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); final DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(AMI_ID); when(amazonEC2ClientMock.describeImages(eq(describeImagesRequest))) .thenReturn(null); final Optional<Image> result = amiProvider.apply(ec2InstanceContextMock); assertThat(result).isEmpty(); verify(ec2InstanceContextMock).getAmiId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeImages(eq(describeImagesRequest)); }
@Test public void testApplyAmiNotFoundWithException() throws Exception { when(ec2InstanceContextMock.getAmiId()).thenReturn(Optional.of(AMI_ID)); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); final DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(AMI_ID); when(amazonEC2ClientMock.describeImages(eq(describeImagesRequest))) .thenThrow(new AmazonClientException("oops, I did it again... Britney")); final Optional<Image> result = amiProvider.apply(ec2InstanceContextMock); assertThat(result).isEmpty(); verify(ec2InstanceContextMock).getAmiId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeImages(eq(describeImagesRequest)); }
public List<VirtualMachineImage> listImages(String ... imageIds) { checkNotNull(imageIds); checkArgument(imageIds.length >= 1); List<Image> images = newArrayList(); try { images = ec2_.describeImages(new DescribeImagesRequest().withImageIds(imageIds)).getImages(); } catch (AmazonClientException exception) { LOG.error(exception.getMessage()); } return IMAGES_FUNCTION.apply(images); }
String lookupImageId(String imgName) { DescribeImagesRequest req = new DescribeImagesRequest(); if(imgName == null || imgName.length() == 0) return null; req.setRequestCredentials(creds); DescribeImagesResult result = ec2Client.describeImages(); for(Image image : result.getImages()) { if(image.getName() != null && image.getName().equals(imgName)) return image.getImageId(); } return null; }
@Override public List<AbstractResource<?>> describeAMIs(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeImagesRequest req = new DescribeImagesRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing amis for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeImagesResult res = ec2.describeImages(req); List<List<LaunchPermission>> imageLaunchPermissions = new ArrayList<>(); for (Image image : res.getImages()) { imageLaunchPermissions.add(findImageLaunchPermissions(account, region, image.getImageId())); } return converter.toEc2AMIs(res.getImages(), imageLaunchPermissions, account.getId(), region, dt); }
@Test public void createInstance() throws Exception { parameters.put(PropertyHandler.FLOW_STATE, new Setting(PropertyHandler.FLOW_STATE, FlowState.CREATION_REQUESTED.name())); parameters.put(PropertyHandler.OPERATION, new Setting( PropertyHandler.OPERATION, Operation.EC2_CREATION.name())); ec2mock.createDescribeImagesResult(IMAGE_ID); ec2mock.createRunInstancesResult(INSTANCE_ID); ec2mock.addDescribeInstancesResult(INSTANCE_ID, "pending", null); ec2mock.addDescribeInstanceStatusResult(INSTANCE_ID, "pending", "initializing", "initializing"); ec2mock.addDescribeInstancesResult(INSTANCE_ID, "running", null); ec2mock.addDescribeInstanceStatusResult(INSTANCE_ID, "running", "ok", "ok"); ec2mock.addDescribeInstancesResult(INSTANCE_ID, "running", "2aws-1-2-3-4"); ec2mock.addDescribeInstancesResult(INSTANCE_ID, "ok", "1.2.3.4"); runUntilReady(); ArgumentCaptor<DescribeImagesRequest> argCapImages = ArgumentCaptor .forClass(DescribeImagesRequest.class); verify(ec2).describeImages(argCapImages.capture()); }
public void createDescribeImagesResult(String... imageIds) { Collection<Image> images = new ArrayList<Image>(); for (int i = 0; i < imageIds.length; i++) { images.add(new Image().withImageId(imageIds[i])); } DescribeImagesResult imagesResult = new DescribeImagesResult() .withImages(images); doReturn(imagesResult).when(ec2) .describeImages(any(DescribeImagesRequest.class)); }
String getImageState(AmazonEC2Async client, ImageCreateRequest createAMIRequest, Context context) { // LambdaLogger logger = context.getLogger(); String imageId = createAMIRequest.getImageId(); DescribeImagesRequest req = new DescribeImagesRequest().withImageIds(imageId); DescribeImagesResult result = client.describeImages(req); return result.getImages().get(0).getState(); }
private String getImageState(String imageId) { String state = "failed"; DescribeImagesResult result = ec2Client .describeImages(new DescribeImagesRequest() .withImageIds(imageId)); if (null != result && null != result.getImages() && !result.getImages().isEmpty()) { state = result.getImages().get(0).getState(); } return state; }
private Optional<Image> getAmiFromEC2Api(final AmazonEC2Client ec2Client, final String imageId) { try { final DescribeImagesResult response = ec2Client.describeImages(new DescribeImagesRequest().withImageIds(imageId)); return ofNullable(response) .map(DescribeImagesResult::getImages) .map(List::stream) .flatMap(Stream::findFirst); } catch (final AmazonClientException e) { log.warn("Could not describe image " + imageId, e); return empty(); } }
private Optional<Image> getAmi(@Nonnull final EC2InstanceContext context) { final Optional<String> amiId = context.getAmiId(); try { return amiId .map(id -> context .getClient(AmazonEC2Client.class) .describeImages(new DescribeImagesRequest().withImageIds(id))) .map(DescribeImagesResult::getImages) .flatMap(images -> images.stream().findFirst()); } catch (final AmazonClientException e) { log.warn("Could not get AMI of: " + amiId.get(), e); return empty(); } }
public List<Image> describeImages(Collection<String> imageIds) { if (imageIds.isEmpty()) throw new IllegalArgumentException("imageIds can not be empty, otherwise it requires all images"); logger.info("describe images, imageIds={}", imageIds); DescribeImagesResult result = ec2.describeImages(new DescribeImagesRequest().withImageIds(imageIds)); return result.getImages(); }
private void waitUntilAMIFinished(String imageId) throws InterruptedException { logger.info("wait until AMI finished, imageId={}", imageId); while (true) { DescribeImagesResult result = AWS.ec2.ec2.describeImages(new DescribeImagesRequest().withImageIds(imageId)); String state = result.getImages().get(0).getState(); logger.info("AMI state {} => {}", imageId, state); if ("available".equals(state)) { break; } else if ("failed".equals(state)) { throw new IllegalStateException("AMI failed to create, please check AWS console for more details"); } Threads.sleepRoughly(Duration.ofSeconds(30)); } }
public Image getImageById(String imageId) { Image image = null; try { DescribeImagesResult describeImages = ec2_.describeImages(new DescribeImagesRequest().withImageIds(imageId)); image = Lists2.first(describeImages.getImages()); } catch (com.amazonaws.AmazonServiceException exception) { LOG.error("Error on describing image: [{}]. Error message: [{}]", imageId, exception.getMessage(), exception); } return image; }
public Image describeImage(AwsProcessClient awsProcessClient, String imageId) { DescribeImagesRequest request = new DescribeImagesRequest(); request.withImageIds(imageId); DescribeImagesResult result = awsProcessClient.getEc2Client().describeImages(request); List<Image> images = result.getImages(); if (images.isEmpty()) { return null; } return images.get(0); }
@Override public ImageCollection getImages(DescribeImagesRequest request) { ResourceCollectionImpl result = service.getCollection("Images", request); if (result == null) return null; return new ImageCollectionImpl(result); }
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(); }
@Override public DescribeImagesResult describeImages(DescribeImagesRequest describeImagesRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
public void createDescribeImagesException(AmazonClientException ase) { doThrow(ase).when(ec2).describeImages(any(DescribeImagesRequest.class)); }
/** * Creates block device mappings based on the specified instance template. * * @param template the instance template * @return the block device mappings */ private List<BlockDeviceMapping> getBlockDeviceMappings(EC2InstanceTemplate template) { // Query the AMI about the root device name & mapping information DescribeImagesResult result = client.describeImages( new DescribeImagesRequest().withImageIds(template.getImage())); if (result.getImages().isEmpty()) { throw new IllegalArgumentException("The description for image " + template.getImage() + " is empty"); } Image templateImage = result.getImages().get(0); String rootDeviceType = templateImage.getRootDeviceType(); if (!DEVICE_TYPE_EBS.equals(rootDeviceType)) { throw new IllegalArgumentException("The root device for image " + template.getImage() + " must be \"" + DEVICE_TYPE_EBS + "\", found: " + rootDeviceType); } List<BlockDeviceMapping> originalMappings = templateImage.getBlockDeviceMappings(); LOG.info(">> Original image block device mappings: {}", originalMappings); if (originalMappings.isEmpty()) { throw new IllegalArgumentException("The image " + template.getImage() + " has no block device mappings"); } BlockDeviceMapping rootDevice = selectRootDevice(originalMappings, templateImage.getRootDeviceName()); if (rootDevice == null) { throw new IllegalArgumentException("Could not determine root device for image " + template.getImage() + " based on root device name " + templateImage.getRootDeviceName()); } // The encrypted property was added to the block device mapping in version 1.8 of the SDK. // It is a Boolean, but defaults to false instead of being unset, so we set it to null here. rootDevice.getEbs().setEncrypted(null); rootDevice.getEbs().setVolumeSize(template.getRootVolumeSizeGB()); rootDevice.getEbs().setVolumeType(template.getRootVolumeType()); rootDevice.getEbs().setDeleteOnTermination(true); List<BlockDeviceMapping> deviceMappings = Lists.newArrayList(rootDevice); int ebsVolumeCount = template.getEbsVolumeCount(); EBSAllocationStrategy ebsAllocationStrategy = EBSAllocationStrategy.get(template); switch (ebsAllocationStrategy) { case NO_EBS_VOLUMES: // The volumes within an instance should be homogeneous. So we only add // instance store volumes when additional EBS volumes aren't mounted. deviceMappings.addAll(ephemeralDeviceMappings.apply(template.getType())); break; case AS_INSTANCE_REQUEST: LOG.info("EBS volumes will be allocated as part of instance launch request"); List<BlockDeviceMapping> ebsDeviceMappings = getEbsBlockDeviceMapping(ebsVolumeCount, template.getEbsVolumeType(), template.getEbsVolumeSizeGiB(), template.isEnableEbsEncryption()); deviceMappings.addAll(ebsDeviceMappings); break; case AS_SEPARATE_REQUESTS: LOG.info("EBS volumes will be separately allocated after instance launch request"); break; default: throw new IllegalStateException("Invalid EBS allocation strategy " + ebsAllocationStrategy); } LOG.info(">> Block device mappings: {}", deviceMappings); return deviceMappings; }
public List<VirtualMachineImage> listImages(OsArchitectureType architecture, Platform platform, HypervisorType hypervisor, VirtualizationType virtualizationType, String... imageIds) { checkArgument(architecture != null); List<VirtualMachineImage> images = newArrayList(); DescribeImagesRequest request = new DescribeImagesRequest(); request.withFilters(new Filter().withName("state").withValues("available"), new Filter().withName("architecture").withValues(architecture.name().toLowerCase()), new Filter().withName("image-type").withValues("machine")); if (platform != null) { request.withFilters(new Filter().withName("platform").withValues(platform.getValue())); } if (hypervisor != null) { request.withFilters(new Filter().withName("hypervisor").withValues(hypervisor.getValue())); } if (virtualizationType != null) { request.withFilters(new Filter().withName("virtualization-type").withValues(hypervisor.getValue())); } if (imageIds != null && imageIds.length > 0) { request.withImageIds(imageIds); } try { images = IMAGES_FUNCTION.apply(ec2_.describeImages(request).getImages()); } catch (AmazonClientException exception) { LOG.error(exception.getMessage(), exception); } return images; }
@Override public boolean load(DescribeImagesRequest request) { return load(request, null); }
@Override public boolean load(DescribeImagesRequest request, ResultCapture<DescribeImagesResult> extractor) { return resource.load(request, extractor); }
@Override public ImageCollection getImages() { return getImages((DescribeImagesRequest)null); }
/** * @return valid image or null if missing */ public Image findImage(final String imageId) throws Exception { /** * work around for image entry not being immediately available right * after create/register operation */ for (int index = 0; index < attemptCount; index++) { try { final DescribeImagesRequest request = new DescribeImagesRequest(); request.setImageIds(wrapList(imageId)); final DescribeImagesResult result = amazonClient .describeImages(request); final List<Image> imageList = result.getImages(); switch (imageList.size()) { case 0: logger.info("image find : missing imageId=" + imageId); break; case 1: logger.info("image find : success imageId=" + imageId); return imageList.get(0); default: logger.info("image find : duplicate imageId=" + imageId); break; } } catch (final Exception e) { logger.info("image find : exception imageId={} / {}", // imageId, e.getMessage()); } logger.info("image find : attempt=" + index); sleep(); } logger.error("image find : failure imageId=" + imageId); return null; }