forked from eclipse-hono/hono-extras
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile2mongo.py
119 lines (97 loc) · 3.72 KB
/
file2mongo.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
#!/usr/bin/env python3
import json
import argparse
from uuid import uuid4
from enum import Enum
from typing import Dict
from datetime import datetime
from collections import Counter
class Collection(Enum):
CREDENTIALS = 'credentials'
DEVICES = 'devices'
TENANTS = 'tenants'
IGNORED_TENANTS = ['DEFAULT_TENANT', 'HTTP_TENANT']
class HonoResourceTransformer:
def __init__(self, collection: Collection, dump_path: str):
self.collection = collection
self.dump_path = dump_path
self.updated_on = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
def transform(self):
transform_func_name = '_transform_%s' % self.collection.value
transform_func = getattr(self, transform_func_name)
with open(self.dump_path) as dumpfile:
dump = json.load(dumpfile)
output = transform_func(dump)
print(json.dumps(output, indent=4))
def _transform_credentials_item(self, tenant: str, device_id: str, item_list: list):
credentials = []
for item in item_list:
credential = {
'auth-id': item['auth-id'],
'type': item['type'],
'secrets': item['secrets'],
'enabled': True
}
credentials.append(credential)
return {
'tenant-id': tenant,
'device-id': device_id,
'version': str(uuid4()),
'updatedOn': self.updated_on,
'credentials': credentials
}
def _transform_credentials(self, dump: Dict):
credentials = []
for tenant_obj in dump:
tenant_id = tenant_obj['tenant']
if tenant_id in IGNORED_TENANTS:
continue
device_list = [x['device-id'] for x in tenant_obj['credentials']]
for device in Counter(device_list).keys():
item_list = [x for x in tenant_obj['credentials'] if x['device-id'] == device]
transformed = self._transform_credentials_item(tenant_id, device, item_list)
credentials.append(transformed)
return credentials
def _transform_devices_item(self, tenant: str, item: Dict):
return {
'tenant-id': tenant,
'device-id': item['device-id'],
'version': str(uuid4()),
'updatedOn': self.updated_on,
'device': item['data']
}
def _transform_devices(self, dump: Dict):
devices = []
for tenant_obj in dump:
tenant_id = tenant_obj['tenant']
if tenant_id in IGNORED_TENANTS:
continue
for item in tenant_obj['devices']:
transformed = self._transform_devices_item(tenant_id, item)
devices.append(transformed)
return devices
def _transform_tenants(self, dump: Dict):
tenants = []
for item in dump:
tenant_id = item['tenant-id']
if tenant_id in IGNORED_TENANTS:
continue
tenant = {
'tenant-id': tenant_id,
'version': str(uuid4()),
'updatedOn': self.updated_on,
'tenant': {}
}
if 'tenant' in item:
tenant['tenant'] = item['tenant']
tenants.append(tenant)
return tenants
def main():
parser = argparse.ArgumentParser(description='Transfrom hono collections from file-based registry to mongo.')
parser.add_argument('collection', type=Collection, help='Type of dump')
parser.add_argument('dump', type=str, help='Json dump to import')
args = parser.parse_args()
transformer = HonoResourceTransformer(args.collection, args.dump)
transformer.transform()
if __name__ == '__main__':
main()