summaryrefslogtreecommitdiffstats
path: root/config.py
blob: 5a79698198c3d9bcdabdda54bf4071e4a0a72bf6 (plain)
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
#!/usr/bin/env python3

# Copyright (C) 2020-2024 Soleta Networks <opengnsys@soleta.eu>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

import json

OG_CLI_CFG_PATH = "/opt/opengnsys/etc/ogcli.json"

def load_config():
    data = None
    try:
        with open(OG_CLI_CFG_PATH, 'r') as json_file:
            data = json.load(json_file)
    except json.JSONDecodeError:
        sys.exit(f'ERROR: Failed parse malformed JSON file {OG_CLI_CFG_PATH}')
    except Exception as e:
        sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}: {e}')

    required_cfg_params = {'api_token', 'ip', 'port'}
    difference_cfg_params = required_cfg_params - data.keys()
    if len(difference_cfg_params) > 0:
        sys.exit(f'Missing {difference_cfg_params} key in '
                    f'json config file')
    return data

cfg = load_config()