Skip to content

Commit 6255c40

Browse files
authored
[azure][feat]: Add safety read/write access to the graph (#2082)
1 parent 21afa20 commit 6255c40

File tree

1 file changed

+12
-12
lines changed
  • plugins/azure/fix_plugin_azure/resource

1 file changed

+12
-12
lines changed

plugins/azure/fix_plugin_azure/resource/base.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
from concurrent.futures import Future
55
from datetime import datetime, timedelta
6-
from threading import Lock
76
from typing import Any, ClassVar, Dict, Optional, TypeVar, List, Type, Callable, cast
87

98
from attr import define, field
@@ -18,6 +17,7 @@
1817
from fixlib.graph import Graph, EdgeKey
1918
from fixlib.json_bender import Bender, bend, S, ForallBend, Bend
2019
from fixlib.threading import ExecutorQueue
20+
from fixlib.lock import RWLock
2121
from fixlib.types import Json
2222
from fixlib.config import current_config
2323

@@ -507,8 +507,8 @@ def __init__(
507507
config: AzureConfig,
508508
location_lookup: Optional[Dict[str, AzureLocation]] = None,
509509
location: Optional[AzureLocation] = None,
510-
graph_nodes_access: Optional[Lock] = None,
511-
graph_edges_access: Optional[Lock] = None,
510+
graph_nodes_access: Optional[RWLock] = None,
511+
graph_edges_access: Optional[RWLock] = None,
512512
last_run_started_at: Optional[datetime] = None,
513513
) -> None:
514514
self.graph = graph
@@ -519,8 +519,8 @@ def __init__(
519519
self.core_feedback = core_feedback
520520
self.location_lookup = location_lookup or {}
521521
self.location = location
522-
self.graph_nodes_access = graph_nodes_access or Lock()
523-
self.graph_edges_access = graph_edges_access or Lock()
522+
self.graph_nodes_access = graph_nodes_access or RWLock()
523+
self.graph_edges_access = graph_edges_access or RWLock()
524524
self.name = f"Azure:{subscription.name}"
525525
self.config = config
526526
self.last_run_started_at = last_run_started_at
@@ -570,7 +570,7 @@ def node(
570570
"""
571571
if isinstance(nd := node.get("node"), AzureResource):
572572
return nd # type: ignore
573-
with self.graph_nodes_access:
573+
with self.graph_nodes_access.read_access:
574574
for n in self.graph:
575575
if clazz and not isinstance(n, clazz):
576576
continue
@@ -591,7 +591,7 @@ def nodes(
591591
result: List[AzureResourceType] = []
592592
if isinstance(nd := node.get("node"), AzureResource):
593593
result.append(nd) # type: ignore
594-
with self.graph_nodes_access:
594+
with self.graph_nodes_access.read_access:
595595
for n in self.graph:
596596
if clazz and not isinstance(n, clazz):
597597
continue
@@ -629,7 +629,7 @@ def add_node(self, node: AzureResourceType, source: Optional[Json] = None) -> Op
629629
node._metadata["provider_link"] = f"https://portal.azure.com/#@/resource{node.id}/overview"
630630

631631
if last_edge_key is not None:
632-
with self.graph_nodes_access:
632+
with self.graph_nodes_access.write_access:
633633
self.graph.add_node(node, source=source or {})
634634
return node
635635
else:
@@ -651,7 +651,7 @@ def add_edge(
651651
if isinstance(from_node, AzureResource) and isinstance(to_n, AzureResource):
652652
start, end = (to_n, from_node) if reverse else (from_node, to_n)
653653
log.debug(f"{self.name}: add edge: {start} -> {end} [{edge_type}]")
654-
with self.graph_edges_access:
654+
with self.graph_edges_access.write_access:
655655
return self.graph.add_edge(start, end, edge_type=edge_type)
656656
return None
657657

@@ -677,21 +677,21 @@ def dependant_node(
677677
if isinstance(from_node, AzureResource) and isinstance(to_n, AzureResource):
678678
start, end = (to_n, from_node) if reverse else (from_node, to_n)
679679
log.debug(f"{self.name}: add edge: {start} -> {end} [default]")
680-
with self.graph_edges_access:
680+
with self.graph_edges_access.write_access:
681681
self.graph.add_edge(start, end, edge_type=EdgeType.default)
682682
if delete_same_as_default:
683683
start, end = end, start
684684
log.debug(f"{self.name}: add edge: {end} -> {start} [delete]")
685685
self.graph.add_edge(end, start, edge_type=EdgeType.delete)
686686

687687
def resources_of(self, resource_type: Type[AzureResourceType]) -> List[AzureResourceType]:
688-
with self.graph_nodes_access:
688+
with self.graph_nodes_access.read_access:
689689
return [n for n in self.graph.nodes if isinstance(n, resource_type)]
690690

691691
def edges_of(
692692
self, from_type: Type[AzureResource], to_type: Type[AzureResource], edge_type: EdgeType = EdgeType.default
693693
) -> List[EdgeKey]:
694-
with self.graph_edges_access:
694+
with self.graph_edges_access.read_access:
695695
return [
696696
key
697697
for (from_node, to_node, key) in self.graph.edges

0 commit comments

Comments
 (0)