forked from device42/servicenow_device42_mapping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.py
210 lines (165 loc) · 9.01 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
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
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
DEBUG = False
def typer(condition, item):
try:
if condition == 'integer':
return int(item)
elif condition == 'float':
return float(item)
elif condition == 'string':
return str(item)
except:
return ''
def get_linked_objects(_target, target_api):
linked_objects = {}
objects = target_api.request(_target.attrib['path'], 'GET')['result']
for obj in objects:
if 'u_device42_id' in obj:
linked_objects[obj['u_device42_id']] = obj['sys_id']
return linked_objects
def to_d42(source, mapping, _target, _resource, target_api, resource_api):
key = mapping.attrib['key']
for row in source['result']:
data = {}
fields = mapping.findall('field')
sys_id = row['sys_id']
stored_device42_id = row['u_device42_id'] if len(row['u_device42_id']) > 0 else None
for field in fields:
if field.attrib['resource'] not in row or row[field.attrib['resource']] is None:
continue
if field.attrib['resource'] == 'name':
data[field.attrib['target']] = typer(field.attrib['type'],
row[field.attrib['resource']]) + ' copied from ServiceNow'
elif 'sub_field' in field.attrib:
sub_link = re.search(r'.service-now.com(.+)', row[field.attrib['resource']]['link'])
sub_link = sub_link.group(1)
sub_field_objects = resource_api.request(sub_link, 'GET')['result']
data[field.attrib['target']] = typer(field.attrib['type'], sub_field_objects[field.attrib['sub_field']])
if 'sub_field2' in field.attrib:
sub_link2 = re.search(r'.service-now.com(.+)', sub_field_objects[field.attrib['sub_field']]['link'])
sub_link2 = sub_link2.group(1)
sub_field2_objects = resource_api.request(sub_link2, 'GET')['result']
data[field.attrib['target']] = \
typer(field.attrib['type'], sub_field2_objects[field.attrib['sub_field2']])
else:
data[field.attrib['target']] = typer(field.attrib['type'], row[field.attrib['resource']])
# update or create new
if stored_device42_id is not None:
data[key] = stored_device42_id
old_name = data['name']
data.pop('name', None)
api_result = target_api.request(_target.attrib['path'], _target.attrib['update_method'], data)
if int(api_result['code']) == 3:
# resend if device removed
stored_device42_id = None
data['name'] = old_name
api_result = target_api.request(_target.attrib['path'], _target.attrib['method'], data)
else:
api_result = target_api.request(_target.attrib['path'], _target.attrib['method'], data)
if DEBUG:
print data
print api_result
# update stored device in ServiceNow
if stored_device42_id is None:
resource_api.request(_resource.attrib['path'] + '/' + sys_id, 'PATCH', {
'u_device42_id': api_result['msg'][1]
})
print '.'
def from_d42(source, mapping, _target, _resource, target_api, resource_api):
source_key = mapping.attrib['source']
key = mapping.attrib['key']
if source_key == '' or source_key not in source:
mapped_source = [source,]
else:
mapped_source = source[source_key]
for row in mapped_source:
data = {}
fields = mapping.findall('field')
# check current device in already linked devices
linked_objects = get_linked_objects(_target, target_api)
linked_sys_id = linked_objects[str(row[key])] if linked_objects and str(row[key]) in linked_objects else None
for field in fields:
if field.attrib['resource'] not in row or row[field.attrib['resource']] is None:
continue
if field.attrib['resource'] == 'name':
data[field.attrib['target']] = typer(field.attrib['type'],
row[field.attrib['resource']]) + ' copied from Device42'
elif field.attrib['resource'] == 'mac_addresses':
row[field.attrib['resource']] = [x for x in row[field.attrib['resource']] if x is not None]
if len(row[field.attrib['resource']]) > 0:
data[field.attrib['target']] = \
typer(field.attrib['type'], row[field.attrib['resource']][int(field.attrib['element'])]['mac'])
elif field.attrib['resource'] == 'ip_addresses':
row[field.attrib['resource']] = [x for x in row[field.attrib['resource']] if x is not None]
if len(row[field.attrib['resource']]) > 0:
data[field.attrib['target']] = \
typer(field.attrib['type'], row[field.attrib['resource']][int(field.attrib['element'])]['ip'])
elif field.attrib['resource'] == 'custom_fields':
row[field.attrib['resource']] = [x for x in row[field.attrib['resource']] if x is not None]
if len(row[field.attrib['resource']]) > 0:
for x in row[field.attrib['resource']]:
if x['key'] == str(field.attrib['key']):
data[field.attrib['target']] = typer(field.attrib['type'], x['value'])
continue
elif field.attrib['resource'] == 'tags':
data[field.attrib['target']] = ', '.join(row[field.attrib['resource']])
elif 'checker' in field.attrib:
sub_field_objects = target_api.request(field.attrib['checker'], 'GET')['result']
parent = None
for sub_field_object in sub_field_objects:
if str(sub_field_object['name']) == str(row[field.attrib['resource']]):
parent = sub_field_object['sys_id']
break
if parent is None:
post_object = {
'name': row[field.attrib['resource']],
}
# special cases
if field.attrib['resource'] == 'manufacturer':
post_object.update({'manufacturer': True})
if field.attrib['resource'] == 'vendor':
post_object.update({'vendor': True})
parent = target_api.request(field.attrib['checker'], 'POST', post_object)['result']['sys_id']
result = parent
if 'checker2' in field.attrib and field.attrib['resource2'] in row:
sub_field2_objects = target_api.request(field.attrib['checker2'], 'GET')['result']
child = None
for sub_field2_object in sub_field2_objects:
if str(sub_field2_object['name']) == str(row[field.attrib['resource2']]):
child = sub_field2_object['sys_id']
break
if child is None:
post_object = {
'name': row[field.attrib['resource2']],
}
# special cases
if field.attrib['resource'] == 'manufacturer':
post_object.update({'manufacturer': parent})
if field.attrib['resource'] == 'vendor':
post_object.update({'vendor': parent})
child = target_api.request(field.attrib['checker2'], 'POST', post_object)['result']['sys_id']
result = child
data[field.attrib['target']] = result
else:
data[field.attrib['target']] = typer(field.attrib['type'], row[field.attrib['resource']])
# update or create new
if linked_sys_id is not None:
data.pop('name', None)
api_result = target_api.request(_target.attrib['path'] + '/' + linked_sys_id,
_target.attrib['update_method'], data)
else:
data['u_device42_id'] = row[key]
if _resource.attrib['path'] == '/api/1.0/buildings/':
data['u_device42_impact_link'] = '%s/admin/rackraj/building/impactgraph/%s/' % (resource_api.url, row[key])
elif _resource.attrib['path'] == '/api/1.0/rooms/':
data['u_device42_impact_link'] = '%s/admin/rackraj/room/impactgraph/%s/' % (resource_api.url, row[key])
elif _resource.attrib['path'] == '/api/1.0/racks/':
data['u_device42_impact_link'] = '%s/admin/rackraj/rack/impactgraph/%s/' % (resource_api.url, row[key])
api_result = target_api.request(_target.attrib['path'], _target.attrib['method'], data)
if DEBUG:
print data
print api_result
print '.'