vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / utils / validation.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 Validation utilities.
18 """
19
20 from .formatting import string_list_as_string
21
22
23 class ValidatorMixin(object):
24     """
25     A mix0in that should be added to classes that require validating user input.
26     """
27
28     _ARGUMENT_TYPE_MESSAGE = '{name} argument must be {type} based, got {arg!r}'
29     _ARGUMENT_CHOICE_MESSAGE = '{name} argument must be in {choices}, got {arg!r}'
30
31     @classmethod
32     def validate_in_choice(cls, name, argument, choices):
33         """
34         Validate ``argument`` is in ``choices``
35         """
36         if argument not in choices:
37             raise TypeError(cls._ARGUMENT_CHOICE_MESSAGE.format(
38                 name=name, choices=choices, arg=argument))
39
40     @classmethod
41     def validate_type(cls, argument_name, argument, expected_type):
42         """
43         Validate ``argument`` is a subclass of ``expected_type``
44         """
45         if not issubclass(argument, expected_type):
46             raise TypeError(cls._ARGUMENT_TYPE_MESSAGE.format(
47                 name=argument_name, type=expected_type, arg=argument))
48
49     @classmethod
50     def validate_instance(cls, argument_name, argument, expected_type):
51         """
52         Validate ``argument`` is a instance of ``expected_type``
53         """
54         if not isinstance(argument, expected_type):
55             raise TypeError(cls._ARGUMENT_TYPE_MESSAGE.format(
56                 name=argument_name, type=expected_type, arg=argument))
57
58     @classmethod
59     def validate_callable(cls, argument_name, argument):
60         """
61         Validate ``argument`` is callable
62         """
63         if not callable(argument):
64             raise TypeError(cls._ARGUMENT_TYPE_MESSAGE.format(
65                 name=argument_name, type='callable', arg=argument))
66
67
68 def validate_function_arguments(func, func_kwargs):
69     """
70     Validates all required arguments are supplied to ``func`` and that no additional arguments are
71     supplied.
72     """
73
74     _kwargs_flags = 8
75
76     has_kwargs = func.func_code.co_flags & _kwargs_flags != 0
77     args_count = func.func_code.co_argcount
78
79     # all args without the ones with default values
80     args = func.func_code.co_varnames[:args_count]
81     non_default_args = args[:len(args) - len(func.func_defaults)] if func.func_defaults else args
82
83     # Check if any args without default values is missing in the func_kwargs
84     for arg in non_default_args:
85         if arg not in func_kwargs:
86             raise ValueError(
87                 'The argument "{arg}" is not provided and does not have a default value for '
88                 'function "{func.__name__}"'.format(arg=arg, func=func))
89
90     # check if there are any extra kwargs
91     extra_kwargs = [arg for arg in func_kwargs.keys() if arg not in args]
92
93     # assert that the function has kwargs
94     if extra_kwargs and not has_kwargs:
95         raise ValueError("The following extra kwargs were supplied: {extra_kwargs}".format(
96             extra_kwargs=string_list_as_string(extra_kwargs)
97         ))