private void runSearch(T type) { AmazonWebServiceRequest descRequest = buildRequest(type); AsyncHandler describeHandler = buildHandler(type); if (type instanceof Instance) { this.amazonEC2Client.describeInstancesAsync( (DescribeInstancesRequest) descRequest, describeHandler); } else if (type instanceof NatGateway) { this.amazonEC2Client.describeNatGatewaysAsync( (DescribeNatGatewaysRequest) descRequest, describeHandler); } else if (type instanceof Volume) { this.amazonEC2Client.describeVolumesAsync( (DescribeVolumesRequest) descRequest, describeHandler); } else { AWSTaskStatusChecker.this.taskManager.patchTaskToFailure( new IllegalArgumentException("Invalid type " + type)); } }
@Override public void onSuccess(DescribeVolumesRequest request, DescribeVolumesResult result) { OperationContext.restoreOperationContext(this.opContext); result.getVolumes() .forEach(volume -> { this.context.remoteAWSVolumes.put(volume.getVolumeId(), volume); this.context.remoteAWSVolumeIds.add(volume.getVolumeId()); }); this.service.logFine(() -> String.format("Successfully enumerated %d volumes on the AWS" + " host", result.getVolumes().size())); // Save the reference to the next token that will be used to retrieve the next page of // results from AWS. this.context.nextToken = result.getNextToken(); if (this.context.remoteAWSVolumes.size() == 0) { if (this.context.nextToken != null) { this.context.subStage = EBSVolumesEnumerationSubStage.GET_NEXT_PAGE; } else { this.context.subStage = EBSVolumesEnumerationSubStage.DELETE_DISKS; } } handleReceivedEnumerationData(); }
/** * Creates a volume and return the volume id. */ public static String createVolume(VerificationHost host, AmazonEC2Client client) { CreateVolumeRequest req = new CreateVolumeRequest() .withAvailabilityZone(zoneId + avalabilityZoneIdentifier) .withSize(1); CreateVolumeResult res = client.createVolume(req); String volumeId = res.getVolume().getVolumeId(); Filter filter = new Filter().withName(VOLUME_ID_ATTRIBUTE).withValues(volumeId); DescribeVolumesRequest volumesRequest = new DescribeVolumesRequest() .withVolumeIds(volumeId) .withFilters(filter); host.waitFor("Timeout waiting for creating volume", () -> { DescribeVolumesResult volumesResult = client.describeVolumes(volumesRequest); String state = volumesResult.getVolumes().get(0).getState(); if (state.equalsIgnoreCase(VOLUME_STATUS_AVAILABLE)) { return true; } return false; }); return volumeId; }
/** * Method to get Disk details directly from Amazon */ public static List<Volume> getAwsDisksByIds(AmazonEC2AsyncClient client, VerificationHost host, List<String> diskIds) throws Throwable { try { host.log("Getting disks with ids " + diskIds + " from the AWS endpoint using the EC2 client."); DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest() .withVolumeIds(diskIds); DescribeVolumesResult describeVolumesResult = client .describeVolumes(describeVolumesRequest); return describeVolumesResult.getVolumes(); } catch (Exception e) { if (e instanceof AmazonEC2Exception && ((AmazonEC2Exception) e).getErrorCode() .equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) { return null; } } return new ArrayList<>(); }
protected Volume getVolume(AmazonEC2AsyncClient client, Instance awsInstance, String deviceName) { InstanceBlockDeviceMapping bootDiskMapping = awsInstance.getBlockDeviceMappings().stream() .filter(blockDeviceMapping -> blockDeviceMapping.getDeviceName().equals(deviceName)) .findAny() .orElse(null); //The ami used in this test is an ebs-backed AMI assertNotNull("Device type should be ebs type", bootDiskMapping.getEbs()); String bootVolumeId = bootDiskMapping.getEbs().getVolumeId(); DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest() .withVolumeIds(bootVolumeId); DescribeVolumesResult describeVolumesResult = client .describeVolumes(describeVolumesRequest); return describeVolumesResult.getVolumes().get(0); }
/** * Get EBS volumes attached to the specified virtual instance id. * * @return list of ebs volumes */ @VisibleForTesting List<Volume> getVolumes(String virtualInstanceId) { String ec2InstanceId = getOnlyElement( getEC2InstanceIdsByVirtualInstanceId( Collections.singletonList(virtualInstanceId) ).values() ); InstanceAttribute instanceAttribute = describeInstanceAttribute(ec2InstanceId, InstanceAttributeName.BlockDeviceMapping); List<InstanceBlockDeviceMapping> blockDeviceMappings = instanceAttribute.getBlockDeviceMappings(); List<String> volumeIds = Lists.newArrayList(); for (InstanceBlockDeviceMapping mapping : blockDeviceMappings) { volumeIds.add(mapping.getEbs().getVolumeId()); } DescribeVolumesResult volumeResults = client.describeVolumes( new DescribeVolumesRequest().withVolumeIds(volumeIds) ); return volumeResults.getVolumes(); }
public Volume describeVolume(AwsProcessClient awsProcessClient, String volumeId) { // 単一ボリュームの参照 DescribeVolumesRequest request = new DescribeVolumesRequest(); request.withVolumeIds(volumeId); DescribeVolumesResult result = awsProcessClient.getEc2Client().describeVolumes(request); List<Volume> volumes = result.getVolumes(); // API実行結果チェック if (volumes.size() == 0) { // ボリュームが存在しない場合 throw new AutoException("EPROCESS-000110", volumeId); } else if (volumes.size() > 1) { // ボリュームを複数参照できた場合 AutoException exception = new AutoException("EPROCESS-000111", volumeId); exception.addDetailInfo("result=" + volumes); throw exception; } return volumes.get(0); }
@Override public List<AbstractResource<?>> describeVolumes(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeVolumesRequest req = new DescribeVolumesRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing volumes for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeVolumesResult res = ec2.describeVolumes(req); List<Boolean> autoEnableIOs = new ArrayList<>(); List<List<ProductCode>> productCodes = new ArrayList<>(); for (Volume volume : res.getVolumes()) { autoEnableIOs.add(findAutoEnableIO(account, region, volume.getVolumeId())); productCodes.add(findProductCodes(account, region, volume.getVolumeId())); } return converter.toEc2Volumes(res.getVolumes(), autoEnableIOs, productCodes, account.getId(), region, dt); }
/** * Describe All Volume. * * @return Collection Volume */ protected final List<Volume> getVolumes() { List<Volume> volumes = null; DescribeVolumesRequest req = new DescribeVolumesRequest(); req.setMaxResults(20); DescribeVolumesResult result = amazonEC2Client.describeVolumes(req); if (result != null && !result.getVolumes().isEmpty()) { volumes = result.getVolumes(); log.info("Page Size : " + volumes.size()); } while(result.getNextToken() != null) { req.setNextToken(result.getNextToken()); result = amazonEC2Client.describeVolumes(req); if (result != null && !result.getVolumes().isEmpty()) { volumes = result.getVolumes(); log.info("Page Size : " + volumes.size()); } } return volumes; }
List<Volume> describeBackupVolumes(AmazonEC2Async client, TagNameRequest target) { Filter tagKey = new Filter().withName("tag-key").withValues("Backup"); Filter tagValue = new Filter().withName("tag-value").withValues(target.getTagName()); DescribeVolumesRequest req = new DescribeVolumesRequest().withFilters(tagKey, tagValue); DescribeVolumesResult result = client.describeVolumes(req); return result.getVolumes(); }
/** * Initializes and saves a reference to the request object that is sent to AWS to get a page of * volumes. Also saves an instance to the async handler that will be used to handle the * responses received from AWS. It sets the nextToken value in the request object sent to AWS * for getting the next page of results from AWS. */ private void createAWSRequestAndAsyncHandler(EBSStorageEnumerationContext aws) { DescribeVolumesRequest request = new DescribeVolumesRequest(); request.setMaxResults(getQueryPageSize()); request.setNextToken(aws.nextToken); aws.describeVolumesRequest = request; AsyncHandler<DescribeVolumesRequest, DescribeVolumesResult> resultHandler = new AWSStorageEnumerationAsyncHandler( this, aws); aws.resultHandler = resultHandler; }
private Volume getVolume(String snap) { DescribeVolumesResult volumes = ec2Client .describeVolumes(new DescribeVolumesRequest() .withFilters(new Filter().withName("snapshot-id") .withValues(snap))); return volumes.getVolumes().stream().findFirst().get(); }
/** * Checks whether volume is created. * @param volumeId the EBS volume ID * @return true if volume created, false otherwise */ private boolean volumeCreated(String volumeId) { DescribeVolumesRequest dvs = new DescribeVolumesRequest(); ArrayList<String> volumeIds = new ArrayList<String>(); volumeIds.add(volumeId); dvs.setVolumeIds(volumeIds); DescribeVolumesResult dvsresult = null; try { dvsresult = this.ec2Api.describeVolumes(dvs); } catch(Exception e) { dvsresult = null; } return dvsresult != null && "available".equals(dvsresult.getVolumes().get(0).getState()); }
@Override public VolumeCollection getVolumes(DescribeVolumesRequest request) { ResourceCollectionImpl result = resource.getCollection("Volumes", request); if (result == null) return null; return new VolumeCollectionImpl(result); }
@Override public VolumeCollection getVolumes(DescribeVolumesRequest request) { ResourceCollectionImpl result = service.getCollection("Volumes", request); if (result == null) return null; return new VolumeCollectionImpl(result); }
@Override protected Boolean doCall() throws Exception { LOGGER.info("Checking if AWS EBS volume '{}' is created.", volumeId); DescribeVolumesResult describeVolumesResult = amazonEC2Client.describeVolumes(new DescribeVolumesRequest().withVolumeIds(volumeId)); Optional<Volume> volume = describeVolumesResult.getVolumes().stream().findFirst(); return volume.isPresent() ? "available".equals(volume.get().getState()) : false; }
/** * Describe Volume. * * @return Volume */ protected final Volume getVolume() { Volume volume = null; DescribeVolumesRequest req = new DescribeVolumesRequest(); DescribeVolumesResult result = amazonEC2Client.describeVolumes(req); if (result != null && !result.getVolumes().isEmpty()) { volume = result.getVolumes().get(0); } return volume; }
@Override public DescribeVolumesResult describeVolumes(DescribeVolumesRequest describeVolumesRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
/** * Wait for the specified list of volumes to be attached within a timeout. * * @param volumeIds collection of volume ids to wait for * @return collection of volumes that were successfully attached * @throws InterruptedException if the operation is interrupted */ private Collection<String> waitUntilVolumesAttached(Collection<String> volumeIds) throws InterruptedException { checkNotNull(volumeIds); if (volumeIds.isEmpty()) { LOG.info("No volumes are being attached, skipping wait"); return Collections.emptySet(); } Set<String> unattachedVolumes = Sets.newHashSet(volumeIds); Set<String> attachedVolumes = Sets.newHashSet(); LOG.info("Waiting for a maximum of {} seconds for volumes to be attached", attachTimeoutSeconds); Stopwatch watch = Stopwatch.createStarted(); while (watch.elapsed(TimeUnit.SECONDS) < attachTimeoutSeconds) { DescribeVolumesRequest volumeRequest = new DescribeVolumesRequest().withVolumeIds(unattachedVolumes); List<Volume> volumes = client.describeVolumes(volumeRequest).getVolumes(); for (Volume volume : volumes) { VolumeAttachment attachment = Iterables.getOnlyElement(volume.getAttachments()); VolumeAttachmentState state = VolumeAttachmentState.fromValue(attachment.getState()); if (state == VolumeAttachmentState.Attached) { unattachedVolumes.remove(volume.getVolumeId()); attachedVolumes.add(volume.getVolumeId()); } } if (unattachedVolumes.isEmpty()) { return attachedVolumes; } LOG.info("Waiting on {} out of {} volumes to be attached, next check in {} seconds", unattachedVolumes.size(), volumeIds.size(), WAIT_UNTIL_ATTACHED_INTERVAL_SECONDS); TimeUnit.SECONDS.sleep(WAIT_UNTIL_ATTACHED_INTERVAL_SECONDS); } LOG.error("Timed out while waiting for all volumes to be attached, {} out of {} volumes were attached", attachedVolumes.size(), volumeIds.size()); return attachedVolumes; }
@Test public void itShouldDeleteTheStorageArtifacts() throws ServiceInstanceBindingExistsException, ServiceBrokerException { when(ec2Client.describeSnapshots()) .thenReturn( new DescribeSnapshotsResult().withSnapshots(Arrays.asList( new Snapshot() .withDescription("Created by CreateImage(source_instance) for ami-9687d2fe from vol-e7526fac"), new Snapshot() .withDescription( "Created by CreateImage(source_instance) for test_image from vol-e7526fac") .withSnapshotId("test_snapshot"), new Snapshot() .withDescription("Created by CreateImage(i-bf72d345) for ami-xx from vol-e7526fac"), new Snapshot()))); Predicate<DescribeVolumesRequest> pred = new Predicate<DescribeVolumesRequest>() { @Override public boolean test(DescribeVolumesRequest r) { Filter filter = r.getFilters().stream().findFirst().get(); return filter.getName().equals("snapshot-id") && filter.getValues().stream().findFirst().get() .equals("test_snapshot"); } }; DescribeVolumesResult volumesResult = new DescribeVolumesResult() .withVolumes(Collections.singletonList(new Volume() .withVolumeId("test_volume"))); when(ec2Client.describeVolumes(awsRqst(pred))) .thenReturn(volumesResult); aws.deleteStorageArtifacts("test_image"); verify(ec2Client).deleteSnapshot( awsRqst(r -> r.getSnapshotId().equals("test_snapshot"))); verify(ec2Client).deleteVolume( awsRqst(r -> r.getVolumeId().equals("test_volume"))); }
@Override public boolean load(DescribeVolumesRequest request) { return load(request, null); }
@Override public boolean load(DescribeVolumesRequest request, ResultCapture<DescribeVolumesResult> extractor) { return resource.load(request, extractor); }
@Override public VolumeCollection getVolumes() { return getVolumes((DescribeVolumesRequest)null); }
@Test public void testRunInstancesWithBlockDevices() throws Exception { // TODO: maybe we should also test for spot instances BlockDevice blockDevice = mock(BlockDevice.class); when(blockDevice.getSize()).thenReturn(8); // TODO: understand why it doesn't work for smaller volumes when(blockDevice.getName()).thenReturn("/dev/sda1"); BlockDevice blockDevice2 = mock(BlockDevice.class); when(blockDevice2.getSize()).thenReturn(16); when(blockDevice2.getName()).thenReturn("/dev/sda4"); when(hardware.getBlockDevices()).thenReturn(Lists.newArrayList(blockDevice, blockDevice2)); activity.execute(execution); Uninterruptibles.sleepUninterruptibly(30, TimeUnit.SECONDS); @SuppressWarnings("unchecked") List<String> instanceIds = (List<String>) collector.getVariable(ProcessVariables.INSTANCE_IDS); DescribeInstancesResult result = client.describeInstances(new DescribeInstancesRequest() .withInstanceIds(instanceIds)); Instance instance = result.getReservations().get(0).getInstances().get(0); List<InstanceBlockDeviceMapping> bdm = instance.getBlockDeviceMappings(); assertThat(bdm).hasSize(2); List<String> volumeIds = Lists.newArrayList(); for (int i = 0; i < bdm.size(); i++) { assertThat(bdm.get(i).getDeviceName()).isEqualTo("/dev/sda" + ((i + 1) * (i + 1))); assertThat(bdm.get(i).getEbs().getDeleteOnTermination()).isTrue(); volumeIds.add(bdm.get(i).getEbs().getVolumeId()); } DescribeVolumesResult volumesResult = client.describeVolumes( new DescribeVolumesRequest().withVolumeIds(volumeIds)); for (Volume volume : volumesResult.getVolumes()) { assertThat(volume.getState()).isIn(Lists.newArrayList("creating", "available", "in-use")); } assertThat(volumesResult.getVolumes().get(0).getSize()) .isNotEqualTo(volumesResult.getVolumes().get(1).getSize()); }
/** * Retrieves the Volumes collection referenced by this resource. */ VolumeCollection getVolumes(DescribeVolumesRequest request);
/** * 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>Volume</code> resource, and any conflicting parameter value set in * the request will be overridden: * <ul> * <li> * <b><code>VolumeIds.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 DescribeVolumesRequest */ boolean load(DescribeVolumesRequest request);
/** * 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>Volume</code> resource, and any conflicting parameter value set in * the request will be overridden: * <ul> * <li> * <b><code>VolumeIds.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 DescribeVolumesRequest */ boolean load(DescribeVolumesRequest request, ResultCapture<DescribeVolumesResult> extractor);