Search in sources :

Example 1 with AmazonElasticLoadBalancingClient

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

the class LoadBalancerRecreatorService method recreate.

@Override
public void recreate(CreateResourcesRequest request, AwsContext awsContext, AuthenticatedContext ac) throws Exception {
    LOGGER.info("Launching elastic load balancers");
    CloudCredential cloudCredential = ac.getCloudCredential();
    String region = ac.getCloudContext().getLocation().getRegion().value();
    AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential);
    AmazonElasticLoadBalancingClient elasticLoadBalancingClient = commonAwsClient.createElasticLoadBalancingClient(awsCredentialView, region);
    CloudStack cloudStack = request.getCloudStack();
    loadBalancerLaunchService.launchLoadBalancerResources(ac, cloudStack, persistenceNotifier, elasticLoadBalancingClient, false);
    List<CloudLoadBalancerMetadata> cloudLoadBalancerMetadata = collectLoadBalancerMetadata(ac, ac.getCloudContext().getId());
    Stack stack = stackService.getByIdWithLists(ac.getCloudContext().getId());
    metadataSetupService.saveLoadBalancerMetadata(stack, cloudLoadBalancerMetadata);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack)

Example 2 with AmazonElasticLoadBalancingClient

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

the class CloudFormationStackUtil method addLoadBalancerTargets.

public void addLoadBalancerTargets(AuthenticatedContext ac, CloudLoadBalancer loadBalancer, List<CloudResource> resourcesToAdd) {
    String region = ac.getCloudContext().getLocation().getRegion().value();
    AmazonElasticLoadBalancingClient amazonElbClient = awsClient.createElasticLoadBalancingClient(new AwsCredentialView(ac.getCloudCredential()), region);
    for (Map.Entry<TargetGroupPortPair, Set<Group>> entry : loadBalancer.getPortToTargetGroupMapping().entrySet()) {
        // Get a list of the new instances in the target groups
        Set<String> updatedInstanceIds = getInstanceIdsForGroups(resourcesToAdd, entry.getValue());
        // Find target group ARN
        AwsLoadBalancerScheme scheme = loadBalancerTypeConverter.convert(loadBalancer.getType());
        String targetGroupArn = getResourceArnByLogicalId(ac, AwsTargetGroup.getTargetGroupName(entry.getKey().getTrafficPort(), scheme), region);
        // Use ARN to fetch a list of current targets
        DescribeTargetHealthResult targetHealthResult = amazonElbClient.describeTargetHealth(new DescribeTargetHealthRequest().withTargetGroupArn(targetGroupArn));
        List<TargetDescription> targetDescriptions = targetHealthResult.getTargetHealthDescriptions().stream().map(TargetHealthDescription::getTarget).collect(Collectors.toList());
        Set<String> alreadyRegisteredInstanceIds = targetDescriptions.stream().map(TargetDescription::getId).collect(Collectors.toSet());
        // Remove any targets that have already been registered from the list being processed
        updatedInstanceIds.removeAll(alreadyRegisteredInstanceIds);
        // Register any new instances
        if (!updatedInstanceIds.isEmpty()) {
            List<TargetDescription> targetsToAdd = updatedInstanceIds.stream().map(instanceId -> new TargetDescription().withId(instanceId)).collect(Collectors.toList());
            RegisterTargetsResult registerTargetsResult = amazonElbClient.registerTargets(new RegisterTargetsRequest().withTargetGroupArn(targetGroupArn).withTargets(targetsToAdd));
        }
    }
}
Also used : RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersRequest) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) TargetHealthDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetHealthDescription) 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) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) Splitter(com.google.common.base.Splitter) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) DescribeFileSystemsResult(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeTargetHealthResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult) Collection(java.util.Collection) Output(com.amazonaws.services.cloudformation.model.Output) Set(java.util.Set) Retry(com.sequenceiq.cloudbreak.service.Retry) Collectors(java.util.stream.Collectors) Backoff(org.springframework.retry.annotation.Backoff) Objects(java.util.Objects) List(java.util.List) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Group(com.sequenceiq.cloudbreak.cloud.model.Group) PRIVATE_ID(com.sequenceiq.cloudbreak.cloud.model.CloudResource.PRIVATE_ID) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) INSTANCE_TYPE(com.sequenceiq.cloudbreak.cloud.model.CloudResource.INSTANCE_TYPE) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) AmazonEfsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEfsClient) Supplier(java.util.function.Supplier) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Service(org.springframework.stereotype.Service) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Retryable(org.springframework.retry.annotation.Retryable) DescribeStackResourceRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourceRequest) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) FileSystemDescription(com.amazonaws.services.elasticfilesystem.model.FileSystemDescription) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DescribeFileSystemsRequest(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsRequest) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) SdkClientException(com.amazonaws.SdkClientException) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Instance(com.amazonaws.services.autoscaling.model.Instance) DescribeStackResourceResult(com.amazonaws.services.cloudformation.model.DescribeStackResourceResult) Collections(java.util.Collections) DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) Set(java.util.Set) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) DescribeTargetHealthResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) Map(java.util.Map) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)

