use of com.amazonaws.services.elasticloadbalancingv2.model.Listener in project datarouter by hotpads.
the class ElbService method getTargetGroupsArn.
public List<String> getTargetGroupsArn(String loadBalancerArn) {
var request = new DescribeListenersRequest().withLoadBalancerArn(loadBalancerArn);
int randomSleepMs = RandomTool.getRandomIntBetweenTwoNumbers(0, 3_000);
return RetryableTool.tryNTimesWithBackoffAndRandomInitialDelayUnchecked(() -> amazonElasticLoadBalancing.get().describeListeners(request).getListeners().stream().map(Listener::getDefaultActions).flatMap(List::stream).map(Action::getTargetGroupArn).filter(// remove action that have no tg (like redirect)
Objects::nonNull).distinct().collect(Collectors.toList()), NUM_ATTEMPTS, randomSleepMs, true);
}
use of com.amazonaws.services.elasticloadbalancingv2.model.Listener in project aws-cloudwatch-exporter by asserts.
the class TargetGroupLBMapProvider method mapListener.
@VisibleForTesting
void mapListener(ElasticLoadBalancingV2Client lbClient, SortedMap<String, String> labels, Resource lbResource, Listener listener) {
SortedMap<String, String> telemetryLabels = new TreeMap<>(labels);
telemetryLabels.put(SCRAPE_OPERATION_LABEL, "ElasticLoadBalancingClientV2/describeRules");
DescribeRulesResponse dLR = rateLimiter.doWithRateLimit("ElasticLoadBalancingClientV2/describeRules", telemetryLabels, () -> lbClient.describeRules(DescribeRulesRequest.builder().listenerArn(listener.listenerArn()).build()));
if (dLR.hasRules()) {
dLR.rules().stream().filter(rule -> !isEmpty(rule.actions())).flatMap(rule -> rule.actions().stream()).filter(action -> action.targetGroupArn() != null).map(action -> resourceMapper.map(action.targetGroupArn())).filter(Optional::isPresent).map(Optional::get).forEach(tg -> tgToLB.put(tg, lbResource));
}
}
use of com.amazonaws.services.elasticloadbalancingv2.model.Listener 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 com.amazonaws.services.elasticloadbalancingv2.model.Listener in project cloudbreak by hortonworks.
the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenTargetGroupResourcesExistAlready.
@Test
void testLaunchLoadBalancerResourcesWhenTargetGroupResourcesExistAlready() {
CloudStack stack = getCloudStack();
when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER), any(), any())).thenReturn("aLoadBalancerName");
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
LoadBalancer loadBalancer = new LoadBalancer().withLoadBalancerArn("anARN");
CreateLoadBalancerResult loadBalancerResult = new CreateLoadBalancerResult().withLoadBalancers(loadBalancer);
when(loadBalancingClient.registerLoadBalancer(any())).thenReturn(loadBalancerResult);
CloudResource targetGroupResource = CloudResource.builder().name("aTargetTG443Master").type(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP).reference("aTargetGroupArn").build();
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.of(targetGroupResource));
when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn("aLoadBalancerTGName");
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
Listener listener = new Listener().withListenerArn("aListenerArn");
CreateListenerResult createListenerResult = new CreateListenerResult().withListeners(listener);
when(loadBalancingClient.registerListener(any())).thenReturn(createListenerResult);
when(loadBalancingClient.registerTargets(any())).thenReturn(new RegisterTargetsResult());
List<CloudResourceStatus> statuses = undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true);
ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
verify(persistenceNotifier, times(2)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
verify(loadBalancingClient, times(1)).registerTargets(any());
verify(loadBalancingClient, times(0)).createTargetGroup(any());
assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_LISTENER.equals(cloudResource.getType())));
assertTrue(resourceExistsWithTypeAndStatus(statuses, ResourceType.ELASTIC_LOAD_BALANCER, ResourceStatus.CREATED));
assertTrue(resourceExistsWithTypeAndStatus(statuses, ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP, ResourceStatus.CREATED));
assertTrue(resourceExistsWithTypeAndStatus(statuses, ResourceType.ELASTIC_LOAD_BALANCER_LISTENER, ResourceStatus.CREATED));
}
use of com.amazonaws.services.elasticloadbalancingv2.model.Listener in project cloudbreak by hortonworks.
the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenLoadBalancerTargetsCouldNotBeRegistered.
@Test
void testLaunchLoadBalancerResourcesWhenLoadBalancerTargetsCouldNotBeRegistered() {
CloudStack stack = getCloudStack();
when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER), any(), any())).thenReturn("aLoadBalancerName");
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
LoadBalancer loadBalancer = new LoadBalancer().withLoadBalancerArn("anARN");
CreateLoadBalancerResult loadBalancerResult = new CreateLoadBalancerResult().withLoadBalancers(loadBalancer);
when(loadBalancingClient.registerLoadBalancer(any())).thenReturn(loadBalancerResult);
when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn("aLoadBalancerTGName");
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
TargetGroup targetGroup = new TargetGroup().withTargetGroupArn("aTargetGroupArn");
CreateTargetGroupResult createTargetGroupResult = new CreateTargetGroupResult().withTargetGroups(List.of(targetGroup));
when(loadBalancingClient.createTargetGroup(any())).thenReturn(createTargetGroupResult);
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
Listener listener = new Listener().withListenerArn("aListenerArn");
CreateListenerResult createListenerResult = new CreateListenerResult().withListeners(listener);
when(loadBalancingClient.registerListener(any())).thenReturn(createListenerResult);
when(loadBalancingClient.registerTargets(any())).thenThrow(new AmazonElasticLoadBalancingException("something went wrong"));
Assertions.assertThrows(CloudConnectorException.class, () -> undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true));
ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
verify(loadBalancingClient, times(1)).registerTargets(any());
verify(persistenceNotifier, times(3)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP.equals(cloudResource.getType())));
assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_LISTENER.equals(cloudResource.getType())));
}
Aggregations