| 1 |
# Copyright 2020 Nokia Software. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import abc |
| 16 |
import copy |
| 17 |
|
| 18 |
from mistral_lib import actions |
| 19 |
from mistral_lib import exceptions as exc |
| 20 |
from mistral_lib import utils |
| 21 |
|
| 22 |
|
| 23 |
def _compare_parameters(expected_params, actual_params): |
| 24 |
"""Compares the expected parameters with the actual parameters. |
| 25 |
|
| 26 |
:param expected_params: Expected dict of parameters. |
| 27 |
:param actual_params: Actual dict of parameters. |
| 28 |
:return: Tuple {missing parameter names, unexpected parameter names} |
| 29 |
""" |
| 30 |
|
| 31 |
missing_params = [] |
| 32 |
unexpected_params = copy.deepcopy(list((actual_params or {}).keys())) |
| 33 |
|
| 34 |
for p_name, p_value in expected_params.items(): |
| 35 |
if p_value is utils.NotDefined and p_name not in unexpected_params: |
| 36 |
missing_params.append(str(p_name)) |
| 37 |
|
| 38 |
if p_name in unexpected_params: |
| 39 |
unexpected_params.remove(p_name) |
| 40 |
|
| 41 |
return missing_params, unexpected_params |
| 42 |
|
| 43 |
|
| 44 |
class ActionDescriptorBase(actions.ActionDescriptor, abc.ABC): |
| 45 |
def __init__(self, name, desc, params_spec, namespace=None, |
| 46 |
project_id=None, scope=None): |
| 47 |
self._name = name |
| 48 |
self._desc = desc |
| 49 |
self._params_spec = params_spec |
| 50 |
self._namespace = namespace |
| 51 |
self._project_id = project_id |
| 52 |
self._scope = scope |
| 53 |
|
| 54 |
@property |
| 55 |
def name(self): |
| 56 |
return self._name |
| 57 |
|
| 58 |
@property |
| 59 |
def description(self): |
| 60 |
return self._desc |
| 61 |
|
| 62 |
@property |
| 63 |
def params_spec(self): |
| 64 |
return self._params_spec |
| 65 |
|
| 66 |
@property |
| 67 |
def namespace(self): |
| 68 |
return self._namespace |
| 69 |
|
| 70 |
@property |
| 71 |
def project_id(self): |
| 72 |
return self._project_id |
| 73 |
|
| 74 |
@property |
| 75 |
def scope(self): |
| 76 |
return self._scope |
| 77 |
|
| 78 |
@property |
| 79 |
def action_class_name(self): |
| 80 |
return None |
| 81 |
|
| 82 |
@property |
| 83 |
def action_class_attributes(self): |
| 84 |
return None |
| 85 |
|
| 86 |
def check_parameters(self, params): |
| 87 |
# Don't validate action input if action initialization |
| 88 |
# method contains ** argument. |
| 89 |
if '**' in self.params_spec: |
| 90 |
return |
| 91 |
|
| 92 |
expected_params = utils.get_dict_from_string(self.params_spec) |
| 93 |
|
| 94 |
actual_params = params or {} |
| 95 |
|
| 96 |
missing, unexpected = _compare_parameters( |
| 97 |
expected_params, |
| 98 |
actual_params |
| 99 |
) |
| 100 |
|
| 101 |
if missing or unexpected: |
| 102 |
msg = 'Invalid input [name=%s, class=%s' |
| 103 |
msg_props = [self.name, self.action_class_name] |
| 104 |
|
| 105 |
if missing: |
| 106 |
msg += ', missing=%s' |
| 107 |
msg_props.append(missing) |
| 108 |
|
| 109 |
if unexpected: |
| 110 |
msg += ', unexpected=%s' |
| 111 |
msg_props.append(unexpected) |
| 112 |
|
| 113 |
msg += ']' |
| 114 |
|
| 115 |
raise exc.ActionException(msg % tuple(msg_props)) |
| 116 |
|
| 117 |
def post_process_result(self, result): |
| 118 |
return result |