Search in sources :

Example 31 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project cloudbreak by hortonworks.

the class AwsResourceConnector method associateElasticIpToInstance.

private void associateElasticIpToInstance(AmazonEC2 amazonEC2Client, String eipAllocationId, String instanceId) {
    AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest().withAllocationId(eipAllocationId).withInstanceId(instanceId);
    amazonEC2Client.associateAddress(associateAddressRequest);
}
Also used : AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest)

Example 32 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project cloudbreak by hortonworks.

the class AwsResourceConnector method isMapPublicOnLaunch.

private boolean isMapPublicOnLaunch(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
    boolean mapPublicIpOnLaunch = true;
    if (awsNetworkView.isExistingVPC() && awsNetworkView.isExistingSubnet()) {
        DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
        describeSubnetsRequest.setSubnetIds(awsNetworkView.getSubnetList());
        DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(describeSubnetsRequest);
        if (!describeSubnetsResult.getSubnets().isEmpty()) {
            mapPublicIpOnLaunch = describeSubnetsResult.getSubnets().get(0).isMapPublicIpOnLaunch();
        }
    }
    return mapPublicIpOnLaunch;
}
Also used : DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest)

Example 33 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project cloudbreak by hortonworks.

the class AwsSetup method validateExistingSubnet.

private void validateExistingSubnet(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
    if (awsNetworkView.isExistingSubnet()) {
        DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
        describeSubnetsRequest.withSubnetIds(awsNetworkView.getSubnetList());
        DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(describeSubnetsRequest);
        if (describeSubnetsResult.getSubnets().size() < awsNetworkView.getSubnetList().size()) {
            throw new CloudConnectorException(String.format(SUBNET_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingSubnet()));
        } else {
            for (Subnet subnet : describeSubnetsResult.getSubnets()) {
                String vpcId = subnet.getVpcId();
                if (vpcId != null && !vpcId.equals(awsNetworkView.getExistingVPC())) {
                    throw new CloudConnectorException(String.format(SUBNETVPC_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingSubnet(), awsNetworkView.getExistingVPC()));
                }
            }
        }
    }
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Subnet(com.amazonaws.services.ec2.model.Subnet) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest)

Example 34 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project cloudbreak by hortonworks.

the class AwsSetup method validateExistingIGW.

private void validateExistingIGW(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
    if (awsNetworkView.isExistingIGW()) {
        DescribeInternetGatewaysRequest describeInternetGatewaysRequest = new DescribeInternetGatewaysRequest();
        describeInternetGatewaysRequest.withInternetGatewayIds(awsNetworkView.getExistingIGW());
        DescribeInternetGatewaysResult describeInternetGatewaysResult = amazonEC2Client.describeInternetGateways(describeInternetGatewaysRequest);
        if (describeInternetGatewaysResult.getInternetGateways().size() < 1) {
            throw new CloudConnectorException(String.format(IGW_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingIGW()));
        } else {
            InternetGateway internetGateway = describeInternetGatewaysResult.getInternetGateways().get(0);
            InternetGatewayAttachment attachment = internetGateway.getAttachments().get(0);
            if (attachment != null && !attachment.getVpcId().equals(awsNetworkView.getExistingVPC())) {
                throw new CloudConnectorException(String.format(IGWVPC_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingIGW(), awsNetworkView.getExistingVPC()));
            }
        }
    }
}
Also used : DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest)

Example 35 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project cloudbreak by hortonworks.

the class RecoveryUtil method deleteAWSInstance.

public static void deleteAWSInstance(Regions region, String instanceId) {
    List<String> idList = new ArrayList<>();
    idList.add(instanceId);
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withRegion(region).build();
    TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(idList);
    ec2.terminateInstances(terminateInstancesRequest);
    LOGGER.info("Instance was deleted with id:" + instanceId);
}
Also used : ArrayList(java.util.ArrayList) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)65 Filter (com.amazonaws.services.ec2.model.Filter)25 Instance (com.amazonaws.services.ec2.model.Instance)20 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)17 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 Reservation (com.amazonaws.services.ec2.model.Reservation)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)9 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)8 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)8 Tag (com.amazonaws.services.ec2.model.Tag)8 AWSCredentials (com.amazonaws.auth.AWSCredentials)6 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)6 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)5 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)5 ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4