1#!/usr/local/bin/python3.8
2from __future__ import (absolute_import, division, print_function)
3# Copyright 2019-2020 Fortinet, Inc.
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18__metaclass__ = type
19
20ANSIBLE_METADATA = {'status': ['preview'],
21                    'supported_by': 'community',
22                    'metadata_version': '1.1'}
23
24DOCUMENTATION = '''
25---
26module: fortios_wireless_controller_hotspot20_icon
27short_description: Configure OSU provider icon in Fortinet's FortiOS and FortiGate.
28description:
29    - This module is able to configure a FortiGate or FortiOS (FOS) device by allowing the
30      user to set and modify wireless_controller_hotspot20 feature and icon category.
31      Examples include all parameters and values need to be adjusted to datasources before usage.
32      Tested with FOS v6.0.0
33version_added: "2.10"
34author:
35    - Link Zheng (@chillancezen)
36    - Jie Xue (@JieX19)
37    - Hongbin Lu (@fgtdev-hblu)
38    - Frank Shen (@frankshen01)
39    - Miguel Angel Munoz (@mamunozgonzalez)
40    - Nicolas Thomas (@thomnico)
41notes:
42    - Legacy fortiosapi has been deprecated, httpapi is the preferred way to run playbooks
43
44requirements:
45    - ansible>=2.9.0
46options:
47    access_token:
48        description:
49            - Token-based authentication.
50              Generated from GUI of Fortigate.
51        type: str
52        required: false
53    enable_log:
54        description:
55            - Enable/Disable logging for task.
56        type: bool
57        required: false
58        default: false
59    vdom:
60        description:
61            - Virtual domain, among those defined previously. A vdom is a
62              virtual instance of the FortiGate that can be configured and
63              used as a different unit.
64        type: str
65        default: root
66
67    state:
68        description:
69            - Indicates whether to create or remove the object.
70        type: str
71        required: true
72        choices:
73            - present
74            - absent
75    wireless_controller_hotspot20_icon:
76        description:
77            - Configure OSU provider icon.
78        default: null
79        type: dict
80        suboptions:
81            icon_list:
82                description:
83                    - Icon list.
84                type: list
85                suboptions:
86                    file:
87                        description:
88                            - Icon file.
89                        type: str
90                    height:
91                        description:
92                            - Icon height.
93                        type: int
94                    lang:
95                        description:
96                            - Language code.
97                        type: str
98                    name:
99                        description:
100                            - Icon name.
101                        required: true
102                        type: str
103                    type:
104                        description:
105                            - Icon type.
106                        type: str
107                        choices:
108                            - bmp
109                            - gif
110                            - jpeg
111                            - png
112                            - tiff
113                    width:
114                        description:
115                            - Icon width.
116                        type: int
117            name:
118                description:
119                    - Icon list ID.
120                required: true
121                type: str
122'''
123
124EXAMPLES = '''
125- hosts: fortigates
126  collections:
127    - fortinet.fortios
128  connection: httpapi
129  vars:
130   vdom: "root"
131   ansible_httpapi_use_ssl: yes
132   ansible_httpapi_validate_certs: no
133   ansible_httpapi_port: 443
134  tasks:
135  - name: Configure OSU provider icon.
136    fortios_wireless_controller_hotspot20_icon:
137      vdom:  "{{ vdom }}"
138      state: "present"
139      access_token: "<your_own_value>"
140      wireless_controller_hotspot20_icon:
141        icon_list:
142         -
143            file: "<your_own_value>"
144            height: "5"
145            lang: "<your_own_value>"
146            name: "default_name_7"
147            type: "bmp"
148            width: "9"
149        name: "default_name_10"
150
151'''
152
153RETURN = '''
154build:
155  description: Build number of the fortigate image
156  returned: always
157  type: str
158  sample: '1547'
159http_method:
160  description: Last method used to provision the content into FortiGate
161  returned: always
162  type: str
163  sample: 'PUT'
164http_status:
165  description: Last result given by FortiGate on last operation applied
166  returned: always
167  type: str
168  sample: "200"
169mkey:
170  description: Master key (id) used in the last call to FortiGate
171  returned: success
172  type: str
173  sample: "id"
174name:
175  description: Name of the table used to fulfill the request
176  returned: always
177  type: str
178  sample: "urlfilter"
179path:
180  description: Path of the table used to fulfill the request
181  returned: always
182  type: str
183  sample: "webfilter"
184revision:
185  description: Internal revision number
186  returned: always
187  type: str
188  sample: "17.0.2.10658"
189serial:
190  description: Serial number of the unit
191  returned: always
192  type: str
193  sample: "FGVMEVYYQT3AB5352"
194status:
195  description: Indication of the operation's result
196  returned: always
197  type: str
198  sample: "success"
199vdom:
200  description: Virtual domain used
201  returned: always
202  type: str
203  sample: "root"
204version:
205  description: Version of the FortiGate
206  returned: always
207  type: str
208  sample: "v5.6.3"
209
210'''
211from ansible.module_utils.basic import AnsibleModule
212from ansible.module_utils.connection import Connection
213from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import FortiOSHandler
214from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import check_legacy_fortiosapi
215from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import schema_to_module_spec
216from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import check_schema_versioning
217from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import FAIL_SOCKET_MSG
218from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import is_same_comparison
219from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import serialize
220
221
222def filter_wireless_controller_hotspot20_icon_data(json):
223    option_list = ['icon_list', 'name']
224    dictionary = {}
225
226    for attribute in option_list:
227        if attribute in json and json[attribute] is not None:
228            dictionary[attribute] = json[attribute]
229
230    return dictionary
231
232
233def underscore_to_hyphen(data):
234    if isinstance(data, list):
235        for i, elem in enumerate(data):
236            data[i] = underscore_to_hyphen(elem)
237    elif isinstance(data, dict):
238        new_data = {}
239        for k, v in data.items():
240            new_data[k.replace('_', '-')] = underscore_to_hyphen(v)
241        data = new_data
242
243    return data
244
245
246def wireless_controller_hotspot20_icon(data, fos, check_mode=False):
247
248    vdom = data['vdom']
249
250    state = data['state']
251
252    wireless_controller_hotspot20_icon_data = data['wireless_controller_hotspot20_icon']
253    filtered_data = underscore_to_hyphen(filter_wireless_controller_hotspot20_icon_data(wireless_controller_hotspot20_icon_data))
254
255    # check_mode starts from here
256    if check_mode:
257        mkey = fos.get_mkey('system', 'interface', filtered_data, vdom=vdom)
258        current_data = fos.get('system', 'interface', vdom=vdom, mkey=mkey)
259        is_existed = current_data and current_data.get('http_status') == 200 \
260            and isinstance(current_data.get('results'), list) \
261            and len(current_data['results']) > 0
262
263        # 2. if it exists and the state is 'present' then compare current settings with desired
264        if state == 'present' or state is True:
265            if mkey is None:
266                return False, True, filtered_data
267
268            # if mkey exists then compare each other
269            # record exits and they're matched or not
270            if is_existed:
271                is_same = is_same_comparison(
272                    serialize(current_data['results'][0]), serialize(filtered_data))
273                return False, not is_same, filtered_data
274
275            # record does not exist
276            return False, True, filtered_data
277
278        if state == 'absent':
279            if mkey is None:
280                return False, False, filtered_data
281
282            if is_existed:
283                return False, True, filtered_data
284            return False, False, filtered_data
285
286        return True, False, {'reason: ': 'Must provide state parameter'}
287
288    if state == "present" or state is True:
289        return fos.set('wireless-controller.hotspot20',
290                       'icon',
291                       data=filtered_data,
292                       vdom=vdom)
293
294    elif state == "absent":
295        return fos.delete('wireless-controller.hotspot20',
296                          'icon',
297                          mkey=filtered_data['name'],
298                          vdom=vdom)
299    else:
300        fos._module.fail_json(msg='state must be present or absent!')
301
302
303def is_successful_status(status):
304    return status['status'] == "success" or \
305        status['http_method'] == "DELETE" and status['http_status'] == 404
306
307
308def fortios_wireless_controller_hotspot20(data, fos, check_mode):
309
310    if data['wireless_controller_hotspot20_icon']:
311        resp = wireless_controller_hotspot20_icon(data, fos, check_mode)
312    else:
313        fos._module.fail_json(msg='missing task body: %s' % ('wireless_controller_hotspot20_icon'))
314    if check_mode:
315        return resp
316    return not is_successful_status(resp), \
317        resp['status'] == "success" and \
318        (resp['revision_changed'] if 'revision_changed' in resp else True), \
319        resp
320
321
322versioned_schema = {
323    "type": "list",
324    "children": {
325        "icon_list": {
326            "type": "list",
327            "children": {
328                "lang": {
329                    "type": "string",
330                    "revisions": {
331                        "v6.0.0": True,
332                        "v7.0.0": True,
333                        "v6.0.5": True,
334                        "v6.4.4": True,
335                        "v6.4.0": True,
336                        "v6.4.1": True,
337                        "v6.2.0": True,
338                        "v6.2.3": True,
339                        "v6.2.5": True,
340                        "v6.2.7": True,
341                        "v6.0.11": True
342                    }
343                },
344                "name": {
345                    "type": "string",
346                    "revisions": {
347                        "v6.0.0": True,
348                        "v7.0.0": True,
349                        "v6.0.5": True,
350                        "v6.4.4": True,
351                        "v6.4.0": True,
352                        "v6.4.1": True,
353                        "v6.2.0": True,
354                        "v6.2.3": True,
355                        "v6.2.5": True,
356                        "v6.2.7": True,
357                        "v6.0.11": True
358                    }
359                },
360                "height": {
361                    "type": "integer",
362                    "revisions": {
363                        "v6.0.0": True,
364                        "v7.0.0": True,
365                        "v6.0.5": True,
366                        "v6.4.4": True,
367                        "v6.4.0": True,
368                        "v6.4.1": True,
369                        "v6.2.0": True,
370                        "v6.2.3": True,
371                        "v6.2.5": True,
372                        "v6.2.7": True,
373                        "v6.0.11": True
374                    }
375                },
376                "width": {
377                    "type": "integer",
378                    "revisions": {
379                        "v6.0.0": True,
380                        "v7.0.0": True,
381                        "v6.0.5": True,
382                        "v6.4.4": True,
383                        "v6.4.0": True,
384                        "v6.4.1": True,
385                        "v6.2.0": True,
386                        "v6.2.3": True,
387                        "v6.2.5": True,
388                        "v6.2.7": True,
389                        "v6.0.11": True
390                    }
391                },
392                "file": {
393                    "type": "string",
394                    "revisions": {
395                        "v6.0.0": True,
396                        "v7.0.0": True,
397                        "v6.0.5": True,
398                        "v6.4.4": True,
399                        "v6.4.0": True,
400                        "v6.4.1": True,
401                        "v6.2.0": True,
402                        "v6.2.3": True,
403                        "v6.2.5": True,
404                        "v6.2.7": True,
405                        "v6.0.11": True
406                    }
407                },
408                "type": {
409                    "type": "string",
410                    "options": [
411                        {
412                            "value": "bmp",
413                            "revisions": {
414                                "v6.0.0": True,
415                                "v7.0.0": True,
416                                "v6.0.5": True,
417                                "v6.4.4": True,
418                                "v6.4.0": True,
419                                "v6.4.1": True,
420                                "v6.2.0": True,
421                                "v6.2.3": True,
422                                "v6.2.5": True,
423                                "v6.2.7": True,
424                                "v6.0.11": True
425                            }
426                        },
427                        {
428                            "value": "gif",
429                            "revisions": {
430                                "v6.0.0": True,
431                                "v7.0.0": True,
432                                "v6.0.5": True,
433                                "v6.4.4": True,
434                                "v6.4.0": True,
435                                "v6.4.1": True,
436                                "v6.2.0": True,
437                                "v6.2.3": True,
438                                "v6.2.5": True,
439                                "v6.2.7": True,
440                                "v6.0.11": True
441                            }
442                        },
443                        {
444                            "value": "jpeg",
445                            "revisions": {
446                                "v6.0.0": True,
447                                "v7.0.0": True,
448                                "v6.0.5": True,
449                                "v6.4.4": True,
450                                "v6.4.0": True,
451                                "v6.4.1": True,
452                                "v6.2.0": True,
453                                "v6.2.3": True,
454                                "v6.2.5": True,
455                                "v6.2.7": True,
456                                "v6.0.11": True
457                            }
458                        },
459                        {
460                            "value": "png",
461                            "revisions": {
462                                "v6.0.0": True,
463                                "v7.0.0": True,
464                                "v6.0.5": True,
465                                "v6.4.4": True,
466                                "v6.4.0": True,
467                                "v6.4.1": True,
468                                "v6.2.0": True,
469                                "v6.2.3": True,
470                                "v6.2.5": True,
471                                "v6.2.7": True,
472                                "v6.0.11": True
473                            }
474                        },
475                        {
476                            "value": "tiff",
477                            "revisions": {
478                                "v6.0.0": True,
479                                "v7.0.0": True,
480                                "v6.0.5": True,
481                                "v6.4.4": True,
482                                "v6.4.0": True,
483                                "v6.4.1": True,
484                                "v6.2.0": True,
485                                "v6.2.3": True,
486                                "v6.2.5": True,
487                                "v6.2.7": True,
488                                "v6.0.11": True
489                            }
490                        }
491                    ],
492                    "revisions": {
493                        "v6.0.0": True,
494                        "v7.0.0": True,
495                        "v6.0.5": True,
496                        "v6.4.4": True,
497                        "v6.4.0": True,
498                        "v6.4.1": True,
499                        "v6.2.0": True,
500                        "v6.2.3": True,
501                        "v6.2.5": True,
502                        "v6.2.7": True,
503                        "v6.0.11": True
504                    }
505                }
506            },
507            "revisions": {
508                "v6.0.0": True,
509                "v7.0.0": True,
510                "v6.0.5": True,
511                "v6.4.4": True,
512                "v6.4.0": True,
513                "v6.4.1": True,
514                "v6.2.0": True,
515                "v6.2.3": True,
516                "v6.2.5": True,
517                "v6.2.7": True,
518                "v6.0.11": True
519            }
520        },
521        "name": {
522            "type": "string",
523            "revisions": {
524                "v6.0.0": True,
525                "v7.0.0": True,
526                "v6.0.5": True,
527                "v6.4.4": True,
528                "v6.4.0": True,
529                "v6.4.1": True,
530                "v6.2.0": True,
531                "v6.2.3": True,
532                "v6.2.5": True,
533                "v6.2.7": True,
534                "v6.0.11": True
535            }
536        }
537    },
538    "revisions": {
539        "v6.0.0": True,
540        "v7.0.0": True,
541        "v6.0.5": True,
542        "v6.4.4": True,
543        "v6.4.0": True,
544        "v6.4.1": True,
545        "v6.2.0": True,
546        "v6.2.3": True,
547        "v6.2.5": True,
548        "v6.2.7": True,
549        "v6.0.11": True
550    }
551}
552
553
554def main():
555    module_spec = schema_to_module_spec(versioned_schema)
556    mkeyname = 'name'
557    fields = {
558        "access_token": {"required": False, "type": "str", "no_log": True},
559        "enable_log": {"required": False, "type": bool},
560        "vdom": {"required": False, "type": "str", "default": "root"},
561        "state": {"required": True, "type": "str",
562                  "choices": ["present", "absent"]},
563        "wireless_controller_hotspot20_icon": {
564            "required": False, "type": "dict", "default": None,
565            "options": {
566            }
567        }
568    }
569    for attribute_name in module_spec['options']:
570        fields["wireless_controller_hotspot20_icon"]['options'][attribute_name] = module_spec['options'][attribute_name]
571        if mkeyname and mkeyname == attribute_name:
572            fields["wireless_controller_hotspot20_icon"]['options'][attribute_name]['required'] = True
573
574    check_legacy_fortiosapi()
575    module = AnsibleModule(argument_spec=fields,
576                           supports_check_mode=True)
577
578    versions_check_result = None
579    if module._socket_path:
580        connection = Connection(module._socket_path)
581        if 'access_token' in module.params:
582            connection.set_option('access_token', module.params['access_token'])
583
584        if 'enable_log' in module.params:
585            connection.set_option('enable_log', module.params['enable_log'])
586        else:
587            connection.set_option('enable_log', False)
588        fos = FortiOSHandler(connection, module, mkeyname)
589        versions_check_result = check_schema_versioning(fos, versioned_schema, "wireless_controller_hotspot20_icon")
590
591        is_error, has_changed, result = fortios_wireless_controller_hotspot20(module.params, fos, module.check_mode)
592
593    else:
594        module.fail_json(**FAIL_SOCKET_MSG)
595
596    if versions_check_result and versions_check_result['matched'] is False:
597        module.warn("Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv")
598
599    if not is_error:
600        if versions_check_result and versions_check_result['matched'] is False:
601            module.exit_json(changed=has_changed, version_check_warning=versions_check_result, meta=result)
602        else:
603            module.exit_json(changed=has_changed, meta=result)
604    else:
605        if versions_check_result and versions_check_result['matched'] is False:
606            module.fail_json(msg="Error in repo", version_check_warning=versions_check_result, meta=result)
607        else:
608            module.fail_json(msg="Error in repo", meta=result)
609
610
611if __name__ == '__main__':
612    main()
613