|
| 1 | +from typing import Dict, Iterator |
| 2 | +from typing import Optional, ClassVar |
| 3 | + |
| 4 | +from attr import define, field |
| 5 | +from resotoclient import ResotoClient, JsObject |
| 6 | +from resotolib.baseplugin import BaseCollectorPlugin |
| 7 | +from resotolib.baseresources import ( |
| 8 | + BaseResource, |
| 9 | + Cloud, |
| 10 | + EdgeType, |
| 11 | + UnknownZone, |
| 12 | + UnknownRegion, |
| 13 | + UnknownAccount, |
| 14 | +) |
| 15 | +from resotolib.config import Config |
| 16 | +from resotolib.core.actions import CoreFeedback |
| 17 | +from resotolib.core.model_export import node_from_dict |
| 18 | +from resotolib.graph import Graph |
| 19 | +from resotolib.json import value_in_path |
| 20 | +from resotolib.logger import log |
| 21 | +from resotolib.types import Json |
| 22 | + |
| 23 | + |
| 24 | +@define |
| 25 | +class RemoteGraphConfig: |
| 26 | + kind: ClassVar[str] = "remote_graph" |
| 27 | + resoto_url: str = field(default="https://localhost:8900", metadata={"description": "URL of the resoto server"}) |
| 28 | + psk: Optional[str] = field(default=None, metadata={"description": "Pre-shared key for the resoto server"}) |
| 29 | + graph: str = field(default="resoto", metadata={"description": "Name of the graph to use"}) |
| 30 | + search: Optional[str] = field( |
| 31 | + default=None, metadata={"description": "Search string to filter resources. None to get all resources."} |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +carz = {"cloud": Cloud, "account": UnknownAccount, "region": UnknownRegion, "zone": UnknownZone} |
| 36 | + |
| 37 | + |
| 38 | +class RemoteGraphCollector(BaseCollectorPlugin): |
| 39 | + cloud = "remote_graph" |
| 40 | + |
| 41 | + def __init__(self) -> None: |
| 42 | + super().__init__() |
| 43 | + self.core_feedback: Optional[CoreFeedback] = None |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def add_config(cfg: Config) -> None: |
| 47 | + cfg.add_config(RemoteGraphConfig) |
| 48 | + |
| 49 | + def collect(self) -> None: |
| 50 | + try: |
| 51 | + self.graph = self._collect_remote_graph() |
| 52 | + except Exception as ex: |
| 53 | + if self.core_feedback: |
| 54 | + self.core_feedback.error(f"Unhandled exception in Remote Plugin: {ex}", log) |
| 55 | + else: |
| 56 | + log.error(f"No CoreFeedback available! Unhandled exception in RemoteGraph Plugin: {ex}") |
| 57 | + raise |
| 58 | + |
| 59 | + def _collect_remote_graph(self) -> Graph: |
| 60 | + config: RemoteGraphConfig = Config.remote_graph |
| 61 | + client = ResotoClient(config.resoto_url, psk=config.psk) |
| 62 | + search = config.search or "is(graph_root) -[2:]->" |
| 63 | + return self._collect_from_graph_iterator(client.search_graph(search, graph=config.graph)) |
| 64 | + |
| 65 | + def _collect_from_graph_iterator(self, graph_iterator: Iterator[JsObject]) -> Graph: |
| 66 | + assert self.core_feedback, "No CoreFeedback available!" |
| 67 | + graph = Graph() |
| 68 | + lookup: Dict[str, BaseResource] = {} |
| 69 | + self.core_feedback.progress_done("Remote Graph", 0, 1) |
| 70 | + |
| 71 | + def set_carz(jsc: Json, rs: BaseResource) -> None: |
| 72 | + for ancestor, clazz in carz.items(): |
| 73 | + path = ["ancestors", ancestor, "reported"] |
| 74 | + if (cv := value_in_path(jsc, path)) and (cid := cv.get("id")) and (cname := cv.get("name")): |
| 75 | + resource = lookup.get(cid, clazz(id=cid, name=cname)) # type: ignore |
| 76 | + lookup[cid] = resource |
| 77 | + setattr(rs, f"_{ancestor}", resource) # xxx is defined by _xxx property |
| 78 | + |
| 79 | + for js in graph_iterator: |
| 80 | + if js.get("type") == "node" and isinstance(js, dict): |
| 81 | + node = node_from_dict(js) |
| 82 | + set_carz(js, node) |
| 83 | + lookup[js["id"]] = node |
| 84 | + graph.add_node(node) |
| 85 | + elif js.get("type") == "edge": |
| 86 | + if (node_from := lookup.get(js["from"])) and (node_to := lookup.get(js["to"])): |
| 87 | + graph.add_edge(node_from, node_to, edge_type=EdgeType.default) |
| 88 | + else: |
| 89 | + raise ValueError(f"Unknown type: {js.get('type')}") |
| 90 | + self.core_feedback.progress_done("Remote Graph", 1, 1) |
| 91 | + return graph |
0 commit comments