1#!/usr/bin/python
2from __future__ import (absolute_import, division, print_function)
3# Copyright 2019 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_system_replacemsg_group
27short_description: Configure replacement message groups 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 system feature and replacemsg_group category.
31      Examples include all parameters and values need to be adjusted to datasources before usage.
32      Tested with FOS v6.0.5
33version_added: "2.9"
34author:
35    - Miguel Angel Munoz (@mamunozgonzalez)
36    - Nicolas Thomas (@thomnico)
37notes:
38    - Requires fortiosapi library developed by Fortinet
39    - Run as a local_action in your playbook
40requirements:
41    - fortiosapi>=0.9.8
42options:
43    host:
44        description:
45            - FortiOS or FortiGate IP address.
46        type: str
47        required: false
48    username:
49        description:
50            - FortiOS or FortiGate username.
51        type: str
52        required: false
53    password:
54        description:
55            - FortiOS or FortiGate password.
56        type: str
57        default: ""
58    vdom:
59        description:
60            - Virtual domain, among those defined previously. A vdom is a
61              virtual instance of the FortiGate that can be configured and
62              used as a different unit.
63        type: str
64        default: root
65    https:
66        description:
67            - Indicates if the requests towards FortiGate must use HTTPS protocol.
68        type: bool
69        default: true
70    ssl_verify:
71        description:
72            - Ensures FortiGate certificate must be verified by a proper CA.
73        type: bool
74        default: true
75    state:
76        description:
77            - Indicates whether to create or remove the object.
78        type: str
79        required: true
80        choices:
81            - present
82            - absent
83    system_replacemsg_group:
84        description:
85            - Configure replacement message groups.
86        default: null
87        type: dict
88        suboptions:
89            admin:
90                description:
91                    - Replacement message table entries.
92                type: list
93                suboptions:
94                    buffer:
95                        description:
96                            - Message string.
97                        type: str
98                    format:
99                        description:
100                            - Format flag.
101                        type: str
102                        choices:
103                            - none
104                            - text
105                            - html
106                            - wml
107                    header:
108                        description:
109                            - Header flag.
110                        type: str
111                        choices:
112                            - none
113                            - http
114                            - 8bit
115                    msg_type:
116                        description:
117                            - Message type.
118                        type: str
119            alertmail:
120                description:
121                    - Replacement message table entries.
122                type: list
123                suboptions:
124                    buffer:
125                        description:
126                            - Message string.
127                        type: str
128                    format:
129                        description:
130                            - Format flag.
131                        type: str
132                        choices:
133                            - none
134                            - text
135                            - html
136                            - wml
137                    header:
138                        description:
139                            - Header flag.
140                        type: str
141                        choices:
142                            - none
143                            - http
144                            - 8bit
145                    msg_type:
146                        description:
147                            - Message type.
148                        type: str
149            auth:
150                description:
151                    - Replacement message table entries.
152                type: list
153                suboptions:
154                    buffer:
155                        description:
156                            - Message string.
157                        type: str
158                    format:
159                        description:
160                            - Format flag.
161                        type: str
162                        choices:
163                            - none
164                            - text
165                            - html
166                            - wml
167                    header:
168                        description:
169                            - Header flag.
170                        type: str
171                        choices:
172                            - none
173                            - http
174                            - 8bit
175                    msg_type:
176                        description:
177                            - Message type.
178                        type: str
179            comment:
180                description:
181                    - Comment.
182                type: str
183            custom_message:
184                description:
185                    - Replacement message table entries.
186                type: list
187                suboptions:
188                    buffer:
189                        description:
190                            - Message string.
191                        type: str
192                    format:
193                        description:
194                            - Format flag.
195                        type: str
196                        choices:
197                            - none
198                            - text
199                            - html
200                            - wml
201                    header:
202                        description:
203                            - Header flag.
204                        type: str
205                        choices:
206                            - none
207                            - http
208                            - 8bit
209                    msg_type:
210                        description:
211                            - Message type.
212                        type: str
213            device_detection_portal:
214                description:
215                    - Replacement message table entries.
216                type: list
217                suboptions:
218                    buffer:
219                        description:
220                            - Message string.
221                        type: str
222                    format:
223                        description:
224                            - Format flag.
225                        type: str
226                        choices:
227                            - none
228                            - text
229                            - html
230                            - wml
231                    header:
232                        description:
233                            - Header flag.
234                        type: str
235                        choices:
236                            - none
237                            - http
238                            - 8bit
239                    msg_type:
240                        description:
241                            - Message type.
242                        type: str
243            ec:
244                description:
245                    - Replacement message table entries.
246                type: list
247                suboptions:
248                    buffer:
249                        description:
250                            - Message string.
251                        type: str
252                    format:
253                        description:
254                            - Format flag.
255                        type: str
256                        choices:
257                            - none
258                            - text
259                            - html
260                            - wml
261                    header:
262                        description:
263                            - Header flag.
264                        type: str
265                        choices:
266                            - none
267                            - http
268                            - 8bit
269                    msg_type:
270                        description:
271                            - Message type.
272                        type: str
273            fortiguard_wf:
274                description:
275                    - Replacement message table entries.
276                type: list
277                suboptions:
278                    buffer:
279                        description:
280                            - Message string.
281                        type: str
282                    format:
283                        description:
284                            - Format flag.
285                        type: str
286                        choices:
287                            - none
288                            - text
289                            - html
290                            - wml
291                    header:
292                        description:
293                            - Header flag.
294                        type: str
295                        choices:
296                            - none
297                            - http
298                            - 8bit
299                    msg_type:
300                        description:
301                            - Message type.
302                        type: str
303            ftp:
304                description:
305                    - Replacement message table entries.
306                type: list
307                suboptions:
308                    buffer:
309                        description:
310                            - Message string.
311                        type: str
312                    format:
313                        description:
314                            - Format flag.
315                        type: str
316                        choices:
317                            - none
318                            - text
319                            - html
320                            - wml
321                    header:
322                        description:
323                            - Header flag.
324                        type: str
325                        choices:
326                            - none
327                            - http
328                            - 8bit
329                    msg_type:
330                        description:
331                            - Message type.
332                        type: str
333            group_type:
334                description:
335                    - Group type.
336                type: str
337                choices:
338                    - default
339                    - utm
340                    - auth
341                    - ec
342            http:
343                description:
344                    - Replacement message table entries.
345                type: list
346                suboptions:
347                    buffer:
348                        description:
349                            - Message string.
350                        type: str
351                    format:
352                        description:
353                            - Format flag.
354                        type: str
355                        choices:
356                            - none
357                            - text
358                            - html
359                            - wml
360                    header:
361                        description:
362                            - Header flag.
363                        type: str
364                        choices:
365                            - none
366                            - http
367                            - 8bit
368                    msg_type:
369                        description:
370                            - Message type.
371                        type: str
372            icap:
373                description:
374                    - Replacement message table entries.
375                type: list
376                suboptions:
377                    buffer:
378                        description:
379                            - Message string.
380                        type: str
381                    format:
382                        description:
383                            - Format flag.
384                        type: str
385                        choices:
386                            - none
387                            - text
388                            - html
389                            - wml
390                    header:
391                        description:
392                            - Header flag.
393                        type: str
394                        choices:
395                            - none
396                            - http
397                            - 8bit
398                    msg_type:
399                        description:
400                            - Message type.
401                        type: str
402            mail:
403                description:
404                    - Replacement message table entries.
405                type: list
406                suboptions:
407                    buffer:
408                        description:
409                            - Message string.
410                        type: str
411                    format:
412                        description:
413                            - Format flag.
414                        type: str
415                        choices:
416                            - none
417                            - text
418                            - html
419                            - wml
420                    header:
421                        description:
422                            - Header flag.
423                        type: str
424                        choices:
425                            - none
426                            - http
427                            - 8bit
428                    msg_type:
429                        description:
430                            - Message type.
431                        type: str
432            nac_quar:
433                description:
434                    - Replacement message table entries.
435                type: list
436                suboptions:
437                    buffer:
438                        description:
439                            - Message string.
440                        type: str
441                    format:
442                        description:
443                            - Format flag.
444                        type: str
445                        choices:
446                            - none
447                            - text
448                            - html
449                            - wml
450                    header:
451                        description:
452                            - Header flag.
453                        type: str
454                        choices:
455                            - none
456                            - http
457                            - 8bit
458                    msg_type:
459                        description:
460                            - Message type.
461                        type: str
462            name:
463                description:
464                    - Group name.
465                required: true
466                type: str
467            nntp:
468                description:
469                    - Replacement message table entries.
470                type: list
471                suboptions:
472                    buffer:
473                        description:
474                            - Message string.
475                        type: str
476                    format:
477                        description:
478                            - Format flag.
479                        type: str
480                        choices:
481                            - none
482                            - text
483                            - html
484                            - wml
485                    header:
486                        description:
487                            - Header flag.
488                        type: str
489                        choices:
490                            - none
491                            - http
492                            - 8bit
493                    msg_type:
494                        description:
495                            - Message type.
496                        type: str
497            spam:
498                description:
499                    - Replacement message table entries.
500                type: list
501                suboptions:
502                    buffer:
503                        description:
504                            - Message string.
505                        type: str
506                    format:
507                        description:
508                            - Format flag.
509                        type: str
510                        choices:
511                            - none
512                            - text
513                            - html
514                            - wml
515                    header:
516                        description:
517                            - Header flag.
518                        type: str
519                        choices:
520                            - none
521                            - http
522                            - 8bit
523                    msg_type:
524                        description:
525                            - Message type.
526                        type: str
527            sslvpn:
528                description:
529                    - Replacement message table entries.
530                type: list
531                suboptions:
532                    buffer:
533                        description:
534                            - Message string.
535                        type: str
536                    format:
537                        description:
538                            - Format flag.
539                        type: str
540                        choices:
541                            - none
542                            - text
543                            - html
544                            - wml
545                    header:
546                        description:
547                            - Header flag.
548                        type: str
549                        choices:
550                            - none
551                            - http
552                            - 8bit
553                    msg_type:
554                        description:
555                            - Message type.
556                        type: str
557            traffic_quota:
558                description:
559                    - Replacement message table entries.
560                type: list
561                suboptions:
562                    buffer:
563                        description:
564                            - Message string.
565                        type: str
566                    format:
567                        description:
568                            - Format flag.
569                        type: str
570                        choices:
571                            - none
572                            - text
573                            - html
574                            - wml
575                    header:
576                        description:
577                            - Header flag.
578                        type: str
579                        choices:
580                            - none
581                            - http
582                            - 8bit
583                    msg_type:
584                        description:
585                            - Message type.
586                        type: str
587            utm:
588                description:
589                    - Replacement message table entries.
590                type: list
591                suboptions:
592                    buffer:
593                        description:
594                            - Message string.
595                        type: str
596                    format:
597                        description:
598                            - Format flag.
599                        type: str
600                        choices:
601                            - none
602                            - text
603                            - html
604                            - wml
605                    header:
606                        description:
607                            - Header flag.
608                        type: str
609                        choices:
610                            - none
611                            - http
612                            - 8bit
613                    msg_type:
614                        description:
615                            - Message type.
616                        type: str
617            webproxy:
618                description:
619                    - Replacement message table entries.
620                type: list
621                suboptions:
622                    buffer:
623                        description:
624                            - Message string.
625                        type: str
626                    format:
627                        description:
628                            - Format flag.
629                        type: str
630                        choices:
631                            - none
632                            - text
633                            - html
634                            - wml
635                    header:
636                        description:
637                            - Header flag.
638                        type: str
639                        choices:
640                            - none
641                            - http
642                            - 8bit
643                    msg_type:
644                        description:
645                            - Message type.
646                        type: str
647'''
648
649EXAMPLES = '''
650- hosts: localhost
651  vars:
652   host: "192.168.122.40"
653   username: "admin"
654   password: ""
655   vdom: "root"
656   ssl_verify: "False"
657  tasks:
658  - name: Configure replacement message groups.
659    fortios_system_replacemsg_group:
660      host:  "{{ host }}"
661      username: "{{ username }}"
662      password: "{{ password }}"
663      vdom:  "{{ vdom }}"
664      https: "False"
665      state: "present"
666      system_replacemsg_group:
667        admin:
668         -
669            buffer: "<your_own_value>"
670            format: "none"
671            header: "none"
672            msg_type: "<your_own_value>"
673        alertmail:
674         -
675            buffer: "<your_own_value>"
676            format: "none"
677            header: "none"
678            msg_type: "<your_own_value>"
679        auth:
680         -
681            buffer: "<your_own_value>"
682            format: "none"
683            header: "none"
684            msg_type: "<your_own_value>"
685        comment: "Comment."
686        custom_message:
687         -
688            buffer: "<your_own_value>"
689            format: "none"
690            header: "none"
691            msg_type: "<your_own_value>"
692        device_detection_portal:
693         -
694            buffer: "<your_own_value>"
695            format: "none"
696            header: "none"
697            msg_type: "<your_own_value>"
698        ec:
699         -
700            buffer: "<your_own_value>"
701            format: "none"
702            header: "none"
703            msg_type: "<your_own_value>"
704        fortiguard_wf:
705         -
706            buffer: "<your_own_value>"
707            format: "none"
708            header: "none"
709            msg_type: "<your_own_value>"
710        ftp:
711         -
712            buffer: "<your_own_value>"
713            format: "none"
714            header: "none"
715            msg_type: "<your_own_value>"
716        group_type: "default"
717        http:
718         -
719            buffer: "<your_own_value>"
720            format: "none"
721            header: "none"
722            msg_type: "<your_own_value>"
723        icap:
724         -
725            buffer: "<your_own_value>"
726            format: "none"
727            header: "none"
728            msg_type: "<your_own_value>"
729        mail:
730         -
731            buffer: "<your_own_value>"
732            format: "none"
733            header: "none"
734            msg_type: "<your_own_value>"
735        nac_quar:
736         -
737            buffer: "<your_own_value>"
738            format: "none"
739            header: "none"
740            msg_type: "<your_own_value>"
741        name: "default_name_65"
742        nntp:
743         -
744            buffer: "<your_own_value>"
745            format: "none"
746            header: "none"
747            msg_type: "<your_own_value>"
748        spam:
749         -
750            buffer: "<your_own_value>"
751            format: "none"
752            header: "none"
753            msg_type: "<your_own_value>"
754        sslvpn:
755         -
756            buffer: "<your_own_value>"
757            format: "none"
758            header: "none"
759            msg_type: "<your_own_value>"
760        traffic_quota:
761         -
762            buffer: "<your_own_value>"
763            format: "none"
764            header: "none"
765            msg_type: "<your_own_value>"
766        utm:
767         -
768            buffer: "<your_own_value>"
769            format: "none"
770            header: "none"
771            msg_type: "<your_own_value>"
772        webproxy:
773         -
774            buffer: "<your_own_value>"
775            format: "none"
776            header: "none"
777            msg_type: "<your_own_value>"
778'''
779
780RETURN = '''
781build:
782  description: Build number of the fortigate image
783  returned: always
784  type: str
785  sample: '1547'
786http_method:
787  description: Last method used to provision the content into FortiGate
788  returned: always
789  type: str
790  sample: 'PUT'
791http_status:
792  description: Last result given by FortiGate on last operation applied
793  returned: always
794  type: str
795  sample: "200"
796mkey:
797  description: Master key (id) used in the last call to FortiGate
798  returned: success
799  type: str
800  sample: "id"
801name:
802  description: Name of the table used to fulfill the request
803  returned: always
804  type: str
805  sample: "urlfilter"
806path:
807  description: Path of the table used to fulfill the request
808  returned: always
809  type: str
810  sample: "webfilter"
811revision:
812  description: Internal revision number
813  returned: always
814  type: str
815  sample: "17.0.2.10658"
816serial:
817  description: Serial number of the unit
818  returned: always
819  type: str
820  sample: "FGVMEVYYQT3AB5352"
821status:
822  description: Indication of the operation's result
823  returned: always
824  type: str
825  sample: "success"
826vdom:
827  description: Virtual domain used
828  returned: always
829  type: str
830  sample: "root"
831version:
832  description: Version of the FortiGate
833  returned: always
834  type: str
835  sample: "v5.6.3"
836
837'''
838
839from ansible.module_utils.basic import AnsibleModule
840from ansible.module_utils.connection import Connection
841from ansible.module_utils.network.fortios.fortios import FortiOSHandler
842from ansible.module_utils.network.fortimanager.common import FAIL_SOCKET_MSG
843
844
845def login(data, fos):
846    host = data['host']
847    username = data['username']
848    password = data['password']
849    ssl_verify = data['ssl_verify']
850
851    fos.debug('on')
852    if 'https' in data and not data['https']:
853        fos.https('off')
854    else:
855        fos.https('on')
856
857    fos.login(host, username, password, verify=ssl_verify)
858
859
860def filter_system_replacemsg_group_data(json):
861    option_list = ['admin', 'alertmail', 'auth',
862                   'comment', 'custom_message', 'device_detection_portal',
863                   'ec', 'fortiguard_wf', 'ftp',
864                   'group_type', 'http', 'icap',
865                   'mail', 'nac_quar', 'name',
866                   'nntp', 'spam', 'sslvpn',
867                   'traffic_quota', 'utm', 'webproxy']
868    dictionary = {}
869
870    for attribute in option_list:
871        if attribute in json and json[attribute] is not None:
872            dictionary[attribute] = json[attribute]
873
874    return dictionary
875
876
877def underscore_to_hyphen(data):
878    if isinstance(data, list):
879        for elem in data:
880            elem = underscore_to_hyphen(elem)
881    elif isinstance(data, dict):
882        new_data = {}
883        for k, v in data.items():
884            new_data[k.replace('_', '-')] = underscore_to_hyphen(v)
885        data = new_data
886
887    return data
888
889
890def system_replacemsg_group(data, fos):
891    vdom = data['vdom']
892    state = data['state']
893    system_replacemsg_group_data = data['system_replacemsg_group']
894    filtered_data = underscore_to_hyphen(filter_system_replacemsg_group_data(system_replacemsg_group_data))
895
896    if state == "present":
897        return fos.set('system',
898                       'replacemsg-group',
899                       data=filtered_data,
900                       vdom=vdom)
901
902    elif state == "absent":
903        return fos.delete('system',
904                          'replacemsg-group',
905                          mkey=filtered_data['name'],
906                          vdom=vdom)
907
908
909def is_successful_status(status):
910    return status['status'] == "success" or \
911        status['http_method'] == "DELETE" and status['http_status'] == 404
912
913
914def fortios_system(data, fos):
915
916    if data['system_replacemsg_group']:
917        resp = system_replacemsg_group(data, fos)
918
919    return not is_successful_status(resp), \
920        resp['status'] == "success", \
921        resp
922
923
924def main():
925    fields = {
926        "host": {"required": False, "type": "str"},
927        "username": {"required": False, "type": "str"},
928        "password": {"required": False, "type": "str", "default": "", "no_log": True},
929        "vdom": {"required": False, "type": "str", "default": "root"},
930        "https": {"required": False, "type": "bool", "default": True},
931        "ssl_verify": {"required": False, "type": "bool", "default": True},
932        "state": {"required": True, "type": "str",
933                  "choices": ["present", "absent"]},
934        "system_replacemsg_group": {
935            "required": False, "type": "dict", "default": None,
936            "options": {
937                "admin": {"required": False, "type": "list",
938                          "options": {
939                              "buffer": {"required": False, "type": "str"},
940                              "format": {"required": False, "type": "str",
941                                         "choices": ["none", "text", "html",
942                                                     "wml"]},
943                              "header": {"required": False, "type": "str",
944                                         "choices": ["none", "http", "8bit"]},
945                              "msg_type": {"required": False, "type": "str"}
946                          }},
947                "alertmail": {"required": False, "type": "list",
948                              "options": {
949                                  "buffer": {"required": False, "type": "str"},
950                                  "format": {"required": False, "type": "str",
951                                             "choices": ["none", "text", "html",
952                                                         "wml"]},
953                                  "header": {"required": False, "type": "str",
954                                             "choices": ["none", "http", "8bit"]},
955                                  "msg_type": {"required": False, "type": "str"}
956                              }},
957                "auth": {"required": False, "type": "list",
958                         "options": {
959                             "buffer": {"required": False, "type": "str"},
960                             "format": {"required": False, "type": "str",
961                                        "choices": ["none", "text", "html",
962                                                    "wml"]},
963                             "header": {"required": False, "type": "str",
964                                        "choices": ["none", "http", "8bit"]},
965                             "msg_type": {"required": False, "type": "str"}
966                         }},
967                "comment": {"required": False, "type": "str"},
968                "custom_message": {"required": False, "type": "list",
969                                   "options": {
970                                       "buffer": {"required": False, "type": "str"},
971                                       "format": {"required": False, "type": "str",
972                                                  "choices": ["none", "text", "html",
973                                                              "wml"]},
974                                       "header": {"required": False, "type": "str",
975                                                  "choices": ["none", "http", "8bit"]},
976                                       "msg_type": {"required": False, "type": "str"}
977                                   }},
978                "device_detection_portal": {"required": False, "type": "list",
979                                            "options": {
980                                                "buffer": {"required": False, "type": "str"},
981                                                "format": {"required": False, "type": "str",
982                                                           "choices": ["none", "text", "html",
983                                                                       "wml"]},
984                                                "header": {"required": False, "type": "str",
985                                                           "choices": ["none", "http", "8bit"]},
986                                                "msg_type": {"required": False, "type": "str"}
987                                            }},
988                "ec": {"required": False, "type": "list",
989                       "options": {
990                           "buffer": {"required": False, "type": "str"},
991                           "format": {"required": False, "type": "str",
992                                      "choices": ["none", "text", "html",
993                                                  "wml"]},
994                           "header": {"required": False, "type": "str",
995                                      "choices": ["none", "http", "8bit"]},
996                           "msg_type": {"required": False, "type": "str"}
997                       }},
998                "fortiguard_wf": {"required": False, "type": "list",
999                                  "options": {
1000                                      "buffer": {"required": False, "type": "str"},
1001                                      "format": {"required": False, "type": "str",
1002                                                 "choices": ["none", "text", "html",
1003                                                             "wml"]},
1004                                      "header": {"required": False, "type": "str",
1005                                                 "choices": ["none", "http", "8bit"]},
1006                                      "msg_type": {"required": False, "type": "str"}
1007                                  }},
1008                "ftp": {"required": False, "type": "list",
1009                        "options": {
1010                            "buffer": {"required": False, "type": "str"},
1011                            "format": {"required": False, "type": "str",
1012                                       "choices": ["none", "text", "html",
1013                                                   "wml"]},
1014                            "header": {"required": False, "type": "str",
1015                                       "choices": ["none", "http", "8bit"]},
1016                            "msg_type": {"required": False, "type": "str"}
1017                        }},
1018                "group_type": {"required": False, "type": "str",
1019                               "choices": ["default", "utm", "auth",
1020                                           "ec"]},
1021                "http": {"required": False, "type": "list",
1022                         "options": {
1023                             "buffer": {"required": False, "type": "str"},
1024                             "format": {"required": False, "type": "str",
1025                                        "choices": ["none", "text", "html",
1026                                                    "wml"]},
1027                             "header": {"required": False, "type": "str",
1028                                        "choices": ["none", "http", "8bit"]},
1029                             "msg_type": {"required": False, "type": "str"}
1030                         }},
1031                "icap": {"required": False, "type": "list",
1032                         "options": {
1033                             "buffer": {"required": False, "type": "str"},
1034                             "format": {"required": False, "type": "str",
1035                                        "choices": ["none", "text", "html",
1036                                                    "wml"]},
1037                             "header": {"required": False, "type": "str",
1038                                        "choices": ["none", "http", "8bit"]},
1039                             "msg_type": {"required": False, "type": "str"}
1040                         }},
1041                "mail": {"required": False, "type": "list",
1042                         "options": {
1043                             "buffer": {"required": False, "type": "str"},
1044                             "format": {"required": False, "type": "str",
1045                                        "choices": ["none", "text", "html",
1046                                                    "wml"]},
1047                             "header": {"required": False, "type": "str",
1048                                        "choices": ["none", "http", "8bit"]},
1049                             "msg_type": {"required": False, "type": "str"}
1050                         }},
1051                "nac_quar": {"required": False, "type": "list",
1052                             "options": {
1053                                 "buffer": {"required": False, "type": "str"},
1054                                 "format": {"required": False, "type": "str",
1055                                            "choices": ["none", "text", "html",
1056                                                        "wml"]},
1057                                 "header": {"required": False, "type": "str",
1058                                            "choices": ["none", "http", "8bit"]},
1059                                 "msg_type": {"required": False, "type": "str"}
1060                             }},
1061                "name": {"required": True, "type": "str"},
1062                "nntp": {"required": False, "type": "list",
1063                         "options": {
1064                             "buffer": {"required": False, "type": "str"},
1065                             "format": {"required": False, "type": "str",
1066                                        "choices": ["none", "text", "html",
1067                                                    "wml"]},
1068                             "header": {"required": False, "type": "str",
1069                                        "choices": ["none", "http", "8bit"]},
1070                             "msg_type": {"required": False, "type": "str"}
1071                         }},
1072                "spam": {"required": False, "type": "list",
1073                         "options": {
1074                             "buffer": {"required": False, "type": "str"},
1075                             "format": {"required": False, "type": "str",
1076                                        "choices": ["none", "text", "html",
1077                                                    "wml"]},
1078                             "header": {"required": False, "type": "str",
1079                                        "choices": ["none", "http", "8bit"]},
1080                             "msg_type": {"required": False, "type": "str"}
1081                         }},
1082                "sslvpn": {"required": False, "type": "list",
1083                           "options": {
1084                               "buffer": {"required": False, "type": "str"},
1085                               "format": {"required": False, "type": "str",
1086                                          "choices": ["none", "text", "html",
1087                                                      "wml"]},
1088                               "header": {"required": False, "type": "str",
1089                                          "choices": ["none", "http", "8bit"]},
1090                               "msg_type": {"required": False, "type": "str"}
1091                           }},
1092                "traffic_quota": {"required": False, "type": "list",
1093                                  "options": {
1094                                      "buffer": {"required": False, "type": "str"},
1095                                      "format": {"required": False, "type": "str",
1096                                                 "choices": ["none", "text", "html",
1097                                                             "wml"]},
1098                                      "header": {"required": False, "type": "str",
1099                                                 "choices": ["none", "http", "8bit"]},
1100                                      "msg_type": {"required": False, "type": "str"}
1101                                  }},
1102                "utm": {"required": False, "type": "list",
1103                        "options": {
1104                            "buffer": {"required": False, "type": "str"},
1105                            "format": {"required": False, "type": "str",
1106                                       "choices": ["none", "text", "html",
1107                                                   "wml"]},
1108                            "header": {"required": False, "type": "str",
1109                                       "choices": ["none", "http", "8bit"]},
1110                            "msg_type": {"required": False, "type": "str"}
1111                        }},
1112                "webproxy": {"required": False, "type": "list",
1113                             "options": {
1114                                 "buffer": {"required": False, "type": "str"},
1115                                 "format": {"required": False, "type": "str",
1116                                            "choices": ["none", "text", "html",
1117                                                        "wml"]},
1118                                 "header": {"required": False, "type": "str",
1119                                            "choices": ["none", "http", "8bit"]},
1120                                 "msg_type": {"required": False, "type": "str"}
1121                             }}
1122
1123            }
1124        }
1125    }
1126
1127    module = AnsibleModule(argument_spec=fields,
1128                           supports_check_mode=False)
1129
1130    # legacy_mode refers to using fortiosapi instead of HTTPAPI
1131    legacy_mode = 'host' in module.params and module.params['host'] is not None and \
1132                  'username' in module.params and module.params['username'] is not None and \
1133                  'password' in module.params and module.params['password'] is not None
1134
1135    if not legacy_mode:
1136        if module._socket_path:
1137            connection = Connection(module._socket_path)
1138            fos = FortiOSHandler(connection)
1139
1140            is_error, has_changed, result = fortios_system(module.params, fos)
1141        else:
1142            module.fail_json(**FAIL_SOCKET_MSG)
1143    else:
1144        try:
1145            from fortiosapi import FortiOSAPI
1146        except ImportError:
1147            module.fail_json(msg="fortiosapi module is required")
1148
1149        fos = FortiOSAPI()
1150
1151        login(module.params, fos)
1152        is_error, has_changed, result = fortios_system(module.params, fos)
1153        fos.logout()
1154
1155    if not is_error:
1156        module.exit_json(changed=has_changed, meta=result)
1157    else:
1158        module.fail_json(msg="Error in repo", meta=result)
1159
1160
1161if __name__ == '__main__':
1162    main()
1163