Debian Codegraph @

ansible_collections/cisco/meraki/plugins/modules/meraki_ms_stack.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, Kevin Breit (@kbreit) <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {
11 'metadata_version': '1.1',
12 "status": ['deprecated'],
13 'supported_by': 'community'
14 }
15
16 DOCUMENTATION = r'''
17 ---
18 module: meraki_ms_stack
19 short_description: Modify switch stacking configuration in Meraki.
20 version_added: "1.3.0"
21 description:
22 - Allows for modification of Meraki MS switch stacks.
23 notes:
24 - Not all actions are idempotent. Specifically, creating a new stack will error if any switch is already in a stack.
25 deprecated:
26 removed_in: '3.0.0'
27 why: Updated modules released with increased functionality
28 alternative: cisco.meraki.networks_switch_stacks
29 options:
30 state:
31 description:
32 - Create or modify an organization.
33 choices: ['present', 'query', 'absent']
34 default: present
35 type: str
36 net_name:
37 description:
38 - Name of network which MX firewall is in.
39 type: str
40 net_id:
41 description:
42 - ID of network which MX firewall is in.
43 type: str
44 stack_id:
45 description:
46 - ID of stack which is to be modified or deleted.
47 type: str
48 serials:
49 description:
50 - List of switch serial numbers which should be included or removed from a stack.
51 type: list
52 elements: str
53 name:
54 description:
55 - Name of stack.
56 type: str
57
58 author:
59 - Kevin Breit (@kbreit)
60 extends_documentation_fragment: cisco.meraki.meraki
61 '''
62
63 EXAMPLES = r'''
64 - name: Create new stack
65 meraki_switch_stack:
66 auth_key: abc123
67 state: present
68 org_name: YourOrg
69 net_name: YourNet
70 name: Test stack
71 serials:
72 - "ABCD-1231-4579"
73 - "ASDF-4321-0987"
74
75 - name: Add switch to stack
76 meraki_switch_stack:
77 auth_key: abc123
78 state: present
79 org_name: YourOrg
80 net_name: YourNet
81 stack_id: ABC12340987
82 serials:
83 - "ABCD-1231-4579"
84
85 - name: Remove switch from stack
86 meraki_switch_stack:
87 auth_key: abc123
88 state: absent
89 org_name: YourOrg
90 net_name: YourNet
91 stack_id: ABC12340987
92 serials:
93 - "ABCD-1231-4579"
94
95 - name: Query one stack
96 meraki_switch_stack:
97 auth_key: abc123
98 state: query
99 org_name: YourOrg
100 net_name: YourNet
101 stack_id: ABC12340987
102 '''
103
104 RETURN = r'''
105 data:
106 description: VPN settings.
107 returned: success
108 type: complex
109 contains:
110 id:
111 description: ID of switch stack.
112 returned: always
113 type: str
114 sample: 7636
115 name:
116 description: Descriptive name of switch stack.
117 returned: always
118 type: str
119 sample: MyStack
120 serials:
121 description: List of serial numbers in switch stack.
122 returned: always
123 type: list
124 sample:
125 - "QBZY-XWVU-TSRQ"
126 - "QBAB-CDEF-GHIJ"
127 '''
128
129 from ansible.module_utils.basic import AnsibleModule, json
130 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
131 from copy import deepcopy
132
133
134 def get_stacks(meraki, net_id):
135 path = meraki.construct_path('get_all', net_id=net_id)
136 return meraki.request(path, method='GET')
137
138
139 def get_stack(stack_id, stacks):
140 for stack in stacks:
141 if stack_id == stack['id']:
142 return stack
143 return None
144
145
146 def get_stack_id(meraki, net_id):
147 stacks = get_stacks(meraki, net_id)
148 for stack in stacks:
149 if stack['name'] == meraki.params['name']:
150 return stack['id']
151
152
153 def does_stack_exist(meraki, stacks):
154 for stack in stacks:
155 have = set(meraki.params['serials'])
156 want = set(stack['serials'])
157 if have == want:
158 return stack
159 return False
160
161
162 def main():
163 # define the available arguments/parameters that a user can pass to
164 # the module
165
166 argument_spec = meraki_argument_spec()
167 argument_spec.update(state=dict(type='str', choices=['present', 'query', 'absent'], default='present'),
168 net_name=dict(type='str'),
169 net_id=dict(type='str'),
170 stack_id=dict(type='str'),
171 serials=dict(type='list', elements='str', default=None),
172 name=dict(type='str'),
173 )
174
175 # the AnsibleModule object will be our abstraction working with Ansible
176 # this includes instantiation, a couple of common attr would be the
177 # args/params passed to the execution, as well as if the module
178 # supports check mode
179 module = AnsibleModule(argument_spec=argument_spec,
180 supports_check_mode=True,
181 )
182 meraki = MerakiModule(module, function='switch_stack')
183
184 meraki.params['follow_redirects'] = 'all'
185
186 query_urls = {'switch_stack': '/networks/{net_id}/switch/stacks'}
187 query_url = {'switch_stack': '/networks/{net_id}/switch/stacks/{stack_id}'}
188 add_urls = {'switch_stack': '/networks/{net_id}/switch/stacks/{stack_id}/add'}
189 remove_urls = {'switch_stack': '/networks/{net_id}/switch/stacks/{stack_id}/remove'}
190 create_urls = {'switch_stack': '/networks/{net_id}/switch/stacks'}
191 delete_urls = {'switch_stack': '/networks/{net_id}/switch/stacks/{stack_id}'}
192
193 meraki.url_catalog['get_all'].update(query_urls)
194 meraki.url_catalog['get_one'].update(query_url)
195 meraki.url_catalog['add'] = add_urls
196 meraki.url_catalog['remove'] = remove_urls
197 meraki.url_catalog['create'] = create_urls
198 meraki.url_catalog['delete'] = delete_urls
199
200 payload = None
201
202 # manipulate or modify the state as needed (this is going to be the
203 # part where your module will do what it needs to do)
204 org_id = meraki.params['org_id']
205 if org_id is None:
206 orgs = meraki.get_orgs()
207 for org in orgs:
208 if org['name'] == meraki.params['org_name']:
209 org_id = org['id']
210 net_id = meraki.params['net_id']
211 if net_id is None:
212 net_id = meraki.get_net_id(net_name=meraki.params['net_name'],
213 data=meraki.get_nets(org_id=org_id))
214
215 # assign and lookup stack_id
216 stack_id = meraki.params['stack_id']
217 if stack_id is None and meraki.params['name'] is not None:
218 stack_id = get_stack_id(meraki, net_id)
219 path = meraki.construct_path('get_all', net_id=net_id)
220 stacks = meraki.request(path, method='GET')
221
222 if meraki.params['state'] == 'query':
223 if stack_id is None:
224 meraki.result['data'] = stacks
225 else:
226 meraki.result['data'] = get_stack(stack_id, stacks)
227 elif meraki.params['state'] == 'present':
228 if meraki.params['stack_id'] is None:
229 payload = {'serials': meraki.params['serials'],
230 'name': meraki.params['name'],
231 }
232 path = meraki.construct_path('create', net_id=net_id)
233 response = meraki.request(path, method='POST', payload=json.dumps(payload))
234 if meraki.status == 201:
235 meraki.result['data'] = response
236 meraki.result['changed'] = True
237 else:
238 payload = {'serial': meraki.params['serials'][0]}
239 original = get_stack(stack_id, stacks)
240 comparable = deepcopy(original)
241 comparable.update(payload)
242 if meraki.params['serials'][0] not in comparable['serials']:
243 comparable['serials'].append(meraki.params['serials'][0])
244 # meraki.fail_json(msg=comparable)
245 if meraki.is_update_required(original, comparable, optional_ignore=['serial']):
246 path = meraki.construct_path('add', net_id=net_id, custom={'stack_id': stack_id})
247 response = meraki.request(path, method='POST', payload=json.dumps(payload))
248 if meraki.status == 200:
249 meraki.result['data'] = response
250 meraki.result['changed'] = True
251 else:
252 meraki.result['data'] = original
253 elif meraki.params['state'] == 'absent':
254 if meraki.params['serials'] is None:
255 path = meraki.construct_path('delete', net_id=net_id, custom={'stack_id': stack_id})
256 response = meraki.request(path, method='DELETE')
257 meraki.result['data'] = {}
258 meraki.result['changed'] = True
259 else:
260 for serial in meraki.params['serials']:
261 payload = {'serial': serial}
262 original = get_stack(stack_id, stacks)
263 comparable = deepcopy(original)
264 comparable.update(payload)
265 if serial in comparable['serials']:
266 comparable['serials'].remove(serial)
267 if meraki.is_update_required(original, comparable, optional_ignore=['serial']):
268 path = meraki.construct_path('remove', net_id=net_id, custom={'stack_id': stack_id})
269 response = meraki.request(path, method='POST', payload=json.dumps(payload))
270 if meraki.status == 200:
271 meraki.result['data'] = response
272 meraki.result['changed'] = True
273 else:
274 meraki.result['data'] = original
275
276 # in the event of a successful module execution, you will want to
277 # simple AnsibleModule.exit_json(), passing the key/value results
278 meraki.exit_json(**meraki.result)
279
280
281 if __name__ == '__main__':
282 main()