forked from device42/device42_samanage_sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.py
148 lines (130 loc) · 5.97 KB
/
lib.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
import json
import sys
from importlib import reload
reload(sys)
DEBUG = True
object_keys = [
"Hardware", "Cpu", "Controller", "Display", "Drive", "Input", "Memory", "Network", "Port",
"Printer", "Software", "Sound", "Storage", "Video"
]
def fill_ci_body_imem(data, subfields):
data_el = {}
for key, value in data.items():
for field in subfields:
if key == field.attrib.get('resource'):
if field.attrib.get('sub-resource'):
value = value[field.attrib.get('sub-resource')]
data_el[field.attrib.get('target')] = value
return data_el
def complete_batch_object_body(body):
body_keys = body.keys()
for key in object_keys:
if key not in body_keys:
data = []
body[key] = {
"data": data,
"metadata": {
"status": "OK",
"data_hash": str(
hash(json.dumps(data)) if hash(json.dumps(data)) > 0 else hash(json.dumps(data)) + sys.maxsize)
}
}
bios_data = [{"serial_number": "N/A"}]
body["Bios"] = {
"data": bios_data,
"metadata": {
"status": "OK",
"data_hash": str(hash(json.dumps(bios_data)) if hash(json.dumps(bios_data)) > 0 else hash(
json.dumps(bios_data)) + sys.maxsize)
}
}
return body
def fill_batch_object(fields, match_map, source_api):
body = {}
data = fields
for key, value in fields.items():
val = ''
if key in match_map:
if match_map[key].attrib.get("url"):
url = match_map[key].attrib.get("url")
if match_map[key].attrib.get("extra-api-additional-param"):
url = "{}{}".format(url, data[match_map[key].attrib.get("extra-api-additional-param")])
response = source_api.request(url, 'GET')
if match_map[key].findall('sub-field'):
if match_map[key].attrib.get('target-ci-type') not in body:
body[match_map[key].attrib.get('target-ci-type')] = {'data': []}
sub_data = response
if match_map[key].attrib.get('sub-key'):
sub_data = response[match_map[key].attrib.get('sub-key')]
for el in sub_data:
body[match_map[key].attrib.get('target-ci-type')]['data'].append(
fill_ci_body_imem(el, match_map[key].findall('sub-field')))
if data.get(match_map[key].attrib['resource']) and not match_map[key].findall('sub-field'):
val = data[match_map[key].attrib['resource']]
if match_map[key].get("is-array") and val:
val = val[0]
if match_map[key].get("sub-key"):
val = val[match_map[key].get("sub-key")]
if key == "FriendlyName" and not val:
val = data["name"]
if match_map[key].findall('sub-field') and not match_map[key].attrib.get("url"):
if match_map[key].attrib.get('target-ci-type') not in body:
body[match_map[key].attrib.get('target-ci-type')] = {'data': []}
sub_data = data[match_map[key].attrib['resource']]
for el in sub_data:
body[match_map[key].attrib.get('target-ci-type')]['data'].append(
fill_ci_body_imem(el, match_map[key].findall('sub-field')))
if val:
if match_map[key].attrib.get('type') == 'integer':
val = int(val)
if match_map[key].attrib.get('target-ci-type') not in body:
body[match_map[key].attrib.get('target-ci-type')] = {"data": [{}]}
body[match_map[key].attrib.get('target-ci-type')]['data'][0][match_map[key].attrib.get('target')] = val
for key, item in body.items():
# if item.get('data'):
item['metadata'] = {
'data_hash': str(hash(json.dumps(item['data'])) if hash(json.dumps(item['data'])) > 0 else hash(
json.dumps(item['data'])) + sys.maxsize),
'status': 'OK'
}
body = complete_batch_object_body(body)
response_object = {
'Name': fields['name'],
'SamanageCMDB__CustomExtIdField__c': '{}{}'.format('D42', fields['device_id']),
'SamanageCMDB__JsonData__c': json.dumps({
'vendor': 1,
'body_version': 1,
"body": body
})
}
return response_object
def perform_butch_request(mapping, match_map, _target, _resource, source, target_api,
resource_api):
offset = source.get("offset", 0)
limit = source.get("limit", 500)
batch = []
for idx, item in enumerate(source[mapping.attrib['source']]):
print("Processing {} of {} records".format(int(offset) + idx + 1, source["total_count"]))
batch.append(
fill_batch_object(item, match_map, resource_api))
target_api.request(batch)
if offset + limit < source["total_count"]:
print("Exported {} of {} records".format(offset + limit, source["total_count"]))
source_url = _resource.attrib['path']
if _resource.attrib.get("extra-filter"):
source_url += _resource.attrib.get("extra-filter") + "&"
source = resource_api.request(
"{}offset={}".format(source_url, offset + limit),
_resource.attrib['method'])
perform_butch_request(mapping, match_map, _target, _resource, source,
target_api,
resource_api)
return True
def from_d42(source, mapping, _target, _resource, target_api, resource_api):
fields = mapping.findall('field')
match_map = {field.attrib['resource']: field for field in fields}
success = perform_butch_request(mapping, match_map, _target, _resource, source, target_api, resource_api)
if success:
print("Success")
else:
print("Something bad happened")