Source code for gcp.compute.resources

from conftest import gcp_client


[docs]def firewalls(): return gcp_client.list("compute", "firewalls")
[docs]def networks(): return gcp_client.list("compute", "networks")
[docs]def instances(): return gcp_client.list("compute", "instances")
[docs]def clusters(): results = [] for project_id in gcp_client.project_list: results += gcp_client.list( "container", "projects.locations.clusters", results_key="clusters", call_kwargs={"parent": "projects/{}/locations/-".format(project_id)}, ) return results
[docs]def networks_with_instances(): allInstances = instances() in_use_networks = [] for network in networks(): network["instances"] = [] for instance in allInstances: if network["selfLink"] in [ interface["network"] for interface in instance["networkInterfaces"] ]: network["instances"].append(instance) if len(network["instances"]): in_use_networks.append(network) return in_use_networks
[docs]def in_use_firewalls(): all_networks = networks_with_instances() results = [] for firewall in firewalls(): if firewall["disabled"] == True: continue for network in all_networks: if ( network["selfLink"] == firewall["network"] and len(network["instances"]) > 0 ): results.append(firewall) return results