summaryrefslogtreecommitdiffstats
path: root/ansible_collections/cisco/meraki/plugins/modules/meraki_ms_stack.py
blob: a5661c68b930f65cee3ac52f25b8fec51620019a (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
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2020, Kevin Breit (@kbreit) <kevin.breit@kevinbreit.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

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

DOCUMENTATION = r'''
---
module: meraki_ms_stack
short_description: Modify switch stacking configuration in Meraki.
version_added: "1.3.0"
description:
- Allows for modification of Meraki MS switch stacks.
notes:
- Not all actions are idempotent. Specifically, creating a new stack will error if any switch is already in a stack.
deprecated:
  removed_in: '3.0.0'
  why: Updated modules released with increased functionality
  alternative: cisco.meraki.networks_switch_stacks
options:
    state:
        description:
        - Create or modify an organization.
        choices: ['present', 'query', 'absent']
        default: present
        type: str
    net_name:
        description:
        - Name of network which MX firewall is in.
        type: str
    net_id:
        description:
        - ID of network which MX firewall is in.
        type: str
    stack_id:
        description:
        - ID of stack which is to be modified or deleted.
        type: str
    serials:
        description:
        - List of switch serial numbers which should be included or removed from a stack.
        type: list
        elements: str
    name:
        description:
        - Name of stack.
        type: str

author:
- Kevin Breit (@kbreit)
extends_documentation_fragment: cisco.meraki.meraki
'''

EXAMPLES = r'''
- name: Create new stack
  meraki_switch_stack:
    auth_key: abc123
    state: present
    org_name: YourOrg
    net_name: YourNet
    name: Test stack
    serials:
    - "ABCD-1231-4579"
    - "ASDF-4321-0987"

- name: Add switch to stack
  meraki_switch_stack:
    auth_key: abc123
    state: present
    org_name: YourOrg
    net_name: YourNet
    stack_id: ABC12340987
    serials:
    - "ABCD-1231-4579"

- name: Remove switch from stack
  meraki_switch_stack:
    auth_key: abc123
    state: absent
    org_name: YourOrg
    net_name: YourNet
    stack_id: ABC12340987
    serials:
    - "ABCD-1231-4579"

- name: Query one stack
  meraki_switch_stack:
    auth_key: abc123
    state: query
    org_name: YourOrg
    net_name: YourNet
    stack_id: ABC12340987
'''

RETURN = r'''
data:
    description: VPN settings.
    returned: success
    type: complex
    contains:
        id:
            description: ID of switch stack.
            returned: always
            type: str
            sample: 7636
        name:
            description: Descriptive name of switch stack.
            returned: always
            type: str
            sample: MyStack
        serials:
            description: List of serial numbers in switch stack.
            returned: always
            type: list
            sample:
            - "QBZY-XWVU-TSRQ"
            - "QBAB-CDEF-GHIJ"
'''

from ansible.module_utils.basic import AnsibleModule, json
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
from copy import deepcopy


def get_stacks(meraki, net_id):
    path = meraki.construct_path('get_all', net_id=net_id)
    return meraki.request(path, method='GET')


def get_stack(stack_id, stacks):
    for stack in stacks:
        if stack_id == stack['id']:
            return stack
    return None


def get_stack_id(meraki, net_id):
    stacks = get_stacks(meraki, net_id)
    for stack in stacks:
        if stack['name'] == meraki.params['name']:
            return stack['id']


def does_stack_exist(meraki, stacks):
    for stack in stacks:
        have = set(meraki.params['serials'])
        want = set(stack['serials'])
        if have == want:
            return stack
    return False


def main():
    # define the available arguments/parameters that a user can pass to
    # the module

    argument_spec = meraki_argument_spec()
    argument_spec.update(state=dict(type='str', choices=['present', 'query', 'absent'], default='present'),
                         net_name=dict(type='str'),
                         net_id=dict(type='str'),
                         stack_id=dict(type='str'),
                         serials=dict(type='list', elements='str', default=None),
                         name=dict(type='str'),
                         )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True,
                           )
    meraki = MerakiModule(module, function='switch_stack')

    meraki.params['follow_redirects'] = 'all'

    query_urls = {'switch_stack': '/networks/{net_id}/switch/stacks'}
    query_url = {'switch_stack': '/networks/{net_id}/switch/stacks/{stack_id}'}
    add_urls = {'switch_stack': '/networks/{net_id}/switch/stacks/{stack_id}/add'}
    remove_urls = {'switch_stack': '/networks/{net_id}/switch/stacks/{stack_id}/remove'}
    create_urls = {'switch_stack': '/networks/{net_id}/switch/stacks'}
    delete_urls = {'switch_stack': '/networks/{net_id}/switch/stacks/{stack_id}'}

    meraki.url_catalog['get_all'].update(query_urls)
    meraki.url_catalog['get_one'].update(query_url)
    meraki.url_catalog['add'] = add_urls
    meraki.url_catalog['remove'] = remove_urls
    meraki.url_catalog['create'] = create_urls
    meraki.url_catalog['delete'] = delete_urls

    payload = None

    # manipulate or modify the state as needed (this is going to be the
    # part where your module will do what it needs to do)
    org_id = meraki.params['org_id']
    if org_id is None:
        orgs = meraki.get_orgs()
        for org in orgs:
            if org['name'] == meraki.params['org_name']:
                org_id = org['id']
    net_id = meraki.params['net_id']
    if net_id is None:
        net_id = meraki.get_net_id(net_name=meraki.params['net_name'],
                                   data=meraki.get_nets(org_id=org_id))

    # assign and lookup stack_id
    stack_id = meraki.params['stack_id']
    if stack_id is None and meraki.params['name'] is not None:
        stack_id = get_stack_id(meraki, net_id)
    path = meraki.construct_path('get_all', net_id=net_id)
    stacks = meraki.request(path, method='GET')

    if meraki.params['state'] == 'query':
        if stack_id is None:
            meraki.result['data'] = stacks
        else:
            meraki.result['data'] = get_stack(stack_id, stacks)
    elif meraki.params['state'] == 'present':
        if meraki.params['stack_id'] is None:
            payload = {'serials': meraki.params['serials'],
                       'name': meraki.params['name'],
                       }
            path = meraki.construct_path('create', net_id=net_id)
            response = meraki.request(path, method='POST', payload=json.dumps(payload))
            if meraki.status == 201:
                meraki.result['data'] = response
                meraki.result['changed'] = True
        else:
            payload = {'serial': meraki.params['serials'][0]}
            original = get_stack(stack_id, stacks)
            comparable = deepcopy(original)
            comparable.update(payload)
            if meraki.params['serials'][0] not in comparable['serials']:
                comparable['serials'].append(meraki.params['serials'][0])
            # meraki.fail_json(msg=comparable)
            if meraki.is_update_required(original, comparable, optional_ignore=['serial']):
                path = meraki.construct_path('add', net_id=net_id, custom={'stack_id': stack_id})
                response = meraki.request(path, method='POST', payload=json.dumps(payload))
                if meraki.status == 200:
                    meraki.result['data'] = response
                    meraki.result['changed'] = True
            else:
                meraki.result['data'] = original
    elif meraki.params['state'] == 'absent':
        if meraki.params['serials'] is None:
            path = meraki.construct_path('delete', net_id=net_id, custom={'stack_id': stack_id})
            response = meraki.request(path, method='DELETE')
            meraki.result['data'] = {}
            meraki.result['changed'] = True
        else:
            for serial in meraki.params['serials']:
                payload = {'serial': serial}
                original = get_stack(stack_id, stacks)
                comparable = deepcopy(original)
                comparable.update(payload)
                if serial in comparable['serials']:
                    comparable['serials'].remove(serial)
                if meraki.is_update_required(original, comparable, optional_ignore=['serial']):
                    path = meraki.construct_path('remove', net_id=net_id, custom={'stack_id': stack_id})
                    response = meraki.request(path, method='POST', payload=json.dumps(payload))
                    if meraki.status == 200:
                        meraki.result['data'] = response
                        meraki.result['changed'] = True
                else:
                    meraki.result['data'] = original

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    meraki.exit_json(**meraki.result)


if __name__ == '__main__':
    main()