Skip to content

Commit 8253e12

Browse files
authored
[aws][feat] Add region_in_use property for region (#2086)
1 parent f8dc977 commit 8253e12

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

plugins/aws/fix_plugin_aws/collector.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ def get_last_run() -> Optional[datetime]:
237237
log.warning(f"Unexpected node type {node} in graph")
238238
raise Exception("Only AWS resources expected")
239239

240+
# final hook when the graph is complete
241+
for node, data in list(self.graph.nodes(data=True)):
242+
if isinstance(node, AwsResource):
243+
node.complete_graph(global_builder, data.get("source", {}))
244+
240245
# wait for all futures to finish
241246
shared_queue.wait_for_submitted_work()
242247
self.core_feedback.progress_done(self.account.dname, 1, 1, context=[self.cloud.id])

plugins/aws/fix_plugin_aws/resource/base.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from math import ceil
1313

14-
from attr import evolve
14+
from attr import evolve, field
1515
from attrs import define
1616
from boto3.exceptions import Boto3Error
1717

@@ -28,6 +28,7 @@
2828
Cloud,
2929
EdgeType,
3030
ModelReference,
31+
PhantomBaseResource,
3132
)
3233
from fixlib.config import Config, current_config
3334
from fixlib.core.actions import CoreFeedback, SuppressWithFeedback
@@ -257,11 +258,15 @@ def called_mutator_apis(cls) -> List[AwsApiSpec]:
257258
return []
258259

259260
def post_process(self, builder: GraphBuilder, source: Json) -> None:
260-
# Default behavior: do nothing
261+
# Hook method: called after the resource has been created and added to the graph.
261262
pass
262263

263264
def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
264-
# Default behavior: add resource to the namespace
265+
# Hook method: called when all resources are collected.
266+
pass
267+
268+
def complete_graph(self, builder: GraphBuilder, source: Json) -> None:
269+
# Hook that is called when all resources have been collected and connected.
265270
pass
266271

267272
def __str__(self) -> str:
@@ -368,13 +373,27 @@ class AwsRegion(BaseRegion, AwsResource):
368373
}
369374
}
370375
ctime: Optional[datetime] = default_ctime
376+
region_in_use: Optional[bool] = field(default=None, metadata={"description": "Indicates if the region is in use."})
371377

372378
def __attrs_post_init__(self) -> None:
373379
super().__attrs_post_init__()
374380
self.long_name = cloud_region_data.get("aws", {}).get(self.id, {}).get("long_name")
375381
self.latitude = cloud_region_data.get("aws", {}).get(self.id, {}).get("latitude")
376382
self.longitude = cloud_region_data.get("aws", {}).get(self.id, {}).get("longitude")
377383

384+
def complete_graph(self, builder: GraphBuilder, source: Json) -> None:
385+
count = 0
386+
# A region with less than 10 real resources is considered not in use.
387+
# AWS is creating a couple of resources in every region automatically.
388+
# The number 10 is chosen by looking into different empty regions.
389+
empty_region = 10
390+
for succ in builder.graph.descendants(self):
391+
if not isinstance(succ, PhantomBaseResource):
392+
count += 1
393+
if count > empty_region:
394+
break
395+
self.region_in_use = count > empty_region
396+
378397

379398
@define(eq=False, slots=False)
380399
class AwsEc2VolumeType(AwsResource, BaseVolumeType):

plugins/aws/test/collector_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
called_collect_apis,
1212
called_mutator_apis,
1313
)
14-
from fix_plugin_aws.resource.base import AwsResource, AwsApiSpec, GraphBuilder
14+
from fix_plugin_aws.resource.base import AwsResource, AwsApiSpec, GraphBuilder, AwsRegion
1515
from fix_plugin_aws.resource.ec2 import AwsEc2Instance
1616
from fixlib.core.model_export import dataclasses_to_fixcore_model
1717
from test import account_collector, builder, aws_client, aws_config, no_feedback # noqa: F401
@@ -36,6 +36,9 @@ def count_kind(clazz: Type[AwsResource]) -> int:
3636
assert count_kind(AwsResource) == 226
3737
assert len(account_collector.graph.edges) == 516
3838
assert len(account_collector.graph.deferred_edges) == 2
39+
for node in account_collector.graph.nodes:
40+
if isinstance(node, AwsRegion):
41+
assert node.region_in_use
3942

4043

4144
def test_dependencies() -> None:

0 commit comments

Comments
 (0)