blob: be1362c7b31b4a0a37d61632557e87582c3ed95b [file] [log] [blame]
Radek Krejci58fb8902017-09-21 13:23:28 +02001"""
2Manipulation with the devices to connect.
3File: devices.py
4Author: Radek Krejci <rkrejci@cesnet.cz>
5"""
6
Radek Krejci58fb8902017-09-21 13:23:28 +02007import json
8import os
9import errno
10
Radek Krejci67c922d2017-09-21 13:56:41 +020011from liberouterapi import auth
12from flask import request
13
Radek Krejci58fb8902017-09-21 13:23:28 +020014from .inventory import INVENTORY, inventory_check
15from .error import NetopeerException
16
17__DEVICES_EMPTY = '{"device":[]}'
18
19
20def __devices_init():
21 return json.loads(__DEVICES_EMPTY)
22
23def __devices_inv_load(path):
24 devicesinv_path = os.path.join(path, 'devices.json')
25 try:
26 with open(devicesinv_path, 'r') as devices_file:
27 devices = json.load(devices_file)
28 except OSError as e:
29 if e.errno == errno.ENOENT:
30 devices = __devices_init()
31 else:
32 raise NetopeerException('Unable to use user\'s devices inventory ' + devicesinv_path + ' (' + str(e) + ').')
33 except ValueError:
34 devices = __devices_init()
35
36 return devices
37
38def __devices_inv_save(path, devices):
39 devicesinv_path = os.path.join(path, 'devices.json')
40
41 #store the list
42 try:
43 with open(devicesinv_path, 'w') as devices_file:
44 json.dump(devices, devices_file)
45 except Exception:
46 pass
47
48 return devices
49
50@auth.required()
51def devices_list():
52 session = auth.lookup(request.headers.get('Authorization', None))
53 user = session['user']
54 path = os.path.join(INVENTORY, user.username)
Radek Krejcia7478322018-06-19 13:09:52 +020055
Radek Krejci58fb8902017-09-21 13:23:28 +020056 inventory_check(path)
57 devices = __devices_inv_load(path)
Radek Krejcia7478322018-06-19 13:09:52 +020058
Radek Krejci6853c112018-04-27 14:17:30 +020059 for dev in devices['device']:
60 del dev['password']
Radek Krejcia7478322018-06-19 13:09:52 +020061
Radek Krejci58fb8902017-09-21 13:23:28 +020062 return(json.dumps(devices['device']))
63
64@auth.required()
65def devices_add():
66 session = auth.lookup(request.headers.get('Authorization', None))
67 user = session['user']
68 path = os.path.join(INVENTORY, user.username)
69
70 device = request.get_json()
71 if not device or not device['id']:
72 raise NetopeerException('Invalid device remove request.')
73
74 devices = __devices_inv_load(path)
75 for dev in devices['device']:
76 if dev['id'] == device['id']:
77 return (json.dumps({'success': False}))
78
Radek Krejci2b510682018-04-27 14:31:39 +020079 device_json = {'id':device['id'],
80 'name':device['name'],
81 'hostname':device['hostname'],
82 'port':device['port'],
83 'autoconnect':device['autoconnect']}
Radek Krejci58fb8902017-09-21 13:23:28 +020084 if 'username' in device:
85 device_json['username'] = device['username']
86 if 'password' in device:
87 device_json['password'] = device['password']
88 devices['device'].append(device_json)
89
90 #store the list
91 __devices_inv_save(path, devices)
92
93 return(json.dumps({'success': True}))
94
95@auth.required()
96def devices_rm():
97 session = auth.lookup(request.headers.get('Authorization', None))
98 user = session['user']
99 path = os.path.join(INVENTORY, user.username)
100
101 rm_id = request.get_json()['id']
102 if not rm_id:
103 raise NetopeerException('Invalid device remove request.')
104
105 devices = __devices_inv_load(path)
106 for i in range(len(devices['device'])):
107 device = devices['device'][i]
108 if device['id'] == rm_id:
109 devices['device'].pop(i)
110 device = None
Radek Krejcia7478322018-06-19 13:09:52 +0200111 break
Radek Krejci58fb8902017-09-21 13:23:28 +0200112
113 if device:
114 # device not in inventory
115 return (json.dumps({'success': False}))
116
117 # update the inventory database
118 __devices_inv_save(path, devices)
119
120 return(json.dumps({'success': True}))
121
122def devices_get(device_id, username):
123 path = os.path.join(INVENTORY, username)
124 devices = __devices_inv_load(path)
125
126 for i in range(len(devices['device'])):
127 device = devices['device'][i]
128 if device['id'] == device_id:
129 return device
130
Radek Krejci2b510682018-04-27 14:31:39 +0200131 return None
Radek Krejcia7478322018-06-19 13:09:52 +0200132
133
134def devices_replace(device_id, username, device):
135 path = os.path.join(INVENTORY, username)
136 devices = __devices_inv_load(path)
137
138 for i in range(len(devices['device'])):
139 if devices['device'][i]['id'] == device_id:
140 devices['device'][i] = device
141 break
142
143 # update the inventory database
144 __devices_inv_save(path, devices)