use of com.amazonaws.services.elasticloadbalancingv2.model.InvalidTargetException 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.InvalidTargetException 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
}
}
}
}
Aggregations