com.amazonaws.services.ec2.model.DescribeImagesRequest Java Examples
The following examples show how to use
com.amazonaws.services.ec2.model.DescribeImagesRequest.
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: AmiProviderImplTest.java From fullstop with Apache License 2.0 | 6 votes |
@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)); }
Example #2
Source File: BaseTest.java From aws-mock with MIT License | 6 votes |
/** * Describe Images. * * @return list of Images */ protected final List<Image> describeImages() { DescribeImagesRequest request = new DescribeImagesRequest(); request.withImageIds("ami-12345678"); DescribeImagesResult result = amazonEC2Client .describeImages(request); List<Image> instanceList = new ArrayList<Image>(); if (result.getImages().size() > 0) { Assert.assertTrue(result.getImages().size() > 0); for (Image reservation : result.getImages()) { instanceList.add(reservation); } } return instanceList; }
Example #3
Source File: AwsStackRequestHelperTest.java From cloudbreak with Apache License 2.0 | 6 votes |
@Test public void testCreateCreateStackRequestForCloudStack() { when(cloudContext.getLocation()).thenReturn(Location.location(Region.region("region"), new AvailabilityZone("az"))); DescribeImagesResult imagesResult = new DescribeImagesResult(); when(amazonEC2Client.describeImages(any(DescribeImagesRequest.class))) .thenReturn(imagesResult.withImages(new com.amazonaws.services.ec2.model.Image())); when(network.getStringParameter(anyString())).thenReturn(""); Collection<Tag> tags = Lists.newArrayList(new Tag().withKey("mytag").withValue("myvalue")); when(awsTaggingService.prepareCloudformationTags(authenticatedContext, cloudStack.getTags())).thenReturn(tags); CreateStackRequest createStackRequest = underTest.createCreateStackRequest(authenticatedContext, cloudStack, "stackName", "subnet", "template"); assertEquals("stackName", createStackRequest.getStackName()); assertEquals("template", createStackRequest.getTemplateBody()); verify(awsTaggingService).prepareCloudformationTags(authenticatedContext, cloudStack.getTags()); assertEquals(tags, createStackRequest.getTags()); }
Example #4
Source File: EncryptedImageCopyService.java From cloudbreak with Apache License 2.0 | 6 votes |
private void deleteImageAndItsSnapshotResources(AmazonEC2Client client, List<CloudResource> resources, String regionName) { Set<CloudResource> encryptedAMIs = filterEncryptedAMIResources(resources); for (CloudResource encryptedImage : encryptedAMIs) { try { DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(encryptedImage.getName()); client.describeImages(describeImagesRequest) .getImages() .stream() .findFirst() .ifPresent(image -> deleteImage(client, encryptedImage, image, regionName)); } catch (Exception e) { String errorMessage = format("Failed to delete image(AMI) [id:'%s'], in region: '%s', detailed message: %s", encryptedImage.getName(), regionName, e.getMessage()); LOGGER.warn(errorMessage, e); if (!e.getMessage().contains(AMI_NOT_FOUND_MSG_CODE) && !e.getMessage().contains(SNAPSHOT_NOT_FOUND_MSG_CODE)) { throw new CloudConnectorException(errorMessage, e); } } } }
Example #5
Source File: EncryptedImageCopyService.java From cloudbreak with Apache License 2.0 | 6 votes |
public Map<String, String> createEncryptedImages(AuthenticatedContext ac, CloudStack cloudStack, PersistenceNotifier resourceNotifier) { String selectedAMIName = cloudStack.getImage().getImageName(); AwsCredentialView awsCredentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonEC2Client client = awsClient.createAccess(awsCredentialView, regionName); Map<String, EncryptedImageConfig> configByGroupName = getEncryptedImageConfigByGroup(cloudStack, selectedAMIName, ac, client, resourceNotifier); Map<String, String> imageIdByGroupName = configByGroupName.entrySet() .stream() .collect(Collectors.toMap(Entry::getKey, e -> e.getValue().getImageId())); if (!imageIdByGroupName.isEmpty()) { Collection<String> imageIds = new HashSet<>(imageIdByGroupName.values()); LOGGER.debug("Start polling the availability of the created encrypted AMIs: '{}'", String.join(",", imageIds)); Waiter<DescribeImagesRequest> imageWaiter = client.waiters().imageAvailable(); DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(imageIds); StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId()); run(imageWaiter, describeImagesRequest, stackCancellationCheck); LOGGER.info("All created encrypted AMIs are available: '{}'", String.join(",", imageIds)); } return imageIdByGroupName; }
Example #6
Source File: AmiProviderImplTest.java From fullstop with Apache License 2.0 | 6 votes |
@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)); }
Example #7
Source File: AmiProviderImplTest.java From fullstop with Apache License 2.0 | 6 votes |
@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)); }
Example #8
Source File: AmiDetailsProviderImpl.java From fullstop with Apache License 2.0 | 6 votes |
@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(); }
Example #9
Source File: ImagesTableProviderTest.java From aws-athena-query-federation with Apache License 2.0 | 6 votes |
@Override protected void setUpRead() { when(mockEc2.describeImages(any(DescribeImagesRequest.class))).thenAnswer((InvocationOnMock invocation) -> { DescribeImagesRequest request = (DescribeImagesRequest) invocation.getArguments()[0]; assertEquals(getIdValue(), request.getImageIds().get(0)); DescribeImagesResult mockResult = mock(DescribeImagesResult.class); List<Image> values = new ArrayList<>(); values.add(makeImage(getIdValue())); values.add(makeImage(getIdValue())); values.add(makeImage("fake-id")); when(mockResult.getImages()).thenReturn(values); return mockResult; }); }
Example #10
Source File: EC2CommunicationTest.java From development with Apache License 2.0 | 6 votes |
@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)); } } }
Example #11
Source File: EC2Communication.java From development with Apache License 2.0 | 6 votes |
/** * 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); }
Example #12
Source File: FetchAmiJob.java From fullstop with Apache License 2.0 | 5 votes |
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(); } }
Example #13
Source File: AMIMapper.java From testgrid with Apache License 2.0 | 5 votes |
/** * This method requests existing AMI details from AWS. * The method only requests the AMIs owned by the accessing AWS account. * @return List of AMIs */ private List<Image> getAMIListFromAWS() { DescribeImagesRequest request = new DescribeImagesRequest(); request.withOwners("self"); DescribeImagesResult result = amazonEC2.describeImages(request); return result.getImages(); }
Example #14
Source File: AWSProvider.java From testgrid with Apache License 2.0 | 5 votes |
/** * This method will retrieve the instance username that is used to log into the instance via ssh. * E.g Ubuntu instance has username ubuntu * CentOS instance has username centos * The username must be present in the ami as a TAG with key USERNAME. * * @param region The aws region where instance is located * @param instanceId ID value for the instance * @return The username extracted from the TAG */ public Optional<String> getInstanceUserName(String region, String instanceId) { final AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.standard() .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString())) .withRegion(region) .build(); List<String> instanceIds = new ArrayList<>(); instanceIds.add(instanceId); DescribeInstancesRequest request = new DescribeInstancesRequest(); request.setInstanceIds(instanceIds); DescribeInstancesResult result = amazonEC2.describeInstances(request); //Get instance id from the results Optional<String> imageId = result.getReservations().stream() .flatMap(reservation -> reservation.getInstances().stream()) .findFirst() .map(Instance::getImageId); if (imageId.isPresent()) { List<String> imageIds = new ArrayList<>(); imageIds.add(imageId.get()); //get ami from the instance ID DescribeImagesRequest imageReq = new DescribeImagesRequest(); imageReq.setImageIds(imageIds); //Get the Tag containing the username from the ami DescribeImagesResult describeImagesResult = amazonEC2.describeImages(imageReq); Optional<String> userName = describeImagesResult.getImages().stream() .flatMap(image -> image.getTags().stream()) .filter(tag -> USERNAME_ENTRY.equals(tag.getKey())) .findFirst() .map(Tag::getValue); return userName; } return Optional.empty(); }
Example #15
Source File: AwsStackRequestHelper.java From cloudbreak with Apache License 2.0 | 5 votes |
private String getRootDeviceName(AuthenticatedContext ac, CloudStack cloudStack) { AmazonEC2Client ec2Client = new AuthenticatedContextView(ac).getAmazonEC2Client(); 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(); }
Example #16
Source File: EC2Impl.java From aws-sdk-java-resources with Apache License 2.0 | 5 votes |
@Override public ImageCollection getImages(DescribeImagesRequest request) { ResourceCollectionImpl result = service.getCollection("Images", request); if (result == null) return null; return new ImageCollectionImpl(result); }
Example #17
Source File: AwsCommonProcess.java From primecloud-controller with GNU General Public License v2.0 | 5 votes |
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); }
Example #18
Source File: ImagesTableProvider.java From aws-athena-query-federation with Apache License 2.0 | 5 votes |
/** * Calls DescribeImagess on the AWS EC2 Client returning all images that match the supplied predicate and attempting * to push down certain predicates (namely queries for specific volumes) to EC2. * * @note Because of the large number of public AMIs we also support using a default 'owner' filter if your query doesn't * filter on owner itself. You can set this using an env variable on your Lambda function defined by DEFAULT_OWNER_ENV. * @See TableProvider */ @Override public void readWithConstraint(BlockSpiller spiller, ReadRecordsRequest recordsRequest, QueryStatusChecker queryStatusChecker) { DescribeImagesRequest request = new DescribeImagesRequest(); ValueSet idConstraint = recordsRequest.getConstraints().getSummary().get("id"); ValueSet ownerConstraint = recordsRequest.getConstraints().getSummary().get("owner"); if (idConstraint != null && idConstraint.isSingleValue()) { request.setImageIds(Collections.singletonList(idConstraint.getSingleValue().toString())); } else if (ownerConstraint != null && ownerConstraint.isSingleValue()) { request.setOwners(Collections.singletonList(ownerConstraint.getSingleValue().toString())); } else if (DEFAULT_OWNER != null) { request.setOwners(Collections.singletonList(DEFAULT_OWNER)); } else { throw new RuntimeException("A default owner account must be set or the query must have owner" + "in the where clause with exactly 1 value otherwise results may be too big."); } DescribeImagesResult response = ec2.describeImages(request); int count = 0; for (Image next : response.getImages()) { if (count++ > MAX_IMAGES) { throw new RuntimeException("Too many images returned, add an owner or id filter."); } instanceToRow(next, spiller); } }
Example #19
Source File: AWSControllerIT.java From development with Apache License 2.0 | 5 votes |
@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()); }
Example #20
Source File: AmiProviderImpl.java From fullstop with Apache License 2.0 | 5 votes |
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(); } }
Example #21
Source File: EC2Mockup.java From development with Apache License 2.0 | 5 votes |
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)); }
Example #22
Source File: ImageImpl.java From aws-sdk-java-resources with Apache License 2.0 | 4 votes |
@Override public boolean load(DescribeImagesRequest request) { return load(request, null); }
Example #23
Source File: ImageImpl.java From aws-sdk-java-resources with Apache License 2.0 | 4 votes |
@Override public boolean load(DescribeImagesRequest request, ResultCapture<DescribeImagesResult> extractor) { return resource.load(request, extractor); }
Example #24
Source File: EC2Impl.java From aws-sdk-java-resources with Apache License 2.0 | 4 votes |
@Override public ImageCollection getImages() { return getImages((DescribeImagesRequest)null); }
Example #25
Source File: EC2Mockup.java From development with Apache License 2.0 | 4 votes |
public void createDescribeImagesException(AmazonClientException ase) { doThrow(ase).when(ec2).describeImages(any(DescribeImagesRequest.class)); }
Example #26
Source File: EC2.java From aws-sdk-java-resources with Apache License 2.0 | 2 votes |
/** * Retrieves the Images collection referenced by this resource. */ ImageCollection getImages(DescribeImagesRequest request);
Example #27
Source File: Image.java From aws-sdk-java-resources with Apache License 2.0 | 2 votes |
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet. * The following request parameters will be populated from the data of this * <code>Image</code> resource, and any conflicting parameter value set in * the request will be overridden: * <ul> * <li> * <b><code>ImageIds.0</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeImagesRequest */ boolean load(DescribeImagesRequest request);
Example #28
Source File: Image.java From aws-sdk-java-resources with Apache License 2.0 | 2 votes |
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet, and use a ResultCapture to retrieve the low-level * client response * The following request parameters will be populated from the data of this * <code>Image</code> resource, and any conflicting parameter value set in * the request will be overridden: * <ul> * <li> * <b><code>ImageIds.0</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeImagesRequest */ boolean load(DescribeImagesRequest request, ResultCapture<DescribeImagesResult> extractor);