Search in sources :

Example 1 with AwsCloudStackView

use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView in project cloudbreak by hortonworks.

the class AwsNativeLoadBalancerLaunchService method launchLoadBalancerResources.

public List<CloudResourceStatus> launchLoadBalancerResources(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier persistenceNotifier, AmazonElasticLoadBalancingClient loadBalancingClient, boolean registerTargetGroups) {
    LOGGER.debug("Creating AWS load balancer and it's resources for cloud stack: '{}'", authenticatedContext.getCloudContext().getCrn());
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    Map<String, List<String>> privateIdsByGroupName = stack.getGroups().stream().collect(Collectors.toMap(Group::getName, group -> group.getInstances().stream().map(in -> String.valueOf(in.getTemplate().getPrivateId())).collect(toList())));
    List<AwsLoadBalancer> loadBalancers = loadBalancerCommonService.getAwsLoadBalancers(stack.getLoadBalancers(), privateIdsByGroupName, awsNetworkView);
    AwsCloudStackView awsCloudStackView = new AwsCloudStackView(stack);
    Long stackId = authenticatedContext.getCloudContext().getId();
    String stackName = authenticatedContext.getCloudContext().getName();
    Collection<Tag> tags = awsTaggingService.prepareElasticLoadBalancingTags(awsCloudStackView.getTags());
    ResourceCreationContext creationContext = new ResourceCreationContext(stackId, stackName, tags, persistenceNotifier, loadBalancingClient, authenticatedContext.getCloudContext());
    try {
        for (AwsLoadBalancer awsLoadBalancer : loadBalancers) {
            createLoadBalancer(creationContext, awsLoadBalancer);
            if (registerTargetGroups) {
                for (AwsListener listener : awsLoadBalancer.getListeners()) {
                    AwsTargetGroup targetGroup = listener.getTargetGroup();
                    creationContext.setTargetGroupName(resourceNameService.resourceName(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP, stackName, awsLoadBalancer.getScheme().resourceName(), targetGroup.getPort()));
                    createTargetGroup(creationContext, awsNetworkView, targetGroup);
                    createListener(creationContext, listener);
                    registerTarget(loadBalancingClient, stackId, targetGroup);
                }
            }
        }
    } catch (Exception ex) {
        String message = "Load balancer and it's resources could not be created. " + ex.getMessage();
        LOGGER.warn(message, ex);
        throw new CloudConnectorException(message, ex);
    }
    return creationContext.getResourceStatuses();
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) LoggerFactory(org.slf4j.LoggerFactory) DescribeListenersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersRequest) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) StringUtils(org.apache.commons.lang3.StringUtils) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) DescribeTargetGroupsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) Collectors.toSet(java.util.stream.Collectors.toSet) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Action(com.amazonaws.services.elasticloadbalancingv2.model.Action) Collection(java.util.Collection) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Set(java.util.Set) LoadBalancerTypeEnum(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerTypeEnum) Collectors(java.util.stream.Collectors) CreateLoadBalancerRequest(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) IpAddressType(com.amazonaws.services.elasticloadbalancingv2.model.IpAddressType) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag) Group(com.sequenceiq.cloudbreak.cloud.model.Group) DescribeListenersRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersRequest) Optional(java.util.Optional) CreateListenerRequest(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerRequest) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) TargetTypeEnum(com.amazonaws.services.elasticloadbalancingv2.model.TargetTypeEnum) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) PersistenceRetriever(com.sequenceiq.cloudbreak.cloud.notification.PersistenceRetriever) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsLoadBalancerCommonService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService) Service(org.springframework.stereotype.Service) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) ActionTypeEnum(com.amazonaws.services.elasticloadbalancingv2.model.ActionTypeEnum) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CreateTargetGroupRequest(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupRequest) ProtocolEnum(com.amazonaws.services.elasticloadbalancingv2.model.ProtocolEnum) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors.toList(java.util.stream.Collectors.toList) AwsCloudStackView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsCloudStackView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag)