Example 3 with AmazonElasticLoadBalancingClient

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

the class AwsNativeLoadBalancerLaunchService method registerTarget.

private void registerTarget(AmazonElasticLoadBalancingClient loadBalancingClient, Long stackId, AwsTargetGroup targetGroup) {
    LOGGER.debug("Registering targets to target group '{}'", targetGroup.getArn());
    Set<TargetDescription> targetDescriptions = targetGroup.getInstanceIds().stream().map(privateId -> {
        String instanceId = persistenceRetriever.notifyRetrieve(stackId, privateId, CommonStatus.CREATED, ResourceType.AWS_INSTANCE).orElseThrow(() -> {
            String notFoundMsg = String.format("No AWS instance resource found with private id('%s') for stack('%s')", privateId, stackId);
            return new CloudConnectorException(notFoundMsg);
        }).getInstanceId();
        return new TargetDescription().withPort(targetGroup.getPort()).withId(instanceId);
    }).collect(toSet());
    RegisterTargetsRequest registerTargetsRequest = new RegisterTargetsRequest().withTargetGroupArn(targetGroup.getArn()).withTargets(targetDescriptions);
    LOGGER.info("Registering target group of load balancer('{}') to instances: '{}'", targetGroup.getArn(), String.join(",", targetGroup.getInstanceIds()));
    loadBalancingClient.registerTargets(registerTargetsRequest);
}
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) RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription)

Example 4 with AmazonElasticLoadBalancingClient

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

the class AwsNativeLoadBalancerListenerResourceBuilder method delete.

@Override
public CloudResource delete(AwsContext context, com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext auth, CloudResource resource) throws Exception {
    LOGGER.info("Trying to delete listener '{}', may it has already been deleted with it's related load balancer '{}'", resource.getReference(), resource.getInstanceId());
    AmazonElasticLoadBalancingClient loadBalancingClient = context.getLoadBalancingClient();
    DeleteListenerRequest deleteListenerRequest = new DeleteListenerRequest().withListenerArn(resource.getReference());
    DeleteListenerResult deleteResult = null;
    try {
        deleteResult = awsMethodExecutor.execute(() -> loadBalancingClient.deleteListener(deleteListenerRequest), null);
    } catch (AmazonServiceException awsException) {
        if (StringUtils.isNotEmpty(awsException.getErrorCode()) && LISTENER_NOT_FOUND_ERROR_CODE.equals(awsException.getErrorCode())) {
            LOGGER.info("Listener doesn't exist with id: '{}'", resource.getReference());
        } else {
            LOGGER.warn("Listener could not be fetched from AWS with id: '{}'", resource.getReference(), awsException);
            throw awsException;
        }
    }
    return deleteResult != null ? resource : null;
}
Also used : AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) DeleteListenerRequest(com.amazonaws.services.elasticloadbalancingv2.model.DeleteListenerRequest) DeleteListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.DeleteListenerResult)

Example 5 with AmazonElasticLoadBalancingClient

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

the class AwsNativeLoadBalancerResourceBuilder method delete.

@Override
public CloudResource delete(AwsContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
    LOGGER.info("Deleting load balancer ('{}') from provider side", resource.getReference());
    AmazonElasticLoadBalancingClient loadBalancingClient = context.getLoadBalancingClient();
    DeleteLoadBalancerRequest deleteLoadBalancerRequest = new DeleteLoadBalancerRequest().withLoadBalancerArn(resource.getReference());
    DeleteLoadBalancerResult deleteResult = awsMethodExecutor.execute(() -> loadBalancingClient.deleteLoadBalancer(deleteLoadBalancerRequest), null);
    return deleteResult != null ? resource : null;
}
Also used : AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DeleteLoadBalancerRequest(com.amazonaws.services.elasticloadbalancingv2.model.DeleteLoadBalancerRequest) DeleteLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.DeleteLoadBalancerResult)

Aggregations

AmazonElasticLoadBalancingClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient)16 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)9 AmazonServiceException (com.amazonaws.AmazonServiceException)8 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)8 DescribeLoadBalancersRequest (com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersRequest)7 DescribeLoadBalancersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult)7 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)7 List (java.util.List)7 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 Inject (javax.inject.Inject)7 Logger (org.slf4j.Logger)7 TargetDescription (com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription)6 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)6 ResourceType (com.sequenceiq.common.api.type.ResourceType)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Service (org.springframework.stereotype.Service)6 RegisterTargetsRequest (com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest)5 AwsLoadBalancerScheme (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)5