Debian Codegraph @

ansible_collections/fortinet/fortimanager/plugins/module_utils/common.py

raw symbols SCIP
1 # This code is part of Ansible, but is an independent component.
2 # This particular file snippet, and this file snippet only, is BSD licensed.
3 # Modules you write using this snippet, which is embedded dynamically by Ansible
4 # still belong to the author of the module, and may assign their own license
5 # to the complete work.
6 #
7 # (c) 2017-2020 Fortinet, Inc
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without modification,
11 # are permitted provided that the following conditions are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above copyright notice,
16 # this list of conditions and the following disclaimer in the documentation
17 # and/or other materials provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 from __future__ import absolute_import, division, print_function
29
30 __metaclass__ = type
31
32
33 # BEGIN STATIC DATA / MESSAGES
34 class FMGRMethods:
35 GET = "get"
36 SET = "set"
37 EXEC = "exec"
38 EXECUTE = "exec"
39 UPDATE = "update"
40 ADD = "add"
41 DELETE = "delete"
42 REPLACE = "replace"
43 CLONE = "clone"
44 MOVE = "move"
45
46
47 COMMON_SCHEMA = {
48 'access_token': {'type': 'str', 'no_log': True},
49 'bypass_validation': {'type': 'bool', 'default': False},
50 'enable_log': {'type': 'bool', 'default': False},
51 'forticloud_access_token': {'type': 'str', 'no_log': True},
52 'rc_succeeded': {'type': 'list', 'elements': 'int'},
53 'rc_failed': {'type': 'list', 'elements': 'int'},
54 'workspace_locking_adom': {'type': 'str'},
55 'workspace_locking_timeout': {'type': 'int', 'default': 300}
56 }
57
58
59 def get_module_arg_spec(task_type):
60 arg_spec = {}
61 for key in COMMON_SCHEMA:
62 arg_spec[key] = COMMON_SCHEMA[key]
63 if task_type != 'exec':
64 arg_spec['proposed_method'] = {
65 'type': 'str',
66 'required': False,
67 'choices': ['set', 'update', 'add']
68 }
69 if task_type in ['full crud', 'object member']:
70 arg_spec['state'] = {
71 'type': 'str',
72 'required': True,
73 'choices': ['present', 'absent']
74 }
75 return arg_spec
76
77
78 # BEGIN ERROR EXCEPTIONS
79 class FMGBaseException(Exception):
80 """Wrapper to catch the unexpected"""
81
82 def __init__(self, msg=None, *args, **kwargs):
83 if msg is None:
84 msg = "An exception occurred within the fortimanager.py httpapi connection plugin."
85 super(FMGBaseException, self).__init__(msg, *args)
86
87 # END ERROR CLASSES
88
89
90 # BEGIN CLASSES
91 class FMGRCommon(object):
92 @staticmethod
93 def format_request(method, url, *args, **kwargs):
94 """
95 Formats the payload from the module, into a payload the API handler can use.
96
97 :param url: Connection URL to access
98 :type url: string
99 :param method: The preferred API Request method (GET, ADD, POST, etc....)
100 :type method: basestring
101 :param kwargs: The payload dictionary from the module to be converted.
102
103 :return: Properly formatted dictionary payload for API Request via Connection Plugin.
104 :rtype: dict
105 """
106
107 params = [{"url": url}]
108 if args:
109 for arg in args:
110 params[0].update(arg)
111 if kwargs:
112 keylist = list(kwargs)
113 for k in keylist:
114 kwargs[k.replace("__", "-")] = kwargs.pop(k)
115 if method == "get" or method == "clone":
116 params[0].update(kwargs)
117 else:
118 if kwargs.get("data", False):
119 params[0]["data"] = kwargs["data"]
120 else:
121 params[0]["data"] = kwargs
122 return params
123
124 @staticmethod
125 def syslog(module, msg):
126 try:
127 module.log(msg=msg)
128 except BaseException:
129 pass