Skip to content

Commit 0c2bc07

Browse files
authored
[inventory][chore] Remove phantom property (#2207)
1 parent 14887b6 commit 0c2bc07

File tree

7 files changed

+16
-28
lines changed

7 files changed

+16
-28
lines changed

fixcore/fixcore/model/graph_access.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from fixcore.model.resolve_in_graph import GraphResolver, NodePath, ResolveProp
3131
from fixcore.model.typed_model import from_js
3232
from fixcore.types import Json, EdgeType, JsonElement
33-
from fixcore.util import utc, utc_str, value_in_path, set_value_in_path, value_in_path_get, path_exists
33+
from fixcore.util import utc, utc_str, value_in_path, set_value_in_path, path_exists
3434

3535
log = logging.getLogger(__name__)
3636

@@ -477,6 +477,7 @@ def resolve(self) -> None:
477477

478478
def __resolve_count_descendants(self) -> None:
479479
visited: Set[str] = set()
480+
empty_set: Set[str] = set()
480481

481482
def count_successors_by(node_id: NodeId, edge_type: EdgeType, path: List[str]) -> Dict[str, int]:
482483
result: Dict[str, int] = {}
@@ -487,7 +488,7 @@ def count_successors_by(node_id: NodeId, edge_type: EdgeType, path: List[str]) -
487488
if elem_id not in visited:
488489
visited.add(elem_id)
489490
elem = self.nodes[elem_id]
490-
if not value_in_path_get(elem, NodePath.is_phantom, False):
491+
if "phantom" not in elem.get("kinds_set", empty_set):
491492
extracted = value_in_path(elem, path)
492493
if isinstance(extracted, str):
493494
result[extracted] = result.get(extracted, 0) + 1

fixcore/fixcore/model/resolve_in_graph.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class NodePath:
2020
descendant_summary = ["metadata", "descendant_summary"]
2121
descendant_count = ["metadata", "descendant_count"]
2222
python_type = ["metadata", "python_type"]
23-
is_phantom = ["metadata", "phantom"]
2423
from_node = ["from"]
2524
to_node = ["to"]
2625
edge_type = ["edge_type"]

fixcore/fixcore/static/api-doc.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,6 @@ paths:
780780
name: root
781781
metadata:
782782
cleaned: false
783-
phantom: true
784783
protected: false
785784
application/yaml:
786785
example: |
@@ -795,7 +794,6 @@ paths:
795794
metadata:
796795
python_type: fixlib.baseresources.GraphRoot
797796
cleaned: false
798-
phantom: true
799797
protected: false
800798
kinds:
801799
- graph_root
@@ -813,7 +811,6 @@ paths:
813811
metadata:
814812
python_type: fixlib.baseresources.Cloud
815813
cleaned: false
816-
phantom: false
817814
protected: false
818815
descendant_summary:
819816
onelogin_account: 1
@@ -983,7 +980,6 @@ paths:
983980
name: root
984981
metadata:
985982
cleaned: false
986-
phantom: true
987983
protected: false
988984
application/yaml:
989985
example: |
@@ -998,7 +994,6 @@ paths:
998994
metadata:
999995
python_type: fixlib.baseresources.GraphRoot
1000996
cleaned: false
1001-
phantom: true
1002997
protected: false
1003998
kinds:
1004999
- graph_root
@@ -1016,7 +1011,6 @@ paths:
10161011
metadata:
10171012
python_type: fixlib.baseresources.Cloud
10181013
cleaned: false
1019-
phantom: false
10201014
protected: false
10211015
descendant_summary:
10221016
onelogin_account: 1
@@ -1454,7 +1448,6 @@ paths:
14541448
name: root
14551449
metadata:
14561450
cleaned: false
1457-
phantom: true
14581451
protected: false
14591452
application/yaml:
14601453
example: |
@@ -1469,7 +1462,6 @@ paths:
14691462
metadata:
14701463
python_type: fixlib.baseresources.GraphRoot
14711464
cleaned: false
1472-
phantom: true
14731465
protected: false
14741466
kinds:
14751467
- graph_root
@@ -1487,7 +1479,6 @@ paths:
14871479
metadata:
14881480
python_type: fixlib.baseresources.Cloud
14891481
cleaned: false
1490-
phantom: false
14911482
protected: false
14921483
descendant_summary:
14931484
onelogin_account: 1

fixcore/tests/fixcore/model/graph_access_test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,11 @@ def add_node(node_id: NodeId) -> None:
201201
"kind": kind,
202202
"some": {"deep": {"nested": node_id}},
203203
}
204+
kinds = [kind]
205+
metadata = {}
204206
# for sake of testing: declare parent as phantom resource
205-
metadata = {"phantom": True} if kind == "parent" else {}
207+
if kind == "parent":
208+
kinds.append("phantom")
206209
if node_id.startswith(replace_on):
207210
metadata["replace"] = True
208211
g.add_node(
@@ -211,8 +214,8 @@ def add_node(node_id: NodeId) -> None:
211214
reported=reported,
212215
metadata=metadata,
213216
kind=kind,
214-
kinds=[kind],
215-
kinds_set={kind},
217+
kinds=kinds,
218+
kinds_set=set(kinds),
216219
)
217220

