-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpsm-resources.py
More file actions
185 lines (145 loc) · 5.91 KB
/
psm-resources.py
File metadata and controls
185 lines (145 loc) · 5.91 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# List all PSM Resources
#
# Parameters:
# profile_name
#
# Other parameters picked up from config file using profile name
# username
# password
# idcs_id (idcs-4656dbcafeb47777d3efabcdef12...) from idcs url
# domain_id (cacct-8b4b0c9b4c40173264564750985ff6... select idcs_users in services from myservices page)
#
# Output
# stdout, readable column format
#
# 27-nov-2019 1.0 mbridge Created
# 22-april-2020 1.1 melkayal Added support for CSV file output
import configparser
import csv
import os
import sys
from datetime import datetime
import requests
# ======================================================================================================================
debug: bool = False
configfile = '~/.oci/config.ini'
output_dir = "./log"
# ======================================================================================================================
field_names = ['Tenancy', 'ServiceType', 'ServiceName', 'Creator', 'State', 'Region', 'CreationDate']
def list_psm_services(tenancy_name, username, password, idcs_guid):
global csv_writer
if debug:
print(f'User:Pass = {username}/{"*" * len(password)}')
print(f'IDCSID = {idcs_guid}')
# Print Headings
print(
f"{'Tenancy':22} "
f"{'Service Type':18} "
f"{'Service Name':20.20} "
f"{'Creator':28.28} "
f"{'State':10} "
f"{'Region':15} "
f"{'CreationDate':32} ")
# Full list - many are now disabled/obsolete in some OCI accounts
# service_type_list = [
# "accs", "adbc", "adwc", "adwcp", "aiacs", "aipod", "analytics", "analyticssub", "andc", "andcp",
# "apicatalog", "apics", "apicsauto", "apicsautopod", "autoanalytics", "autoanalyticsinst", "autoanalyticspod",
# "autoblockchain", "bcsmgr", "bcsmgrpod", "bdcsce", "bigdataappliance", "botmxp", "botsaasauto",
# "botscfg", "botscon", "botsint", "botsmgm", "botspip", "botsxp", "caching", "cec", "cecauto", "cecs", "cecsauto",
# "container", "containerpod", "cxaana", "cxacfg", "cxacol", "cxapod", "dbcs", "demo",
# "devserviceapp", "devserviceappauto", "devservicepod", "devservicepodauto", "dhcs", "dics",
# "dipcauto", "dipcinst", "dipcpod", "erp", "ggcs", "integrationcauto", "integrationcloud",
# "iotassetmon", "iotconnectedwrker", "iotenterpriseapps", "iotfleetmon", "iotjls", "iotprodmonitoring", "iotsvcasset",
# "jcs", "mobileccc", "mobilecccom", "mobilecorepod", "mobilecorepodom", "mobileserviceauto",
# "mobilestandard", "mobilestdccc", "mobilestdcore", "mysqlcs", "oabcsinst", "oabcspod", "oaics",
# "oehcs", "oehpcs", "oicinst", "oicpod", "oicsubinst", "omce", "omcexternal", "omcp",
# "ratscontrolplane", "search", "searchcloudapp", "soa", "ssi", "ssip",
# # "stack",
# "vbinst", "vbpod", "visualbuilder", "visualbuilderauto", "wtss"]
service_type_list = ["adbc", "andc", "apicsauto", "autoanalytics", "autoanalyticsinst", "autoblockchain", "bcsmgr",
"bdcsce", "botsaasauto", "cecsauto", "dbcs", "devserviceappauto", "dipcauto", "dipcinst",
"integrationcauto", "jcs", "mobilestandard", "oabcsinst", "oehcs", "oehpcs", "oicinst", "omcexternal",
"searchcloudapp", "soa", "ssi", "vbinst", "visualbuilderauto", "wtss"]
for service_type in service_type_list:
resp = requests.get(
"https://psm.europe.oraclecloud.com/paas/api/v1.1/instancemgmt/"
+ idcs_guid + "/services/" + service_type + "/instances?limit=500",
auth=(username, password),
headers={'X-ID-TENANT-NAME': idcs_guid}
)
if resp.status_code != 200:
# This means something went wrong.
print(f'Error in GET: {resp.status_code} ({resp.reason}) '
f'on tenancy {tenancy_name}, service type {service_type}', file=sys.stderr)
# msg = json.loads(resp.text)['errorMessage']
# print(f' {msg}', file=sys.stderr)
# return -1
else:
for services in resp.json()['services'].items():
svc = services[1]
dttm = datetime.strptime(svc['creationDate'], "%Y-%m-%dT%H:%M:%S.%f%z")
create_date = datetime.strftime(dttm, "%Y-%m-%d %H:%M:%S")
# Region not always available (e.g. when service initializing)
reg = svc.get('region', "N/A")
print(
f"{tenancy_name:22} "
f"{svc['serviceType']:18} "
f"{svc['serviceName']:20.20} "
f"{svc['creator']:28.28} "
f"{svc['state']:12} "
f"{reg:15} "
f"{create_date:32} ")
output_dict = {
'Tenancy': tenancy_name,
'ServiceType': svc['serviceType'],
'ServiceName': svc['serviceName'],
'Creator': svc['creator'],
'State': svc['state'],
'Region': reg,
'CreationDate': create_date
}
format_output(output_dict)
# TODO: Handle isBYOL flag
return
def tenancy_usage(tenancy_name):
# Just in case we use the tilde (~) home directory character
configfilepath = os.path.expanduser(configfile)
if not os.path.isfile(configfilepath):
print(f'Error: Config file not found ({configfilepath})', file=sys.stderr)
sys.exit(0)
config = configparser.ConfigParser()
config.read(configfilepath)
ini_data = config[tenancy_name]
# Get all service details
list_psm_services(tenancy_name, ini_data['username'], ini_data['password'], ini_data['idcs_guid'])
def csv_open(filename):
csv_path = f'{output_dir}/{filename}.csv'
csv_file = open(csv_path, 'wt')
if debug:
print('CSV File : ' + csv_path)
csv_writer = csv.DictWriter(
csv_file,
fieldnames=field_names, delimiter=',',
dialect='excel',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writeheader()
return csv_writer
# Output a line for each cloud resource (output_dict should be a dictionary)
def format_output(output_dict):
global csv_writer
try:
# CSV to file
csv_writer.writerow(output_dict)
except Exception as error:
print(f'Error {error.code} [{output_dict}', file=sys.stderr)
if __name__ == "__main__":
# Get profile from command line
if len(sys.argv) != 2:
print(f'Usage: {sys.argv[0]} <profile_name>')
sys.exit()
else:
tenancy_name = sys.argv[1]
csv_writer = csv_open(f"psm-{tenancy_name}")
tenancy_usage(tenancy_name)
if debug:
print('DONE')