Debian Codegraph @

ansible_collections/fortinet/fortimanager/plugins/modules/fmgr_generic.py

raw symbols SCIP
1 #!/usr/bin/python
2 from __future__ import absolute_import, division, print_function
3
4 # Copyright 2019-2021 Fortinet, Inc.
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19 __metaclass__ = type
20
21 ANSIBLE_METADATA = {
22 "status": ["preview"],
23 "supported_by": "community",
24 "metadata_version": "1.1",
25 }
26
27 DOCUMENTATION = """
28 ---
29 module: fmgr_generic
30 short_description: Build and send generic FortiManager API request.
31 description:
32 - This module is for generic fortimanager requests. it receives raw json-rpc
33 data, and sends it to fortimanager, finally returns the response to users.
34 - This module also rely on fortimanager httpapi plugin as the transport.
35 - the payload doesn't include session, the httpapi plugin will automatically
36 fill the session later.
37 - the username and password is not managed by the module, but by the plugin.
38 version_added: "2.0.0"
39 author:
40 - Xinwei Du (@dux-fortinet)
41 - Xing Li (@lix-fortinet)
42 - Link Zheng (@zhengl)
43 - Jie Xue (@JieX19)
44 - Frank Shen (@fshen01)
45 - Hongbin Lu (@fgtdev-hblu)
46 notes:
47 - two parameters schemes are supported, either in raw json format or in
48 ansible recognnizable top-level parameters format.
49 - json is defined as string, user is response for make it json-formatted
50 - method and params should be specified by users if 'json' is not present
51 - if all three parameters are provided, the 'json' is preferred.
52 options:
53 access_token:
54 description: The token to access FortiManager without using username and password.
55 required: false
56 type: str
57 enable_log:
58 description: Enable/Disable logging for task
59 required: false
60 type: bool
61 default: false
62 forticloud_access_token:
63 description: authenticate Ansible client with forticloud API access token
64 required: false
65 type: str
66 rc_succeeded:
67 description: the rc codes list with which the conditions to succeed will be overriden
68 type: list
69 required: false
70 elements: int
71 rc_failed:
72 description: the rc codes list with which the conditions to fail will be overriden
73 type: list
74 elements: int
75 required: false
76 workspace_locking_adom:
77 description: no description
78 type: str
79 required: false
80 workspace_locking_timeout:
81 description: no description
82 type: int
83 required: false
84 default: 300
85 method:
86 description:
87 - the method of the json-rpc
88 - it must be in [get, add, set, update, delete, move, clone, exec]
89 type: str
90 required: false
91 params:
92 description:
93 - the parameter collection.
94 type: list
95 elements: dict
96 required: false
97 json:
98 description:
99 - the raw json-formatted payload to send to fortimanager
100 type: str
101 required: false
102 """
103
104 EXAMPLES = """
105 - name: Generic module
106 hosts: fortimanagers
107 connection: httpapi
108 vars:
109 adom: "root"
110 ansible_httpapi_use_ssl: true
111 ansible_httpapi_validate_certs: false
112 ansible_httpapi_port: 443
113 tasks:
114 - name: Login a user
115 fortinet.fortimanager.fmgr_generic:
116 method: "exec"
117 params:
118 - url: "sys/login/user"
119 data:
120 - user: "APIUser"
121 passwd: "Fortinet1!e"
122 - name: Login another user
123 fortinet.fortimanager.fmgr_generic:
124 json: |
125 {
126 "method":"exec",
127 "params":[
128 {
129 "url":"sys/login/user",
130 "data":[
131 {
132 "user":"APIUser",
133 "passwd":"Fortinet1!"
134 }
135 ]
136 }
137 ]
138 }
139 """
140
141 RETURN = """
142 api_result:
143 description: full API response, includes status code and message
144 returned: always
145 type: str
146 """
147
148 from ansible.module_utils.basic import AnsibleModule
149 from ansible.module_utils.connection import Connection
150 from ansible_collections.fortinet.fortimanager.plugins.module_utils.napi import NAPIManager
151 import json
152
153
154 def main():
155
156 module_arg_spec = {
157 "access_token": {"type": "str", "required": False, "no_log": True},
158 "enable_log": {"type": "bool", "required": False, "default": False},
159 "forticloud_access_token": {"type": "str", "required": False, "no_log": True},
160 "workspace_locking_adom": {"type": "str", "required": False},
161 "workspace_locking_timeout": {"type": "int", "required": False, "default": 300},
162 "rc_succeeded": {"type": "list", "required": False, 'elements': 'int'},
163 "rc_failed": {"type": "list", "required": False, 'elements': 'int'},
164 "method": {"type": "str", "required": False},
165 "params": {"type": "list", "required": False, 'elements': 'dict'},
166 "json": {"type": "str", "required": False},
167 }
168
169 module = AnsibleModule(argument_spec=module_arg_spec, supports_check_mode=False)
170 if not module._socket_path:
171 module.fail_json(msg="Only Httpapi plugin is supported in this module.")
172 connection = Connection(module._socket_path)
173 fmgr = NAPIManager('generic', None, None, None, None, module, connection)
174 method = None
175 params = None
176
177 if module.params["json"]:
178 raw_json_params = None
179 try:
180 raw_json_params = json.loads(module.params["json"])
181 method = raw_json_params["method"]
182 params = raw_json_params["params"]
183 except Exception as e:
184 module.fail_json(msg="invalid json content: %s" % (e))
185 else:
186 if not module.params["method"] or not module.params["params"]:
187 raise AssertionError("method and params must be given!")
188 method = module.params["method"]
189 params = module.params["params"]
190
191 if method not in ["get", "add", "set", "update", "delete", "move", "clone", "exec"]:
192 module.fail_json(msg="method:%s not supported" % (method))
193
194 if not isinstance(params, list):
195 module.fail_json(msg="parameter:params must be an array")
196 for param_block in params:
197 if "url" not in param_block:
198 module.fail_json(msg="url must be specified in params")
199 try:
200 fmgr.process_generic(method, params)
201
202 except Exception as e:
203 module.fail_json(msg="error sending request: %s" % (e))
204
205
206 if __name__ == "__main__":
207 main()