-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist-gce.py
95 lines (79 loc) · 3.88 KB
/
list-gce.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# List GCE instances
# 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;zone;instance_name;cpuPlatform;machineType;',
'status;lastStartTimestamp;preemptible;automaticRestart;onHostMaintenance;',
'disk_amount;disk_total_size;publicIP;nic_amount;creationTimestamp')
for project_validated in project_valid:
try:
zone_request = compute.zones().list(project=project_validated)
zones = zone_request.execute()
#print(zones)
for zone in zones['items']:
resp = compute.instances().list(project=project_validated, zone=zone.get('name')).execute()
#print(resp)
try:
for gce in resp['items']:
diskAmt = diskSiz =0
ipAmt = 0
ipExt = 'None'
for disk in gce['disks']:
diskAmt +=1
diskSiz = diskSiz + float(disk.get('diskSizeGb'))
for accessConfig in gce['networkInterfaces']:
ipAmt +=1
try:
for nats in accessConfig['accessConfigs']:
if nats.get('type',{}) == 'ONE_TO_ONE_NAT':
ipExt=nats.get('natIP')
except KeyError: pass
# Remove the full url for machineType
machineTypeUrl=gce.get('machineType').split(sep="/")
machineType=machineTypeUrl[len(machineTypeUrl)-1]
print (
project_validated, ';',
#project.name, ';',
zone.get('name'),';',
gce.get('name'), ';',
gce.get('cpuPlatform'), ';',
machineType, ';',
gce.get('status'), ';',
gce.get('lastStartTimestamp'), ';',
gce.get('scheduling').get('preemptible'), ';',
gce.get('scheduling').get('automaticRestart'), ';',
gce.get('scheduling').get('onHostMaintenance'), ';',
diskAmt, ';',
diskSiz, ';',
ipExt, ';',
ipAmt, ';',
gce.get('creationTimestamp')
)
except KeyError: pass
except: pass