Debian Codegraph @

ansible_collections/cisco/meraki/plugins/modules/meraki_ms_storm_control.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_ms_storm_control
19 short_description: Manage storm control configuration on a switch in the Meraki cloud
20 version_added: "0.0.1"
21 description:
22 - Allows for management of storm control settings for Meraki MS switches.
23 deprecated:
24 removed_in: '3.0.0'
25 why: Updated modules released with increased functionality
26 alternative: cisco.meraki.networks_switch_storm_control
27 options:
28 state:
29 description:
30 - Specifies whether storm control configuration should be queried or modified.
31 choices: [query, present]
32 default: query
33 type: str
34 net_name:
35 description:
36 - Name of network.
37 type: str
38 net_id:
39 description:
40 - ID of network.
41 type: str
42 broadcast_threshold:
43 description:
44 - Percentage (1 to 99) of total available port bandwidth for broadcast traffic type.
45 - Default value 100 percent rate is to clear the configuration.
46 type: int
47 multicast_threshold:
48 description:
49 - Percentage (1 to 99) of total available port bandwidth for multicast traffic type.
50 - Default value 100 percent rate is to clear the configuration.
51 type: int
52 unknown_unicast_threshold:
53 description:
54 - Percentage (1 to 99) of total available port bandwidth for unknown unicast traffic type.
55 - Default value 100 percent rate is to clear the configuration.
56 type: int
57
58 author:
59 - Kevin Breit (@kbreit)
60 extends_documentation_fragment: cisco.meraki.meraki
61 '''
62
63 EXAMPLES = r'''
64 - name: Set broadcast settings
65 meraki_switch_storm_control:
66 auth_key: abc123
67 state: present
68 org_name: YourOrg
69 net_name: YourNet
70 broadcast_threshold: 75
71 multicast_threshold: 70
72 unknown_unicast_threshold: 65
73 delegate_to: localhost
74
75 - name: Query storm control settings
76 meraki_switch_storm_control:
77 auth_key: abc123
78 state: query
79 org_name: YourOrg
80 net_name: YourNet
81 delegate_to: localhost
82 '''
83
84 RETURN = r'''
85 data:
86 description: Information queried or updated storm control configuration.
87 returned: success
88 type: complex
89 contains:
90 broadcast_threshold:
91 description:
92 - Percentage (1 to 99) of total available port bandwidth for broadcast traffic type.
93 - Default value 100 percent rate is to clear the configuration.
94 returned: success
95 type: int
96 sample: 42
97 multicast_threshold:
98 description:
99 - Percentage (1 to 99) of total available port bandwidth for multicast traffic type.
100 - Default value 100 percent rate is to clear the configuration.
101 returned: success
102 type: int
103 sample: 42
104 unknown_unicast_threshold:
105 description:
106 - Percentage (1 to 99) of total available port bandwidth for unknown unicast traffic type.
107 - Default value 100 percent rate is to clear the configuration.
108 returned: success
109 type: int
110 sample: 42
111 '''
112
113 from ansible.module_utils.basic import AnsibleModule, json
114 from ansible.module_utils.common.dict_transformations import recursive_diff
115 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
116
117
118 def construct_payload(params):
119 payload = dict()
120 if 'broadcast_threshold' in params:
121 payload['broadcastThreshold'] = params['broadcast_threshold']
122 if 'multicast_threshold' in params:
123 payload['multicastThreshold'] = params['multicast_threshold']
124 if 'unknown_unicast_threshold' in params:
125 payload['unknownUnicastThreshold'] = params['unknown_unicast_threshold']
126 return payload
127
128
129 def main():
130 # define the available arguments/parameters that a user can pass to
131 # the module
132 argument_spec = meraki_argument_spec()
133 argument_spec.update(state=dict(type='str', choices=['present', 'query'], default='query'),
134 net_name=dict(type='str'),
135 net_id=dict(type='str'),
136 broadcast_threshold=dict(type='int'),
137 multicast_threshold=dict(type='int'),
138 unknown_unicast_threshold=dict(type='int'),
139 )
140
141 # the AnsibleModule object will be our abstraction working with Ansible
142 # this includes instantiation, a couple of common attr would be the
143 # args/params passed to the execution, as well as if the module
144 # supports check mode
145 module = AnsibleModule(argument_spec=argument_spec,
146 supports_check_mode=True,
147 )
148 meraki = MerakiModule(module, function='switch_storm_control')
149 meraki.params['follow_redirects'] = 'all'
150
151 query_urls = {'switch_storm_control': '/networks/{net_id}/switch/stormControl'}
152 update_url = {'switch_storm_control': '/networks/{net_id}/switch/stormControl'}
153
154 meraki.url_catalog['get_all'].update(query_urls)
155 meraki.url_catalog['update'] = update_url
156
157 payload = None
158
159 org_id = meraki.params['org_id']
160 if not org_id:
161 org_id = meraki.get_org_id(meraki.params['org_name'])
162 net_id = meraki.params['net_id']
163 if net_id is None:
164 nets = meraki.get_nets(org_id=org_id)
165 net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
166
167 # execute checks for argument completeness
168
169 # manipulate or modify the state as needed (this is going to be the
170 # part where your module will do what it needs to do)
171 if meraki.params['state'] == 'query':
172 path = meraki.construct_path('get_all', net_id=net_id)
173 response = meraki.request(path, method='GET')
174 if meraki.status == 200:
175 meraki.result['data'] = response
176 elif meraki.params['state'] == 'present':
177 path = meraki.construct_path('get_all', net_id=net_id)
178 original = meraki.request(path, method='GET')
179 payload = construct_payload(meraki.params)
180 if meraki.is_update_required(original, payload) is True:
181 diff = recursive_diff(original, payload)
182 if meraki.check_mode is True:
183 original.update(payload)
184 meraki.result['data'] = original
185 meraki.result['changed'] = True
186 meraki.result['diff'] = {'before': diff[0],
187 'after': diff[1]}
188 meraki.exit_json(**meraki.result)
189 path = meraki.construct_path('update', net_id=net_id)
190 response = meraki.request(path, method='PUT', payload=json.dumps(payload))
191 if meraki.status == 200:
192 meraki.result['diff'] = {'before': diff[0],
193 'after': diff[1]}
194 meraki.result['data'] = response
195 meraki.result['changed'] = True
196 else:
197 meraki.result['data'] = original
198
199 # in the event of a successful module execution, you will want to
200 # simple AnsibleModule.exit_json(), passing the key/value results
201 meraki.exit_json(**meraki.result)
202
203
204 if __name__ == '__main__':
205 main()