blob: 595c9d29c0b0a08657d662566c5550b3bcc990d3 [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)
55
56 inventory_check(path)
57 devices = __devices_inv_load(path)
58
Radek Krejci6853c112018-04-27 14:17:30 +020059 for dev in devices['device']:
60 del dev['password']
Radek Krejci58fb8902017-09-21 13:23:28 +020061 return(json.dumps(devices['device']))
62
63@auth.required()
64def devices_add():
65 session = auth.lookup(request.headers.get('Authorization', None))
66 user = session['user']
67 path = os.path.join(INVENTORY, user.username)
68
69 device = request.get_json()
70 if not device or not device['id']:
71 raise NetopeerException('Invalid device remove request.')
72
73 devices = __devices_inv_load(path)
74 for dev in devices['device']:
75 if dev['id'] == device['id']:
76 return (json.dumps({'success': False}))
77
78 device_json = {'id':device['id'], 'hostname':device['hostname'], 'port':device['port']}
79 if 'username' in device:
80 device_json['username'] = device['username']
81 if 'password' in device:
82 device_json['password'] = device['password']
83 devices['device'].append(device_json)
84
85 #store the list
86 __devices_inv_save(path, devices)
87
88 return(json.dumps({'success': True}))
89
90@auth.required()
91def devices_rm():
92 session = auth.lookup(request.headers.get('Authorization', None))
93 user = session['user']
94 path = os.path.join(INVENTORY, user.username)
95
96 rm_id = request.get_json()['id']
97 if not rm_id:
98 raise NetopeerException('Invalid device remove request.')
99
100 devices = __devices_inv_load(path)
101 for i in range(len(devices['device'])):
102 device = devices['device'][i]
103 if device['id'] == rm_id:
104 devices['device'].pop(i)
105 device = None
106 break;
107
108 if device:
109 # device not in inventory
110 return (json.dumps({'success': False}))
111
112 # update the inventory database
113 __devices_inv_save(path, devices)
114
115 return(json.dumps({'success': True}))
116
117def devices_get(device_id, username):
118 path = os.path.join(INVENTORY, username)
119 devices = __devices_inv_load(path)
120
121 for i in range(len(devices['device'])):
122 device = devices['device'][i]
123 if device['id'] == device_id:
124 return device
125
126 return None