use of com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair 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