Search in sources :

Example 6 with AmazonCloudFormationClient

use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsResourceConnector method terminate.

@Override
public List<CloudResourceStatus> terminate(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
    LOGGER.info("Deleting stack: {}", ac.getCloudContext().getId());
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    if (resources != null && !resources.isEmpty()) {
        AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
        CloudResource stackResource = getCloudFormationStackResource(resources);
        if (stackResource == null) {
            return Collections.emptyList();
        }
        String cFStackName = stackResource.getName();
        LOGGER.info("Deleting CloudFormation stack for stack: {} [cf stack id: {}]", cFStackName, ac.getCloudContext().getId());
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
        try {
            retryService.testWith2SecDelayMax5Times(() -> {
                try {
                    cfClient.describeStacks(describeStacksRequest);
                } catch (AmazonServiceException e) {
                    if (!e.getErrorMessage().contains(cFStackName + " does not exist")) {
                        throw e;
                    }
                    throw new ActionWentFailException("Stack not exists");
                }
                return Boolean.TRUE;
            });
        } catch (ActionWentFailException ignored) {
            LOGGER.info(String.format("Stack not found with name: %s", cFStackName));
            AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
            releaseReservedIp(amazonEC2Client, resources);
            return Collections.emptyList();
        }
        resumeAutoScalingPolicies(ac, stack);
        DeleteStackRequest deleteStackRequest = new DeleteStackRequest().withStackName(cFStackName);
        cfClient.deleteStack(deleteStackRequest);
        PollTask<Boolean> task = awsPollTaskFactory.newAwsTerminateStackStatusCheckerTask(ac, cfClient, DELETE_COMPLETE, DELETE_FAILED, ERROR_STATUSES, cFStackName);
        try {
            Boolean statePollerResult = task.call();
            if (!task.completed(statePollerResult)) {
                syncPollingScheduler.schedule(task);
            }
        } catch (Exception e) {
            throw new CloudConnectorException(e.getMessage(), e);
        }
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
        releaseReservedIp(amazonEC2Client, resources);
        deleteKeyPair(ac, stack);
    } else if (resources != null) {
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
        releaseReservedIp(amazonEC2Client, resources);
        LOGGER.info("No CloudFormation stack saved for stack.");
    } else {
        LOGGER.info("No resources to release.");
    }
    return check(ac, resources);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 7 with AmazonCloudFormationClient

use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsClient method createCloudFormationClient.

public AmazonCloudFormationClient createCloudFormationClient(AwsCredentialView awsCredential, String regionName) {
    AmazonCloudFormationClient client = isRoleAssumeRequired(awsCredential) ? new AmazonCloudFormationClient(credentialClient.retrieveCachedSessionCredentials(awsCredential)) : new AmazonCloudFormationClient(createAwsCredentials(awsCredential));
    client.setRegion(RegionUtils.getRegion(regionName));
    return client;
}
Also used : AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 8 with AmazonCloudFormationClient

use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsMetadataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
    List<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>();
    try {
        String region = ac.getCloudContext().getLocation().getRegion().value();
        AmazonCloudFormationClient amazonCFClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), region);
        AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), region);
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);
        // contains all instances
        ListMultimap<String, CloudInstance> groupByInstanceGroup = groupByInstanceGroup(vms);
        for (String key : groupByInstanceGroup.keySet()) {
            List<CloudInstance> cloudInstances = groupByInstanceGroup.get(key);
            cloudVmMetaDataStatuses.addAll(collectGroupMetaData(ac, amazonASClient, amazonEC2Client, amazonCFClient, key, cloudInstances));
        }
        return cloudVmMetaDataStatuses;
    } catch (RuntimeException e) {
        throw new CloudConnectorException(e.getMessage(), e);
    }
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 9 with AmazonCloudFormationClient

use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsCreateVpcNetworkTest method createNetwork.

@Test
@Parameters({ "networkName", "description", "publicInAccount", "regionName", "vpcStackName", "vpcName", "existingSubnet" })
public void createNetwork(String networkName, @Optional("") String description, @Optional("false") boolean publicInAccount, String regionName, @Optional("it-vpc-stack") String vpcStackName, @Optional("it-vpc") String vpcName, boolean existingSubnet) {
    AmazonCloudFormationClient client = new AmazonCloudFormationClient();
    client.setRegion(RegionUtils.getRegion(regionName));
    Map<String, Object> networkMap = new HashMap<>();
    String vpcCreationJson = existingSubnet ? "public_vpc_with_subnet.json" : "public_vpc_wihout_subnet.json";
    try (InputStream vpcJsonInputStream = getClass().getResourceAsStream("/cloudformation/" + vpcCreationJson)) {
        String vpcCFTemplateString = IOUtils.toString(vpcJsonInputStream);
        CreateStackRequest stackRequest = createStackRequest(vpcStackName, vpcName, vpcCFTemplateString);
        client.createStack(stackRequest);
        List<Output> outputForRequest = getOutputForRequest(vpcStackName, client);
        if (existingSubnet) {
            networkMap.put("vpcId", outputForRequest.get(0).getOutputValue());
            networkMap.put("subnetId", outputForRequest.get(1).getOutputValue());
        } else {
            networkMap.put("vpcId", outputForRequest.get(1).getOutputValue());
            networkMap.put("internetGatewayId", outputForRequest.get(0).getOutputValue());
        }
    } catch (IOException e) {
        LOGGER.error("can't read vpc cloudformation template file");
        throw new RuntimeException(e);
    }
    NetworkRequest networkRequest = new NetworkRequest();
    networkRequest.setName(networkName);
    networkRequest.setDescription(description);
    networkRequest.setParameters(networkMap);
    if (!existingSubnet) {
        networkRequest.setSubnetCIDR("10.0.0.0/24");
    }
    networkRequest.setCloudPlatform("AWS");
    String id = getCloudbreakClient().networkEndpoint().postPrivate(networkRequest).getId().toString();
    getItContext().putContextParam(CloudbreakITContextConstants.NETWORK_ID, id, true);
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) Output(com.amazonaws.services.cloudformation.model.Output) NetworkRequest(com.sequenceiq.cloudbreak.api.model.NetworkRequest) IOException(java.io.IOException) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) CreateStackRequest(com.amazonaws.services.cloudformation.model.CreateStackRequest) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test)

Aggregations

AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)9 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)6 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)4 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)4 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)3 DeleteStackRequest (com.amazonaws.services.cloudformation.model.DeleteStackRequest)3 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)3 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)2 CreateStackRequest (com.amazonaws.services.cloudformation.model.CreateStackRequest)2 Output (com.amazonaws.services.cloudformation.model.Output)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AmazonAutoScaling (com.amazonaws.services.autoscaling.AmazonAutoScaling)1 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)1 DetachInstancesRequest (com.amazonaws.services.autoscaling.model.DetachInstancesRequest)1 ResumeProcessesRequest (com.amazonaws.services.autoscaling.model.ResumeProcessesRequest)1 SuspendProcessesRequest (com.amazonaws.services.autoscaling.model.SuspendProcessesRequest)1 UpdateAutoScalingGroupRequest (com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest)1