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: if ArgumentParser.args.cleanup_dry_run: log.debug(f'Resource {node.resource_type} {node.dname} is marked for removal, not calling cleanup method because of dry run flag') return log.debug(f'Resource {node.resource_type} {node.dname} is marked for removal, calling cleanup method') try: node.cleanup(self.graph) except Exception: log.exception(f'An exception occurred when running resource cleanup on {node.resource_type} {node.dname}')
def clean(self, node: BaseResource) -> None: log_prefix = f"Resource {node.rtdname} is marked for removal" if ArgumentParser.args.cleanup_dry_run: log.debug( f"{log_prefix}, not calling cleanup method because of dry run flag" ) return log.debug(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 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 ArgumentParser.args.cleanup_dry_run: log.debug( f"{log_prefix}, not calling pre cleanup method because of dry run flag" ) return log.debug(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 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)