summaryrefslogtreecommitdiff
path: root/src/services/api/graphql/recipes/session.py
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2021-11-22 15:29:46 -0600
committerJohn Estabrook <jestabro@vyos.io>2021-11-23 10:46:55 -0600
commitef7f5ca2fd2c0113875dbd9143342e925cf00621 (patch)
tree5a5ddc62f15f429062e42625a1517aad52d55a0a /src/services/api/graphql/recipes/session.py
parentfb2dc58d91bd93ba3aaa63d46e49e6609c18d46f (diff)
downloadvyos-1x-ef7f5ca2fd2c0113875dbd9143342e925cf00621.tar.gz
vyos-1x-ef7f5ca2fd2c0113875dbd9143342e925cf00621.zip
graphql: T3993: refactor directive and mutation definitions
Diffstat (limited to 'src/services/api/graphql/recipes/session.py')
-rw-r--r--src/services/api/graphql/recipes/session.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/services/api/graphql/recipes/session.py b/src/services/api/graphql/recipes/session.py
new file mode 100644
index 000000000..aa3932ab9
--- /dev/null
+++ b/src/services/api/graphql/recipes/session.py
@@ -0,0 +1,68 @@
+from ariadne import convert_camel_case_to_snake
+import vyos.defaults
+from vyos.template import render
+
+class Session(object):
+ def __init__(self, session, data):
+ self._session = session
+ self.data = data
+ self._name = convert_camel_case_to_snake(type(self).__name__)
+
+ @property
+ def data(self):
+ return self.__data
+
+ @data.setter
+ def data(self, data):
+ if isinstance(data, dict):
+ self.__data = data
+ else:
+ raise ValueError("data must be of type dict")
+
+ 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 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