summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/network/aos/aos_external_router.py
blob: 37bb041e11a935c5b4fa94c5776a9ea6aa727da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python
#
# (c) 2017 Apstra Inc, <community@apstra.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: aos_external_router
author: Damien Garros (@dgarros)
version_added: "2.3"
short_description: Manage AOS External Router
description:
  - Apstra AOS External Router module let you manage your External Router easily. You can create
    create and delete External Router by Name, ID or by using a JSON File. This module
    is idempotent and support the I(check) mode. It's using the AOS REST API.
requirements:
  - "aos-pyez >= 0.6.0"
options:
  session:
    description:
      - An existing AOS session as obtained by M(aos_login) module.
    required: true
  name:
    description:
      - Name of the External Router to manage.
        Only one of I(name), I(id) or I(content) can be set.
  id:
    description:
      - AOS Id of the External Router to manage (can't be used to create a new External Router),
        Only one of I(name), I(id) or I(content) can be set.
  content:
    description:
      - Datastructure of the External Router to create. The format is defined by the
        I(content_format) parameter. It's the same datastructure that is returned
        on success in I(value).
  state:
    description:
      - Indicate what is the expected state of the External Router (present or not).
    default: present
    choices: ['present', 'absent']
  loopback:
    description:
      - IP address of the Loopback interface of the external_router.
  asn:
    description:
      - ASN id of the external_router.
'''

EXAMPLES = '''

- name: "Create an External Router"
  aos_external_router:
    session: "{{ aos_session }}"
    name: "my-external-router"
    loopback: 10.0.0.1
    asn: 65000
    state: present

- name: "Check if an External Router exist by ID"
  aos_external_router:
    session: "{{ aos_session }}"
    name: "45ab26fc-c2ed-4307-b330-0870488fa13e"
    state: present

- name: "Delete an External Router by name"
  aos_external_router:
    session: "{{ aos_session }}"
    name: "my-external-router"
    state: absent

- name: "Delete an External Router by id"
  aos_external_router:
    session: "{{ aos_session }}"
    id: "45ab26fc-c2ed-4307-b330-0870488fa13e"
    state: absent

# Save an External Router to a file
- name: "Access External Router 1/3"
  aos_external_router:
    session: "{{ aos_session }}"
    name: "my-external-router"
    state: present
  register: external_router

- name: "Save External Router into a file in JSON 2/3"
  copy:
    content: "{{ external_router.value | to_nice_json }}"
    dest: external_router_saved.json

- name: "Save External Router into a file in YAML 3/3"
  copy:
    content: "{{ external_router.value | to_nice_yaml }}"
    dest: external_router_saved.yaml

- name: "Load External Router from a JSON file"
  aos_external_router:
    session: "{{ aos_session }}"
    content: "{{ lookup('file', 'resources/external_router_saved.json') }}"
    state: present

- name: "Load External Router from a YAML file"
  aos_external_router:
    session: "{{ aos_session }}"
    content: "{{ lookup('file', 'resources/external_router_saved.yaml') }}"
    state: present
'''

RETURNS = '''
name:
  description: Name of the External Router
  returned: always
  type: str
  sample: Server-IpAddrs

id:
  description: AOS unique ID assigned to the External Router
  returned: always
  type: str
  sample: fcc4ac1c-e249-4fe7-b458-2138bfb44c06

value:
  description: Value of the object as returned by the AOS Server
  returned: always
  type: dict
  sample: {'...'}
'''

import json
import time

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.aos.aos import get_aos_session, find_collection_item, do_load_resource, check_aos_version, content_to_dict


def create_new_ext_router(module, my_ext_router, name, loopback, asn):

    # Create value
    datum = dict(display_name=name, address=loopback, asn=asn)

    my_ext_router.datum = datum

    # Write to AOS
    return my_ext_router.write()

#########################################################
# State Processing
#########################################################


def ext_router_absent(module, aos, my_ext_router):

    margs = module.params

    # If the module do not exist, return directly
    if my_ext_router.exists is False:
        module.exit_json(changed=False,
                         name=margs['name'],
                         id=margs['id'],
                         value={})

    # If not in check mode, delete External Router
    if not module.check_mode:
        try:
            # Add Sleep before delete to workaround a bug in AOS
            time.sleep(2)
            my_ext_router.delete()
        except:
            module.fail_json(msg="An error occurred, while trying to delete the External Router")

    module.exit_json(changed=True,
                     name=my_ext_router.name,
                     id=my_ext_router.id,
                     value={})


def ext_router_present(module, aos, my_ext_router):

    margs = module.params

    # if content is defined, create object from Content
    if my_ext_router.exists is False and margs['content'] is not None:
        do_load_resource(module, aos.ExternalRouters, module.params['content']['display_name'])

    # if my_ext_router doesn't exist already, create a new one
    if my_ext_router.exists is False and margs['name'] is None:
        module.fail_json(msg="Name is mandatory for module that don't exist currently")

    elif my_ext_router.exists is False:

        if not module.check_mode:
            try:
                my_new_ext_router = create_new_ext_router(module,
                                                          my_ext_router,
                                                          margs['name'],
                                                          margs['loopback'],
                                                          margs['asn'])
                my_ext_router = my_new_ext_router
            except:
                module.fail_json(msg="An error occurred while trying to create a new External Router")

        module.exit_json(changed=True,
                         name=my_ext_router.name,
                         id=my_ext_router.id,
                         value=my_ext_router.value)

    # if external Router already exist, check if loopback and ASN are the same
    # if same just return the object and report change false
    loopback = None
    asn = None

    # Identify the Loopback, parameter 'loopback' has priority over 'content'
    if margs['loopback'] is not None:
        loopback = margs['loopback']
    elif margs['content'] is not None:
        if 'address' in margs['content'].keys():
            loopback = margs['content']['address']

    # Identify the ASN, parameter 'asn' has priority over 'content'
    if margs['asn'] is not None:
        asn = margs['asn']
    elif margs['content'] is not None:
        if 'asn' in margs['content'].keys():
            asn = margs['content']['asn']

    # Compare Loopback and ASN if defined
    if loopback is not None:
        if loopback != my_ext_router.value['address']:
            module.fail_json(msg="my_ext_router already exist but Loopback is different, currently not supported to update a module")

    if asn is not None:
        if int(asn) != int(my_ext_router.value['asn']):
            module.fail_json(msg="my_ext_router already exist but ASN is different, currently not supported to update a module")

    module.exit_json(changed=False,
                     name=my_ext_router.name,
                     id=my_ext_router.id,
                     value=my_ext_router.value)

#########################################################
# Main Function
#########################################################


def ext_router(module):

    margs = module.params

    try:
        aos = get_aos_session(module, margs['session'])
    except:
        module.fail_json(msg="Unable to login to the AOS server")

    item_name = False
    item_id = False

    if margs['content'] is not None:

        content = content_to_dict(module, margs['content'])

        if 'display_name' in content.keys():
            item_name = content['display_name']
        else:
            module.fail_json(msg="Unable to extract 'display_name' from 'content'")

    elif margs['name'] is not None:
        item_name = margs['name']

    elif margs['id'] is not None:
        item_id = margs['id']

    # ----------------------------------------------------
    # Find Object if available based on ID or Name
    # ----------------------------------------------------
    try:
        my_ext_router = find_collection_item(aos.ExternalRouters,
                                             item_name=item_name,
                                             item_id=item_id)
    except:
        module.fail_json(msg="Unable to find the IP Pool based on name or ID, something went wrong")

    # ----------------------------------------------------
    # Proceed based on State value
    # ----------------------------------------------------
    if margs['state'] == 'absent':

        ext_router_absent(module, aos, my_ext_router)

    elif margs['state'] == 'present':

        ext_router_present(module, aos, my_ext_router)


def main():
    module = AnsibleModule(
        argument_spec=dict(
            session=dict(required=True, type="dict"),
            name=dict(required=False),
            id=dict(required=False),
            content=dict(required=False, type="json"),
            state=dict(required=False,
                       choices=['present', 'absent'],
                       default="present"),
            loopback=dict(required=False),
            asn=dict(required=False)
        ),
        mutually_exclusive=[('name', 'id', 'content')],
        required_one_of=[('name', 'id', 'content')],
        supports_check_mode=True
    )

    # Check if aos-pyez is present and match the minimum version
    check_aos_version(module, '0.6.0')

    ext_router(module)

if __name__ == "__main__":
    main()