use of software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancer in project datarouter by hotpads.
the class AwsElbConfigScanner method getAlbSchemeForEc2Instance.
private Map<String, Boolean> getAlbSchemeForEc2Instance(String serverType, String ec2InstanceId) {
Map<String, Boolean> privatePublicAlbsMap = new HashMap<>();
boolean unknownAlb = true;
boolean instanceHasPublicAlb = false;
boolean instanceHasPrivateAlb = false;
for (LoadBalancer loadBalancer : elbService.getLoadBalancers()) {
// exact ALB name
if (!loadBalancer.getLoadBalancerName().equals(serverType) && !loadBalancer.getLoadBalancerName().startsWith(serverType + "-privat")) {
continue;
}
for (String targetGroupArn : elbService.getTargetGroupsArn(loadBalancer.getLoadBalancerArn())) {
for (String targetEc2InstanceId : elbService.getTargetEc2InstancesId(targetGroupArn)) {
if (!targetEc2InstanceId.equals(ec2InstanceId)) {
continue;
}
LoadBalancerSchemeEnum loadBalancerScheme = LoadBalancerSchemeEnum.fromValue(loadBalancer.getScheme());
if (LoadBalancerSchemeEnum.InternetFacing == loadBalancerScheme) {
instanceHasPublicAlb = true;
}
if (LoadBalancerSchemeEnum.Internal == loadBalancerScheme) {
instanceHasPrivateAlb = true;
}
unknownAlb = false;
}
}
}
privatePublicAlbsMap.put(HAS_PUBLIC_LOAD_BALANCER_KEY, instanceHasPublicAlb);
privatePublicAlbsMap.put(HAS_PRIVATE_LOAD_BALANCER_KEY, instanceHasPrivateAlb);
privatePublicAlbsMap.put(UNKNOWN_LOAD_BALANCER, unknownAlb);
return privatePublicAlbsMap;
}
use of software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancer in project aws-cloudwatch-exporter by asserts.
the class TargetGroupLBMapProviderTest method update.
@Test
public void update() {
LoadBalancer loadBalancer = LoadBalancer.builder().loadBalancerArn("lb-arn").build();
expect(awsClientProvider.getELBV2Client("region")).andReturn(lbClient);
expect(lbClient.describeLoadBalancers()).andReturn(DescribeLoadBalancersResponse.builder().loadBalancers(loadBalancer).build());
metricCollector.recordLatency(eq(SCRAPE_LATENCY_METRIC), anyObject(SortedMap.class), anyLong());
AtomicInteger sideEffect = new AtomicInteger();
TargetGroupLBMapProvider testClass = new TargetGroupLBMapProvider(scrapeConfigProvider, awsClientProvider, resourceMapper, new RateLimiter(metricCollector)) {
@Override
void mapLB(ElasticLoadBalancingV2Client client, SortedMap<String, String> theLabels, LoadBalancer theLb) {
assertEquals(lbClient, client);
assertEquals(labels, theLabels);
assertEquals(loadBalancer, theLb);
sideEffect.incrementAndGet();
}
};
lbClient.close();
replayAll();
testClass.update();
assertEquals(1, sideEffect.get());
verifyAll();
}
use of software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancer in project adama-lang by mathgladiator.
the class Fleet method fleetDeployUpdateELB.
private static void fleetDeployUpdateELB(Config config, HashSet<String> frontendInstances) {
ElasticLoadBalancingV2Client elb = getELB(config);
String loadBalancerArn = config.get_string("loadbalancer-arn", null);
if (loadBalancerArn == null) {
throw new NullPointerException("config has no 'loadbalancer-arn'");
}
ArrayList<TargetDescription> toRemove = new ArrayList<>();
ArrayList<TargetDescription> toAdd = new ArrayList<>();
for (TargetHealthDescription health : elb.describeTargetHealth(DescribeTargetHealthRequest.builder().targetGroupArn(loadBalancerArn).build()).targetHealthDescriptions()) {
if (frontendInstances.contains(health.target().id())) {
frontendInstances.remove(health.target().id());
} else {
toRemove.add(health.target());
}
}
for (String idToAdd : frontendInstances) {
toAdd.add(TargetDescription.builder().id(idToAdd).port(8080).build());
}
if (toAdd.size() > 0) {
elb.registerTargets(RegisterTargetsRequest.builder().targetGroupArn(loadBalancerArn).targets(toAdd).build());
System.err.println("registered " + toAdd.size() + " targets");
}
if (toRemove.size() > 0) {
elb.deregisterTargets(DeregisterTargetsRequest.builder().targetGroupArn(loadBalancerArn).targets(toRemove).build());
System.err.println("deregistered " + toRemove.size() + " targets");
}
}
use of software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancer in project cloudbreak by hortonworks.
the class AwsMetadataCollector method collectLoadBalancer.
@Override
public List<CloudLoadBalancerMetadata> collectLoadBalancer(AuthenticatedContext ac, List<LoadBalancerType> loadBalancerTypes, List<CloudResource> resources) {
LOGGER.debug("Collect AWS load balancer metadata, for cluster {}", ac.getCloudContext().getName());
List<CloudLoadBalancerMetadata> cloudLoadBalancerMetadata = new ArrayList<>();
for (LoadBalancerType type : loadBalancerTypes) {
AwsLoadBalancerScheme scheme = loadBalancerTypeConverter.convert(type);
String loadBalancerName = AwsLoadBalancer.getLoadBalancerName(scheme);
LOGGER.debug("Attempting to collect metadata for load balancer {}, type {}", loadBalancerName, type);
try {
LoadBalancer loadBalancer = cloudFormationStackUtil.getLoadBalancerByLogicalId(ac, loadBalancerName);
LOGGER.debug("Parsing all listener and target group information for load balancer {}", loadBalancerName);
Map<String, Object> parameters = awsLoadBalancerMetadataCollector.getParameters(ac, loadBalancer, scheme);
CloudLoadBalancerMetadata loadBalancerMetadata = new CloudLoadBalancerMetadata.Builder().withType(type).withCloudDns(loadBalancer.getDNSName()).withHostedZoneId(loadBalancer.getCanonicalHostedZoneId()).withName(loadBalancerName).withParameters(parameters).build();
cloudLoadBalancerMetadata.add(loadBalancerMetadata);
LOGGER.debug("Saved metadata for load balancer {}: DNS {}, zone id {}", loadBalancerName, loadBalancer.getDNSName(), loadBalancer.getCanonicalHostedZoneId());
} catch (RuntimeException e) {
LOGGER.debug("Unable to find metadata for load balancer " + loadBalancerName, e);
}
}
return cloudLoadBalancerMetadata;
}
use of software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancer 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));
}
}
}
Aggregations