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_router_ospf
27short_description: Configure OSPF 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 router feature and ospf 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.8"
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        version_added: 2.9
76    router_ospf:
77        description:
78            - Configure OSPF.
79        default: null
80        type: dict
81        suboptions:
82            abr_type:
83                description:
84                    - Area border router type.
85                type: str
86                choices:
87                    - cisco
88                    - ibm
89                    - shortcut
90                    - standard
91            area:
92                description:
93                    - OSPF area configuration.
94                type: list
95                suboptions:
96                    authentication:
97                        description:
98                            - Authentication type.
99                        type: str
100                        choices:
101                            - none
102                            - text
103                            - md5
104                    default_cost:
105                        description:
106                            - Summary default cost of stub or NSSA area.
107                        type: int
108                    filter_list:
109                        description:
110                            - OSPF area filter-list configuration.
111                        type: list
112                        suboptions:
113                            direction:
114                                description:
115                                    - Direction.
116                                type: str
117                                choices:
118                                    - in
119                                    - out
120                            id:
121                                description:
122                                    - Filter list entry ID.
123                                required: true
124                                type: int
125                            list:
126                                description:
127                                    - Access-list or prefix-list name. Source router.access-list.name router.prefix-list.name.
128                                type: str
129                    id:
130                        description:
131                            - Area entry IP address.
132                        required: true
133                        type: str
134                    nssa_default_information_originate:
135                        description:
136                            - Redistribute, advertise, or do not originate Type-7 default route into NSSA area.
137                        type: str
138                        choices:
139                            - enable
140                            - always
141                            - disable
142                    nssa_default_information_originate_metric:
143                        description:
144                            - OSPF default metric.
145                        type: int
146                    nssa_default_information_originate_metric_type:
147                        description:
148                            - OSPF metric type for default routes.
149                        type: str
150                        choices:
151                            - 1
152                            - 2
153                    nssa_redistribution:
154                        description:
155                            - Enable/disable redistribute into NSSA area.
156                        type: str
157                        choices:
158                            - enable
159                            - disable
160                    nssa_translator_role:
161                        description:
162                            - NSSA translator role type.
163                        type: str
164                        choices:
165                            - candidate
166                            - never
167                            - always
168                    range:
169                        description:
170                            - OSPF area range configuration.
171                        type: list
172                        suboptions:
173                            advertise:
174                                description:
175                                    - Enable/disable advertise status.
176                                type: str
177                                choices:
178                                    - disable
179                                    - enable
180                            id:
181                                description:
182                                    - Range entry ID.
183                                required: true
184                                type: int
185                            prefix:
186                                description:
187                                    - Prefix.
188                                type: str
189                            substitute:
190                                description:
191                                    - Substitute prefix.
192                                type: str
193                            substitute_status:
194                                description:
195                                    - Enable/disable substitute status.
196                                type: str
197                                choices:
198                                    - enable
199                                    - disable
200                    shortcut:
201                        description:
202                            - Enable/disable shortcut option.
203                        type: str
204                        choices:
205                            - disable
206                            - enable
207                            - default
208                    stub_type:
209                        description:
210                            - Stub summary setting.
211                        type: str
212                        choices:
213                            - no-summary
214                            - summary
215                    type:
216                        description:
217                            - Area type setting.
218                        type: str
219                        choices:
220                            - regular
221                            - nssa
222                            - stub
223                    virtual_link:
224                        description:
225                            - OSPF virtual link configuration.
226                        type: list
227                        suboptions:
228                            authentication:
229                                description:
230                                    - Authentication type.
231                                type: str
232                                choices:
233                                    - none
234                                    - text
235                                    - md5
236                            authentication_key:
237                                description:
238                                    - Authentication key.
239                                type: str
240                            dead_interval:
241                                description:
242                                    - Dead interval.
243                                type: int
244                            hello_interval:
245                                description:
246                                    - Hello interval.
247                                type: int
248                            md5_key:
249                                description:
250                                    - MD5 key.
251                                type: str
252                            name:
253                                description:
254                                    - Virtual link entry name.
255                                required: true
256                                type: str
257                            peer:
258                                description:
259                                    - Peer IP.
260                                type: str
261                            retransmit_interval:
262                                description:
263                                    - Retransmit interval.
264                                type: int
265                            transmit_delay:
266                                description:
267                                    - Transmit delay.
268                                type: int
269            auto_cost_ref_bandwidth:
270                description:
271                    - Reference bandwidth in terms of megabits per second.
272                type: int
273            bfd:
274                description:
275                    - Bidirectional Forwarding Detection (BFD).
276                type: str
277                choices:
278                    - enable
279                    - disable
280            database_overflow:
281                description:
282                    - Enable/disable database overflow.
283                type: str
284                choices:
285                    - enable
286                    - disable
287            database_overflow_max_lsas:
288                description:
289                    - Database overflow maximum LSAs.
290                type: int
291            database_overflow_time_to_recover:
292                description:
293                    - Database overflow time to recover (sec).
294                type: int
295            default_information_metric:
296                description:
297                    - Default information metric.
298                type: int
299            default_information_metric_type:
300                description:
301                    - Default information metric type.
302                type: str
303                choices:
304                    - 1
305                    - 2
306            default_information_originate:
307                description:
308                    - Enable/disable generation of default route.
309                type: str
310                choices:
311                    - enable
312                    - always
313                    - disable
314            default_information_route_map:
315                description:
316                    - Default information route map. Source router.route-map.name.
317                type: str
318            default_metric:
319                description:
320                    - Default metric of redistribute routes.
321                type: int
322            distance:
323                description:
324                    - Distance of the route.
325                type: int
326            distance_external:
327                description:
328                    - Administrative external distance.
329                type: int
330            distance_inter_area:
331                description:
332                    - Administrative inter-area distance.
333                type: int
334            distance_intra_area:
335                description:
336                    - Administrative intra-area distance.
337                type: int
338            distribute_list:
339                description:
340                    - Distribute list configuration.
341                type: list
342                suboptions:
343                    access_list:
344                        description:
345                            - Access list name. Source router.access-list.name.
346                        type: str
347                    id:
348                        description:
349                            - Distribute list entry ID.
350                        required: true
351                        type: int
352                    protocol:
353                        description:
354                            - Protocol type.
355                        type: str
356                        choices:
357                            - connected
358                            - static
359                            - rip
360            distribute_list_in:
361                description:
362                    - Filter incoming routes. Source router.access-list.name router.prefix-list.name.
363                type: str
364            distribute_route_map_in:
365                description:
366                    - Filter incoming external routes by route-map. Source router.route-map.name.
367                type: str
368            log_neighbour_changes:
369                description:
370                    - Enable logging of OSPF neighbour's changes
371                type: str
372                choices:
373                    - enable
374                    - disable
375            neighbor:
376                description:
377                    - OSPF neighbor configuration are used when OSPF runs on non-broadcast media
378                type: list
379                suboptions:
380                    cost:
381                        description:
382                            - Cost of the interface, value range from 0 to 65535, 0 means auto-cost.
383                        type: int
384                    id:
385                        description:
386                            - Neighbor entry ID.
387                        required: true
388                        type: int
389                    ip:
390                        description:
391                            - Interface IP address of the neighbor.
392                        type: str
393                    poll_interval:
394                        description:
395                            - Poll interval time in seconds.
396                        type: int
397                    priority:
398                        description:
399                            - Priority.
400                        type: int
401            network:
402                description:
403                    - OSPF network configuration.
404                type: list
405                suboptions:
406                    area:
407                        description:
408                            - Attach the network to area.
409                        type: str
410                    id:
411                        description:
412                            - Network entry ID.
413                        required: true
414                        type: int
415                    prefix:
416                        description:
417                            - Prefix.
418                        type: str
419            ospf_interface:
420                description:
421                    - OSPF interface configuration.
422                type: list
423                suboptions:
424                    authentication:
425                        description:
426                            - Authentication type.
427                        type: str
428                        choices:
429                            - none
430                            - text
431                            - md5
432                    authentication_key:
433                        description:
434                            - Authentication key.
435                        type: str
436                    bfd:
437                        description:
438                            - Bidirectional Forwarding Detection (BFD).
439                        type: str
440                        choices:
441                            - global
442                            - enable
443                            - disable
444                    cost:
445                        description:
446                            - Cost of the interface, value range from 0 to 65535, 0 means auto-cost.
447                        type: int
448                    database_filter_out:
449                        description:
450                            - Enable/disable control of flooding out LSAs.
451                        type: str
452                        choices:
453                            - enable
454                            - disable
455                    dead_interval:
456                        description:
457                            - Dead interval.
458                        type: int
459                    hello_interval:
460                        description:
461                            - Hello interval.
462                        type: int
463                    hello_multiplier:
464                        description:
465                            - Number of hello packets within dead interval.
466                        type: int
467                    interface:
468                        description:
469                            - Configuration interface name. Source system.interface.name.
470                        type: str
471                    ip:
472                        description:
473                            - IP address.
474                        type: str
475                    md5_key:
476                        description:
477                            - MD5 key.
478                        type: str
479                    mtu:
480                        description:
481                            - MTU for database description packets.
482                        type: int
483                    mtu_ignore:
484                        description:
485                            - Enable/disable ignore MTU.
486                        type: str
487                        choices:
488                            - enable
489                            - disable
490                    name:
491                        description:
492                            - Interface entry name.
493                        required: true
494                        type: str
495                    network_type:
496                        description:
497                            - Network type.
498                        type: str
499                        choices:
500                            - broadcast
501                            - non-broadcast
502                            - point-to-point
503                            - point-to-multipoint
504                            - point-to-multipoint-non-broadcast
505                    prefix_length:
506                        description:
507                            - Prefix length.
508                        type: int
509                    priority:
510                        description:
511                            - Priority.
512                        type: int
513                    resync_timeout:
514                        description:
515                            - Graceful restart neighbor resynchronization timeout.
516                        type: int
517                    retransmit_interval:
518                        description:
519                            - Retransmit interval.
520                        type: int
521                    status:
522                        description:
523                            - Enable/disable status.
524                        type: str
525                        choices:
526                            - disable
527                            - enable
528                    transmit_delay:
529                        description:
530                            - Transmit delay.
531                        type: int
532            passive_interface:
533                description:
534                    - Passive interface configuration.
535                type: list
536                suboptions:
537                    name:
538                        description:
539                            - Passive interface name. Source system.interface.name.
540                        required: true
541                        type: str
542            redistribute:
543                description:
544                    - Redistribute configuration.
545                type: list
546                suboptions:
547                    metric:
548                        description:
549                            - Redistribute metric setting.
550                        type: int
551                    metric_type:
552                        description:
553                            - Metric type.
554                        type: str
555                        choices:
556                            - 1
557                            - 2
558                    name:
559                        description:
560                            - Redistribute name.
561                        required: true
562                        type: str
563                    routemap:
564                        description:
565                            - Route map name. Source router.route-map.name.
566                        type: str
567                    status:
568                        description:
569                            - status
570                        type: str
571                        choices:
572                            - enable
573                            - disable
574                    tag:
575                        description:
576                            - Tag value.
577                        type: int
578            restart_mode:
579                description:
580                    - OSPF restart mode (graceful or LLS).
581                type: str
582                choices:
583                    - none
584                    - lls
585                    - graceful-restart
586            restart_period:
587                description:
588                    - Graceful restart period.
589                type: int
590            rfc1583_compatible:
591                description:
592                    - Enable/disable RFC1583 compatibility.
593                type: str
594                choices:
595                    - enable
596                    - disable
597            router_id:
598                description:
599                    - Router ID.
600                type: str
601            spf_timers:
602                description:
603                    - SPF calculation frequency.
604                type: str
605            summary_address:
606                description:
607                    - IP address summary configuration.
608                type: list
609                suboptions:
610                    advertise:
611                        description:
612                            - Enable/disable advertise status.
613                        type: str
614                        choices:
615                            - disable
616                            - enable
617                    id:
618                        description:
619                            - Summary address entry ID.
620                        required: true
621                        type: int
622                    prefix:
623                        description:
624                            - Prefix.
625                        type: str
626                    tag:
627                        description:
628                            - Tag value.
629                        type: int
630'''
631
632EXAMPLES = '''
633- hosts: localhost
634  vars:
635   host: "192.168.122.40"
636   username: "admin"
637   password: ""
638   vdom: "root"
639   ssl_verify: "False"
640  tasks:
641  - name: Configure OSPF.
642    fortios_router_ospf:
643      host:  "{{ host }}"
644      username: "{{ username }}"
645      password: "{{ password }}"
646      vdom:  "{{ vdom }}"
647      https: "False"
648      router_ospf:
649        abr_type: "cisco"
650        area:
651         -
652            authentication: "none"
653            default_cost: "6"
654            filter_list:
655             -
656                direction: "in"
657                id:  "9"
658                list: "<your_own_value> (source router.access-list.name router.prefix-list.name)"
659            id:  "11"
660            nssa_default_information_originate: "enable"
661            nssa_default_information_originate_metric: "13"
662            nssa_default_information_originate_metric_type: "1"
663            nssa_redistribution: "enable"
664            nssa_translator_role: "candidate"
665            range:
666             -
667                advertise: "disable"
668                id:  "19"
669                prefix: "<your_own_value>"
670                substitute: "<your_own_value>"
671                substitute_status: "enable"
672            shortcut: "disable"
673            stub_type: "no-summary"
674            type: "regular"
675            virtual_link:
676             -
677                authentication: "none"
678                authentication_key: "<your_own_value>"
679                dead_interval: "29"
680                hello_interval: "30"
681                md5_key: "<your_own_value>"
682                name: "default_name_32"
683                peer: "<your_own_value>"
684                retransmit_interval: "34"
685                transmit_delay: "35"
686        auto_cost_ref_bandwidth: "36"
687        bfd: "enable"
688        database_overflow: "enable"
689        database_overflow_max_lsas: "39"
690        database_overflow_time_to_recover: "40"
691        default_information_metric: "41"
692        default_information_metric_type: "1"
693        default_information_originate: "enable"
694        default_information_route_map: "<your_own_value> (source router.route-map.name)"
695        default_metric: "45"
696        distance: "46"
697        distance_external: "47"
698        distance_inter_area: "48"
699        distance_intra_area: "49"
700        distribute_list:
701         -
702            access_list: "<your_own_value> (source router.access-list.name)"
703            id:  "52"
704            protocol: "connected"
705        distribute_list_in: "<your_own_value> (source router.access-list.name router.prefix-list.name)"
706        distribute_route_map_in: "<your_own_value> (source router.route-map.name)"
707        log_neighbour_changes: "enable"
708        neighbor:
709         -
710            cost: "58"
711            id:  "59"
712            ip: "<your_own_value>"
713            poll_interval: "61"
714            priority: "62"
715        network:
716         -
717            area: "<your_own_value>"
718            id:  "65"
719            prefix: "<your_own_value>"
720        ospf_interface:
721         -
722            authentication: "none"
723            authentication_key: "<your_own_value>"
724            bfd: "global"
725            cost: "71"
726            database_filter_out: "enable"
727            dead_interval: "73"
728            hello_interval: "74"
729            hello_multiplier: "75"
730            interface: "<your_own_value> (source system.interface.name)"
731            ip: "<your_own_value>"
732            md5_key: "<your_own_value>"
733            mtu: "79"
734            mtu_ignore: "enable"
735            name: "default_name_81"
736            network_type: "broadcast"
737            prefix_length: "83"
738            priority: "84"
739            resync_timeout: "85"
740            retransmit_interval: "86"
741            status: "disable"
742            transmit_delay: "88"
743        passive_interface:
744         -
745            name: "default_name_90 (source system.interface.name)"
746        redistribute:
747         -
748            metric: "92"
749            metric_type: "1"
750            name: "default_name_94"
751            routemap: "<your_own_value> (source router.route-map.name)"
752            status: "enable"
753            tag: "97"
754        restart_mode: "none"
755        restart_period: "99"
756        rfc1583_compatible: "enable"
757        router_id: "<your_own_value>"
758        spf_timers: "<your_own_value>"
759        summary_address:
760         -
761            advertise: "disable"
762            id:  "105"
763            prefix: "<your_own_value>"
764            tag: "107"
765'''
766
767RETURN = '''
768build:
769  description: Build number of the fortigate image
770  returned: always
771  type: str
772  sample: '1547'
773http_method:
774  description: Last method used to provision the content into FortiGate
775  returned: always
776  type: str
777  sample: 'PUT'
778http_status:
779  description: Last result given by FortiGate on last operation applied
780  returned: always
781  type: str
782  sample: "200"
783mkey:
784  description: Master key (id) used in the last call to FortiGate
785  returned: success
786  type: str
787  sample: "id"
788name:
789  description: Name of the table used to fulfill the request
790  returned: always
791  type: str
792  sample: "urlfilter"
793path:
794  description: Path of the table used to fulfill the request
795  returned: always
796  type: str
797  sample: "webfilter"
798revision:
799  description: Internal revision number
800  returned: always
801  type: str
802  sample: "17.0.2.10658"
803serial:
804  description: Serial number of the unit
805  returned: always
806  type: str
807  sample: "FGVMEVYYQT3AB5352"
808status:
809  description: Indication of the operation's result
810  returned: always
811  type: str
812  sample: "success"
813vdom:
814  description: Virtual domain used
815  returned: always
816  type: str
817  sample: "root"
818version:
819  description: Version of the FortiGate
820  returned: always
821  type: str
822  sample: "v5.6.3"
823
824'''
825
826from ansible.module_utils.basic import AnsibleModule
827from ansible.module_utils.connection import Connection
828from ansible.module_utils.network.fortios.fortios import FortiOSHandler
829from ansible.module_utils.network.fortimanager.common import FAIL_SOCKET_MSG
830
831
832def login(data, fos):
833    host = data['host']
834    username = data['username']
835    password = data['password']
836    ssl_verify = data['ssl_verify']
837
838    fos.debug('on')
839    if 'https' in data and not data['https']:
840        fos.https('off')
841    else:
842        fos.https('on')
843
844    fos.login(host, username, password, verify=ssl_verify)
845
846
847def filter_router_ospf_data(json):
848    option_list = ['abr_type', 'area', 'auto_cost_ref_bandwidth',
849                   'bfd', 'database_overflow', 'database_overflow_max_lsas',
850                   'database_overflow_time_to_recover', 'default_information_metric', 'default_information_metric_type',
851                   'default_information_originate', 'default_information_route_map', 'default_metric',
852                   'distance', 'distance_external', 'distance_inter_area',
853                   'distance_intra_area', 'distribute_list', 'distribute_list_in',
854                   'distribute_route_map_in', 'log_neighbour_changes', 'neighbor',
855                   'network', 'ospf_interface', 'passive_interface',
856                   'redistribute', 'restart_mode', 'restart_period',
857                   'rfc1583_compatible', 'router_id', 'spf_timers',
858                   'summary_address']
859    dictionary = {}
860
861    for attribute in option_list:
862        if attribute in json and json[attribute] is not None:
863            dictionary[attribute] = json[attribute]
864
865    return dictionary
866
867
868def underscore_to_hyphen(data):
869    if isinstance(data, list):
870        for elem in data:
871            elem = underscore_to_hyphen(elem)
872    elif isinstance(data, dict):
873        new_data = {}
874        for k, v in data.items():
875            new_data[k.replace('_', '-')] = underscore_to_hyphen(v)
876        data = new_data
877
878    return data
879
880
881def router_ospf(data, fos):
882    vdom = data['vdom']
883    router_ospf_data = data['router_ospf']
884    filtered_data = underscore_to_hyphen(filter_router_ospf_data(router_ospf_data))
885
886    return fos.set('router',
887                   'ospf',
888                   data=filtered_data,
889                   vdom=vdom)
890
891
892def is_successful_status(status):
893    return status['status'] == "success" or \
894        status['http_method'] == "DELETE" and status['http_status'] == 404
895
896
897def fortios_router(data, fos):
898
899    if data['router_ospf']:
900        resp = router_ospf(data, fos)
901
902    return not is_successful_status(resp), \
903        resp['status'] == "success", \
904        resp
905
906
907def main():
908    fields = {
909        "host": {"required": False, "type": "str"},
910        "username": {"required": False, "type": "str"},
911        "password": {"required": False, "type": "str", "default": "", "no_log": True},
912        "vdom": {"required": False, "type": "str", "default": "root"},
913        "https": {"required": False, "type": "bool", "default": True},
914        "ssl_verify": {"required": False, "type": "bool", "default": True},
915        "router_ospf": {
916            "required": False, "type": "dict", "default": None,
917            "options": {
918                "abr_type": {"required": False, "type": "str",
919                             "choices": ["cisco", "ibm", "shortcut",
920                                         "standard"]},
921                "area": {"required": False, "type": "list",
922                         "options": {
923                             "authentication": {"required": False, "type": "str",
924                                                "choices": ["none", "text", "md5"]},
925                             "default_cost": {"required": False, "type": "int"},
926                             "filter_list": {"required": False, "type": "list",
927                                             "options": {
928                                                 "direction": {"required": False, "type": "str",
929                                                               "choices": ["in", "out"]},
930                                                 "id": {"required": True, "type": "int"},
931                                                 "list": {"required": False, "type": "str"}
932                                             }},
933                             "id": {"required": True, "type": "str"},
934                             "nssa_default_information_originate": {"required": False, "type": "str",
935                                                                    "choices": ["enable", "always", "disable"]},
936                             "nssa_default_information_originate_metric": {"required": False, "type": "int"},
937                             "nssa_default_information_originate_metric_type": {"required": False, "type": "str",
938                                                                                "choices": ["1", "2"]},
939                             "nssa_redistribution": {"required": False, "type": "str",
940                                                     "choices": ["enable", "disable"]},
941                             "nssa_translator_role": {"required": False, "type": "str",
942                                                      "choices": ["candidate", "never", "always"]},
943                             "range": {"required": False, "type": "list",
944                                       "options": {
945                                           "advertise": {"required": False, "type": "str",
946                                                         "choices": ["disable", "enable"]},
947                                           "id": {"required": True, "type": "int"},
948                                           "prefix": {"required": False, "type": "str"},
949                                           "substitute": {"required": False, "type": "str"},
950                                           "substitute_status": {"required": False, "type": "str",
951                                                                 "choices": ["enable", "disable"]}
952                                       }},
953                             "shortcut": {"required": False, "type": "str",
954                                          "choices": ["disable", "enable", "default"]},
955                             "stub_type": {"required": False, "type": "str",
956                                           "choices": ["no-summary", "summary"]},
957                             "type": {"required": False, "type": "str",
958                                      "choices": ["regular", "nssa", "stub"]},
959                             "virtual_link": {"required": False, "type": "list",
960                                              "options": {
961                                                  "authentication": {"required": False, "type": "str",
962                                                                     "choices": ["none", "text", "md5"]},
963                                                  "authentication_key": {"required": False, "type": "str", "no_log": True},
964                                                  "dead_interval": {"required": False, "type": "int"},
965                                                  "hello_interval": {"required": False, "type": "int"},
966                                                  "md5_key": {"required": False, "type": "str", "no_log": True},
967                                                  "name": {"required": True, "type": "str"},
968                                                  "peer": {"required": False, "type": "str"},
969                                                  "retransmit_interval": {"required": False, "type": "int"},
970                                                  "transmit_delay": {"required": False, "type": "int"}
971                                              }}
972                         }},
973                "auto_cost_ref_bandwidth": {"required": False, "type": "int"},
974                "bfd": {"required": False, "type": "str",
975                        "choices": ["enable", "disable"]},
976                "database_overflow": {"required": False, "type": "str",
977                                      "choices": ["enable", "disable"]},
978                "database_overflow_max_lsas": {"required": False, "type": "int"},
979                "database_overflow_time_to_recover": {"required": False, "type": "int"},
980                "default_information_metric": {"required": False, "type": "int"},
981                "default_information_metric_type": {"required": False, "type": "str",
982                                                    "choices": ["1", "2"]},
983                "default_information_originate": {"required": False, "type": "str",
984                                                  "choices": ["enable", "always", "disable"]},
985                "default_information_route_map": {"required": False, "type": "str"},
986                "default_metric": {"required": False, "type": "int"},
987                "distance": {"required": False, "type": "int"},
988                "distance_external": {"required": False, "type": "int"},
989                "distance_inter_area": {"required": False, "type": "int"},
990                "distance_intra_area": {"required": False, "type": "int"},
991                "distribute_list": {"required": False, "type": "list",
992                                    "options": {
993                                        "access_list": {"required": False, "type": "str"},
994                                        "id": {"required": True, "type": "int"},
995                                        "protocol": {"required": False, "type": "str",
996                                                     "choices": ["connected", "static", "rip"]}
997                                    }},
998                "distribute_list_in": {"required": False, "type": "str"},
999                "distribute_route_map_in": {"required": False, "type": "str"},
1000                "log_neighbour_changes": {"required": False, "type": "str",
1001                                          "choices": ["enable", "disable"]},
1002                "neighbor": {"required": False, "type": "list",
1003                             "options": {
1004                                 "cost": {"required": False, "type": "int"},
1005                                 "id": {"required": True, "type": "int"},
1006                                 "ip": {"required": False, "type": "str"},
1007                                 "poll_interval": {"required": False, "type": "int"},
1008                                 "priority": {"required": False, "type": "int"}
1009                             }},
1010                "network": {"required": False, "type": "list",
1011                            "options": {
1012                                "area": {"required": False, "type": "str"},
1013                                "id": {"required": True, "type": "int"},
1014                                "prefix": {"required": False, "type": "str"}
1015                            }},
1016                "ospf_interface": {"required": False, "type": "list",
1017                                   "options": {
1018                                       "authentication": {"required": False, "type": "str",
1019                                                          "choices": ["none", "text", "md5"]},
1020                                       "authentication_key": {"required": False, "type": "str", "no_log": True},
1021                                       "bfd": {"required": False, "type": "str",
1022                                               "choices": ["global", "enable", "disable"]},
1023                                       "cost": {"required": False, "type": "int"},
1024                                       "database_filter_out": {"required": False, "type": "str",
1025                                                               "choices": ["enable", "disable"]},
1026                                       "dead_interval": {"required": False, "type": "int"},
1027                                       "hello_interval": {"required": False, "type": "int"},
1028                                       "hello_multiplier": {"required": False, "type": "int"},
1029                                       "interface": {"required": False, "type": "str"},
1030                                       "ip": {"required": False, "type": "str"},
1031                                       "md5_key": {"required": False, "type": "str", "no_log": True},
1032                                       "mtu": {"required": False, "type": "int"},
1033                                       "mtu_ignore": {"required": False, "type": "str",
1034                                                      "choices": ["enable", "disable"]},
1035                                       "name": {"required": True, "type": "str"},
1036                                       "network_type": {"required": False, "type": "str",
1037                                                        "choices": ["broadcast", "non-broadcast", "point-to-point",
1038                                                                    "point-to-multipoint", "point-to-multipoint-non-broadcast"]},
1039                                       "prefix_length": {"required": False, "type": "int"},
1040                                       "priority": {"required": False, "type": "int"},
1041                                       "resync_timeout": {"required": False, "type": "int"},
1042                                       "retransmit_interval": {"required": False, "type": "int"},
1043                                       "status": {"required": False, "type": "str",
1044                                                  "choices": ["disable", "enable"]},
1045                                       "transmit_delay": {"required": False, "type": "int"}
1046                                   }},
1047                "passive_interface": {"required": False, "type": "list",
1048                                      "options": {
1049                                          "name": {"required": True, "type": "str"}
1050                                      }},
1051                "redistribute": {"required": False, "type": "list",
1052                                 "options": {
1053                                     "metric": {"required": False, "type": "int"},
1054                                     "metric_type": {"required": False, "type": "str",
1055                                                     "choices": ["1", "2"]},
1056                                     "name": {"required": True, "type": "str"},
1057                                     "routemap": {"required": False, "type": "str"},
1058                                     "status": {"required": False, "type": "str",
1059                                                "choices": ["enable", "disable"]},
1060                                     "tag": {"required": False, "type": "int"}
1061                                 }},
1062                "restart_mode": {"required": False, "type": "str",
1063                                 "choices": ["none", "lls", "graceful-restart"]},
1064                "restart_period": {"required": False, "type": "int"},
1065                "rfc1583_compatible": {"required": False, "type": "str",
1066                                       "choices": ["enable", "disable"]},
1067                "router_id": {"required": False, "type": "str"},
1068                "spf_timers": {"required": False, "type": "str"},
1069                "summary_address": {"required": False, "type": "list",
1070                                    "options": {
1071                                        "advertise": {"required": False, "type": "str",
1072                                                      "choices": ["disable", "enable"]},
1073                                        "id": {"required": True, "type": "int"},
1074                                        "prefix": {"required": False, "type": "str"},
1075                                        "tag": {"required": False, "type": "int"}
1076                                    }}
1077
1078            }
1079        }
1080    }
1081
1082    module = AnsibleModule(argument_spec=fields,
1083                           supports_check_mode=False)
1084
1085    # legacy_mode refers to using fortiosapi instead of HTTPAPI
1086    legacy_mode = 'host' in module.params and module.params['host'] is not None and \
1087                  'username' in module.params and module.params['username'] is not None and \
1088                  'password' in module.params and module.params['password'] is not None
1089
1090    if not legacy_mode:
1091        if module._socket_path:
1092            connection = Connection(module._socket_path)
1093            fos = FortiOSHandler(connection)
1094
1095            is_error, has_changed, result = fortios_router(module.params, fos)
1096        else:
1097            module.fail_json(**FAIL_SOCKET_MSG)
1098    else:
1099        try:
1100            from fortiosapi import FortiOSAPI
1101        except ImportError:
1102            module.fail_json(msg="fortiosapi module is required")
1103
1104        fos = FortiOSAPI()
1105
1106        login(module.params, fos)
1107        is_error, has_changed, result = fortios_router(module.params, fos)
1108        fos.logout()
1109
1110    if not is_error:
1111        module.exit_json(changed=has_changed, meta=result)
1112    else:
1113        module.fail_json(msg="Error in repo", meta=result)
1114
1115
1116if __name__ == '__main__':
1117    main()
1118