@Override public InstanceAttribute getInstanceAttribute(String instanceId, String attributeName) { OperationStats ops = new OperationStats("es2InstanceStore", "getInstanceAttribute"); try { awsRateLimiter.acquire(); DescribeInstanceAttributeRequest request = new DescribeInstanceAttributeRequest() .withInstanceId(instanceId) .withAttribute(InstanceAttributeName.fromValue(attributeName)) .withSdkRequestTimeout(300 * 1000).withSdkClientExecutionTimeout(600 * 1000); DescribeInstanceAttributeResult result = defaultClient.describeInstanceAttribute(request); while (result != null) { ops.succeed(); return result.getInstanceAttribute(); } } catch (Exception ex) { ops.failed(); throw ex; } return null; }
/** * 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(); }
private void fillMissingProperties(Instance instance) { try { InstanceAttribute instanceAttribute = describeInstanceAttribute( instance.getInstanceId(), InstanceAttributeName.SriovNetSupport); String sriovNetSupport = instanceAttribute.getSriovNetSupport(); instance.setSriovNetSupport(sriovNetSupport); } catch (AmazonServiceException e) { // In practice, users may not have appropriate IAM permission for // DescribeInstanceAttribute. We need to be more forgiving in those cases, // and simply leave a warning in the log here. LOG.warn("Could not fill missing properties. Failed to perform " + "DescribeInstanceAttribute action.", e); } }
/** * Retrieves an instance attribute. * * @param instanceId instance ID * @param attribute name of attribute to describe * @return attribute */ @VisibleForTesting InstanceAttribute describeInstanceAttribute(String instanceId, InstanceAttributeName attribute) { DescribeInstanceAttributeRequest request = new DescribeInstanceAttributeRequest() .withInstanceId(instanceId) .withAttribute(attribute); return client.describeInstanceAttribute(request).getInstanceAttribute(); }
/** * Adds the security groups to an EC2 instance. * * @param ec2InstanceId the ec2 instance id. * @param securityGroups security groups to be added. * @param awsParams awsParamsDto object * * @return updated security groups. */ @Override public List<String> addSecurityGroupsToEc2Instance(String ec2InstanceId, List<String> securityGroups, AwsParamsDto awsParams) { Set<String> updatedSecurityGroups = new HashSet<>(); for (String securityGroup : securityGroups) { updatedSecurityGroups.add(securityGroup); } // Get existing security groups DescribeInstanceAttributeRequest describeInstanceAttributeRequest = new DescribeInstanceAttributeRequest().withInstanceId(ec2InstanceId).withAttribute(InstanceAttributeName.GroupSet); DescribeInstanceAttributeResult describeInstanceAttributeResult = ec2Operations.describeInstanceAttribute(getEc2Client(awsParams), describeInstanceAttributeRequest); List<GroupIdentifier> groups = describeInstanceAttributeResult.getInstanceAttribute().getGroups(); for (GroupIdentifier groupIdentifier : groups) { updatedSecurityGroups.add(groupIdentifier.getGroupId()); } // Add security group on master EC2 instance ModifyInstanceAttributeRequest modifyInstanceAttributeRequest = new ModifyInstanceAttributeRequest().withInstanceId(ec2InstanceId).withGroups(updatedSecurityGroups); ec2Operations.modifyInstanceAttribute(getEc2Client(awsParams), modifyInstanceAttributeRequest); return new ArrayList<>(updatedSecurityGroups); }
/** * Adds a delete on termination flag to all volumes in an {@code InstanceEbsVolumes} list * that have the ATTACHED status. This makes sure that the volumes associated with the * instance will be automatically cleaned up upon instance termination. * * @param instanceEbsVolumesList list of instances along with their associated volumes */ public void addDeleteOnTerminationFlag(List<InstanceEbsVolumes> instanceEbsVolumesList) { Set<String> volumesToFlag = getAllVolumeIdsWithStatus(instanceEbsVolumesList, InstanceEbsVolumes.Status.ATTACHED); if (!volumesToFlag.isEmpty()) { for (InstanceEbsVolumes instanceEbsVolumes : instanceEbsVolumesList) { String ec2InstanceId = instanceEbsVolumes.getEc2InstanceId(); DescribeInstanceAttributeRequest instanceAttributeRequest = new DescribeInstanceAttributeRequest() .withAttribute(InstanceAttributeName.BlockDeviceMapping) .withInstanceId(ec2InstanceId); List<InstanceBlockDeviceMapping> blockDeviceMappings = client.describeInstanceAttribute(instanceAttributeRequest) .getInstanceAttribute() .getBlockDeviceMappings(); for (InstanceBlockDeviceMapping blockDeviceMapping : blockDeviceMappings) { String volumeId = blockDeviceMapping.getEbs().getVolumeId(); // The block device mapping may have volumes associated with it that were not // provisioned by us. We skip marking those volumes for deletion. if (!volumesToFlag.contains(volumeId)) { continue; } InstanceBlockDeviceMappingSpecification updatedSpec = new InstanceBlockDeviceMappingSpecification() .withEbs( new EbsInstanceBlockDeviceSpecification() .withDeleteOnTermination(true) .withVolumeId(volumeId) ) .withDeviceName(blockDeviceMapping.getDeviceName()); ModifyInstanceAttributeRequest modifyRequest = new ModifyInstanceAttributeRequest() .withBlockDeviceMappings(updatedSpec) .withInstanceId(ec2InstanceId); client.modifyInstanceAttribute(modifyRequest); } } } }