218221
def add_edge(from_node: str, to_node: str, edge_type: EdgeType = EdgeTypes.default) -> None:

fixlib/fixlib/baseresources.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,9 @@ class BaseResource(ABC):
253253
the resource within the Graph. The name is used for display purposes. Tags are
254254
key/value pairs that get exported in the GRAPHML view.
255255
256-
There's also class variables, kind, phantom and reference_kinds.
256+
There's also class variables, kind, and reference_kinds.
257257
`kind` is a string describing the type of resource, e.g. 'aws_ec2_instance'
258258
or 'some_cloud_load_balancer'.
259-
`phantom` is a bool describing whether the resource actually exists within
260-
the cloud or if it's just a phantom resource like pricing information
261-
or usage quota. I.e. some information relevant to the cloud account
262-
but not actually existing in the form of a usable resource.
263259
`reference_kinds` is a list of kinds that can be connected to this resource for
264260
the related edge type as successor or predecessor.
265261
"""
@@ -268,7 +264,6 @@ class BaseResource(ABC):
268264
kind_display: ClassVar[str] = "Resource"
269265
kind_description: ClassVar[str] = "A generic resource."
270266
kind_service: ClassVar[Optional[str]] = None
271-
phantom: ClassVar[bool] = False
272267
reference_kinds: ClassVar[ModelReference] = {}
273268
metadata: ClassVar[Dict[str, Any]] = {"icon": "resource", "group": "misc"}
274269

@@ -455,7 +450,7 @@ def clean(self) -> bool:
455450
@clean.setter
456451
@unless_protected
457452
def clean(self, value: bool) -> None:
458-
if self.phantom and value:
453+
if isinstance(self, PhantomBaseResource) and value:
459454
raise ValueError(f"Can't cleanup phantom resource {self.rtdname}")
460455

461456
clean_str = "" if value else "not "
@@ -491,7 +486,7 @@ def protected(self, value: bool) -> None:
491486
@metrics_resource_cleanup.time()
492487
@unless_protected
493488
def cleanup(self, graph: Optional[Any] = None) -> bool:
494-
if self.phantom:
489+
if isinstance(self, PhantomBaseResource):
495490
raise RuntimeError(f"Can't cleanup phantom resource {self.rtdname}")
496491

497492
if self.cleaned:
@@ -540,7 +535,7 @@ def pre_cleanup(self, graph: Optional[Any] = None) -> bool:
540535
if graph is None:
541536
graph = self._graph
542537

543-
if self.phantom:
538+
if isinstance(self, PhantomBaseResource):
544539
raise RuntimeError(f"Can't cleanup phantom resource {self.rtdname}")
545540

546541
if self.cleaned:
@@ -759,7 +754,6 @@ class PhantomBaseResource(BaseResource):
759754
kind: ClassVar[str] = "phantom_resource"
760755
kind_display: ClassVar[str] = "Phantom Resource"
761756
kind_description: ClassVar[str] = "A generic phantom resource."
762-
phantom: ClassVar[bool] = True
763757

764758
def update_tag(self, key: str, value: str) -> bool:
765759
log.error(f"Resource {self.rtdname} is a phantom resource and does not maintain tags")

fixlib/fixlib/core/model_export.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ def node_to_dict(node: BaseResource, changes_only: bool = False, include_revisio
339339
"metadata": {
340340
"python_type": type_str(node),
341341
"cleaned": node.cleaned,
342-
"phantom": node.phantom,
343342
"protected": node.protected,
344343
"categories": node.categories(),
345344
**node._metadata,

plugins/aws/fix_plugin_aws/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
Cloud,
2222
metrics_resource_cleanup_exceptions,
2323
metrics_resource_pre_cleanup_exceptions,
24+
PhantomBaseResource,
2425
)
2526
from fixlib.config import Config, RunningConfig
2627
from fixlib.core.actions import CoreFeedback
@@ -272,7 +273,7 @@ def pre_cleanup(config: Config, resource: BaseResource, graph: Graph) -> bool:
272273
resource.log("Modification was requested even though resource is protected" " - refusing")
273274
return False
274275

275-
if resource.phantom:
276+
if isinstance(resource, PhantomBaseResource):
276277
log.warning(f"Can't cleanup phantom resource {resource.rtdname}")
277278
return False
278279

@@ -314,7 +315,7 @@ def pre_cleanup(config: Config, resource: BaseResource, graph: Graph) -> bool:
314315
@staticmethod
315316
def cleanup(config: Config, resource: BaseResource, graph: Graph) -> bool:
316317
if isinstance(resource, AwsResource):
317-
if resource.phantom:
318+
if isinstance(resource, PhantomBaseResource):
318319
raise RuntimeError(f"Can't cleanup phantom resource {resource.rtdname}")
319320

320321
if resource.cleaned:

0 commit comments

Comments
 (0)