com.amazonaws.services.ec2.model.ModifyInstanceAttributeRequest Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.ModifyInstanceAttributeRequest. 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: PublicAccessAutoFix.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Apply security groups to ec 2.
 *
 * @param amazonEC2 the amazon EC 2
 * @param sgIdToBeAttached the sg id to be attached
 * @param resourceId the resource id
 * @return true, if successful
 * @throws Exception the exception
 */
public static boolean applySecurityGroupsToEc2(AmazonEC2 amazonEC2, Set<String> sgIdToBeAttached, String resourceId) throws Exception {
    boolean applysgFlg = false;
    try {
    	ModifyInstanceAttributeRequest modifyInstanceAttributeRequest = new ModifyInstanceAttributeRequest();
    	modifyInstanceAttributeRequest.setInstanceId(resourceId);
    	modifyInstanceAttributeRequest.setGroups(sgIdToBeAttached);
    	amazonEC2.modifyInstanceAttribute(modifyInstanceAttributeRequest);
        applysgFlg = true;
    } catch (Exception e) {
        logger.error("Apply Security Group operation failed for ec2 {}" , resourceId );
     throw new Exception(e);
    }
    return applysgFlg;
}
 
Example #2
Source File: MockEc2OperationsImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
@Override
public void modifyInstanceAttribute(AmazonEC2Client ec2Client, ModifyInstanceAttributeRequest modifyInstanceAttributeRequest)
{
    if (modifyInstanceAttributeRequest.getGroups() != null &&
        modifyInstanceAttributeRequest.getGroups().get(0).equals(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION))
    {
        throw new AmazonServiceException(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION);
    }

    if (modifyInstanceAttributeRequest.getGroups() != null &&
        modifyInstanceAttributeRequest.getGroups().get(0).equals(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION))
    {
        AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception");
        throttlingException.setErrorCode("ThrottlingException");

        throw throttlingException;
    }

}
 
Example #3
Source File: AwsVolumeResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void turnOnDeleteOnterminationOnAttachedVolumes(CloudResource resource, List<CloudResourceStatus> cloudResourceStatuses,
        AmazonEc2RetryClient client) {
    List<InstanceBlockDeviceMappingSpecification> deviceMappingSpecifications = cloudResourceStatuses.stream()
            .filter(cloudResourceStatus -> ResourceStatus.ATTACHED.equals(cloudResourceStatus.getStatus()))
            .map(CloudResourceStatus::getCloudResource)
            .map(cloudResource -> cloudResource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class))
            .map(VolumeSetAttributes::getVolumes)
            .flatMap(List::stream)
            .map(toInstanceBlockDeviceMappingSpecification)
            .collect(Collectors.toList());
    ModifyInstanceAttributeRequest modifyInstanceAttributeRequest = new ModifyInstanceAttributeRequest()
            .withInstanceId(resource.getInstanceId())
            .withBlockDeviceMappings(deviceMappingSpecifications);

    ModifyInstanceAttributeResult modifyIdentityIdFormatResult = client.modifyInstanceAttribute(modifyInstanceAttributeRequest);
    LOGGER.debug("Delete on termination set to ture. {}", modifyIdentityIdFormatResult);
}
 
Example #4
Source File: Ec2DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public void modifyAttribute(String attribute, ResultCapture<Void> extractor)
        {

    ModifyInstanceAttributeRequest request = new
            ModifyInstanceAttributeRequest()

        .withAttribute(attribute);
    modifyAttribute(request, extractor);
}
 
Example #6
Source File: Ec2OperationsImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Modify EC2 instance attributes
 */
@Override
public void modifyInstanceAttribute(AmazonEC2Client ec2Client, ModifyInstanceAttributeRequest modifyInstanceAttributeRequest)
{
    ec2Client.modifyInstanceAttribute(modifyInstanceAttributeRequest);
}
 
Example #7
Source File: Ec2MachineConfigurator.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
/**
 * Attaches volume(s) for EBS.
 * @return true if successful attachment, or nothing to do. false otherwise
 */
