Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_system_qos_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_system_qos_policy
16 short_description: System QoS policy configuration for Cisco Intersight
17 description:
18 - System QoS policy configuration for Cisco Intersight.
19 - For more information see L(Cisco Intersight,https://intersight.com/apidocs).
20 extends_documentation_fragment: intersight
21 options:
22 state:
23 description:
24 - If C(present), will verify the resource is present and will create if needed.
25 - If C(absent), will verify the resource is absent and will delete if needed.
26 type: str
27 choices: [present, absent]
28 default: present
29 organization:
30 description:
31 - The name of the Organization this resource is assigned to.
32 - Profiles and Policies that are created within a Custom Organization are applicable only to devices in the same Organization.
33 type: str
34 default: default
35 name:
36 description:
37 - The name assigned to the System QoS policy.
38 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
39 type: str
40 required: true
41 tags:
42 description:
43 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
44 type: list
45 elements: dict
46 description:
47 description:
48 - The user-defined description of the System QoS policy.
49 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
50 type: str
51 aliases: [descr]
52 classes:
53 description:
54 - List of QoS classes configured in the policy.
55 - System includes predefined classes (Bronze, Silver, Gold, Platinum, Best Effort, FC).
56 - Best Effort and FC classes are always enabled and have restrictions on modifiable parameters.
57 type: list
58 elements: dict
59 suboptions:
60 name:
61 description:
62 - Name of the QoS class.
63 - Valid class names are Bronze, Silver, Gold, Platinum, Best Effort, FC.
64 type: str
65 required: true
66 choices: [Bronze, Silver, Gold, Platinum, "Best Effort", FC]
67 admin_state:
68 description:
69 - Administrative state of the QoS class.
70 - Best Effort and FC classes are always enabled.
71 - Bronze, Silver, Gold, and Platinum classes are disabled by default when not specified.
72 - If not specified, appropriate class defaults will be applied automatically.
73 type: str
74 choices: [Enabled, Disabled]
75 cos:
76 description:
77 - Class of service received by the traffic tagged with this QoS.
78 - Valid range is 0-6.
79 - FC class defaults to 3.
80 - Bronze defaults to 1, Silver to 2, Gold to 4, Platinum to 5.
81 - If not specified, appropriate class defaults will be applied automatically.
82 type: int
83 mtu:
84 description:
85 - Maximum transmission unit (MTU) is the largest size packet or frame, that can be sent in a packet- or frame-based network such as the Internet.
86 - Valid range is 1500-9216.
87 - Normal (default) maps to 1500, FC maps to 2240
88 - FC class defaults to 2240, others default to 1500.
89 - If not specified, appropriate class defaults will be applied automatically.
90 type: int
91 packet_drop:
92 description:
93 - If enabled, this QoS class will allow packet drops within an acceptable limit.
94 - FC class defaults to false (no packet drop).
95 - All other classes default to true (packet drop enabled).
96 - If not specified, appropriate class defaults will be applied automatically.
97 type: bool
98 weight:
99 description:
100 - The weight of the QoS Class controls the distribution of bandwidth between QoS Classes.
101 - Valid range is 0-10.
102 - Bronze defaults to 7, Silver to 8, Gold to 9, Platinum to 10, Best Effort and FC to 5.
103 - If not specified, appropriate class defaults will be applied automatically.
104 type: int
105
106 author:
107 - Ron Gershburg (@rgershbu)
108 '''
109
110 EXAMPLES = r'''
111 - name: Configure System QoS Policy with default classes
112 cisco.intersight.intersight_system_qos_policy:
113 api_private_key: "{{ api_private_key }}"
114 api_key_id: "{{ api_key_id }}"
115 organization: DevNet
116 name: lab-system-qos
117 description: System QoS policy for lab use
118 tags:
119 - Key: Site
120 Value: RCDN
121 classes:
122 - name: Bronze
123 admin_state: Enabled
124 weight: 7
125 - name: Silver
126 admin_state: Enabled
127 weight: 8
128 - name: Gold
129 admin_state: Enabled
130 weight: 9
131 - name: Platinum
132 admin_state: Enabled
133 weight: 10
134 - name: 'Best Effort'
135 weight: 5
136 - name: FC
137 weight: 5
138
139 - name: Configure System QoS Policy with custom settings
140 cisco.intersight.intersight_system_qos_policy:
141 api_private_key: "{{ api_private_key }}"
142 api_key_id: "{{ api_key_id }}"
143 organization: DevNet
144 name: custom-system-qos
145 description: Custom System QoS policy
146 classes:
147 - name: Bronze
148 admin_state: Enabled
149 cos: 1
150 mtu: 1500
151 packet_drop: true
152 weight: 7
153 - name: Gold
154 admin_state: Enabled
155 cos: 4
156 mtu: 1500
157 packet_drop: true
158 weight: 9
159 - name: 'Best Effort'
160 mtu: 1500
161 weight: 6
162 - name: FC
163 weight: 8
164
165 - name: Delete System QoS Policy
166 cisco.intersight.intersight_system_qos_policy:
167 api_private_key: "{{ api_private_key }}"
168 api_key_id: "{{ api_key_id }}"
169 organization: DevNet
170 name: lab-system-qos
171 state: absent
172 '''
173
174 RETURN = r'''
175 api_response:
176 description: The API response output returned by the specified resource.
177 returned: always
178 type: dict
179 sample:
180 "Name": "lab-system-qos"
181 "Description": "System QoS policy for lab use"
182 "Organization": {
183 "ObjectType": "organization.Organization",
184 "Moid": "675450ee69726530014753e2"
185 }
186 "Classes": [
187 {
188 "Name": "Bronze",
189 "AdminState": "Enabled",
190 "Cos": 1,
191 "Mtu": 1500,
192 "PacketDrop": true,
193 "Weight": 7,
194 "MulticastOptimize": false
195 }
196 ]
197 '''
198
199 from ansible.module_utils.basic import AnsibleModule
200 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
201
202 ALLOWED_CLASSES = ['Bronze', 'Silver', 'Gold', 'Platinum', 'Best Effort', 'FC']
203
204
205 def get_qos_class_defaults(class_name):
206 """
207 Get default values for QoS classes based on the class name.
208 """
209 defaults = {
210 'Bronze': {
211 'admin_state': 'Disabled',
212 'cos': 1,
213 'mtu': 1500,
214 'packet_drop': True,
215 'weight': 7
216 },
217 'Silver': {
218 'admin_state': 'Disabled',
219 'cos': 2,
220 'mtu': 1500,
221 'packet_drop': True,
222 'weight': 8
223 },
224 'Gold': {
225 'admin_state': 'Disabled',
226 'cos': 4,
227 'mtu': 1500,
228 'packet_drop': True,
229 'weight': 9
230 },
231 'Platinum': {
232 'admin_state': 'Disabled',
233 'cos': 5,
234 'mtu': 1500,
235 'packet_drop': True,
236 'weight': 10
237 },
238 'Best Effort': {
239 'admin_state': 'Enabled',
240 'cos': 255,
241 'mtu': 1500,
242 'packet_drop': True,
243 'weight': 5
244 },
245 'FC': {
246 'admin_state': 'Enabled',
247 'cos': 3,
248 'mtu': 2240,
249 'packet_drop': False,
250 'weight': 5
251 }
252 }
253 return defaults.get(class_name, {})
254
255
256 def validate_qos_class(qos_class, module):
257 """
258 Validate QoS class configuration and apply restrictions.
259 """
260 class_name = qos_class.get('name')
261 if not class_name:
262 module.fail_json(msg="QoS class name is required")
263
264 # Apply restrictions for FC class
265 if class_name == 'FC':
266 if qos_class.get('admin_state') and qos_class['admin_state'] != 'Enabled':
267 module.fail_json(msg="FC class must always be enabled")
268 if qos_class.get('cos') and qos_class['cos'] != 3:
269 module.fail_json(msg="FC class CoS must be 3")
270 if qos_class.get('packet_drop') and qos_class['packet_drop']:
271 module.fail_json(msg="FC class packet drop must be false")
272 if qos_class.get('mtu') and qos_class['mtu'] != 2240:
273 module.fail_json(msg="FC class MTU must be 2240")
274
275 # Apply restrictions for Best Effort class
276 if class_name == 'Best Effort':
277 if qos_class.get('admin_state') and qos_class['admin_state'] != 'Enabled':
278 module.fail_json(msg="Best Effort class must always be enabled")
279 if qos_class.get('packet_drop') and not qos_class['packet_drop']:
280 module.fail_json(msg="Best Effort class packet drop must be true")
281 if qos_class.get('cos'):
282 module.fail_json(msg="Best Effort class CoS cannot be modified by user")
283
284 # Validate CoS values (Best Effort is handled above)
285 if qos_class.get('cos') and class_name != 'Best Effort':
286 cos_value = qos_class['cos']
287 if cos_value < 0 or cos_value > 6:
288 module.fail_json(msg="CoS value must be between 0-6")
289
290 # Validate weight values
291 if qos_class.get('weight'):
292 weight_value = qos_class['weight']
293 if weight_value < 0 or weight_value > 10:
294 module.fail_json(msg="Weight value must be between 0-10")
295
296 # Validate MTU values
297 if qos_class.get('mtu'):
298 mtu_value = qos_class['mtu']
299 if mtu_value < 1500 or mtu_value > 9216:
300 module.fail_json(msg="MTU value must be between 1500-9216")
301
302
303 def format_qos_classes(classes, module):
304 """
305 Format QoS classes for API submission, applying defaults and validations.
306 """
307 if not classes:
308 # Return default configuration with all 6 classes
309 default_classes = []
310 for class_name in ALLOWED_CLASSES:
311 defaults = get_qos_class_defaults(class_name)
312 default_classes.append({
313 'Name': class_name,
314 'AdminState': defaults['admin_state'],
315 'Cos': defaults['cos'],
316 'Mtu': defaults['mtu'],
317 'PacketDrop': defaults['packet_drop'],
318 'Weight': defaults['weight'],
319 'MulticastOptimize': False # Always false, not user-configurable
320 })
321 return default_classes
322
323 formatted_classes = []
324 provided_class_names = [cls.get('name') for cls in classes]
325
326 # Process provided classes
327 for qos_class in classes:
328 validate_qos_class(qos_class, module)
329 class_name = qos_class['name']
330 defaults = get_qos_class_defaults(class_name)
331
332 formatted_class = {
333 'Name': class_name,
334 'AdminState': qos_class.get('admin_state') if qos_class.get('admin_state') else defaults['admin_state'],
335 'Cos': qos_class.get('cos') if qos_class.get('cos') else defaults['cos'],
336 'Mtu': qos_class.get('mtu') if qos_class.get('mtu') else defaults['mtu'],
337 'PacketDrop': qos_class.get('packet_drop') if qos_class.get('packet_drop') else defaults['packet_drop'],
338 'Weight': qos_class.get('weight') if qos_class.get('weight') else defaults['weight'],
339 'MulticastOptimize': False # Always false, not user-configurable
340 }
341 formatted_classes.append(formatted_class)
342
343 # Add missing classes with defaults
344 for class_name in ALLOWED_CLASSES:
345 if class_name not in provided_class_names:
346 defaults = get_qos_class_defaults(class_name)
347 formatted_classes.append({
348 'Name': class_name,
349 'AdminState': defaults['admin_state'],
350 'Cos': defaults['cos'],
351 'Mtu': defaults['mtu'],
352 'PacketDrop': defaults['packet_drop'],
353 'Weight': defaults['weight'],
354 'MulticastOptimize': False # Always false, not user-configurable
355 })
356
357 return formatted_classes
358
359
360 def main():
361 class_spec = dict(
362 name=dict(type='str', required=True, choices=ALLOWED_CLASSES),
363 admin_state=dict(type='str', choices=['Enabled', 'Disabled']),
364 cos=dict(type='int'),
365 mtu=dict(type='int'),
366 packet_drop=dict(type='bool'),
367 weight=dict(type='int')
368 )
369
370 argument_spec = intersight_argument_spec.copy()
371 argument_spec.update(
372 state=dict(type='str', choices=['present', 'absent'], default='present'),
373 organization=dict(type='str', default='default'),
374 name=dict(type='str', required=True),
375 description=dict(type='str', aliases=['descr']),
376 tags=dict(type='list', elements='dict'),
377 classes=dict(type='list', elements='dict', options=class_spec)
378 )
379
380 module = AnsibleModule(
381 argument_spec,
382 supports_check_mode=True,
383 )
384
385 intersight = IntersightModule(module)
386 intersight.result['api_response'] = {}
387 intersight.result['trace_id'] = ''
388
389 # Resource path used to configure policy
390 resource_path = '/fabric/SystemQosPolicies'
391 # Define API body used in compares or create
392 intersight.api_body = {
393 'Organization': {
394 'Name': intersight.module.params['organization']
395 },
396 'Name': intersight.module.params['name']
397 }
398
399 if module.params['state'] == 'present':
400 # Format QoS classes with validation and defaults
401 formatted_classes = format_qos_classes(module.params.get('classes'), module)
402 intersight.api_body['Classes'] = formatted_classes
403 intersight.set_tags_and_description()
404
405 intersight.configure_policy_or_profile(resource_path=resource_path)
406
407 module.exit_json(**intersight.result)
408
409
410 if __name__ == '__main__':
411 main()