-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource_info.py
77 lines (68 loc) · 2.49 KB
/
resource_info.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
import oci
import json
from operator import itemgetter
from flatten_dict import flatten
from extract_info import get_resource_specific_info
def convert_response_to_dict(oci_response):
return oci.util.to_dict(oci_response.data)
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 get_resource_info_from_search(
searchResult,
tenancy_id,
region_name,
computeShapeLookupTable,
clients,
compartment_kv,
compartment_parent_ocid_kv,
):
parent_compartment_name = (
compartment_kv[compartment_parent_ocid_kv[searchResult["compartment_id"]]]
if searchResult["compartment_id"] != tenancy_id
else "ROOT"
)
chosen_client = clients[searchResult["resource_type"]]
try:
searchResult, res_dict = get_resource_specific_info(
searchResult, computeShapeLookupTable, chosen_client
)
except:
res_dict = {"resource_specific_info": "No Info"}
pass
norm_item_dict = {
"region": region_name,
"availability_domain": searchResult["availability_domain"],
"resource_type": searchResult["resource_type"],
"shape": searchResult["shape"],
"OCPU_Qty": searchResult["OCPU_Qty"],
"license_model": searchResult["license_model"],
"display_name": searchResult["display_name"],
"resource_ocid": searchResult["identifier"],
"compartment_id": searchResult["compartment_id"],
"compartment_name": compartment_kv[searchResult["compartment_id"]],
"parent_compartment_name": parent_compartment_name,
"CreatedBy": ("").join(extract_value_by_field(searchResult, "CreatedBy")),
"CreatedOn": searchResult["time_created"]
}
norm_item_dict.update(res_dict)
return norm_item_dict