Debian Codegraph @

ansible_collections/cisco/meraki/plugins/modules/meraki_ms_stack_l3_interface.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_l3_interface
19 short_description: Manage routed interfaces on MS switches
20 description:
21 - Allows for creation, management, and visibility into routed interfaces on Meraki MS switches.
22 notes:
23 - Once a layer 3 interface is created, the API does not allow updating the interface and specifying C(default_gateway).
24 deprecated:
25 removed_in: '3.0.0'
26 why: Updated modules released with increased functionality
27 alternative: cisco.meraki.networks_switch_stacks_routing_interfaces
28 options:
29 state:
30 description:
31 - Create or modify an organization.
32 type: str
33 choices: [ present, query, absent ]
34 default: present
35 net_name:
36 description:
37 - Name of network which configuration is applied to.
38 aliases: [network]
39 type: str
40 net_id:
41 description:
42 - ID of network which configuration is applied to.
43 type: str
44 stack_id:
45 description:
46 - The unique identifier of the stack.
47 type: str
48 vlan_id:
49 description:
50 - The VLAN this routed interface is on.
51 - VLAN must be between 1 and 4094.
52 type: int
53 default_gateway:
54 description:
55 - The next hop for any traffic that isn't going to a directly connected subnet or over a static route.
56 - This IP address must exist in a subnet with a routed interface.
57 type: str
58 interface_ip:
59 description:
60 - The IP address this switch will use for layer 3 routing on this VLAN or subnet.
61 - This cannot be the same as the switch's management IP.
62 type: str
63 interface_id:
64 description:
65 - Uniqiue identification number for layer 3 interface.
66 type: str
67 multicast_routing:
68 description:
69 - Enable multicast support if multicast routing between VLANs is required.
70 type: str
71 choices: [disabled, enabled, IGMP snooping querier]
72 name:
73 description:
74 - A friendly name or description for the interface or VLAN.
75 type: str
76 subnet:
77 description:
78 - The network that this routed interface is on, in CIDR notation.
79 type: str
80 ospf_settings:
81 description:
82 - The OSPF routing settings of the interface.
83 type: dict
84 suboptions:
85 cost:
86 description:
87 - The path cost for this interface.
88 type: int
89 area:
90 description:
91 - The OSPF area to which this interface should belong.
92 - Can be either 'disabled' or the identifier of an existing OSPF area.
93 type: str
94 is_passive_enabled:
95 description:
96 - When enabled, OSPF will not run on the interface, but the subnet will still be advertised.
97 type: bool
98 author:
99 - Kevin Breit (@kbreit)
100 extends_documentation_fragment: cisco.meraki.meraki
101 '''
102
103 EXAMPLES = r'''
104 - name: Query all l3 interfaces
105 meraki_ms_stack_l3_interface:
106 auth_key: abc123
107 state: query
108 serial: aaa-bbb-ccc
109
110 - name: Query one l3 interface
111 meraki_ms_stack_l3_interface:
112 auth_key: abc123
113 state: query
114 serial: aaa-bbb-ccc
115 name: Test L3 interface
116
117 - name: Create l3 interface
118 meraki_ms_stack_l3_interface:
119 auth_key: abc123
120 state: present
121 serial: aaa-bbb-ccc
122 name: "Test L3 interface 2"
123 subnet: "192.168.3.0/24"
124 interface_ip: "192.168.3.2"
125 multicast_routing: disabled
126 vlan_id: 11
127 ospf_settings:
128 area: 0
129 cost: 1
130 is_passive_enabled: true
131
132 - name: Update l3 interface
133 meraki_ms_stack_l3_interface:
134 auth_key: abc123
135 state: present
136 serial: aaa-bbb-ccc
137 name: "Test L3 interface 2"
138 subnet: "192.168.3.0/24"
139 interface_ip: "192.168.3.2"
140 multicast_routing: disabled
141 vlan_id: 11
142 ospf_settings:
143 area: 0
144 cost: 2
145 is_passive_enabled: true
146
147 - name: Delete l3 interface
148 meraki_ms_stack_l3_interface:
149 auth_key: abc123
150 state: absent
151 serial: aaa-bbb-ccc
152 interface_id: abc123344566
153 '''
154
155 RETURN = r'''
156 data:
157 description: Information about the layer 3 interfaces.
158 returned: success
159 type: complex
160 contains:
161 vlan_id:
162 description: The VLAN this routed interface is on.
163 returned: success
164 type: int
165 sample: 10
166 default_gateway:
167 description: The next hop for any traffic that isn't going to a directly connected subnet or over a static route.
168 returned: success
169 type: str
170 sample: 192.168.2.1
171 interface_ip:
172 description: The IP address this switch will use for layer 3 routing on this VLAN or subnet.
173 returned: success
174 type: str
175 sample: 192.168.2.2
176 interface_id:
177 description: Uniqiue identification number for layer 3 interface.
178 returned: success
179 type: str
180 sample: 62487444811111120
181 multicast_routing:
182 description: Enable multicast support if multicast routing between VLANs is required.
183 returned: success
184 type: str
185 sample: disabled
186 name:
187 description: A friendly name or description for the interface or VLAN.
188 returned: success
189 type: str
190 sample: L3 interface
191 subnet:
192 description: The network that this routed interface is on, in CIDR notation.
193 returned: success
194 type: str
195 sample: 192.168.2.0/24
196 ospf_settings:
197 description: The OSPF routing settings of the interface.
198 returned: success
199 type: complex
200 contains:
201 cost:
202 description: The path cost for this interface.
203 returned: success
204 type: int
205 sample: 1
206 area:
207 description: The OSPF area to which this interface should belong.
208 returned: success
209 type: str
210 sample: 0
211 is_passive_enabled:
212 description: When enabled, OSPF will not run on the interface, but the subnet will still be advertised.
213 returned: success
214 type: bool
215 sample: true
216 '''
217
218 from ansible.module_utils.basic import AnsibleModule, json
219 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
220
221
222 def construct_payload(meraki):
223 payload = {}
224 if meraki.params['name'] is not None:
225 payload['name'] = meraki.params['name']
226 if meraki.params['subnet'] is not None:
227 payload['subnet'] = meraki.params['subnet']
228 if meraki.params['interface_ip'] is not None:
229 payload['interfaceIp'] = meraki.params['interface_ip']
230 if meraki.params['multicast_routing'] is not None:
231 payload['multicastRouting'] = meraki.params['multicast_routing']
232 if meraki.params['vlan_id'] is not None:
233 payload['vlanId'] = meraki.params['vlan_id']
234 if meraki.params['default_gateway'] is not None:
235 payload['defaultGateway'] = meraki.params['default_gateway']
236 if meraki.params['ospf_settings'] is not None:
237 payload['ospfSettings'] = {}
238 if meraki.params['ospf_settings']['area'] is not None:
239 payload['ospfSettings']['area'] = meraki.params['ospf_settings']['area']
240 if meraki.params['ospf_settings']['cost'] is not None:
241 payload['ospfSettings']['cost'] = meraki.params['ospf_settings']['cost']
242 if meraki.params['ospf_settings']['is_passive_enabled'] is not None:
243 payload['ospfSettings']['isPassiveEnabled'] = meraki.params['ospf_settings']['is_passive_enabled']
244 return payload
245
246
247 def get_interface_id(meraki, data, name):
248 # meraki.fail_json(msg=data)
249 for interface in data:
250 if interface['name'] == name:
251 return interface['interfaceId']
252 return None
253
254
255 def get_interface(interfaces, interface_id):
256 for interface in interfaces:
257 if interface['interfaceId'] == interface_id:
258 return interface
259 return None
260
261
262 def main():
263 # define the available arguments/parameters that a user can pass to
264 # the module
265
266 ospf_arg_spec = dict(area=dict(type='str'),
267 cost=dict(type='int'),
268 is_passive_enabled=dict(type='bool'),
269 )
270
271 argument_spec = meraki_argument_spec()
272 argument_spec.update(state=dict(type='str', choices=['present', 'query', 'absent'], default='present'),
273 net_id=dict(type='str'),
274 net_name=dict(type='str', aliases=['network']),
275 stack_id=dict(type='str'),
276 name=dict(type='str'),
277 subnet=dict(type='str'),
278 interface_id=dict(type='str'),
279 interface_ip=dict(type='str'),
280 multicast_routing=dict(type='str', choices=['disabled', 'enabled', 'IGMP snooping querier']),
281 vlan_id=dict(type='int'),
282 default_gateway=dict(type='str'),
283 ospf_settings=dict(type='dict', default=None, options=ospf_arg_spec),
284 )
285
286 # the AnsibleModule object will be our abstraction working with Ansible
287 # this includes instantiation, a couple of common attr would be the
288 # args/params passed to the execution, as well as if the module
289 # supports check mode
290 module = AnsibleModule(argument_spec=argument_spec,
291 supports_check_mode=True,
292 )
293 meraki = MerakiModule(module, function='ms_stack_l3_interfaces')
294
295 meraki.params['follow_redirects'] = 'all'
296
297 query_urls = {'ms_stack_l3_interfaces': '/networks/{net_id}/switch/stacks/{stack_id}/routing/interfaces'}
298 query_one_urls = {'ms_stack_l3_interfaces': '/networks/{net_id}/switch/stacks/{stack_id}/routing/interfaces'}
299 create_urls = {'ms_stack_l3_interfaces': '/networks/{net_id}/switch/stacks/{stack_id}/routing/interfaces'}
300 update_urls = {'ms_stack_l3_interfaces': '/networks/{net_id}/switch/stacks/{stack_id}/routing/interfaces/{interface_id}'}
301 delete_urls = {'ms_stack_l3_interfaces': '/networks/{net_id}/switch/stacks/{stack_id}/routing/interfaces/{interface_id}'}
302
303 meraki.url_catalog['get_all'].update(query_urls)
304 meraki.url_catalog['get_one'].update(query_one_urls)
305 meraki.url_catalog['create'] = create_urls
306 meraki.url_catalog['update'] = update_urls
307 meraki.url_catalog['delete'] = delete_urls
308
309 payload = None
310
311 if meraki.params['vlan_id'] is not None:
312 if meraki.params['vlan_id'] < 1 or meraki.params['vlan_id'] > 4094:
313 meraki.fail_json(msg='vlan_id must be between 1 and 4094')
314
315 org_id = meraki.params['org_id']
316 if org_id is None:
317 orgs = meraki.get_orgs()
318 for org in orgs:
319 if org['name'] == meraki.params['org_name']:
320 org_id = org['id']
321 net_id = meraki.params['net_id']
322 if net_id is None:
323 net_id = meraki.get_net_id(net_name=meraki.params['net_name'],
324 data=meraki.get_nets(org_id=org_id))
325
326 interface_id = meraki.params['interface_id']
327 interfaces = None
328 if interface_id is None:
329 if meraki.params['name'] is not None:
330 path = meraki.construct_path('get_all', net_id=net_id, custom={'stack_id': meraki.params['stack_id']})
331 interfaces = meraki.request(path, method='GET')
332 interface_id = get_interface_id(meraki, interfaces, meraki.params['name'])
333
334 # manipulate or modify the state as needed (this is going to be the
335 # part where your module will do what it needs to do)
336
337 if meraki.params['state'] == 'query':
338 if interface_id is not None: # Query one interface
339 path = meraki.construct_path('get_one', net_id=net_id, custom={'stack_id': meraki.params['stack_id'],
340 'interface_id': interface_id})
341 response = meraki.request(path, method='GET')
342 meraki.result['data'] = response
343 meraki.exit_json(**meraki.result)
344 else: # Query all interfaces
345 path = meraki.construct_path('get_all', net_id=net_id, custom={'stack_id': meraki.params['stack_id']})
346 response = meraki.request(path, method='GET')
347 meraki.result['data'] = response
348 meraki.exit_json(**meraki.result)
349 elif meraki.params['state'] == 'present':
350 if interface_id is None: # Create a new interface
351 payload = construct_payload(meraki)
352 if meraki.check_mode is True:
353 meraki.result['data'] = payload
354 meraki.result['changed'] = True
355 meraki.exit_json(**meraki.result)
356 path = meraki.construct_path('create', net_id=net_id, custom={'stack_id': meraki.params['stack_id']})
357 response = meraki.request(path, method='POST', payload=json.dumps(payload))
358 meraki.result['data'] = response
359 meraki.result['changed'] = True
360 meraki.exit_json(**meraki.result)
361 else:
362 if interfaces is None:
363 path = meraki.construct_path('get_all', net_id=net_id, custom={'stack_id': meraki.params['stack_id']})
364 interfaces = meraki.request(path, method='GET')
365 payload = construct_payload(meraki)
366 interface = get_interface(interfaces, interface_id)
367 if meraki.is_update_required(interface, payload):
368 if meraki.check_mode is True:
369 interface.update(payload)
370 meraki.result['data'] = interface
371 meraki.result['changed'] = True
372 meraki.exit_json(**meraki.result)
373 path = meraki.construct_path('update', net_id=net_id, custom={'stack_id': meraki.params['stack_id'],
374 'interface_id': interface_id})
375 response = meraki.request(path, method='PUT', payload=json.dumps(payload))
376 meraki.result['data'] = response
377 meraki.result['changed'] = True
378 meraki.exit_json(**meraki.result)
379 else:
380 meraki.result['data'] = interface
381 meraki.exit_json(**meraki.result)
382 elif meraki.params['state'] == 'absent':
383 if meraki.check_mode is True:
384 meraki.result['data'] = {}
385 meraki.result['changed'] = True
386 meraki.exit_json(**meraki.result)
387 path = meraki.construct_path('delete', net_id=net_id, custom={'stack_id': meraki.params['stack_id'],
388 'interface_id': meraki.params['interface_id']})
389 response = meraki.request(path, method='DELETE')
390 meraki.result['data'] = response
391 meraki.result['changed'] = True
392
393 # in the event of a successful module execution, you will want to
394 # simple AnsibleModule.exit_json(), passing the key/value results
395 meraki.exit_json(**meraki.result)
396
397
398 if __name__ == '__main__':
399 main()