Search in sources :

Example 6 with TargetDescription

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription in project datarouter by hotpads.

the class ElbService method getTargetEc2InstancesId.

public List<String> getTargetEc2InstancesId(String targetGroupArn) {
    var request = new DescribeTargetHealthRequest().withTargetGroupArn(targetGroupArn);
    int randomSleepMs = RandomTool.getRandomIntBetweenTwoNumbers(0, 3_000);
    return RetryableTool.tryNTimesWithBackoffAndRandomInitialDelayUnchecked(() -> amazonElasticLoadBalancing.get().describeTargetHealth(request).getTargetHealthDescriptions().stream().map(TargetHealthDescription::getTarget).map(TargetDescription::getId).collect(Collectors.toList()), NUM_ATTEMPTS, randomSleepMs, true);
}
Also used : DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription)

Example 7 with TargetDescription

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription in project gridgain by gridgain.

the class TcpDiscoveryAlbIpFinder method getRegisteredAddresses.

/**
 * {@inheritDoc}
 */
@Override
public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException {
    initClients();
    DescribeTargetHealthRequest req = new DescribeTargetHealthRequest().withTargetGroupArn(targetGrpARN);
    List<TargetHealthDescription> desc = amazonELBClient.describeTargetHealth(req).getTargetHealthDescriptions();
    // instance ips
    List<String> instanceIps = new LinkedList<>();
    // instance ids
    List<String> instanceIds = new LinkedList<>();
    // Fetch the ids of instances in the given ARN via target health
    for (TargetHealthDescription targetHealthDesc : desc) {
        TargetDescription target = targetHealthDesc.getTarget();
        String targetId = target.getId();
        // divide the target ids into ips and instance ids
        if (isIPAddress(targetId))
            instanceIps.add(targetId);
        else
            instanceIds.add(targetId);
    }
    DescribeInstancesRequest descInstReq = new DescribeInstancesRequest().withInstanceIds(instanceIds);
    List<Reservation> reservations = amazonEC2Client.describeInstances(descInstReq).getReservations();
    // Convert instance ids to instance ips
    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();
        for (Instance instance : instances) {
            String ip = instance.getPrivateIpAddress();
            instanceIps.add(ip);
        }
    }
    List<InetSocketAddress> addrs = new LinkedList<>();
    for (String ip : instanceIps) {
        InetSocketAddress addr = new InetSocketAddress(ip, 0);
        addrs.add(addr);
    }
    return addrs;
}
Also used : DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) Instance(com.amazonaws.services.ec2.model.Instance) InetSocketAddress(java.net.InetSocketAddress) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) LinkedList(java.util.LinkedList) Reservation(com.amazonaws.services.ec2.model.Reservation) TargetHealthDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetHealthDescription)

Example 8 with TargetDescription

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription in project ignite-extensions by apache.

the class TcpDiscoveryAlbIpFinder method getRegisteredAddresses.

/**
 * {@inheritDoc}
 */
@Override
public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException {
    initClients();
    DescribeTargetHealthRequest req = new DescribeTargetHealthRequest().withTargetGroupArn(targetGrpARN);
    List<TargetHealthDescription> desc = amazonELBClient.describeTargetHealth(req).getTargetHealthDescriptions();
    // instance ips
    List<String> instanceIps = new LinkedList<>();
    // instance ids
    List<String> instanceIds = new LinkedList<>();
    // Fetch the ids of instances in the given ARN via target health
    for (TargetHealthDescription targetHealthDesc : desc) {
        TargetDescription target = targetHealthDesc.getTarget();
        String targetId = target.getId();
        // divide the target ids into ips and instance ids
        if (isIPAddress(targetId))
            instanceIps.add(targetId);
        else
            instanceIds.add(targetId);
    }
    DescribeInstancesRequest descInstReq = new DescribeInstancesRequest().withInstanceIds(instanceIds);
    List<Reservation> reservations = amazonEC2Client.describeInstances(descInstReq).getReservations();
    // Convert instance ids to instance ips
    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();
        for (Instance instance : instances) {
            String ip = instance.getPrivateIpAddress();
            instanceIps.add(ip);
        }
    }
    List<InetSocketAddress> addrs = new LinkedList<>();
    for (String ip : instanceIps) {
        InetSocketAddress addr = new InetSocketAddress(ip, 0);
        addrs.add(addr);
    }
    return addrs;
}
Also used : DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) Instance(com.amazonaws.services.ec2.model.Instance) InetSocketAddress(java.net.InetSocketAddress) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) LinkedList(java.util.LinkedList) Reservation(com.amazonaws.services.ec2.model.Reservation) TargetHealthDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetHealthDescription)

Example 9 with TargetDescription

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription in project cloudbreak by hortonworks.

the class CloudFormationStackUtil method removeLoadBalancerTargets.

public void removeLoadBalancerTargets(AuthenticatedContext ac, CloudLoadBalancer loadBalancer, List<CloudResource> resourcesToRemove) {
    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 instance ids to remove
        Set<String> instancesToRemove = getInstanceIdsForGroups(resourcesToRemove, entry.getValue());
        // Find target group ARN
        AwsLoadBalancerScheme scheme = loadBalancerTypeConverter.convert(loadBalancer.getType());
        String targetGroupArn = getResourceArnByLogicalId(ac, AwsTargetGroup.getTargetGroupName(entry.getKey().getTrafficPort(), scheme), region);
        // Deregister any instances that no longer exist
        if (!instancesToRemove.isEmpty()) {
            try {
                List<TargetDescription> targetsToRemove = instancesToRemove.stream().map(instanceId -> new TargetDescription().withId(instanceId)).collect(Collectors.toList());
                DeregisterTargetsResult deregisterTargetsResult = amazonElbClient.deregisterTargets(new DeregisterTargetsRequest().withTargetGroupArn(targetGroupArn).withTargets(targetsToRemove));
            } catch (InvalidTargetException ignored) {
            // no-op - we tried to remove a target that wasn't in the target group, which is fine
            }
        }
    }
}
Also used : DeregisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsRequest) 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) InvalidTargetException(com.amazonaws.services.elasticloadbalancingv2.model.InvalidTargetException) 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) DeregisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsResult) 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) Set(java.util.Set) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DeregisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsResult) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) DeregisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsRequest) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) InvalidTargetException(com.amazonaws.services.elasticloadbalancingv2.model.InvalidTargetException) Map(java.util.Map) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)

Aggregations

TargetDescription (com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription)9 DescribeTargetHealthRequest (com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 Logger (org.slf4j.Logger)6 RegisterTargetsRequest (com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest)5 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)4 DeregisterTargetsRequest (com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsRequest)4 AmazonElasticLoadBalancingClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient)4 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)4 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)4 List (java.util.List)4 DescribeTargetHealthResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult)3 TargetHealthDescription (com.amazonaws.services.elasticloadbalancingv2.model.TargetHealthDescription)3 Collections (java.util.Collections)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 Arn (com.amazonaws.arn.Arn)2 Instance (com.amazonaws.services.ec2.model.Instance)2 Reservation (com.amazonaws.services.ec2.model.Reservation)2