Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 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 __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_mx_uplink_bandwidth
19 short_description: Manage uplinks on Meraki MX appliances
20 version_added: "1.1.0"
21 description:
22 - Configure and query information about uplinks on Meraki MX appliances.
23 notes:
24 - Some of the options are likely only used for developers within Meraki.
25 - Module was formerly named M(cisco.meraki.meraki_mx_uplink).
26 deprecated:
27 removed_in: '3.0.0'
28 why: Updated modules released with increased functionality
29 alternative: cisco.meraki.networks_appliance_traffic_shaping_uplink_bandwidth
30 options:
31 state:
32 description:
33 - Specifies whether object should be queried, created/modified, or removed.
34 choices: [absent, present, query]
35 default: query
36 type: str
37 org_name:
38 description:
39 - Name of organization associated to a network.
40 type: str
41 org_id:
42 description:
43 - ID of organization associated to a network.
44 type: str
45 net_name:
46 description:
47 - Name of network which VLAN is in or should be in.
48 aliases: [network]
49 type: str
50 net_id:
51 description:
52 - ID of network which VLAN is in or should be in.
53 type: str
54 wan1:
55 description:
56 - Configuration of WAN1 uplink
57 type: dict
58 suboptions:
59 bandwidth_limits:
60 description:
61 - Structure for configuring bandwidth limits
62 type: dict
63 suboptions:
64 limit_up:
65 description:
66 - Maximum upload speed for interface
67 type: int
68 limit_down:
69 description:
70 - Maximum download speed for interface
71 type: int
72 wan2:
73 description:
74 - Configuration of WAN2 uplink
75 type: dict
76 suboptions:
77 bandwidth_limits:
78 description:
79 - Structure for configuring bandwidth limits
80 type: dict
81 suboptions:
82 limit_up:
83 description:
84 - Maximum upload speed for interface
85 type: int
86 limit_down:
87 description:
88 - Maximum download speed for interface
89 type: int
90 cellular:
91 description:
92 - Configuration of cellular uplink
93 type: dict
94 suboptions:
95 bandwidth_limits:
96 description:
97 - Structure for configuring bandwidth limits
98 type: dict
99 suboptions:
100 limit_up:
101 description:
102 - Maximum upload speed for interface
103 type: int
104 limit_down:
105 description:
106 - Maximum download speed for interface
107 type: int
108 author:
109 - Kevin Breit (@kbreit)
110 extends_documentation_fragment: cisco.meraki.meraki
111 '''
112
113 EXAMPLES = r'''
114 - name: Set MX uplink settings
115 meraki_mx_uplink_bandwidth:
116 auth_key: '{{ auth_key }}'
117 state: present
118 org_name: '{{ test_org_name }}'
119 net_name: '{{ test_net_name }} - Uplink'
120 wan1:
121 bandwidth_limits:
122 limit_down: 1000000
123 limit_up: 1000
124 cellular:
125 bandwidth_limits:
126 limit_down: 0
127 limit_up: 0
128 delegate_to: localhost
129
130 - name: Query MX uplink settings
131 meraki_mx_uplink_bandwidth:
132 auth_key: '{{ auth_key }}'
133 state: query
134 org_name: '{{ test_org_name }}'
135 net_name: '{{ test_net_name }} - Uplink'
136 delegate_to: localhost
137 '''
138
139 RETURN = r'''
140
141 data:
142 description: Information about the organization which was created or modified
143 returned: success
144 type: complex
145 contains:
146 wan1:
147 description: WAN1 interface
148 returned: success
149 type: complex
150 contains:
151 bandwidth_limits:
152 description: Structure for uplink bandwidth limits
153 returned: success
154 type: complex
155 contains:
156 limit_up:
157 description: Upload bandwidth limit
158 returned: success
159 type: int
160 limit_down:
161 description: Download bandwidth limit
162 returned: success
163 type: int
164 wan2:
165 description: WAN2 interface
166 returned: success
167 type: complex
168 contains:
169 bandwidth_limits:
170 description: Structure for uplink bandwidth limits
171 returned: success
172 type: complex
173 contains:
174 limit_up:
175 description: Upload bandwidth limit
176 returned: success
177 type: int
178 limit_down:
179 description: Download bandwidth limit
180 returned: success
181 type: int
182 cellular:
183 description: cellular interface
184 returned: success
185 type: complex
186 contains:
187 bandwidth_limits:
188 description: Structure for uplink bandwidth limits
189 returned: success
190 type: complex
191 contains:
192 limit_up:
193 description: Upload bandwidth limit
194 returned: success
195 type: int
196 limit_down:
197 description: Download bandwidth limit
198 returned: success
199 type: int
200 '''
201
202 from ansible.module_utils.basic import AnsibleModule, json
203 from ansible.module_utils.common.dict_transformations import recursive_diff
204 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
205
206 INT_NAMES = ('wan1', 'wan2', 'cellular')
207
208
209 def clean_custom_format(data):
210 for interface in data:
211 if data[interface]['bandwidth_limits']['limit_up'] is None:
212 data[interface]['bandwidth_limits']['limit_up'] = 0
213 if data[interface]['bandwidth_limits']['limit_down'] is None:
214 data[interface]['bandwidth_limits']['limit_down'] = 0
215 return data
216
217
218 def meraki_struct_to_custom_format(data):
219 new_struct = {}
220 for interface in INT_NAMES:
221 if interface in data['bandwidthLimits']:
222 new_struct[interface] = {'bandwidth_limits': {'limit_up': data['bandwidthLimits'][interface]['limitUp'],
223 'limit_down': data['bandwidthLimits'][interface]['limitDown'],
224 }
225 }
226 # return snake_dict_to_camel_dict(new_struct)
227 return new_struct
228
229
230 def main():
231 # define the available arguments/parameters that a user can pass to
232 # the module
233
234 bandwidth_arg_spec = dict(limit_up=dict(type='int'),
235 limit_down=dict(type='int'),
236 )
237
238 interface_arg_spec = dict(bandwidth_limits=dict(type='dict', default=None, options=bandwidth_arg_spec),
239 )
240
241 argument_spec = meraki_argument_spec()
242 argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'query'], default='query'),
243 net_name=dict(type='str', aliases=['network']),
244 net_id=dict(type='str'),
245 wan1=dict(type='dict', default=None, options=interface_arg_spec),
246 wan2=dict(type='dict', default=None, options=interface_arg_spec),
247 cellular=dict(type='dict', default=None, options=interface_arg_spec),
248 )
249
250 # the AnsibleModule object will be our abstraction working with Ansible
251 # this includes instantiation, a couple of common attr would be the
252 # args/params passed to the execution, as well as if the module
253 # supports check mode
254 module = AnsibleModule(argument_spec=argument_spec,
255 supports_check_mode=True,
256 )
257 meraki = MerakiModule(module, function='mx_uplink')
258
259 meraki.params['follow_redirects'] = 'all'
260
261 query_urls = {'mx_uplink': '/networks/{net_id}/appliance/trafficShaping/uplinkBandwidth'}
262 update_bw_url = {'mx_uplink': '/networks/{net_id}/appliance/trafficShaping/uplinkBandwidth'}
263
264 meraki.url_catalog['get_all'].update(query_urls)
265 meraki.url_catalog['update_bw'] = update_bw_url
266
267 payload = dict()
268
269 org_id = meraki.params['org_id']
270 if org_id is None:
271 org_id = meraki.get_org_id(meraki.params['org_name'])
272 net_id = meraki.params['net_id']
273 if net_id is None:
274 nets = meraki.get_nets(org_id=org_id)
275 net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
276
277 if meraki.params['state'] == 'query':
278 path = meraki.construct_path('get_all', net_id=net_id)
279 response = meraki.request(path, method='GET')
280 data = clean_custom_format(meraki_struct_to_custom_format(response))
281 meraki.result['data'] = data
282 elif meraki.params['state'] == 'present':
283 path = meraki.construct_path('get_all', net_id=net_id)
284 original = meraki.request(path, method='GET')
285 payload = {'bandwidthLimits': {}}
286 for interface in INT_NAMES:
287 if meraki.params[interface] is not None:
288 if meraki.params[interface]['bandwidth_limits'] is not None:
289 payload['bandwidthLimits'][interface] = None
290 payload['bandwidthLimits'][interface] = {'limitUp': meraki.params[interface]['bandwidth_limits']['limit_up'],
291 'limitDown': meraki.params[interface]['bandwidth_limits']['limit_down'],
292 }
293 if payload['bandwidthLimits'][interface]['limitUp'] == 0:
294 payload['bandwidthLimits'][interface]['limitUp'] = None
295 if payload['bandwidthLimits'][interface]['limitDown'] == 0:
296 payload['bandwidthLimits'][interface]['limitDown'] = None
297 if meraki.is_update_required(original, payload):
298 if meraki.module.check_mode is True:
299 diff = recursive_diff(clean_custom_format(meraki_struct_to_custom_format(original)),
300 clean_custom_format(meraki_struct_to_custom_format(payload)))
301 original.update(payload)
302 meraki.result['data'] = clean_custom_format(meraki_struct_to_custom_format(original))
303 meraki.result['diff'] = {'before': diff[0],
304 'after': diff[1],
305 }
306 meraki.result['changed'] = True
307 meraki.exit_json(**meraki.result)
308 path = meraki.construct_path('update_bw', net_id=net_id)
309 response = meraki.request(path, method='PUT', payload=json.dumps(payload))
310 if meraki.status == 200:
311 formatted_original = clean_custom_format(meraki_struct_to_custom_format(original))
312 formatted_response = clean_custom_format(meraki_struct_to_custom_format(response))
313 diff = recursive_diff(formatted_original, formatted_response)
314 meraki.result['diff'] = {'before': diff[0],
315 'after': diff[1],
316 }
317 meraki.result['data'] = formatted_response
318 meraki.result['changed'] = True
319 else:
320 meraki.result['data'] = clean_custom_format(meraki_struct_to_custom_format(original))
321
322 # in the event of a successful module execution, you will want to
323 # simple AnsibleModule.exit_json(), passing the key/value results
324 meraki.exit_json(**meraki.result)
325
326
327 if __name__ == '__main__':
328 main()