-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.py
244 lines (210 loc) · 8.58 KB
/
helpers.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import oci
import json
import pprint
from operator import itemgetter
from resource_info import get_resource_info_from_search
from oci_clients import clients_init
import asyncio
import time
import logging
import concurrent.futures as cf
import itertools
import time
def list_region_subscriptions(config):
pprint.pprint("Fetching all regions in tenancy")
identity_client = oci.identity.IdentityClient(config)
tenancy_id = config['tenancy']
regions = identity_client.list_region_subscriptions(tenancy_id=tenancy_id).data
region_names = list(map(itemgetter("region_name"), oci.util.to_dict(regions)))
pprint.pprint("List of regions subscribed to : {}".format(region_names))
return region_names
def populate_search_results(region_name, search_client, resourceString, conditionString):
search_client.base_client.set_region(region_name)
search_details = oci.resource_search.models.StructuredSearchDetails(
type="Structured",
query="query " + resourceString + " resources where (" + conditionString + ")",
)
response = oci.pagination.list_call_get_all_results(
search_client.search_resources, search_details=search_details
)
rawSearchData = json.dumps(convert_response_to_dict(response))
with open("rawSearchData-" + region_name + ".json", "w") as f:
f.write(rawSearchData)
return True
def fetch_compartment_heirarchy(config):
tenancy_id = config['tenancy']
pprint.pprint("Populate Compartment Herirachies in Tenancy")
identity_client = oci.identity.IdentityClient(config)
compartment_dict = fetch_all_compartments_in_tenancy(identity_client, tenancy_id)
compartment_dict.append(
convert_response_to_dict(identity_client.get_compartment(tenancy_id))
)
compartment_name_list = extract_value_by_field(compartment_dict, "name")
compartment_ocid_list = extract_value_by_field(compartment_dict, "id")
compartment_parentocid_list = extract_value_by_field(
compartment_dict, "compartment_id"
)
compartment_kv = dict(zip(compartment_ocid_list, compartment_name_list))
compartment_parent_ocid_kv = dict(
zip(compartment_ocid_list, compartment_parentocid_list)
)
compartment_parentname_list = []
for compartment_parent_ocid in compartment_parentocid_list:
compartment_parentname_list.append(
compartment_kv[compartment_parent_ocid]
if compartment_parent_ocid in compartment_kv.keys()
else "None"
)
return compartment_kv, compartment_parent_ocid_kv
def populate_SupportedShapeList(config, region_name, compute_client):
shape_list = []
tenancy_id = config['tenancy']
compute_client.base_client.set_region(region_name)
compute_shapes = convert_response_to_dict(compute_client.list_shapes(tenancy_id))
shape_list.extend(list(compute_shape["shape"] for compute_shape in compute_shapes))
shape_list = set(shape_list)
OCPU_Count = list(
shape_list_searchResult.split(".")[-1:][0]
for shape_list_searchResult in shape_list
)
lookupTable = dict(zip(shape_list, OCPU_Count))
lookupTable["VM.GPU.2.1"] = "1"
lookupTable["VM.GPU2.1"] = "1"
pprint.pprint("Populated Compute Shapes for region {}".format(region_name))
return lookupTable
async def search_region_and_populate(
executor,
config,
region_name,
resourceString,
conditionString,
compartment_kv,
compartment_parent_ocid_kv):
if region_name != "ap-hyderabad-1":
tenancy_id = config['tenancy']
# Set the region
clients = await clients_init(config, region_name)
computeShapeLookupTable = populate_SupportedShapeList(
config, region_name, clients["Instance"]
)
region_distribution = []
populate_search_results(region_name, clients["Search"], resourceString, conditionString)
pprint.pprint(
"Generated Raw Search Result JSON for region: {}".format(region_name)
)
with open("rawSearchData-" + region_name + ".json", "r") as f:
rawSearchData = json.load(f)
loop = asyncio.get_event_loop()
region_distribution = [
loop.run_in_executor(
executor,
get_resource_info_from_search,
searchResult,
tenancy_id,
region_name,
computeShapeLookupTable,
clients,
compartment_kv,
compartment_parent_ocid_kv,
)
for searchResult in rawSearchData
]
completed, pending = await asyncio.wait(region_distribution)
completed_region = [t.result() for t in completed]
region_distribution_json = json.dumps(completed_region)
with open("region_distribution-" + region_name + ".json", "w") as f:
f.write(region_distribution_json)
pprint.pprint(
"Generated Region Distribution JSON for region: {}".format(region_name)
)
def convert_response_to_dict(oci_response):
return oci.util.to_dict(oci_response.data)
def fetch_all_compartments_in_tenancy(client, tenancy_id):
"""Fetch all Compartments in Tenancy , and look across all subtrees."""
compartmentResponse = oci.pagination.list_call_get_all_results(
client.list_compartments,
compartment_id=tenancy_id,
limit=200,
access_level="ACCESSIBLE",
compartment_id_in_subtree=True,
retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY,
)
return convert_response_to_dict(compartmentResponse)
def extract_value_by_field(obj, key):
"""Pull all values of specified key from nested JSON."""
arr = []
def extract(obj, arr, key):
"""Recursively search for values of key in JSON tree."""
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
extract(v, arr, key)
elif k == key:
if v is not None:
arr.append(v)
else:
arr.append("None")
elif isinstance(obj, list):
for searchResult in obj:
extract(searchResult, arr, key)
elif isinstance(obj, type(None)):
arr.append("None")
return arr
results = extract(obj, arr, key)
return results
def populate_unsupported_resources(tenancy_id,
computeShapeLookupTable,
clients,
compartment_kv,
compartment_parent_ocid_kv,
element):
resourceType = element[1]
compartment_id = element[0]
if resourceType == "LoadBalancer":
loadbalancer_client = clients[resourceType]
return_dict = convert_response_to_dict(loadbalancer_client.list_load_balancers(compartment_id))
elif resourceType == "DynamicRoutingGateway":
vcn_client = clients[resourceType]
return_dict = convert_response_to_dict(vcn_client.list_drgs(compartment_id))
if len(return_dict) >0:
return return_dict
else:
return None
async def list_unsupported_resources_and_populate(
executor,
config,
region_name,
compartment_kv,
compartment_parent_ocid_kv,
resource_list):
if region_name != "ap-hyderabad-1":
tenancy_id = config['tenancy']
# Set the region
clients = await clients_init(config, region_name)
computeShapeLookupTable = populate_SupportedShapeList(
config, region_name, clients["Instance"]
)
region_distribution = []
combo = list(itertools.product(compartment_kv.keys(),resource_list))
loop = asyncio.get_event_loop()
region_distribution = [
loop.run_in_executor(
executor,
populate_unsupported_resources,
tenancy_id,
computeShapeLookupTable,
clients,
compartment_kv,
compartment_parent_ocid_kv,
element
)
for element in combo
]
completed, pending = await asyncio.wait(region_distribution)
completed_region = [t.result() for t in completed if t.result() is not None]
region_distribution_json = json.dumps(completed_region)
with open("unsupported-" + region_name + ".json", "w") as f:
f.write(region_distribution_json)
pprint.pprint(
"Generated Region Distribution JSON for region: {}".format(region_name)
)