Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, 2019 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
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["deprecated"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 author:
19 - Kevin Breit (@kbreit)
20 deprecated:
21 alternative: cisco.meraki.networks_appliance_static_routes
22 removed_in: 3.0.0
23 why: Updated modules released with increased functionality
24 description:
25 - Allows for creation, management, and visibility into static routes within Meraki.
26 extends_documentation_fragment: cisco.meraki.meraki
27 module: meraki_mx_static_route
28 options:
29 enabled:
30 description:
31 - Indicates whether static route is enabled within a network.
32 type: bool
33 fixed_ip_assignments:
34 description:
35 - List of fixed MAC to IP bindings for DHCP.
36 elements: dict
37 suboptions:
38 ip:
39 description:
40 - IP address of endpoint.
41 type: str
42 mac:
43 description:
44 - MAC address of endpoint.
45 type: str
46 name:
47 description:
48 - Hostname of endpoint.
49 type: str
50 type: list
51 gateway_ip:
52 description:
53 - IP address of the gateway for the subnet.
54 type: str
55 gateway_vlan_id:
56 description:
57 - The gateway IP (next hop) VLAN ID of the static route.
58 type: int
59 name:
60 description:
61 - Descriptive name of the static route.
62 type: str
63 net_id:
64 description:
65 - ID number of a network.
66 type: str
67 net_name:
68 description:
69 - Name of a network.
70 type: str
71 reserved_ip_ranges:
72 description:
73 - List of IP ranges reserved for static IP assignments.
74 elements: dict
75 suboptions:
76 comment:
77 description:
78 - Human readable description of reservation range.
79 type: str
80 end:
81 description:
82 - Last IP address of reserved range.
83 type: str
84 start:
85 description:
86 - First IP address of reserved range.
87 type: str
88 type: list
89 route_id:
90 description:
91 - Unique ID of static route.
92 type: str
93 state:
94 choices:
95 - absent
96 - query
97 - present
98 default: present
99 description:
100 - Create or modify an organization.
101 type: str
102 subnet:
103 description:
104 - CIDR notation based subnet for static route.
105 type: str
106 short_description: Manage static routes in the Meraki cloud
107 """
108
109 EXAMPLES = r"""
110 - name: Create static_route
111 meraki_static_route:
112 auth_key: abc123
113 state: present
114 org_name: YourOrg
115 net_name: YourNet
116 name: Test Route
117 subnet: 192.0.1.0/24
118 gateway_ip: 192.168.128.1
119 delegate_to: localhost
120 - name: Update static route with fixed IP assignment
121 meraki_static_route:
122 auth_key: abc123
123 state: present
124 org_name: YourOrg
125 net_name: YourNet
126 route_id: d6fa4821-1234-4dfa-af6b-ae8b16c20c39
127 fixed_ip_assignments:
128 - mac: aa:bb:cc:dd:ee:ff
129 ip: 192.0.1.11
130 comment: Server
131 delegate_to: localhost
132 - name: Query static routes
133 meraki_static_route:
134 auth_key: abc123
135 state: query
136 org_name: YourOrg
137 net_name: YourNet
138 delegate_to: localhost
139 - name: Delete static routes
140 meraki_static_route:
141 auth_key: abc123
142 state: absent
143 org_name: YourOrg
144 net_name: YourNet
145 route_id: '{{ item }}'
146 delegate_to: localhost
147 """
148
149 RETURN = r"""
150 data:
151 description: Information about the created or manipulated object.
152 returned: info
153 type: complex
154 contains:
155 id:
156 description: Unique identification string assigned to each static route.
157 returned: success
158 type: str
159 sample: d6fa4821-1234-4dfa-af6b-ae8b16c20c39
160 net_id:
161 description: Identification string of network.
162 returned: query or update
163 type: str
164 sample: N_12345
165 name:
166 description: Name of static route.
167 returned: success
168 type: str
169 sample: Data Center static route
170 subnet:
171 description: CIDR notation subnet for static route.
172 returned: success
173 type: str
174 sample: 192.0.1.0/24
175 gatewayIp:
176 description: Next hop IP address.
177 returned: success
178 type: str
179 sample: 192.1.1.1
180 enabled:
181 description: Enabled state of static route.
182 returned: query or update
183 type: bool
184 sample: true
185 reservedIpRanges:
186 description: List of IP address ranges which are reserved for static assignment.
187 returned: query or update
188 type: complex
189 contains:
190 start:
191 description: First address in reservation range, inclusive.
192 returned: query or update
193 type: str
194 sample: 192.0.1.2
195 end:
196 description: Last address in reservation range, inclusive.
197 returned: query or update
198 type: str
199 sample: 192.0.1.10
200 comment:
201 description: Human readable description of range.
202 returned: query or update
203 type: str
204 sample: Server range
205 fixedIpAssignments:
206 description: List of static MAC to IP address bindings.
207 returned: query or update
208 type: complex
209 contains:
210 mac:
211 description: Key is MAC address of endpoint.
212 returned: query or update
213 type: complex
214 contains:
215 ip:
216 description: IP address to be bound to the endpoint.
217 returned: query or update
218 type: str
219 sample: 192.0.1.11
220 name:
221 description: Hostname given to the endpoint.
222 returned: query or update
223 type: str
224 sample: JimLaptop
225 """
226
227 from ansible.module_utils.basic import AnsibleModule, json
228 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import (
229 MerakiModule,
230 meraki_argument_spec,
231 )
232
233
234 def fixed_ip_factory(meraki, data):
235 fixed_ips = dict()
236 for item in data:
237 fixed_ips[item["mac"]] = {"ip": item["ip"], "name": item["name"]}
238 return fixed_ips
239
240
241 def get_static_routes(meraki, net_id):
242 path = meraki.construct_path("get_all", net_id=net_id)
243 r = meraki.request(path, method="GET")
244 return r
245
246
247 def get_static_route(meraki, net_id, route_id):
248 path = meraki.construct_path(
249 "get_one", net_id=net_id, custom={"route_id": route_id}
250 )
251 r = meraki.request(path, method="GET")
252 return r
253
254
255 def does_route_exist(name, routes):
256 for route in routes:
257 if name == route["name"]:
258 return route
259 return None
260
261
262 def update_dict(original, proposed):
263 for k, v in proposed.items():
264 if v is not None:
265 original[k] = v
266 return original
267
268
269 def main():
270
271 # define the available arguments/parameters that a user can pass to
272 # the module
273
274 fixed_ip_arg_spec = dict(
275 mac=dict(type="str"),
276 ip=dict(type="str"),
277 name=dict(type="str"),
278 )
279
280 reserved_ip_arg_spec = dict(
281 start=dict(type="str"),
282 end=dict(type="str"),
283 comment=dict(type="str"),
284 )
285
286 argument_spec = meraki_argument_spec()
287 argument_spec.update(
288 net_id=dict(type="str"),
289 net_name=dict(type="str"),
290 name=dict(type="str"),
291 subnet=dict(type="str"),
292 gateway_ip=dict(type="str"),
293 state=dict(
294 type="str", default="present", choices=["absent", "present", "query"]
295 ),
296 fixed_ip_assignments=dict(
297 type="list", elements="dict", options=fixed_ip_arg_spec
298 ),
299 reserved_ip_ranges=dict(
300 type="list", elements="dict", options=reserved_ip_arg_spec
301 ),
302 route_id=dict(type="str"),
303 enabled=dict(type="bool"),
304 gateway_vlan_id=dict(type="int"),
305 )
306
307 # the AnsibleModule object will be our abstraction working with Ansible
308 # this includes instantiation, a couple of common attr would be the
309 # args/params passed to the execution, as well as if the module
310 # supports check mode
311 module = AnsibleModule(
312 argument_spec=argument_spec,
313 supports_check_mode=True,
314 )
315
316 meraki = MerakiModule(module, function="static_route")
317 module.params["follow_redirects"] = "all"
318 payload = None
319
320 query_urls = {"static_route": "/networks/{net_id}/appliance/staticRoutes"}
321 query_one_urls = {
322 "static_route": "/networks/{net_id}/appliance/staticRoutes/{route_id}"
323 }
324 create_urls = {"static_route": "/networks/{net_id}/appliance/staticRoutes/"}
325 update_urls = {
326 "static_route": "/networks/{net_id}/appliance/staticRoutes/{route_id}"
327 }
328 delete_urls = {
329 "static_route": "/networks/{net_id}/appliance/staticRoutes/{route_id}"
330 }
331 meraki.url_catalog["get_all"].update(query_urls)
332 meraki.url_catalog["get_one"].update(query_one_urls)
333 meraki.url_catalog["create"] = create_urls
334 meraki.url_catalog["update"] = update_urls
335 meraki.url_catalog["delete"] = delete_urls
336
337 if not meraki.params["org_name"] and not meraki.params["org_id"]:
338 meraki.fail_json(
339 msg="Parameters 'org_name' or 'org_id' parameters are required"
340 )
341 if not meraki.params["net_name"] and not meraki.params["net_id"]:
342 meraki.fail_json(
343 msg="Parameters 'net_name' or 'net_id' parameters are required"
344 )
345 if meraki.params["net_name"] and meraki.params["net_id"]:
346 meraki.fail_json(msg="'net_name' and 'net_id' are mutually exclusive")
347
348 # Construct payload
349 if meraki.params["state"] == "present":
350 payload = dict()
351 if meraki.params["net_name"]:
352 payload["name"] = meraki.params["net_name"]
353
354 # manipulate or modify the state as needed (this is going to be the
355 # part where your module will do what it needs to do)
356
357 org_id = meraki.params["org_id"]
358 if not org_id:
359 org_id = meraki.get_org_id(meraki.params["org_name"])
360 net_id = meraki.params["net_id"]
361 if net_id is None:
362 nets = meraki.get_nets(org_id=org_id)
363 net_id = meraki.get_net_id(net_name=meraki.params["net_name"], data=nets)
364
365 if meraki.params["state"] == "query":
366 if meraki.params["route_id"] is not None:
367 meraki.result["data"] = get_static_route(
368 meraki, net_id, meraki.params["route_id"]
369 )
370 else:
371 meraki.result["data"] = get_static_routes(meraki, net_id)
372 elif meraki.params["state"] == "present":
373 payload = {
374 "name": meraki.params["name"],
375 "subnet": meraki.params["subnet"],
376 "gatewayIp": meraki.params["gateway_ip"],
377 }
378 if meraki.params["fixed_ip_assignments"] is not None:
379 payload["fixedIpAssignments"] = fixed_ip_factory(
380 meraki, meraki.params["fixed_ip_assignments"]
381 )
382 if meraki.params["reserved_ip_ranges"] is not None:
383 payload["reservedIpRanges"] = meraki.params["reserved_ip_ranges"]
384 if meraki.params["enabled"] is not None:
385 payload["enabled"] = meraki.params["enabled"]
386 if meraki.params["gateway_vlan_id"] is not None:
387 payload["gatewayVlanId"] = meraki.params["gateway_vlan_id"]
388
389 route_id = meraki.params["route_id"]
390 if meraki.params["name"] is not None and route_id is None:
391 route_status = does_route_exist(
392 meraki.params["name"], get_static_routes(meraki, net_id)
393 )
394 if route_status is not None: # Route exists, assign route_id
395 route_id = route_status["id"]
396
397 if route_id is not None:
398 existing_route = get_static_route(meraki, net_id, route_id)
399 original = existing_route.copy()
400 payload = update_dict(existing_route, payload)
401 if module.check_mode:
402 meraki.result["data"] = payload
403 meraki.exit_json(**meraki.result)
404 if meraki.is_update_required(original, payload, optional_ignore=["id"]):
405 path = meraki.construct_path(
406 "update", net_id=net_id, custom={"route_id": route_id}
407 )
408 meraki.result["data"] = meraki.request(
409 path, method="PUT", payload=json.dumps(payload)
410 )
411 meraki.result["changed"] = True
412 else:
413 meraki.result["data"] = original
414 else:
415 if module.check_mode:
416 meraki.result["data"] = payload
417 meraki.exit_json(**meraki.result)
418 path = meraki.construct_path("create", net_id=net_id)
419 meraki.result["data"] = meraki.request(
420 path, method="POST", payload=json.dumps(payload)
421 )
422 meraki.result["changed"] = True
423 elif meraki.params["state"] == "absent":
424 if module.check_mode:
425 meraki.exit_json(**meraki.result)
426 path = meraki.construct_path(
427 "delete", net_id=net_id, custom={"route_id": meraki.params["route_id"]}
428 )
429 meraki.result["data"] = meraki.request(path, method="DELETE")
430 meraki.result["changed"] = True
431
432 # in the event of a successful module execution, you will want to
433 # simple AnsibleModule.exit_json(), passing the key/value results
434 meraki.exit_json(**meraki.result)
435
436
437 if __name__ == "__main__":
438 main()