Example 2 with AwsCloudStackView

use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView in project cloudbreak by hortonworks.

the class AwsNativeInstanceResourceBuilder method build.

@Override
public List<CloudResource> build(AwsContext context, CloudInstance cloudInstance, long privateId, AuthenticatedContext ac, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
    if (buildableResource.isEmpty()) {
        throw new CloudConnectorException("Buildable resources cannot be empty!");
    }
    AmazonEc2Client amazonEc2Client = context.getAmazonEc2Client();
    InstanceTemplate instanceTemplate = group.getReferenceInstanceTemplate();
    AwsCloudStackView awsCloudStackView = new AwsCloudStackView(cloudStack);
    CloudResource cloudResource = buildableResource.get(0);
    Optional<Instance> existedOpt = resourceByName(amazonEc2Client, cloudResource.getName());
    Instance instance;
    if (existedOpt.isPresent() && existedOpt.get().getState().getCode() != AWS_INSTANCE_TERMINATED_CODE) {
        instance = existedOpt.get();
        LOGGER.info("Instance exists with name: {} ({}), check the state: {}", cloudResource.getName(), instance.getInstanceId(), instance.getState().getName());
        if (!instanceRunning(instance)) {
            LOGGER.info("Instance is existing but not running, try to start: {}, {}", instance.getInstanceId(), instance.getState());
            amazonEc2Client.startInstances(new StartInstancesRequest().withInstanceIds(instance.getInstanceId()));
        }
    } else {
        LOGGER.info("Create new instance with name: {}", cloudResource.getName());
        TagSpecification tagSpecification = awsTaggingService.prepareEc2TagSpecification(awsCloudStackView.getTags(), com.amazonaws.services.ec2.model.ResourceType.Instance);
        String securityGroupId = getSecurityGroupId(context, group);
        tagSpecification.withTags(new Tag().withKey("Name").withValue(cloudResource.getName()));
        RunInstancesRequest request = new RunInstancesRequest().withInstanceType(instanceTemplate.getFlavor()).withImageId(cloudStack.getImage().getImageName()).withSubnetId(cloudInstance.getSubnetId()).withSecurityGroupIds(singletonList(securityGroupId)).withEbsOptimized(isEbsOptimized(instanceTemplate)).withTagSpecifications(tagSpecification).withIamInstanceProfile(getIamInstanceProfile(group)).withUserData(getUserData(cloudStack, group)).withMinCount(1).withMaxCount(1).withBlockDeviceMappings(blocks(group, cloudStack, ac)).withKeyName(cloudStack.getInstanceAuthentication().getPublicKeyId());
        RunInstancesResult instanceResult = amazonEc2Client.createInstance(request);
        instance = instanceResult.getReservation().getInstances().get(0);
        LOGGER.info("Instance creation inited with name: {} and instance id: {}", cloudResource.getName(), instance.getInstanceId());
    }
    cloudResource.setInstanceId(instance.getInstanceId());
    return buildableResource;
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) TagSpecification(com.amazonaws.services.ec2.model.TagSpecification) AwsCloudStackView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Tag(com.amazonaws.services.ec2.model.Tag) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 3 with AwsCloudStackView

use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView in project cloudbreak by hortonworks.

the class AwsNativeLoadBalancerLaunchService method launchLoadBalancerResources.