private boolean attachVolumes() {

	// If volume is found in map, it has been successfully created (no need to check here)
	for( Map.Entry<String,String> entry : this.storageIdToVolumeId.entrySet()) {

		String volumeId = entry.getValue();
		String storageId = entry.getKey();

		// Give a name to the volume before attaching
		String nameTemplate = Ec2IaasHandler.findStorageProperty(this.targetProperties, storageId, VOLUME_NAME_PREFIX);
		String name = Ec2IaasHandler.expandVolumeName(
				nameTemplate, this.applicationName, this.scopedInstance.getName());
		if(Utils.isEmptyOrWhitespaces(name)) name = "Created by Roboconf for " + this.tagName;
		tagResource(volumeId, name);

		// Attach volume now
		String mountPoint = Ec2IaasHandler.findStorageProperty(this.targetProperties, storageId, VOLUME_MOUNT_POINT_PREFIX);
		if(Utils.isEmptyOrWhitespaces(mountPoint)) mountPoint = "/dev/sdf";

		AttachVolumeRequest attachRequest = new AttachVolumeRequest()
			.withInstanceId(this.machineId)
			.withDevice(mountPoint)
			.withVolumeId(volumeId);

		try {
			this.ec2Api.attachVolume(attachRequest);
		} catch(Exception e) {
			this.logger.warning("EBS Volume attachment error: " + e);
		}

		// Set deleteOnTermination flag ?
		if(Boolean.parseBoolean(Ec2IaasHandler.findStorageProperty(this.targetProperties, storageId, VOLUME_DELETE_OT_PREFIX))) {
			EbsInstanceBlockDeviceSpecification ebsSpecification = new EbsInstanceBlockDeviceSpecification()
				.withVolumeId(volumeId)
				.withDeleteOnTermination(true);

			InstanceBlockDeviceMappingSpecification mappingSpecification = new InstanceBlockDeviceMappingSpecification()
				.withDeviceName(mountPoint)
				.withEbs(ebsSpecification);

			ModifyInstanceAttributeRequest request = new ModifyInstanceAttributeRequest()
				.withInstanceId(this.machineId)
				.withBlockDeviceMappings(mappingSpecification);

			this.ec2Api.modifyInstanceAttribute(request);
		}
	}

	return true;
}
 
Example #8
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public void modifyAttribute(ModifyInstanceAttributeRequest request) {
    modifyAttribute(request, null);
}
 
Example #9
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public void modifyAttribute(ModifyInstanceAttributeRequest request,
        ResultCapture<Void> extractor) {

    resource.performAction("ModifyAttribute", request, extractor);
}
 
Example #10
Source File: AmazonEc2RetryClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public ModifyInstanceAttributeResult modifyInstanceAttribute(ModifyInstanceAttributeRequest request) {
    return retry.testWith2SecDelayMax15Times(() -> mapThrottlingError(() -> client.modifyInstanceAttribute(request)));
}
 
Example #11
Source File: Ec2Operations.java    From herd with Apache License 2.0 2 votes vote down vote up
/**
 * {@link AmazonEC2#modifyInstanceAttribute(ModifyInstanceAttributeRequest)}
 * 
 * @param ec2Client {@link AmazonEC2} to use.
 * @param modifyInstanceAttributeRequest The request object.
 */
public void modifyInstanceAttribute(AmazonEC2Client ec2Client, ModifyInstanceAttributeRequest modifyInstanceAttributeRequest);
 
Example #12
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>ModifyAttribute</code> action.
 *
 * <p>
 * The following request parameters will be populated from the data of this
 * <code>Instance</code> resource, and any conflicting parameter value set
 * in the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>InstanceId</code></b>
 *         - mapped from the <code>Id</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @see ModifyInstanceAttributeRequest
 */
void modifyAttribute(ModifyInstanceAttributeRequest request);
 
Example #13
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>ModifyAttribute</code> action and use a ResultCapture
 * to retrieve the low-level client response.
 *
 * <p>
 * The following request parameters will be populated from the data of this
 * <code>Instance</code> resource, and any conflicting parameter value set
 * in the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>InstanceId</code></b>
 *         - mapped from the <code>Id</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @see ModifyInstanceAttributeRequest
 */
void modifyAttribute(ModifyInstanceAttributeRequest request,
        ResultCapture<Void> extractor);