Skip to content

Commit

Permalink
[aws][fix] Collect capacity providers only once (#1918)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored Feb 15, 2024
1 parent a94f5bf commit 90ef895
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion plugins/aws/resoto_plugin_aws/resource/cloudfront.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ class AwsCloudFrontDistributionConfig:
class AwsCloudFrontDistribution(CloudFrontTaggable, CloudFrontResource, AwsResource):
kind: ClassVar[str] = "aws_cloudfront_distribution"
api_spec: ClassVar[AwsApiSpec] = AwsApiSpec("cloudfront", "get-distribution", "Distribution")
aws_metadata: ClassVar[Dict[str, Any]] = {"provider_link_tpl": "https://{region_id}.console.aws.amazon.com/cloudfront/v4/home#/distributions/{id}", "arn_tpl": "arn:{partition}:cloudfront:{region}:{account}:distribution/{id}"} # fmt: skip
aws_metadata: ClassVar[Dict[str, Any]] = {"provider_link_tpl": "https://{region_id}.console.aws.amazon.com/cloudfront/v4/home#/distributions/{id}", "arn_tpl": "arn:{partition}:cloudfront::{account}:distribution/{id}"} # fmt: skip
kind_display: ClassVar[str] = "AWS CloudFront Distribution"
kind_description: ClassVar[str] = (
"CloudFront Distributions are a content delivery network (CDN) offered by"
Expand Down
35 changes: 22 additions & 13 deletions plugins/aws/resoto_plugin_aws/resource/ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,7 @@ def called_collect_apis(cls) -> List[AwsApiSpec]:

@classmethod
def collect(cls: Type[AwsResource], json: List[Json], builder: GraphBuilder) -> None:
instances = []
for cluster_arn in json:
cluster = builder.client.list(
service_name,
Expand All @@ -1989,6 +1990,7 @@ def collect(cls: Type[AwsResource], json: List[Json], builder: GraphBuilder) ->
)
if cluster_instance := AwsEcsCluster.from_api(cluster[0], builder):
builder.add_node(cluster_instance, cluster_arn)
instances.append(cluster_instance)

container_arns = builder.client.list(
service_name, "list-container-instances", "containerInstanceArns", cluster=cluster_arn
Expand Down Expand Up @@ -2038,19 +2040,26 @@ def collect(cls: Type[AwsResource], json: List[Json], builder: GraphBuilder) ->
builder.add_node(task_instance, task)
builder.add_edge(cluster_instance, edge_type=EdgeType.default, node=task_instance)

provider_names = cluster_instance.cluster_capacity_providers
for chunk in chunks(provider_names, 100):
providers = builder.client.list(
service_name,
"describe-capacity-providers",
"capacityProviders",
capacityProviders=chunk,
include=["TAGS"],
)
for provider in providers:
if provider_instance := AwsEcsCapacityProvider.from_api(provider, builder):
builder.add_node(provider_instance, provider)
builder.add_edge(cluster_instance, edge_type=EdgeType.default, node=provider_instance)
# once all clusters are collected, collect capacity providers
provider_names = {name for instance in instances for name in instance.cluster_capacity_providers}
providers: Dict[str, AwsEcsCapacityProvider] = {}
for chunk in chunks(list(provider_names), 100):
for provider in builder.client.list(
service_name,
"describe-capacity-providers",
"capacityProviders",
capacityProviders=chunk,
include=["TAGS"],
):
if provider_instance := AwsEcsCapacityProvider.from_api(provider, builder):
builder.add_node(provider_instance, provider)
providers[provider_instance.safe_name] = provider_instance

# connect clusters to providers
for instance in instances:
for name in instance.cluster_capacity_providers:
if provider := providers.get(name):
builder.add_edge(instance, edge_type=EdgeType.default, node=provider)

def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
# TODO add edge to CloudWatchLogs LogGroup when applicable
Expand Down

0 comments on commit 90ef895

Please sign in to comment.