Skip to content

Commit

Permalink
[azure][feat] Case insensitive connect (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored Jul 17, 2024
1 parent 398ba01 commit f593e93
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion plugins/azure/fix_plugin_azure/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions plugins/azure/fix_plugin_azure/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from fnmatch import fnmatch
from typing import ClassVar, Optional, Dict, List, Union

from attr import define, field
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion plugins/azure/fix_plugin_azure/resource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions plugins/azure/fix_plugin_azure/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugins/azure/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit f593e93

Please sign in to comment.