-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsync.py
111 lines (89 loc) · 3.75 KB
/
sync.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
import json
import base64
import requests
import xml.etree.ElementTree as eTree
from lib import *
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class Service:
def __init__(self, settings):
self.user = settings.attrib["user"]
self.password = settings.attrib["password"]
self.url = settings.attrib["url"]
class ServiceNow(Service):
def request(self, path, method, data=()):
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
result = {}
if method == 'GET':
response = requests.get(self.url + path, auth=(self.user, self.password),
headers=headers, verify=False)
result = response.json()
elif method == 'POST':
response = requests.post(self.url + path, {}, data, auth=(self.user, self.password),
headers=headers, verify=False)
result = response.json()
elif method == 'PUT':
response = requests.put(self.url + path, json.dumps(data), auth=(self.user, self.password),
headers=headers, verify=False)
result = response.json()
elif method == 'PATCH':
response = requests.patch(self.url + path, json.dumps(data), auth=(self.user, self.password),
headers=headers, verify=False)
result = response.json()
return result
class Device42(Service):
def request(self, path, method, data=()):
headers = {
'Authorization': 'Basic ' + base64.b64encode(self.user + ':' + self.password),
'Content-Type': 'application/x-www-form-urlencoded'
}
result = None
if method == 'GET':
response = requests.get(self.url + path,
headers=headers, verify=False)
result = response.json()
elif method == 'POST':
response = requests.post(self.url + path, data,
headers=headers, verify=False)
result = response.json()
elif method == 'PUT':
response = requests.put(self.url + path, data,
headers=headers, verify=False)
result = response.json()
elif method == 'PATCH':
response = requests.patch(self.url + path, json.dumps(data), auth=(self.user, self.password),
headers=headers, verify=False)
result = response.json()
return result
def init_services(settings):
return {
'serviceNow': ServiceNow(settings.find('servicenow')),
'device42': Device42(settings.find('device42'))
}
def task_execute(task, services):
print 'Execute task:', task.attrib['description']
_resource = task.find('api/resource')
_target = task.find('api/target')
if _resource.attrib['target'] == 'serviceNow':
resource_api = services['serviceNow']
target_api = services['device42']
else:
resource_api = services['device42']
target_api = services['serviceNow']
mapping = task.find('mapping')
source = resource_api.request(_resource.attrib['path'], _resource.attrib['method'])
globals()[mapping.attrib['callback']](source, mapping, _target, _resource, target_api, resource_api)
print 'Running...'
# Load mapping
config = eTree.parse('mapping.xml')
meta = config.getroot()
# Init transports services
services = init_services(meta.find('settings'))
# Parse tasks
tasks = meta.find('tasks')
for task in tasks:
if task.attrib['enable'] == 'true':
task_execute(task, services)