Skip to content

Commit 73218b4

Browse files
authored
[azure][fix] Compute unused regions as last step (#2228)
1 parent c6e9b82 commit 73218b4

File tree

7 files changed

+19
-10
lines changed

7 files changed

+19
-10
lines changed

plugins/aws/fix_plugin_aws/collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def rm_leaf_nodes(cls: Any, ignore_kinds: Optional[Type[Any]] = None, check_pred
366366

367367
rm_leaf_nodes(bedrock.AwsBedrockFoundationModel, check_pred=False)
368368
# remove regions that are not in use
369-
self.graph.remove_recursively(builder.nodes(AwsRegion, lambda r: r.region_in_use is False))
369+
self.graph.remove_recursively(builder.nodes(AwsRegion, lambda r: r.compute_region_in_use(builder) is False))
370370

371371
# TODO: move into separate AwsAccountSettings
372372
def update_account(self) -> None:

plugins/aws/fix_plugin_aws/resource/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def __attrs_post_init__(self) -> None:
370370
self.latitude = cloud_region_data.get("aws", {}).get(self.id, {}).get("latitude")
371371
self.longitude = cloud_region_data.get("aws", {}).get(self.id, {}).get("longitude")
372372

373-
def complete_graph(self, builder: GraphBuilder, source: Json) -> None:
373+
def compute_region_in_use(self, builder: GraphBuilder) -> bool:
374374
count = 0
375375
ignore_kinds = {
376376
"aws_athena_work_group",
@@ -399,7 +399,9 @@ def ignore_for_count(resource: BaseResource) -> bool:
399399
count += 1
400400
if count > empty_region:
401401
break
402-
self.region_in_use = count > empty_region
402+
in_use = count > empty_region
403+
self.region_in_use = in_use
404+
return in_use
403405

404406

405407
@define(eq=False, slots=False)

plugins/azure/fix_plugin_azure/collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def remove_usage_zero_value() -> None:
277277
rm_leaf_nodes(AzureCosmosDBLocation, AzureLocation, check_pred=False)
278278
rm_leaf_nodes(AzureLocation, check_pred=False)
279279
remove_usage_zero_value()
280-
self.graph.remove_recursively(builder.nodes(AzureLocation, lambda r: r.region_in_use is False))
280+
self.graph.remove_recursively(builder.nodes(AzureLocation, lambda r: r.compute_region_in_use(builder) is False))
281281

282282
def after_collect(self, builder: GraphBuilder) -> None:
283283
# Filter unnecessary nodes such as AzureComputeDiskTypePricing

plugins/azure/fix_plugin_azure/resource/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class AzureLocation(MicrosoftResource, BaseRegion):
342342
regional_display_name: Optional[str] = field(default=None, metadata={'description': 'The display name of the location and its region.'}) # fmt: skip
343343
subscription_id: Optional[str] = field(default=None, metadata={"description": "The subscription id."})
344344

345-
def post_process(self, graph_builder: GraphBuilder, source: Json) -> None:
345+
def compute_region_in_use(self, graph_builder: GraphBuilder) -> bool:
346346
ignore_kinds: Set[str] = {"azure_network_virtual_network", "azure_network_watcher"}
347347

348348
def ignore_for_count(resource: BaseResource) -> bool:
@@ -361,7 +361,10 @@ def ignore_for_count(resource: BaseResource) -> bool:
361361
count += 1
362362
if count > empty_region:
363363
break
364-
self.region_in_use = count > empty_region
364+
365+
in_use = count > empty_region
366+
self.region_in_use = in_use
367+
return in_use
365368

366369

367370
@define(eq=False, slots=False)

plugins/azure/test/collector_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def test_collect(
4848
config, Cloud(id="azure"), azure_subscription, credentials, core_feedback
4949
)
5050
subscription_collector.collect()
51-
assert len(subscription_collector.graph.nodes) == 455
52-
assert len(subscription_collector.graph.edges) == 679
51+
assert len(subscription_collector.graph.nodes) == 588
52+
assert len(subscription_collector.graph.edges) == 977
5353

5454
graph_collector = MicrosoftGraphOrganizationCollector(
5555
config, Cloud(id="azure"), MicrosoftGraphOrganization(id="test", name="test"), credentials, core_feedback

plugins/gcp/fix_plugin_gcp/collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def rm_leaf_nodes(clazz: Any, ignore_kinds: Optional[Type[Any]] = None) -> None:
140140
rm_leaf_nodes(billing.GcpSku)
141141
rm_leaf_nodes(billing.GcpService)
142142
# remove regions that are not in use
143-
self.graph.remove_recursively(builder.nodes(GcpRegion, lambda r: r.region_in_use is False))
143+
self.graph.remove_recursively(builder.nodes(GcpRegion, lambda r: r.compute_region_in_use(builder) is False))
144144

145145
def collect_region(self, regional_builder: GraphBuilder) -> None:
146146
# fetch all region level resources

plugins/gcp/fix_plugin_gcp/resources/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ def post_process(self, graph_builder: GraphBuilder, source: Json) -> None:
569569
graph_builder.add_node(region_quota, source)
570570
graph_builder.add_edge(self, node=region_quota)
571571

572+
def compute_region_in_use(self, graph_builder: GraphBuilder) -> bool:
572573
ignore_kinds = {
573574
"gcp_subnetwork", # There are subnetworks that are created by GCP automatically.
574575
}
@@ -589,7 +590,10 @@ def ignore_for_count(resource: BaseResource) -> bool:
589590
count += 1
590591
if count > empty_region:
591592
break
592-
self.region_in_use = count > empty_region
593+
594+
in_use = count > empty_region
595+
self.region_in_use = in_use
596+
return in_use
593597

594598
def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
595599
super().connect_in_graph(builder, source)

0 commit comments

Comments
 (0)