Debian Codegraph @

ansible_collections/fortinet/fortios/plugins/modules/fortios_json_generic.py

raw symbols SCIP
1 #!/usr/bin/python
2 from __future__ import (absolute_import, division, print_function)
3 # Copyright (c) 2022 Fortinet
4 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
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 __metaclass__ = type
12
13 ANSIBLE_METADATA = {'status': ['preview'],
14 'supported_by': 'community',
15 'metadata_version': '1.1'}
16
17 DOCUMENTATION = '''
18 ---
19 module: fortios_json_generic
20 short_description: Config Fortinet's FortiOS and FortiGate with json generic method.
21 description:
22 - This module is able to configure a FortiGate or FortiOS (FOS) device by allowing the
23 user to set and modify json feature and generic category.
24 Examples include all parameters and values need to be adjusted to datasources before usage.
25 Tested with FOS v6.0.4
26 version_added: "2.0.0"
27 author:
28 - Link Zheng (@chillancezen)
29 - Jie Xue (@JieX19)
30 - Frank Shen (@fshen01)
31 - Hongbin Lu (@fgtdev-hblu)
32 options:
33 access_token:
34 description:
35 - Token-based authentication.
36 Generated from GUI of Fortigate.
37 type: str
38 required: false
39 headers:
40 description:
41 - Custom HTTP headers to include in this request.
42 - To auto-inject connection password, set C(X-Admin-Passwd) to C(true).
43 type: dict
44 required: false
45 enable_log:
46 description:
47 - Enable/Disable logging for task.
48 type: bool
49 required: false
50 default: false
51 vdom:
52 description:
53 - Virtual domain, among those defined previously. A vdom is a
54 virtual instance of the FortiGate that can be configured and
55 used as a different unit.
56 type: str
57 default: root
58 json_generic:
59 description:
60 - json generic
61 default: null
62 type: dict
63 suboptions:
64 dictbody:
65 description:
66 - Body with YAML list of key/value format
67 type: dict
68 jsonbody:
69 description:
70 - Body with JSON string format, will always give priority to jsonbody
71 type: str
72 method:
73 description:
74 - HTTP methods
75 type: str
76 required: true
77 choices:
78 - 'GET'
79 - 'PUT'
80 - 'POST'
81 - 'DELETE'
82 path:
83 description:
84 - URL path, e.g./api/v2/cmdb/firewall/address
85 type: str
86 required: true
87 specialparams:
88 description:
89 - Extra URL parameters, e.g.start=1&count=10
90 type: str
91 '''
92
93 EXAMPLES = '''
94 - name: add firewall address
95 fortinet.fortios.fortios_json_generic:
96 vdom: "root"
97 json_generic:
98 method: "POST"
99 path: "/api/v2/cmdb/firewall/address"
100 jsonbody: |
101 {
102 "name": "111",
103 "type": "geography",
104 "fqdn": "",
105 "country": "AL",
106 "comment": "ccc",
107 "visibility": "enable",
108 "associated-interface": "port1",
109 "allow-routing": "disable"
110 }
111 register: info
112
113 - name: display vars
114 debug: msg="{{info}}"
115
116 - name: delete firewall address
117 fortinet.fortios.fortios_json_generic:
118 vdom: "root"
119 json_generic:
120 method: "DELETE"
121 path: "/api/v2/cmdb/firewall/address/111"
122 register: info
123
124 - name: display vars
125 debug: msg="{{info}}"
126
127 - name: test add with dict
128 fortinet.fortios.fortios_json_generic:
129 vdom: "root"
130 json_generic:
131 method: "POST"
132 path: "/api/v2/cmdb/firewall/address"
133 dictbody:
134 name: "111"
135 type: "geography"
136 fqdn: ""
137 country: "AL"
138 comment: "it's a test"
139 visibility: "enable"
140 associated-interface: "port1"
141 allow-routing: "disable"
142 register: info
143
144 - name: display vars
145 debug: msg="{{info}}"
146
147 - name: test speical params
148 fortinet.fortios.fortios_json_generic:
149 vdom: "root"
150 json_generic:
151 method: "PUT"
152 path: "/api/v2/cmdb/firewall/policy/1"
153 specialparams: "action=move&after=2"
154 register: info
155
156 - name: test request headers
157 fortinet.fortios.fortios_json_generic:
158 vdom: "root"
159 headers:
160 X-Admin-Passwd: true
161 json_generic:
162 method: "POST"
163 path: "/api/v2/cmdb/system/api-user"
164 register: info
165
166 - name: display vars
167 debug: msg="{{info}}"
168 '''
169
170 RETURN = '''
171 build:
172 description: Build number of the fortigate image
173 returned: always
174 type: str
175 sample: '1547'
176 http_method:
177 description: Last method used to provision the content into FortiGate
178 returned: always
179 type: str
180 sample: 'PUT'
181 http_status:
182 description: Last result given by FortiGate on last operation applied
183 returned: always
184 type: str
185 sample: "200"
186 mkey:
187 description: Master key (id) used in the last call to FortiGate
188 returned: success
189 type: str
190 sample: "id"
191 name:
192 description: Name of the table used to fulfill the request
193 returned: always
194 type: str
195 sample: "urlfilter"
196 path:
197 description: Path of the table used to fulfill the request
198 returned: always
199 type: str
200 sample: "webfilter"
201 revision:
202 description: Internal revision number
203 returned: always
204 type: str
205 sample: "17.0.2.10658"
206 serial:
207 description: Serial number of the unit
208 returned: always
209 type: str
210 sample: "FGVMEVYYQT3AB5352"
211 status:
212 description: Indication of the operation's result
213 returned: always
214 type: str
215 sample: "success"
216 vdom:
217 description: Virtual domain used
218 returned: always
219 type: str
220 sample: "root"
221 version:
222 description: Version of the FortiGate
223 returned: always
224 type: str
225 sample: "v5.6.3"
226
227 '''
228
229 from ansible.module_utils.basic import AnsibleModule
230 from ansible.module_utils.connection import Connection
231 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import FortiOSHandler
232 from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import FAIL_SOCKET_MSG
233 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import check_legacy_fortiosapi
234
235 import json
236
237
238 def json_generic(data, fos, request_headers=None):
239 vdom = data['vdom']
240 json_generic_data = data['json_generic']
241
242 # Give priority to jsonbody
243 body = ""
244 if json_generic_data['jsonbody']:
245 try:
246 body = json.loads(json_generic_data['jsonbody'])
247 except Exception as e:
248 fos._module.fail_json("invalid json content: %s" % (e))
249 else:
250 if json_generic_data['dictbody']:
251 body = json_generic_data['dictbody']
252
253 return fos.jsonraw(json_generic_data['method'],
254 json_generic_data['path'],
255 data=body,
256 specific_params=json_generic_data['specialparams'],
257 vdom=vdom,
258 headers=request_headers)
259
260
261 def is_successful_status(resp):
262 return 'status' in resp and resp['status'] == 'success' \
263 or 'http_method' in resp and resp['http_method'] == 'DELETE' \
264 and 'http_status' in resp and resp['http_status'] == 404
265
266
267 def fortios_json(data, fos, request_headers=None):
268
269 if data['json_generic']:
270 resp = json_generic(data, fos, request_headers)
271
272 return not is_successful_status(resp), \
273 resp['status'] == "success", \
274 resp
275
276
277 def main():
278 fields = {
279 "access_token": {"required": False, "type": "str", "no_log": True},
280 "headers": {"required": False, "type": "dict", "no_log": True},
281 "enable_log": {"required": False, "type": 'bool', "default": False},
282 "vdom": {"required": False, "type": "str", "default": "root"},
283 "json_generic": {
284 "required": False, "type": "dict", "default": None,
285 "options": {
286 "dictbody": {"required": False, "type": "dict"},
287 "jsonbody": {"required": False, "type": "str"},
288 "method": {"required": True, "type": "str",
289 "choices": ["GET", "PUT", "POST",
290 "DELETE"]},
291 "path": {"required": True, "type": "str"},
292 "specialparams": {"required": False, "type": "str"}
293
294 }
295 }
296 }
297
298 module = AnsibleModule(argument_spec=fields,
299 supports_check_mode=False)
300 check_legacy_fortiosapi(module)
301
302 is_error = False
303 has_changed = False
304 result = None
305
306 if module._socket_path:
307 connection = Connection(module._socket_path)
308 if 'access_token' in module.params:
309 connection.set_custom_option('access_token', module.params['access_token'])
310 if 'enable_log' in module.params:
311 connection.set_custom_option('enable_log', module.params['enable_log'])
312 else:
313 connection.set_custom_option('enable_log', False)
314
315 request_headers = module.params.get('headers')
316
317 fos = FortiOSHandler(connection, module)
318 is_error, has_changed, result = fortios_json(module.params, fos, request_headers)
319 else:
320 module.fail_json(**FAIL_SOCKET_MSG)
321
322 if not is_error:
323 module.exit_json(changed=has_changed, meta=result)
324 else:
325 module.fail_json(msg="Unable to precess the request, please provide correct parameters and make sure the path exists.", meta=result)
326
327
328 if __name__ == '__main__':
329 main()