1#!/usr/local/bin/python3.8
2#
3# This file is part of Ansible
4#
5# Ansible 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# Ansible 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 Ansible.  If not, see <http://www.gnu.org/licenses/>.
17#
18
19from __future__ import absolute_import, division, print_function
20__metaclass__ = type
21
22DOCUMENTATION = '''
23---
24module: fmgr_fwobj_ippool6
25notes:
26    - Full Documentation at U(https://ftnt-ansible-docs.readthedocs.io/en/latest/).
27author:
28    - Luke Weighall (@lweighall)
29    - Andrew Welsh (@Ghilli3)
30    - Jim Huber (@p4r4n0y1ng)
31short_description: Allows the editing of IP Pool Objects within FortiManager.
32description:
33  -  Allows users to add/edit/delete IPv6 Pool Objects.
34
35options:
36  adom:
37    description:
38      - The ADOM the configuration should belong to.
39    required: false
40    default: root
41
42  mode:
43    description:
44      - Sets one of three modes for managing the object.
45      - Allows use of soft-adds instead of overwriting existing values
46    choices: ['add', 'set', 'delete', 'update']
47    required: false
48    default: add
49
50  startip:
51    description:
52      - First IPv6 address (inclusive) in the range for the address pool.
53    required: false
54
55  name:
56    description:
57      - IPv6 IP pool name.
58    required: false
59
60  endip:
61    description:
62      - Final IPv6 address (inclusive) in the range for the address pool.
63    required: false
64
65  comments:
66    description:
67      - Comment.
68    required: false
69
70  dynamic_mapping:
71    description:
72      - EXPERTS ONLY! KNOWLEDGE OF FMGR JSON API IS REQUIRED!
73      - List of multiple child objects to be added. Expects a list of dictionaries.
74      - Dictionaries must use FortiManager API parameters, not the ansible ones listed below.
75      - If submitted, all other prefixed sub-parameters ARE IGNORED.
76      - This object is MUTUALLY EXCLUSIVE with its options.
77      - We expect that you know what you are doing with these list parameters, and are leveraging the JSON API Guide.
78      - WHEN IN DOUBT, USE THE SUB OPTIONS BELOW INSTEAD TO CREATE OBJECTS WITH MULTIPLE TASKS
79    required: false
80
81  dynamic_mapping_comments:
82    description:
83      - Dynamic Mapping clone of original suffixed parameter.
84    required: false
85
86  dynamic_mapping_endip:
87    description:
88      - Dynamic Mapping clone of original suffixed parameter.
89    required: false
90
91  dynamic_mapping_startip:
92    description:
93      - Dynamic Mapping clone of original suffixed parameter.
94    required: false
95
96
97'''
98
99EXAMPLES = '''
100- name: ADD FMGR_FIREWALL_IPPOOL6
101  fmgr_firewall_ippool6:
102    mode: "add"
103    adom: "ansible"
104    startip:
105    name: "IPv6 IPPool"
106    endip:
107    comments: "Created by Ansible"
108
109- name: DELETE FMGR_FIREWALL_IPPOOL6
110  fmgr_firewall_ippool6:
111    mode: "delete"
112    adom: "ansible"
113    name: "IPv6 IPPool"
114'''
115
116RETURN = """
117api_result:
118  description: full API response, includes status code and message
119  returned: always
120  type: str
121"""
122
123from ansible.module_utils.basic import AnsibleModule, env_fallback
124from ansible.module_utils.connection import Connection
125from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.fortimanager import FortiManagerHandler
126from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import FMGBaseException
127from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import FMGRCommon
128from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import DEFAULT_RESULT_OBJ
129from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import FAIL_SOCKET_MSG
130from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import prepare_dict
131from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import scrub_dict
132
133
134def fmgr_fwobj_ippool6_modify(fmgr, paramgram):
135    """
136    :param fmgr: The fmgr object instance from fortimanager.py
137    :type fmgr: class object
138    :param paramgram: The formatted dictionary of options to process
139    :type paramgram: dict
140    :return: The response from the FortiManager
141    :rtype: dict
142    """
143
144    mode = paramgram["mode"]
145    adom = paramgram["adom"]
146    # INIT A BASIC OBJECTS
147    response = DEFAULT_RESULT_OBJ
148    url = ""
149    datagram = {}
150
151    # EVAL THE MODE PARAMETER FOR SET OR ADD
152    if mode in ['set', 'add', 'update']:
153        url = '/pm/config/adom/{adom}/obj/firewall/ippool6'.format(adom=adom)
154        datagram = scrub_dict(prepare_dict(paramgram))
155
156    # EVAL THE MODE PARAMETER FOR DELETE
157    elif mode == "delete":
158        # SET THE CORRECT URL FOR DELETE
159        url = '/pm/config/adom/{adom}/obj/firewall/ippool6/{name}'.format(adom=adom, name=paramgram["name"])
160        datagram = {}
161
162    response = fmgr.process_request(url, datagram, paramgram["mode"])
163    return response
164
165
166def main():
167    argument_spec = dict(
168        adom=dict(type="str", default="root"),
169        mode=dict(choices=["add", "set", "delete", "update"], type="str", default="add"),
170        startip=dict(required=False, type="str"),
171        name=dict(required=False, type="str"),
172        endip=dict(required=False, type="str"),
173        comments=dict(required=False, type="str"),
174        dynamic_mapping=dict(required=False, type="list"),
175        dynamic_mapping_comments=dict(required=False, type="str"),
176        dynamic_mapping_endip=dict(required=False, type="str"),
177        dynamic_mapping_startip=dict(required=False, type="str"),
178
179    )
180
181    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False, )
182    # MODULE PARAMGRAM
183    paramgram = {
184        "mode": module.params["mode"],
185        "adom": module.params["adom"],
186        "startip": module.params["startip"],
187        "name": module.params["name"],
188        "endip": module.params["endip"],
189        "comments": module.params["comments"],
190        "dynamic_mapping": {
191            "comments": module.params["dynamic_mapping_comments"],
192            "endip": module.params["dynamic_mapping_endip"],
193            "startip": module.params["dynamic_mapping_startip"],
194        }
195    }
196    module.paramgram = paramgram
197    fmgr = None
198    if module._socket_path:
199        connection = Connection(module._socket_path)
200        fmgr = FortiManagerHandler(connection, module)
201        fmgr.tools = FMGRCommon()
202    else:
203        module.fail_json(**FAIL_SOCKET_MSG)
204
205    list_overrides = ['dynamic_mapping']
206    paramgram = fmgr.tools.paramgram_child_list_override(list_overrides=list_overrides,
207                                                         paramgram=paramgram, module=module)
208
209    results = DEFAULT_RESULT_OBJ
210
211    try:
212        results = fmgr_fwobj_ippool6_modify(fmgr, paramgram)
213        fmgr.govern_response(module=module, results=results,
214                             ansible_facts=fmgr.construct_ansible_facts(results, module.params, paramgram))
215
216    except Exception as err:
217        raise FMGBaseException(err)
218
219    return module.exit_json(**results[1])
220
221
222if __name__ == "__main__":
223    main()
224