Radek Krejci | 58fb890 | 2017-09-21 13:23:28 +0200 | [diff] [blame] | 1 | """ |
| 2 | Manipulation with the devices to connect. |
| 3 | File: devices.py |
| 4 | Author: Radek Krejci <rkrejci@cesnet.cz> |
| 5 | """ |
| 6 | |
Radek Krejci | 58fb890 | 2017-09-21 13:23:28 +0200 | [diff] [blame] | 7 | import json |
| 8 | import os |
| 9 | import errno |
| 10 | |
Radek Krejci | 67c922d | 2017-09-21 13:56:41 +0200 | [diff] [blame] | 11 | from liberouterapi import auth |
| 12 | from flask import request |
| 13 | |
Radek Krejci | 58fb890 | 2017-09-21 13:23:28 +0200 | [diff] [blame] | 14 | from .inventory import INVENTORY, inventory_check |
| 15 | from .error import NetopeerException |
| 16 | |
| 17 | __DEVICES_EMPTY = '{"device":[]}' |
| 18 | |
| 19 | |
| 20 | def __devices_init(): |
| 21 | return json.loads(__DEVICES_EMPTY) |
| 22 | |
| 23 | def __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 | |
| 38 | def __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() |
| 51 | def devices_list(): |
| 52 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 53 | user = session['user'] |
| 54 | path = os.path.join(INVENTORY, user.username) |
Radek Krejci | a747832 | 2018-06-19 13:09:52 +0200 | [diff] [blame] | 55 | |
Radek Krejci | 58fb890 | 2017-09-21 13:23:28 +0200 | [diff] [blame] | 56 | inventory_check(path) |
| 57 | devices = __devices_inv_load(path) |
Radek Krejci | a747832 | 2018-06-19 13:09:52 +0200 | [diff] [blame] | 58 | |
Radek Krejci | 6853c11 | 2018-04-27 14:17:30 +0200 | [diff] [blame] | 59 | for dev in devices['device']: |
| 60 | del dev['password'] |
Radek Krejci | a747832 | 2018-06-19 13:09:52 +0200 | [diff] [blame] | 61 | |
Radek Krejci | 58fb890 | 2017-09-21 13:23:28 +0200 | [diff] [blame] | 62 | return(json.dumps(devices['device'])) |
| 63 | |
| 64 | @auth.required() |
| 65 | def 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 Krejci | 2b51068 | 2018-04-27 14:31:39 +0200 | [diff] [blame] | 79 | device_json = {'id':device['id'], |
| 80 | 'name':device['name'], |
| 81 | 'hostname':device['hostname'], |
| 82 | 'port':device['port'], |
| 83 | 'autoconnect':device['autoconnect']} |
Radek Krejci | 58fb890 | 2017-09-21 13:23:28 +0200 | [diff] [blame] | 84 | 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() |
| 96 | def 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 Krejci | a747832 | 2018-06-19 13:09:52 +0200 | [diff] [blame] | 111 | break |
Radek Krejci | 58fb890 | 2017-09-21 13:23:28 +0200 | [diff] [blame] | 112 | |
| 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 | |
| 122 | def 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 Krejci | 2b51068 | 2018-04-27 14:31:39 +0200 | [diff] [blame] | 131 | return None |
Radek Krejci | a747832 | 2018-06-19 13:09:52 +0200 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def 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) |