def _load_resource_collection(self, collection, boto_base_resource=None, awspx_base_resource=None): resources = [] label = "AWS::{service}::{type}".format( service=self.__class__.__name__.capitalize(), type=getattr(boto_base_resource, collection)._model.resource.type) # Known cases of misaligned naming conventions (boto and lib.aws.RESOURCES) if label.endswith("Version"): label = label[:-7] elif label == "AWS::Ec2::KeyPairInfo": label = "AWS::Ec2::KeyPair" # Skip excluded types or types unknown to us if label not in RESOURCES.types \ or (not self.run_all and label not in self.run): return try: # Note: Depending on the stage at which the exception is thrown, we # may miss certain resources. resources = [ r for r in getattr(boto_base_resource, collection).all() ] except Exception as e: print(f"[!] Couldn't load {collection} of {boto_base_resource} " "-- probably due to a resource based policy or something.") for resource in resources: # Get properties properties = resource.meta.data if properties is None: continue # Get Arn and Name arn = self._get_resource_arn(resource, boto_base_resource) name = self._get_resource_name(resource) # Include or exclude this ARN if self.only_arns and arn not in self.only_arns: continue elif self.except_arns and arn in self.except_arns: continue properties["Arn"] = arn properties["Name"] = name if not (properties["Arn"] and properties["Name"]): print(label) print(json.dumps(properties, indent=2, default=str)) raise ValueError # Create resource r = Resource(labels=[label], properties=properties) if r not in self: self._print(f"[*] Adding {r}") self.append(r) # Add associative relationship with parent if awspx_base_resource: assocs = [set(a) for a in self.associates] if {awspx_base_resource.labels()[0], r.labels()[0]} in assocs \ or not self.associates: e = Associative(properties={"Name": "Attached"}, source=r, target=awspx_base_resource) oe = Associative(properties={"Name": "Attached"}, source=awspx_base_resource, target=r) if not (e in self or oe in self): self.append(e) # Load resources from this one's collections if self._get_collections(resource): self._load_resources(resource, r) # Return when we've seen all explicit resources if self.only_arns and all( [r in map(lambda x: x.id(), self) for r in self.only_arns]): return
def _load_resource_collection(self, collection, boto_base_resource=None, awspx_base_resource=None): resources = [] try: # Note: Depending on the stage at which the exception is thrown, we # may miss certain resources. resources = [ r for r in getattr(boto_base_resource, collection).all() ] except Exception as e: print(f"Couldn't load {collection} of {boto_base_resource} " "- probably due to a resource based policy or something.") for resource in resources: # Get properties properties = resource.meta.data if properties is None: continue # Get label resource_type = resource.__class__.__name__.split(".")[-1] label = self._get_resource_type_label(resource_type) # Get Arn and Name arn = self._get_resource_arn(resource, boto_base_resource) name = self._get_resource_name(resource) # Include or exclude this ARN if self.only_arns and arn not in self.only_arns: continue elif self.except_arns and arn in self.except_arns: continue properties["Arn"] = arn properties["Name"] = name if not (properties["Arn"] and properties["Name"]): print(label) print(json.dumps(properties, indent=2, default=str)) raise ValueError # Create resource r = Resource(labels=[label], properties=properties) if r not in self: print(f" \-> Adding {r}") self.append(r) # Add associative relationship with parent if awspx_base_resource: assocs = [set(a) for a in self.associates] if {awspx_base_resource.labels()[0], r.labels()[0]} in assocs \ or not self.associates: e = Associative(properties={"Name": "Attached"}, source=r, target=awspx_base_resource) oe = Associative(properties={"Name": "Attached"}, source=awspx_base_resource, target=r) if not (e in self or oe in self): self.append(e) # Load resources from this one's collections if self._get_collections(resource): self._load_resources(resource, r) # Return when we've seen all explicit resources if self.only_arns and all( [r in map(lambda x: x.id(), self) for r in self.only_arns]): return