-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsync.py
165 lines (137 loc) · 4.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
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
import json
import base64
import requests
import urllib.parse as urllib
import xml.etree.ElementTree as eTree
import lib
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from sys import exit
class Service:
def __init__(self, settings):
self.user = settings.attrib["user"]
self.password = settings.attrib["password"]
self.url = settings.attrib["url"]
class Atrium(Service):
def __init__(self, settings):
super().__init__(settings)
headers = {
'content-type': "application/x-www-form-urlencoded",
}
body = {
'password': self.password,
'username': self.user
}
url = "%s/api/jwt/login/" % self.url
print('url: ', url)
response = requests.request("POST", url, data=body, headers=headers)
if response.status_code == 200:
print('JWT auth response: ', response.text)
self.access_token = response.text
else:
print(response.content)
exit(0)
def request(self, path, method, data=()):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": self.access_token
}
result = {}
url = self.url + path
if method == 'GET':
response = requests.get(url, headers=headers, verify=False)
if response.status_code == requests.codes.ok:
result = json.loads(response.content.decode())
else:
print("ERROR %s - %s" % (response.status_code, response.text))
elif method == 'POST':
response = requests.post(
url,
json.dumps(data),
headers=headers,
verify=False
)
if response.status_code == requests.codes.ok:
result = json.loads(response.content.decode())
else:
print("ERROR %s - %s" % (response.status_code, response.text))
return result
class Device42(Service):
def request(self, path, method, data=(), doql=None):
headers = {
'Authorization': 'Basic ' + base64.b64encode((self.user + ':' + self.password).encode()).decode(),
'Content-Type': 'application/x-www-form-urlencoded'
}
result = None
if method == 'GET':
response = requests.get(self.url + path,
headers=headers, verify=False)
result = json.loads(response.content.decode())
if method == 'POST' and doql is not None:
payload = {
"query": doql,
"header": "yes"
}
response = requests.post(
self.url + path,
headers=headers,
verify=False,
data=payload
)
result = response.text
return result
def init_services(settings):
return {
'atrium': Atrium(settings.find('atrium')),
'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'] == 'atrium':
resource_api = services['atrium']
target_api = services['device42']
else:
resource_api = services['device42']
target_api = services['atrium']
mapping = task.find('mapping')
source_url = _resource.attrib['path']
method = _resource.attrib['method']
doql = None
if _resource.attrib.get("extra-filter"):
source_url += _resource.attrib.get("extra-filter") + "&"
print(source_url)
# sys.exit()
if _resource.attrib.get('doql'):
print(_resource.attrib['doql'])
doql = _resource.attrib['doql']
# source will contain the objects from the _resource endpoint
if doql is not None:
source = resource_api.request(source_url, method, doql=doql)
lib.from_d42(
source, mapping,
_target, _resource,
target_api, resource_api,
doql=True
)
else:
source = resource_api.request(source_url, method)
lib.from_d42(
source, mapping,
_target, _resource,
target_api, resource_api,
doql=False
)
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)