diff --git a/src/main/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilder.java b/src/main/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilder.java index 3c3daf3c..c31c2a70 100644 --- a/src/main/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilder.java +++ b/src/main/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilder.java @@ -4,8 +4,8 @@ */ package ai.asserts.aws.exporter; -import ai.asserts.aws.AWSClientProvider; import ai.asserts.aws.AWSApiCallRateLimiter; +import ai.asserts.aws.AWSClientProvider; import ai.asserts.aws.SimpleTenantTask; import ai.asserts.aws.TaskExecutorUtil; import ai.asserts.aws.account.AWSAccount; @@ -80,42 +80,44 @@ private Pair, Set> buildRelations(String region, try { ElasticLoadBalancingV2Client elbV2Client = awsClientProvider.getELBV2Client(region, accountRegion); Map tgToLB = targetGroupLBMapProvider.getTgToLB(); - tgToLB.keySet().forEach(tg -> { - try { - String api = "ElasticLoadBalancingV2Client/describeTargetHealth"; - DescribeTargetHealthResponse response = rateLimiter.doWithRateLimit( - api, - ImmutableSortedMap.of( - SCRAPE_REGION_LABEL, region, - SCRAPE_ACCOUNT_ID_LABEL, accountRegion.getAccountId(), - SCRAPE_OPERATION_LABEL, api - ) - , () -> elbV2Client.describeTargetHealth(DescribeTargetHealthRequest.builder() - .targetGroupArn(tg.getArn()) - .build())); - if (!isEmpty(response.targetHealthDescriptions())) { - response.targetHealthDescriptions().stream() - .map(tH -> resourceMapper.map(tH.target().id())) - .filter(opt -> opt.isPresent() && opt.get().getType().equals(LambdaFunction)) - .map(Optional::get) - .forEach(lambda -> routing.add(ResourceRelation.builder() - .from(tgToLB.get(tg)) - .to(lambda) - .name("ROUTES_TO") - .build())); - } - } catch (TargetGroupNotFoundException e) { - log.warn("LoadBalancer-2-TargetGroup Cache refers to non-existent TargetGroup {}", tg); - missingTgs.add(tg); - } catch (Exception e) { - if (e.getCause() instanceof TargetGroupNotFoundException) { - log.warn("LoadBalancer-2-TargetGroup Cache refers to non-existent TargetGroup {}", tg); - missingTgs.add(tg); - } else { - log.error("Failed to build resource relations", e); - } - } - }); + tgToLB.keySet().stream() + .filter(tg -> tg.getAccount().equals(accountRegion.getAccountId()) && region.equals(tg.getRegion())) + .forEach(tg -> { + try { + String api = "ElasticLoadBalancingV2Client/describeTargetHealth"; + DescribeTargetHealthResponse response = rateLimiter.doWithRateLimit( + api, + ImmutableSortedMap.of( + SCRAPE_REGION_LABEL, region, + SCRAPE_ACCOUNT_ID_LABEL, accountRegion.getAccountId(), + SCRAPE_OPERATION_LABEL, api + ) + , () -> elbV2Client.describeTargetHealth(DescribeTargetHealthRequest.builder() + .targetGroupArn(tg.getArn()) + .build())); + if (!isEmpty(response.targetHealthDescriptions())) { + response.targetHealthDescriptions().stream() + .map(tH -> resourceMapper.map(tH.target().id())) + .filter(opt -> opt.isPresent() && opt.get().getType().equals(LambdaFunction)) + .map(Optional::get) + .forEach(lambda -> routing.add(ResourceRelation.builder() + .from(tgToLB.get(tg)) + .to(lambda) + .name("ROUTES_TO") + .build())); + } + } catch (TargetGroupNotFoundException e) { + log.warn("LoadBalancer-2-TargetGroup Cache refers to non-existent TargetGroup {}", tg); + missingTgs.add(tg); + } catch (Exception e) { + if (e.getCause() instanceof TargetGroupNotFoundException) { + log.warn("LoadBalancer-2-TargetGroup Cache refers to non-existent TargetGroup {}", tg); + missingTgs.add(tg); + } else { + log.error("Failed to build resource relations", e); + } + } + }); } catch (Exception e) { log.error("Error " + accountRegion, e); } diff --git a/src/test/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilderTest.java b/src/test/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilderTest.java index f5ffa177..cb496406 100644 --- a/src/test/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilderTest.java +++ b/src/test/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilderTest.java @@ -70,7 +70,7 @@ public void setup() { new TaskExecutorUtil(new TestTaskThreadPool(), new AWSApiCallRateLimiter(metricCollector, (account) -> "acme"))); - AWSAccount awsAccount = new AWSAccount("tenant", "account", "accessId", "secretKey", "role", + AWSAccount awsAccount = new AWSAccount("acme", "account", "accessId", "secretKey", "role", ImmutableSet.of("region")); expect(accountProvider.getAccounts()).andReturn(ImmutableSet.of(awsAccount)).anyTimes(); expect(awsClientProvider.getELBV2Client("region", awsAccount)).andReturn(elbV2Client).anyTimes(); @@ -80,7 +80,11 @@ public void setup() { @Test void getRoutings() { + expect(targetGroupResource.getAccount()).andReturn("account"); + expect(targetGroupResource.getRegion()).andReturn("region"); expect(targetGroupResource.getArn()).andReturn("tg-arn"); + expect(targetGroupResource2.getAccount()).andReturn("account"); + expect(targetGroupResource2.getRegion()).andReturn("region"); expect(targetGroupResource2.getArn()).andReturn("tg-arn2"); expect(elbV2Client.describeTargetHealth(DescribeTargetHealthRequest.builder() .targetGroupArn("tg-arn")