summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/network/aos/_aos_blueprint_virtnet.py
blob: 8814b26f46e3e427eab11455abbbd18b8a4171f2 (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
#!/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': ['deprecated'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: aos_blueprint_virtnet
author: Damien Garros (@dgarros)
version_added: "2.3"
short_description: Manage AOS blueprint parameter values
deprecated:
    removed_in: "2.9"
    why: This module does not support AOS 2.1 or later
    alternative: See new modules at U(https://www.ansible.com/ansible-apstra).
description:
 - Apstra AOS Blueprint Virtual Network module let you manage your Virtual Network easily.
   You can create access, define and delete Virtual Network by name or by using a JSON / Yaml 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
  blueprint:
    description:
      - Blueprint Name or Id as defined in AOS.
    required: True
  name:
    description:
      - Name of Virtual Network as part of the Blueprint.
  content:
    description:
      - Datastructure of the Virtual Network to manage. The data can be in YAML / JSON or
        directly a variable. It's the same datastructure that is returned on success in I(value).
  state:
    description:
      - Indicate what is the expected state of the Virtual Network (present or not).
    default: present
    choices: ['present', 'absent']
'''

EXAMPLES = '''

- name: "Access Existing Virtual Network"
  aos_blueprint_virtnet:
    session: "{{ aos_session }}"
    blueprint: "my-blueprint-l2"
    name: "my-virtual-network"
    state: present

- name: "Delete Virtual Network with JSON File"
  aos_blueprint_virtnet:
    session: "{{ aos_session }}"
    blueprint: "my-blueprint-l2"
    content: "{{ lookup('file', 'resources/virtual-network-02.json') }}"
    state: absent

- name: "Create Virtual Network"
  aos_blueprint_virtnet:
    session: "{{ aos_session }}"
    blueprint: "my-blueprint-l2"
    content: "{{ lookup('file', 'resources/virtual-network-02.json') }}"
    state: present
'''

import json

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


def ensure_present(module, aos, blueprint, virtnet):

    # if exist already return tru
    if virtnet.exists:
        module.exit_json(changed=False,
                         blueprint=blueprint.name,
                         name=virtnet.name,
                         id=virtnet.id,
                         value=virtnet.value)

    else:
        if not module.check_mode:
            try:
                virtnet.create(module.params['content'])
            except Exception as e:
                module.fail_json(msg="unable to create virtual-network : %s" % to_native(e))

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


def ensure_absent(module, aos, blueprint, virtnet):

    if virtnet.exists:
        if not module.check_mode:
            try:
                virtnet.delete()
            except Exception as e:
                module.fail_json(msg="unable to delete virtual-network %s : %s" % (virtnet.name, to_native(e)))

        module.exit_json(changed=True,
                         blueprint=blueprint.name)

    else:
        module.exit_json(changed=False,
                         blueprint=blueprint.name)


def blueprint_virtnet(module):

    margs = module.params

    # --------------------------------------------------------------------
    # Get AOS session object based on Session Info
    # --------------------------------------------------------------------
    try:
        aos = get_aos_session(module, margs['session'])
    except Exception:
        module.fail_json(msg="Unable to login to the AOS server")

    # --------------------------------------------------------------------
    # Get the blueprint Object based on either name or ID
    # --------------------------------------------------------------------
    try:
        blueprint = find_collection_item(aos.Blueprints,
                                         item_name=margs['blueprint'],
                                         item_id=margs['blueprint'])
    except Exception:
        module.fail_json(msg="Unable to find the Blueprint based on name or ID, something went wrong")

    if blueprint.exists is False:
        module.fail_json(msg='Blueprint %s does not exist.\n'
                             'known blueprints are [%s]' %
                             (margs['blueprint'], ','.join(aos.Blueprints.names)))

    # --------------------------------------------------------------------
    # Convert "content" to dict and extract name
    # --------------------------------------------------------------------
    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']

    # --------------------------------------------------------------------
    # Try to find VirtualNetwork object
    # --------------------------------------------------------------------
    try:
        virtnet = blueprint.VirtualNetworks[item_name]
    except Exception:
        module.fail_json(msg="Something went wrong while trying to find Virtual Network %s in blueprint %s"
                         % (item_name, blueprint.name))

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

        ensure_absent(module, aos, blueprint, virtnet)

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

        ensure_present(module, aos, blueprint, virtnet)


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

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

    blueprint_virtnet(module)


if __name__ == '__main__':
    main()