Search in sources :

Example 26 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class CloudFormationStackUtil method getResourceArnByLogicalId.

public String getResourceArnByLogicalId(AuthenticatedContext ac, String logicalId, String region) {
    String cFStackName = getCfStackName(ac);
    AmazonCloudFormationClient amazonCfClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), region);
    DescribeStackResourceResult result = amazonCfClient.describeStackResource(new DescribeStackResourceRequest().withStackName(cFStackName).withLogicalResourceId(logicalId));
    return result.getStackResourceDetail().getPhysicalResourceId();
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) DescribeStackResourceRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourceRequest) DescribeStackResourceResult(com.amazonaws.services.cloudformation.model.DescribeStackResourceResult)

Example 27 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsLoadBalancerLaunchService method updateCloudformationWithLoadBalancers.

public List<CloudResourceStatus> updateCloudformationWithLoadBalancers(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, ModelContext modelContext) {
    List<CloudResourceStatus> statuses = new ArrayList<>();
    List<CloudLoadBalancer> cloudLoadBalancers = stack.getLoadBalancers();
    String cFStackName = cfStackUtil.getCfStackName(ac);
    if (!cloudLoadBalancers.isEmpty()) {
        LOGGER.debug("Creating load balancers of types " + cloudLoadBalancers.stream().map(CloudLoadBalancer::getType).map(LoadBalancerType::name).collect(Collectors.joining(",")));
        AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
        String regionName = ac.getCloudContext().getLocation().getRegion().value();
        AmazonCloudFormationClient cfRetryClient = awsClient.createCloudFormationClient(credentialView, regionName);
        AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
        List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfRetryClient, amazonASClient, stack.getGroups());
        Network network = stack.getNetwork();
        AwsNetworkView awsNetworkView = new AwsNetworkView(network);
        if (modelContext == null) {
            LOGGER.debug("Creating default model context");
            modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
        }
        Map<String, List<String>> instanceIdsByGroupName = instances.stream().collect(Collectors.groupingBy(CloudResource::getGroup, mapping(CloudResource::getInstanceId, toList())));
        List<AwsLoadBalancer> awsLoadBalancers = loadBalancerCommonService.getAwsLoadBalancers(cloudLoadBalancers, instanceIdsByGroupName, awsNetworkView);
        modelContext.withLoadBalancers(awsLoadBalancers);
        LOGGER.debug("Starting CloudFormation update to create load balancer and target groups.");
        ListStackResourcesResult result;
        if (checkForLoadBalancerAndTargetGroupResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
            LOGGER.debug("Load balancer and target group resources already exist, skipping creation");
            result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
        } else {
            result = updateCloudFormationStack(ac, stack, modelContext);
        }
        setLoadBalancerMetadata(awsLoadBalancers, result);
        LOGGER.debug("Starting CloudFormation update to create listeners.");
        if (checkForListenerResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
            LOGGER.debug("Listener resources already exist, skipping creation");
            result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
        } else {
            result = updateCloudFormationStack(ac, stack, modelContext);
        }
        ListStackResourcesResult finalResult = result;
        awsLoadBalancers.forEach(lb -> statuses.add(createLoadBalancerStatus(ac, lb, finalResult)));
    } else {
        LOGGER.debug("No load balancers in stack");
    }
    return statuses;
}
Also used : ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) ArrayList(java.util.ArrayList) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 28 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsRdsTerminateService method initiateCFTemplateDeletion.

private void initiateCFTemplateDeletion(AuthenticatedContext ac, String cFStackName, AwsCredentialView credentialView, String regionName) {
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    boolean exists = retryService.testWith2SecDelayMax15Times(() -> cfStackUtil.isCfStackExists(cfClient, cFStackName));
    if (exists) {
        DeleteStackRequest deleteStackRequest = awsStackRequestHelper.createDeleteStackRequest(cFStackName);
        cfClient.deleteStack(deleteStackRequest);
        LOGGER.debug("CloudFormation stack deletion request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
        Waiter<DescribeStacksRequest> stackDeleteCompleteWaiter = cfClient.waiters().stackDeleteComplete();
        StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
        WaiterParameters<DescribeStacksRequest> describeStacksRequestWaiterParameters = new WaiterParameters<>(describeStacksRequest).withPollingStrategy(getBackoffCancellablePollingStrategy(stackCancellationCheck));
        stackDeleteCompleteWaiter.run(describeStacksRequestWaiterParameters);
    }
}
Also used : DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 29 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsMetadataCollector method collectInstancesForGroup.

public List<Instance> collectInstancesForGroup(AuthenticatedContext ac, AmazonAutoScalingClient amazonASClient, AmazonEc2Client amazonEC2Client, AmazonCloudFormationClient amazonCFClient, String group) {
    LOGGER.debug("Collect aws instances for group: {}", group);
    String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, group);
    List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName);
    DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds);
    DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest);
    return instancesResult.getReservations().stream().flatMap(reservation -> reservation.getInstances().stream()).collect(Collectors.toList());
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) FQDN(com.sequenceiq.cloudbreak.cloud.model.CloudInstance.FQDN) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) EntitlementService(com.sequenceiq.cloudbreak.auth.altus.EntitlementService) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) InstanceStoreMetadata(com.sequenceiq.cloudbreak.cloud.model.InstanceStoreMetadata) Collection(java.util.Collection) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AwsPlatformResources(com.sequenceiq.cloudbreak.cloud.aws.common.AwsPlatformResources) Entry(java.util.Map.Entry) Optional(java.util.Optional) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) AwsLifeCycleMapper(com.sequenceiq.cloudbreak.cloud.aws.common.util.AwsLifeCycleMapper) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) SUBNET_ID(com.sequenceiq.cloudbreak.common.network.NetworkConstants.SUBNET_ID) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Service(org.springframework.stereotype.Service) Subnet(com.amazonaws.services.ec2.model.Subnet) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Instance(com.amazonaws.services.ec2.model.Instance) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) MetadataCollector(com.sequenceiq.cloudbreak.cloud.MetadataCollector) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 30 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsStackValidator method validateStackNameAvailability.

private void validateStackNameAvailability(AuthenticatedContext ac) {
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    String cFStackName = cfStackUtil.getCfStackName(ac);
    try {
        LOGGER.debug("Checking stack name availability. [{}]", cFStackName);
        cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
        throw new CloudConnectorException(String.format("Stack already exists with the given name: %s", cFStackName));
    } catch (AmazonServiceException e) {
        if (e.getErrorMessage().contains(cFStackName + " does not exist")) {
            LOGGER.info("Stack name is available, CF stack not found by name {}", cFStackName);
        } else {
            LOGGER.warn("Exception while checking stack name availability.", e);
        }
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Aggregations

AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)32 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)27 AmazonAutoScalingClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient)15 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)12 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)11 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)11 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)11 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)10 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)9 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)9 AmazonServiceException (com.amazonaws.AmazonServiceException)8 List (java.util.List)8 ArrayList (java.util.ArrayList)7 Inject (javax.inject.Inject)7 DescribeAutoScalingGroupsResult (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult)6 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)6 Group (com.sequenceiq.cloudbreak.cloud.model.Group)6 AdjustmentTypeWithThreshold (com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6