summaryrefslogtreecommitdiff
path: root/src/services/api/graphql/recipes/session.py
blob: 5ece78ee668aa679525503302e81809c31ef1c9a (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import json

from ariadne import convert_camel_case_to_snake

import vyos.defaults
from vyos.config import Config
from vyos.configtree import ConfigTree
from vyos.template import render

class Session:
    """
    Wrapper for calling configsession functions based on GraphQL requests.
    Non-nullable fields in the respective schema allow avoiding a key check
    in 'data'.
    """
    def __init__(self, session, data):
        self._session = session
        self._data = data
        self._name = convert_camel_case_to_snake(type(self).__name__)

    def configure(self):
        session = self._session
        data = self._data
        func_base_name = self._name

        tmpl_file = f'{func_base_name}.tmpl'
        cmd_file = f'/tmp/{func_base_name}.cmds'
        tmpl_dir = vyos.defaults.directories['api_templates']

        try:
            render(cmd_file, tmpl_file, data, location=tmpl_dir)
            commands = []
            with open(cmd_file) as f:
                lines = f.readlines()
            for line in lines:
                commands.append(line.split())
            for cmd in commands:
                if cmd[0] == 'set':
                    session.set(cmd[1:])
                elif cmd[0] == 'delete':
                    session.delete(cmd[1:])
                else:
                    raise ValueError('Operation must be "set" or "delete"')
            session.commit()
        except Exception as error:
            raise error

    def delete_path_if_childless(self, path):
        session = self._session
        config = Config(session.get_session_env())
        if not config.list_nodes(path):
            session.delete(path)
            session.commit()

    def show_config(self):
        session = self._session
        data = self._data
        out = ''

        try:
            out = session.show_config(data['path'])
            if data.get('config_format', '') == 'json':
                config_tree = vyos.configtree.ConfigTree(out)
                out = json.loads(config_tree.to_json())
        except Exception as error:
            raise error

        return out

    def save(self):
        session = self._session
        data = self._data
        if 'file_name' not in data or not data['file_name']:
            data['file_name'] = '/config/config.boot'

        try:
            session.save_config(data['file_name'])
        except Exception as error:
            raise error

    def load(self):
        session = self._session
        data = self._data

        try:
            session.load_config(data['file_name'])
            session.commit()
        except Exception as error:
            raise error

    def show(self):
        session = self._session
        data = self._data
        out = ''

        try:
            out = session.show(data['path'])
        except Exception as error:
            raise error

        return out

    def add(self):
        session = self._session
        data = self._data

        try:
            res = session.install_image(data['location'])
        except Exception as error:
            raise error

        return res

    def delete(self):
        session = self._session
        data = self._data

        try:
            res = session.remove_image(data['name'])
        except Exception as error:
            raise error

        return res