From f593e93c0d3f4c322935cde4fa99b37a6177cfa7 Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Wed, 17 Jul 2024 11:23:03 +0200 Subject: [PATCH] [azure][feat] Case insensitive connect (#2140) --- plugins/azure/fix_plugin_azure/collector.py | 3 ++- plugins/azure/fix_plugin_azure/config.py | 27 +++++++++++++++++++ .../azure/fix_plugin_azure/resource/base.py | 5 +++- plugins/azure/fix_plugin_azure/utils.py | 7 +++++ plugins/azure/test/conftest.py | 2 +- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/plugins/azure/fix_plugin_azure/collector.py b/plugins/azure/fix_plugin_azure/collector.py index 29a72b3afc..b82c00ae90 100644 --- a/plugins/azure/fix_plugin_azure/collector.py +++ b/plugins/azure/fix_plugin_azure/collector.py @@ -167,7 +167,8 @@ def work_done(_: Future[None]) -> None: group_futures = [] self.core_feedback.progress_done(name, 0, 1, context=[self.cloud.id, self.account.id]) for resource_type in resources: - group_futures.append(builder.submit_work("azure_all", collect_resource, resource_type)) + if self.config.should_collect(resource_type.kind): + group_futures.append(builder.submit_work("azure_all", collect_resource, resource_type)) all_done = GatherFutures.all(group_futures) all_done.add_done_callback(work_done) return all_done diff --git a/plugins/azure/fix_plugin_azure/config.py b/plugins/azure/fix_plugin_azure/config.py index 77c087a99f..985ca0a52e 100644 --- a/plugins/azure/fix_plugin_azure/config.py +++ b/plugins/azure/fix_plugin_azure/config.py @@ -1,3 +1,4 @@ +from fnmatch import fnmatch from typing import ClassVar, Optional, Dict, List, Union from attr import define, field @@ -83,3 +84,29 @@ class AzureConfig: default=True, metadata={"description": "Collect resource usage metrics via Azure Metric, enabled by default"}, ) + collect: List[str] = field( + factory=list, + metadata={ + "description": ( + "List of Azure services to collect (default: all).\n" + "You can use GLOB patterns like ? and * to match multiple services." + ) + }, + ) + no_collect: List[str] = field( + factory=list, + metadata={ + "description": ( + "List of Azure services to exclude (default: none).\n" + "You can use GLOB patterns like ? and * to match multiple services." + ) + }, + ) + + def should_collect(self, name: str) -> bool: + # no_collect has precedence over collect + if self.no_collect and any(fnmatch(name, p) for p in self.no_collect): + return False + if self.collect: + return any(fnmatch(name, p) for p in self.collect) + return True diff --git a/plugins/azure/fix_plugin_azure/resource/base.py b/plugins/azure/fix_plugin_azure/resource/base.py index 8cb1b22fc9..a618607886 100644 --- a/plugins/azure/fix_plugin_azure/resource/base.py +++ b/plugins/azure/fix_plugin_azure/resource/base.py @@ -10,6 +10,7 @@ from fix_plugin_azure.azure_client import AzureResourceSpec, MicrosoftClient, MicrosoftRestSpec from fix_plugin_azure.config import AzureConfig +from fix_plugin_azure.utils import case_insensitive_eq from fixlib.baseresources import ( BaseGroup, BaseResource, @@ -653,7 +654,9 @@ def node( for n in self.graph: if clazz and not isinstance(n, clazz): continue - if (filter_fn(n) if filter_fn else True) and all(getattr(n, k, None) == v for k, v in node.items()): + if (filter_fn(n) if filter_fn else True) and all( + case_insensitive_eq(getattr(n, k, None), v) for k, v in node.items() + ): return n # type: ignore return None diff --git a/plugins/azure/fix_plugin_azure/utils.py b/plugins/azure/fix_plugin_azure/utils.py index a751b9d639..0a1c51da27 100644 --- a/plugins/azure/fix_plugin_azure/utils.py +++ b/plugins/azure/fix_plugin_azure/utils.py @@ -37,6 +37,13 @@ def identity(x: T) -> T: return x +def case_insensitive_eq(left: T, right: T) -> bool: + if isinstance(left, str) and isinstance(right, str): + return left.lower() == right.lower() + else: + return left == right + + @frozen(kw_only=True) class MetricNormalization: metric_name: MetricName diff --git a/plugins/azure/test/conftest.py b/plugins/azure/test/conftest.py index 1dcac8cc73..e9db1baad9 100644 --- a/plugins/azure/test/conftest.py +++ b/plugins/azure/test/conftest.py @@ -185,9 +185,9 @@ def connect_resources( # collect all defined resource kinds before we can connect them for resource_kind in collect_resources or []: resource_kind.collect_resources(builder) + builder.executor.wait_for_submitted_work() # connect all resources for node, data in list(builder.graph.nodes(data=True)): if not filter_class or isinstance(node, filter_class): node.connect_in_graph(builder, data.get("source", {})) - builder.executor.wait_for_submitted_work()