summaryrefslogtreecommitdiff
path: root/src/services/api/graphql/recipes/session.py
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2022-07-29 12:14:13 -0500
committerGitHub <noreply@github.com>2022-07-29 12:14:13 -0500
commit516b86bb970c7e039b58e554cb3b22e8f996841f (patch)
tree9cd51989cd3fb03ca0d88c32cf0cc6d8c04bb892 /src/services/api/graphql/recipes/session.py
parentb5d9ebf16f63b86013ce4e72a8023ac65fc2df8d (diff)
parentfa8dcff5d2e18ded5310d3f86ea0dc1bf2795af8 (diff)
downloadvyos-1x-516b86bb970c7e039b58e554cb3b22e8f996841f.tar.gz
vyos-1x-516b86bb970c7e039b58e554cb3b22e8f996841f.zip
Merge pull request #1432 from jestabro/gql-op-mode
graphql: T4554: Automate GraphQL handling of standardized op-mode requests
Diffstat (limited to 'src/services/api/graphql/recipes/session.py')
-rw-r--r--src/services/api/graphql/recipes/session.py61
1 files changed, 58 insertions, 3 deletions
diff --git a/src/services/api/graphql/recipes/session.py b/src/services/api/graphql/recipes/session.py
index c436de08a..6b580af01 100644
--- a/src/services/api/graphql/recipes/session.py
+++ b/src/services/api/graphql/recipes/session.py
@@ -1,4 +1,4 @@
-# Copyright 2021 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 2021-2022 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -13,15 +13,20 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
+import os
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.defaults import directories
from vyos.template import render
+from api.graphql.utils.util import load_op_mode_as_module, split_compound_op_mode_name
+
+op_mode_include_file = os.path.join(directories['data'], 'op-mode-standardized.json')
+
class Session:
"""
Wrapper for calling configsession functions based on GraphQL requests.
@@ -33,6 +38,12 @@ class Session:
self._data = data
self._name = convert_camel_case_to_snake(type(self).__name__)
+ try:
+ with open(op_mode_include_file) as f:
+ self._op_mode_list = f.read()
+ except Exception:
+ self._op_mode_list = None
+
def configure(self):
session = self._session
data = self._data
@@ -40,7 +51,7 @@ class Session:
tmpl_file = f'{func_base_name}.tmpl'
cmd_file = f'/tmp/{func_base_name}.cmds'
- tmpl_dir = vyos.defaults.directories['api_templates']
+ tmpl_dir = directories['api_templates']
try:
render(cmd_file, tmpl_file, data, location=tmpl_dir)
@@ -150,3 +161,47 @@ class Session:
status['ram'] = system_status.get_system_ram_usage()
return status
+
+ def gen_op_query(self):
+ session = self._session
+ data = self._data
+ name = self._name
+ op_mode_list = self._op_mode_list
+
+ # handle the case that the op-mode file contains underscores:
+ if op_mode_list is None:
+ raise FileNotFoundError(f"No op-mode file list at '{op_mode_include_file}'")
+ (func_name, basename) = split_compound_op_mode_name(name, op_mode_list)
+ if basename == '':
+ raise FileNotFoundError(f"No op-mode file basename in string '{name}'")
+
+ mod = load_op_mode_as_module(f'{basename}.py')
+ func = getattr(mod, func_name)
+ if len(list(data)) > 0:
+ res = func(True, **data)
+ else:
+ res = func(True)
+
+ return res
+
+ def gen_op_mutation(self):
+ session = self._session
+ data = self._data
+ name = self._name
+ op_mode_list = self._op_mode_list
+
+ # handle the case that the op-mode file name contains underscores:
+ if op_mode_list is None:
+ raise FileNotFoundError(f"No op-mode file list at '{op_mode_include_file}'")
+ (func_name, basename) = split_compound_op_mode_name(name, op_mode_list)
+ if basename == '':
+ raise FileNotFoundError(f"No op-mode file basename in string '{name}'")
+
+ mod = load_op_mode_as_module(f'{basename}.py')
+ func = getattr(mod, func_name)
+ if len(list(data)) > 0:
+ res = func(**data)
+ else:
+ res = func()
+
+ return res