-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist-vpc.py
51 lines (44 loc) · 1.82 KB
/
list-vpc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# List VPCs
# Official GCP SDK (Python) Documentation: https://googleapis.github.io/google-api-python-client/docs/dyn/
import json
import sys
import argparse
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from google.cloud import resource_manager
client = resource_manager.Client()
credentials = GoogleCredentials.get_application_default()
compute = discovery.build('compute', 'v1', credentials=credentials)
# Filter of Projects that will be scanned
parser_args = argparse.ArgumentParser(description='Define the projetc_id filter.'
'if empity will looking for all the active project_id that the credential have access.'
'Support for comma separeted projects')
parser_args.add_argument('--project')
project_filter = parser_args.parse_args()
# Project parameter validation
project_valid = []
if project_filter.project is None:
env_filter = {'lifecycleState': 'ACTIVE' }
for project_param in client.list_projects(env_filter):
project_valid.append(project_param.project_id)
else:
project_list = project_filter.project.split(',')
for project_listed in project_list:
env_filter = {'projectId': project_listed ,'lifecycleState': 'ACTIVE' }
for project_param in client.list_projects(env_filter):
project_valid.append(project_param.project_id)
# print csv header
print ('project_id;vpc;autoCreateSubnetworks;subnets_count')
for project_validated in project_valid:
try:
networks = compute.networks().list(project=project_validated)
vpcs = networks.execute()
#print(vpc)
for vpc in vpcs['items']:
print(
project_validated,';',
vpc.get('name'),';',
vpc.get('autoCreateSubnetworks'),';',
len(vpc.get('subnetworks')),
)
except: pass