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
|
# 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
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# 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/>.
from importlib import import_module
from typing import Any, Dict
from ariadne import ObjectType, convert_kwargs_to_snake_case, convert_camel_case_to_snake
from graphql import GraphQLResolveInfo
from makefun import with_signature
from .. import state
from .. import key_auth
from api.graphql.recipes.session import Session
mutation = ObjectType("Mutation")
def make_mutation_resolver(mutation_name, class_name, session_func):
"""Dynamically generate a resolver for the mutation named in the
schema by 'mutation_name'.
Dynamic generation is provided using the package 'makefun' (via the
decorator 'with_signature'), which provides signature-preserving
function wrappers; it provides several improvements over, say,
functools.wraps.
:raise Exception:
raising ConfigErrors, or internal errors
"""
func_base_name = convert_camel_case_to_snake(class_name)
resolver_name = f'resolve_{func_base_name}'
func_sig = '(obj: Any, info: GraphQLResolveInfo, data: Dict)'
@mutation.field(mutation_name)
@convert_kwargs_to_snake_case
@with_signature(func_sig, func_name=resolver_name)
async def func_impl(*args, **kwargs):
try:
if 'data' not in kwargs:
return {
"success": False,
"errors": ['missing data']
}
data = kwargs['data']
key = data['key']
auth = key_auth.auth_required(key)
if auth is None:
return {
"success": False,
"errors": ['invalid API key']
}
# We are finished with the 'key' entry, and may remove so as to
# pass the rest of data (if any) to function.
del data['key']
session = state.settings['app'].state.vyos_session
# one may override the session functions with a local subclass
try:
mod = import_module(f'api.graphql.recipes.{func_base_name}')
klass = getattr(mod, class_name)
except ImportError:
# otherwise, dynamically generate subclass to invoke subclass
# name based functions
klass = type(class_name, (Session,), {})
k = klass(session, data)
method = getattr(k, session_func)
result = method()
data['result'] = result
return {
"success": True,
"data": data
}
except Exception as error:
return {
"success": False,
"errors": [str(error)]
}
return func_impl
def make_prefix_resolver(mutation_name, prefix=[]):
for pre in prefix:
Pre = pre.capitalize()
if Pre in mutation_name:
class_name = mutation_name.replace(Pre, '', 1)
return make_mutation_resolver(mutation_name, class_name, pre)
raise Exception
def make_configure_resolver(mutation_name):
class_name = mutation_name
return make_mutation_resolver(mutation_name, class_name, 'configure')
def make_config_file_resolver(mutation_name):
return make_prefix_resolver(mutation_name, prefix=['save', 'load'])
def make_image_resolver(mutation_name):
return make_prefix_resolver(mutation_name, prefix=['add', 'delete'])
def make_gen_op_mutation_resolver(mutation_name):
class_name = mutation_name
return make_mutation_resolver(mutation_name, class_name, 'gen_op_mutation')
|