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 | |
| 7 | from liberouterapi import auth, config |
| 8 | from flask import request |
| 9 | import json |
| 10 | import os |
| 11 | import errno |
| 12 | |
| 13 | from .inventory import INVENTORY, inventory_check |
| 14 | from .error import NetopeerException |
| 15 | |
| 16 | __DEVICES_EMPTY = '{"device":[]}' |
| 17 | |
| 18 | |
| 19 | def __devices_init(): |
| 20 | return json.loads(__DEVICES_EMPTY) |
| 21 | |
| 22 | def __devices_inv_load(path): |
| 23 | devicesinv_path = os.path.join(path, 'devices.json') |
| 24 | try: |
| 25 | with open(devicesinv_path, 'r') as devices_file: |
| 26 | devices = json.load(devices_file) |
| 27 | except OSError as e: |
| 28 | if e.errno == errno.ENOENT: |
| 29 | devices = __devices_init() |
| 30 | else: |
| 31 | raise NetopeerException('Unable to use user\'s devices inventory ' + devicesinv_path + ' (' + str(e) + ').') |
| 32 | except ValueError: |
| 33 | devices = __devices_init() |
| 34 | |
| 35 | return devices |
| 36 | |
| 37 | def __devices_inv_save(path, devices): |
| 38 | devicesinv_path = os.path.join(path, 'devices.json') |
| 39 | |
| 40 | #store the list |
| 41 | try: |
| 42 | with open(devicesinv_path, 'w') as devices_file: |
| 43 | json.dump(devices, devices_file) |
| 44 | except Exception: |
| 45 | pass |
| 46 | |
| 47 | return devices |
| 48 | |
| 49 | @auth.required() |
| 50 | def devices_list(): |
| 51 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 52 | user = session['user'] |
| 53 | path = os.path.join(INVENTORY, user.username) |
| 54 | |
| 55 | inventory_check(path) |
| 56 | devices = __devices_inv_load(path) |
| 57 | |
| 58 | return(json.dumps(devices['device'])) |
| 59 | |
| 60 | @auth.required() |
| 61 | def devices_add(): |
| 62 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 63 | user = session['user'] |
| 64 | path = os.path.join(INVENTORY, user.username) |
| 65 | |
| 66 | device = request.get_json() |
| 67 | if not device or not device['id']: |
| 68 | raise NetopeerException('Invalid device remove request.') |
| 69 | |
| 70 | devices = __devices_inv_load(path) |
| 71 | for dev in devices['device']: |
| 72 | if dev['id'] == device['id']: |
| 73 | return (json.dumps({'success': False})) |
| 74 | |
| 75 | device_json = {'id':device['id'], 'hostname':device['hostname'], 'port':device['port']} |
| 76 | if 'username' in device: |
| 77 | device_json['username'] = device['username'] |
| 78 | if 'password' in device: |
| 79 | device_json['password'] = device['password'] |
| 80 | devices['device'].append(device_json) |
| 81 | |
| 82 | #store the list |
| 83 | __devices_inv_save(path, devices) |
| 84 | |
| 85 | return(json.dumps({'success': True})) |
| 86 | |
| 87 | @auth.required() |
| 88 | def devices_rm(): |
| 89 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 90 | user = session['user'] |
| 91 | path = os.path.join(INVENTORY, user.username) |
| 92 | |
| 93 | rm_id = request.get_json()['id'] |
| 94 | if not rm_id: |
| 95 | raise NetopeerException('Invalid device remove request.') |
| 96 | |
| 97 | devices = __devices_inv_load(path) |
| 98 | for i in range(len(devices['device'])): |
| 99 | device = devices['device'][i] |
| 100 | if device['id'] == rm_id: |
| 101 | devices['device'].pop(i) |
| 102 | device = None |
| 103 | break; |
| 104 | |
| 105 | if device: |
| 106 | # device not in inventory |
| 107 | return (json.dumps({'success': False})) |
| 108 | |
| 109 | # update the inventory database |
| 110 | __devices_inv_save(path, devices) |
| 111 | |
| 112 | return(json.dumps({'success': True})) |
| 113 | |
| 114 | def devices_get(device_id, username): |
| 115 | path = os.path.join(INVENTORY, username) |
| 116 | devices = __devices_inv_load(path) |
| 117 | |
| 118 | for i in range(len(devices['device'])): |
| 119 | device = devices['device'][i] |
| 120 | if device['id'] == device_id: |
| 121 | return device |
| 122 | |
| 123 | return None |