-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist-sql.py
66 lines (60 loc) · 2.73 KB
/
list-sql.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
# List CloudSQL 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()
service = discovery.build('sqladmin', 'v1beta4', 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 ('project_name;project_id;instance_name;tier;dbversion;backendType;state;replicationType;'
'diksType;diskSize(GB);storageAutoResize;availabilityType;automatedBackup;publicIP')
for project_validated in project_valid:
try:
req = service.instances().list(project=project_validated)
resp = req.execute()
#print(resp)
try:
for sql in resp['items']:
public_ip='None'
for ip in sql['ipAddresses']:
if ip.get('type') in ('PRIMARY'):
public_ip=ip.get('ipAddress')
print(
project_validated, ';',
sql.get('project'),';',
sql.get('name'),';',
sql.get('settings').get('tier'),';',
sql.get('databaseVersion') ,';',
sql.get('backendType'),';',
sql.get('settings').get('activationPolicy'), ';',
sql.get('settings').get('replicationType'), ';',
sql.get('settings').get('dataDiskType'), ';',
sql.get('settings').get('dataDiskSizeGb'), ';',
sql.get('settings').get('storageAutoResize'), ';',
sql.get('availabilityType'), ';',
sql.get('settings').get('backupConfiguration').get('enabled'), ';',
public_ip
)
except : pass
except : pass