use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription 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));
}
}
}
use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription 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);
}
use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription in project cloudbreak by hortonworks.
the class LoadBalancerService method removeLoadBalancerTargets.
public void removeLoadBalancerTargets(AuthenticatedContext ac, List<String> targetGroupArns, List<CloudResource> resourcesToRemove) {
if (targetGroupArns.isEmpty()) {
LOGGER.info("Cannot find target group for the stack, skip the removing of the targets");
return;
}
for (String targetGroupArn : targetGroupArns) {
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonElasticLoadBalancingClient amazonElbClient = awsClient.createElasticLoadBalancingClient(new AwsCredentialView(ac.getCloudCredential()), region);
LOGGER.debug("Get a list of the instance ids to remove");
Set<String> instancesToRemove = getInstanceIdsForGroups(resourcesToRemove);
LOGGER.debug("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());
amazonElbClient.deregisterTargets(new DeregisterTargetsRequest().withTargetGroupArn(targetGroupArn).withTargets(targetsToRemove));
LOGGER.debug("Targets deregistered: {}", targetsToRemove);
} catch (InvalidTargetException ignored) {
LOGGER.debug("no-op - we tried to remove a target that wasn't in the target group, which is fine");
}
}
}
}
use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription in project titus-control-plane by Netflix.
the class AwsLoadBalancerConnector method registerAll.
@Override
public Completable registerAll(String loadBalancerId, Set<String> ipAddresses) {
if (CollectionsExt.isNullOrEmpty(ipAddresses)) {
return Completable.complete();
}
// TODO: retry logic
// TODO: handle partial failures in the batch
// TODO: timeouts
final Set<TargetDescription> targetDescriptions = ipAddresses.stream().map(ipAddress -> new TargetDescription().withId(ipAddress)).collect(Collectors.toSet());
final RegisterTargetsRequest request = new RegisterTargetsRequest().withTargetGroupArn(loadBalancerId).withTargets(targetDescriptions);
long startTime = registry.clock().wallTime();
// force observeOn(scheduler) since the callback will be called from the AWS SDK threadpool
return AwsObservableExt.asyncActionCompletable(factory -> getClient(loadBalancerId).registerTargetsAsync(request, factory.handler((req, resp) -> {
logger.debug("Registered targets {}", resp);
connectorMetrics.success(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.RegisterTargets, startTime);
}, (t) -> {
logger.error("Error registering targets on " + loadBalancerId, t);
connectorMetrics.failure(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.RegisterTargets, t, startTime);
}))).observeOn(scheduler);
}
use of com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription in project titus-control-plane by Netflix.
the class AwsLoadBalancerConnector method deregisterAll.
@Override
public Completable deregisterAll(String loadBalancerId, Set<String> ipAddresses) {
if (CollectionsExt.isNullOrEmpty(ipAddresses)) {
return Completable.complete();
}
// TODO: retry logic
// TODO: handle partial failures in the batch
// TODO: timeouts
final DeregisterTargetsRequest request = new DeregisterTargetsRequest().withTargetGroupArn(loadBalancerId).withTargets(ipAddresses.stream().map(ipAddress -> new TargetDescription().withId(ipAddress)).collect(Collectors.toSet()));
long startTime = registry.clock().wallTime();
// force observeOn(scheduler) since the callback will be called from the AWS SDK threadpool
return AwsObservableExt.asyncActionCompletable(supplier -> getClient(loadBalancerId).deregisterTargetsAsync(request, supplier.handler((req, resp) -> {
logger.debug("Deregistered targets {}", resp);
connectorMetrics.success(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.DeregisterTargets, startTime);
}, (t) -> {
logger.error("Error deregistering targets on " + loadBalancerId, t);
connectorMetrics.failure(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.DeregisterTargets, t, startTime);
}))).observeOn(scheduler);
}
Aggregations