def set_max_depth(graph: Graph, node: BaseResource, current_depth: int = 0) -> None: if isinstance(node, BaseResource): if current_depth > node.max_graph_depth: node.max_graph_depth = current_depth for child_node in node.successors(graph): set_max_depth(graph, child_node, current_depth + 1)
def clean(self, node: BaseResource) -> None: log_prefix = f"Resource {node.rtdname} is marked for removal" if Config.resotoworker.cleanup_dry_run: log.info( f"{log_prefix}, not calling cleanup method because of dry run flag" ) return log.info(f"{log_prefix}, calling cleanup method") try: node.cleanup(self.graph) except Exception: log.exception( f"An exception occurred when running resource cleanup on {node.rtdname}" )
def add_edge( self, src: BaseResource, dst: BaseResource, key: EdgeKey = None, edge_type: EdgeType = None, **attr, ): if src is None or dst is None: log.error(f"Not creating edge from or to NoneType: {src} to {dst}") return if edge_type is None: edge_type = EdgeType.default if key is None: key = EdgeKey(src=src, dst=dst, edge_type=edge_type) if self.has_edge(src, dst, key=key): log.error(f"Edge from {src} to {dst} already exists in graph") return return_key = super().add_edge(src, dst, key=key, **attr) if ( self._log_edge_creation and isinstance(src, BaseResource) and isinstance(dst, BaseResource) ): log.debug( f"Added edge from {src.rtdname} to {dst.rtdname} (type: {edge_type.value})" ) try: src.successor_added(dst, self) except Exception: log.exception( ( f"Unhandled exception while telling {src.rtdname}" f" that {dst.rtdname} was added as a successor" ) ) try: dst.predecessor_added(src, self) except Exception: log.exception( ( f"Unhandled exception while telling {dst.rtdname}" f" that {src.rtdname} was added as a predecessor" ) ) return return_key
def update_label(resource: BaseResource, key: str, value: str) -> bool: get_kwargs = {str(resource._get_identifier): resource.name} set_labels_kwargs = {str(resource._set_label_identifier): resource.name} common_kwargs = common_resource_kwargs(resource) get_kwargs.update(common_kwargs) set_labels_kwargs.update(common_kwargs) labels = dict(resource.tags) if value is None: if key in labels: del labels[key] else: return False else: labels.update({key: value}) body = {"labels": labels, "labelFingerprint": resource.label_fingerprint} set_labels_kwargs["body"] = body gr = gcp_resource(resource) request = gr.setLabels(**set_labels_kwargs) response = request.execute() # Update label_fingerprint request = gr.get(**get_kwargs) response = request.execute() resource.label_fingerprint = response.get("labelFingerprint") return True
def pre_clean(self, node: BaseResource) -> None: if not hasattr(node, "pre_delete"): return log_prefix = f"Resource {node.rtdname} is marked for removal" if Config.resotoworker.cleanup_dry_run: log.info( f"{log_prefix}, not calling pre cleanup method because of dry run flag" ) return log.info(f"{log_prefix}, calling pre cleanup method") try: node.pre_cleanup(self.graph) except Exception: log.exception( ("An exception occurred when running resource pre cleanup on" f" {node.rtdname}"))
def common_resource_kwargs(resource: BaseResource) -> Dict: common_kwargs = {} if resource.account().id != "undefined" and "project" in resource.resource_args: common_kwargs["project"] = resource.account().id if resource.zone().name != "undefined" and "zone" in resource.resource_args: common_kwargs["zone"] = resource.zone().name elif resource.region().name != "undefined" and "region" in resource.resource_args: common_kwargs["region"] = resource.region().name return common_kwargs
def gcp_service(resource: BaseResource, graph: Graph = None): service_kwargs = {} if resource.account().id != "undefined": service_kwargs["credentials"] = Credentials.get(resource.account(graph).id) return gcp_client(resource.client, resource.api_version, **service_kwargs)
def add_node(self, node_for_adding: BaseResource, **attr): super().add_node(node_for_adding, **attr) if isinstance(node_for_adding, BaseResource): # We hand a reference to ourselves to the added BaseResource # which stores it as a weakref. node_for_adding._graph = self
def aws_resource(resource: BaseResource, service: str, graph: Graph = None): return aws_session(resource.account(graph).id, resource.account(graph).role).resource( service, region_name=resource.region(graph).id )