public List<CloudResourceStatus> launchLoadBalancerResources(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier persistenceNotifier, AmazonElasticLoadBalancingClient loadBalancingClient) {
    LOGGER.debug("Creating AWS load balancer and it's resources for cloud stack: '{}'", authenticatedContext.getCloudContext().getCrn());
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    Map<String, List<String>> privateIdsByGroupName = stack.getGroups().stream().collect(Collectors.toMap(Group::getName, group -> group.getInstances().stream().map(in -> String.valueOf(in.getTemplate().getPrivateId())).collect(toList())));
    List<AwsLoadBalancer> loadBalancers = loadBalancerCommonService.getAwsLoadBalancers(stack.getLoadBalancers(), privateIdsByGroupName, awsNetworkView);
    AwsCloudStackView awsCloudStackView = new AwsCloudStackView(stack);
    Long stackId = authenticatedContext.getCloudContext().getId();
    String stackName = authenticatedContext.getCloudContext().getName();
    Collection<Tag> tags = awsTaggingService.prepareElasticLoadBalancingTags(awsCloudStackView.getTags());
    ResourceCreationContext creationContext = new ResourceCreationContext(stackId, stackName, tags, persistenceNotifier, loadBalancingClient, authenticatedContext.getCloudContext());
    try {
        for (AwsLoadBalancer awsLoadBalancer : loadBalancers) {
            createLoadBalancer(creationContext, awsLoadBalancer);
            for (AwsListener listener : awsLoadBalancer.getListeners()) {
                AwsTargetGroup targetGroup = listener.getTargetGroup();
                creationContext.setTargetGroupName(resourceNameService.resourceName(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP, stackName, awsLoadBalancer.getScheme().resourceName(), targetGroup.getPort()));
                createTargetGroup(creationContext, awsNetworkView, targetGroup);
                createListener(creationContext, listener);
                registerTarget(loadBalancingClient, stackId, targetGroup);
            }
        }
    } catch (Exception ex) {
        String message = "Load balancer and it's resources could not be created";
        LOGGER.warn(message, ex);
        throw new CloudConnectorException(message, ex);
    }
    return creationContext.getResourceStatuses();
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) LoggerFactory(org.slf4j.LoggerFactory) DescribeListenersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersRequest) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) StringUtils(org.apache.commons.lang3.StringUtils) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) DescribeTargetGroupsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) Collectors.toSet(java.util.stream.Collectors.toSet) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Action(com.amazonaws.services.elasticloadbalancingv2.model.Action) Collection(java.util.Collection) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Set(java.util.Set) LoadBalancerTypeEnum(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerTypeEnum) Collectors(java.util.stream.Collectors) CreateLoadBalancerRequest(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) IpAddressType(com.amazonaws.services.elasticloadbalancingv2.model.IpAddressType) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag) Group(com.sequenceiq.cloudbreak.cloud.model.Group) DescribeListenersRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersRequest) Optional(java.util.Optional) CreateListenerRequest(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerRequest) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) TargetTypeEnum(com.amazonaws.services.elasticloadbalancingv2.model.TargetTypeEnum) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) PersistenceRetriever(com.sequenceiq.cloudbreak.cloud.notification.PersistenceRetriever) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsLoadBalancerCommonService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService) Service(org.springframework.stereotype.Service) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) ActionTypeEnum(com.amazonaws.services.elasticloadbalancingv2.model.ActionTypeEnum) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CreateTargetGroupRequest(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupRequest) ProtocolEnum(com.amazonaws.services.elasticloadbalancingv2.model.ProtocolEnum) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors.toList(java.util.stream.Collectors.toList) AwsCloudStackView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsCloudStackView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag)

Aggregations

AwsCloudStackView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView)3 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)3 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 Action (com.amazonaws.services.elasticloadbalancingv2.model.Action)2 ActionTypeEnum (com.amazonaws.services.elasticloadbalancingv2.model.ActionTypeEnum)2 CreateListenerRequest (com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerRequest)2 CreateListenerResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult)2 CreateLoadBalancerRequest (com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerRequest)2 CreateLoadBalancerResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult)2 CreateTargetGroupRequest (com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupRequest)2 CreateTargetGroupResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult)2 DescribeListenersRequest (com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersRequest)2 DescribeListenersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult)2 DescribeLoadBalancersRequest (com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersRequest)2 DescribeLoadBalancersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult)2 DescribeTargetGroupsRequest (com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest)2 DescribeTargetGroupsResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult)2 IpAddressType (com.amazonaws.services.elasticloadbalancingv2.model.IpAddressType)2 LoadBalancerTypeEnum (com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerTypeEnum)2