Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_ipsla_monitoring_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Shreyas Srish (@shrsr) <[email protected]>
5
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18 DOCUMENTATION = r"""
19 ---
20 module: ndo_ipsla_monitoring_policy
21 short_description: Manage IPSLA Monitoring Policies on Cisco Nexus Dashboard Orchestrator (NDO).
22 description:
23 - Manage IPSLA Monitoring Policies on Cisco Nexus Dashboard Orchestrator (NDO).
24 - This module is only supported on ND v3.1 (NDO v4.3) and later.
25 author:
26 - Shreyas Srish (@shrsr)
27 options:
28 template:
29 description:
30 - The name of the template.
31 - The template must be a tenant template.
32 type: str
33 required: true
34 ipsla_monitoring_policy:
35 description:
36 - The name of the IPSLA Monitoring Policy.
37 type: str
38 aliases: [ name ]
39 description:
40 description:
41 - The description of the IPSLA Monitoring Policy.
42 type: str
43 ipsla_monitoring_policy_uuid:
44 description:
45 - The UUID of the IPSLA Monitoring Policy.
46 - This parameter is required when the O(ipsla_monitoring_policy) attribute needs to be updated.
47 type: str
48 aliases: [ uuid ]
49 sla_type:
50 description:
51 - The type of SLA.
52 - The default value is C(icmp).
53 type: str
54 choices: [ http, tcp, icmp, l2ping ]
55 destination_port:
56 description:
57 - The port used for SLA.
58 - This is only applicable when O(sla_type=tcp) or O(sla_type=http).
59 - The value defaults to 80 when O(sla_type=http).
60 - The value must be in the range 1 - 65535 when O(sla_type=tcp).
61 type: int
62 http_version:
63 description:
64 - The HTTP version used for SLA.
65 - This is only applicable when O(sla_type=http).
66 - The default value is C(HTTP10).
67 type: str
68 choices: [ HTTP10, HTTP11 ]
69 http_uri:
70 description:
71 - The URI used for HTTP SLA.
72 - This is only applicable when O(sla_type=http).
73 - The default value is '/'.
74 type: str
75 sla_frequency:
76 description:
77 - The frequency of SLA monitoring in seconds.
78 - The value must be in the range 1 - 300.
79 - The default value is 60 seconds.
80 type: int
81 detect_multiplier:
82 description:
83 - The detection multiplier for SLA.
84 - The value must be in the range 1 - 100.
85 - The default value is 3.
86 type: int
87 request_data_size:
88 description:
89 - The size of the request data in bytes.
90 - The value must be in the range 0 - 17513.
91 - The default value is 28 bytes.
92 type: int
93 type_of_service:
94 description:
95 - The IPv4 Type of Service.
96 - The value must be in the range 0 - 255.
97 - The default value is 0.
98 type: int
99 operation_timeout:
100 description:
101 - The operation_timeout for SLA in milliseconds.
102 - The default value is 900 milliseconds.
103 type: int
104 threshold:
105 description:
106 - The threshold for SLA in milliseconds.
107 - The default value is 900 milliseconds.
108 type: int
109 ipv6_traffic_class:
110 description:
111 - The IPv6 Traffic Class.
112 - The value must be in the range 0 - 255.
113 - The default value is 0.
114 type: int
115 state:
116 description:
117 - Use C(absent) for removing.
118 - Use C(query) for listing an object or multiple objects.
119 - Use C(present) for creating or updating.
120 type: str
121 choices: [ absent, query, present ]
122 default: query
123 extends_documentation_fragment: cisco.mso.modules
124 """
125
126 EXAMPLES = r"""
127 - name: Create a new IPSLA monitoring policy
128 cisco.mso.ndo_ipsla_monitoring_policy:
129 host: mso_host
130 username: admin
131 password: SomeSecretPassword
132 template: ansible_tenant_template
133 ipsla_monitoring_policy: ansible_test_ipsla_monitoring_policy
134 sla_type: http
135 http_version: HTTP11
136 http_uri: /test
137 sla_frequency: 120
138 detect_multiplier: 5
139 request_data_size: 64
140 type_of_service: 16
141 operation_timeout: 1000
142 threshold: 1000
143 ipv6_traffic_class: 32
144 state: present
145
146 - name: Query an IPSLA monitoring policy with name
147 cisco.mso.ndo_ipsla_monitoring_policy:
148 host: mso_host
149 username: admin
150 password: SomeSecretPassword
151 template: ansible_tenant_template
152 ipsla_monitoring_policy: ansible_test_ipsla_monitoring_policy
153 state: query
154 register: query_one
155
156 - name: Query all IPSLA monitoring policies in the template
157 cisco.mso.ndo_ipsla_monitoring_policy:
158 host: mso_host
159 username: admin
160 password: SomeSecretPassword
161 template: ansible_tenant_template
162 state: query
163 register: query_all
164
165 - name: Delete an IPSLA monitoring policy
166 cisco.mso.ndo_ipsla_monitoring_policy:
167 host: mso_host
168 username: admin
169 password: SomeSecretPassword
170 template: ansible_tenant_template
171 ipsla_monitoring_policy: ansible_test_ipsla_monitoring_policy
172 state: absent
173
174 - name: Query an IPSLA monitoring policy with UUID
175 cisco.mso.ndo_ipsla_monitoring_policy:
176 host: mso_host
177 username: admin
178 password: SomeSecretPassword
179 template: ansible_tenant_template
180 ipsla_monitoring_policy_uuid: "{{ query_one.current.uuid }}"
181 state: query
182 register: query_one_uuid
183
184 - name: Delete an IPSLA monitoring policy with UUID
185 cisco.mso.ndo_ipsla_monitoring_policy:
186 host: mso_host
187 username: admin
188 password: SomeSecretPassword
189 template: ansible_tenant_template
190 ipsla_monitoring_policy_uuid: "{{ query_one.current.uuid }}"
191 state: absent
192 """
193
194 RETURN = r"""
195 """
196
197 import copy
198 from ansible.module_utils.basic import AnsibleModule
199 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
200 MSOModule,
201 mso_argument_spec,
202 )
203 from ansible_collections.cisco.mso.plugins.module_utils.template import (
204 MSOTemplate,
205 KVPair,
206 )
207 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
208
209
210 def main():
211 argument_spec = mso_argument_spec()
212 argument_spec.update(
213 template=dict(type="str", required=True),
214 ipsla_monitoring_policy=dict(type="str", aliases=["name"]),
215 description=dict(type="str"),
216 ipsla_monitoring_policy_uuid=dict(type="str", aliases=["uuid"]),
217 sla_type=dict(type="str", choices=["http", "tcp", "icmp", "l2ping"]),
218 destination_port=dict(type="int"),
219 http_version=dict(type="str", choices=["HTTP10", "HTTP11"]),
220 http_uri=dict(type="str"),
221 sla_frequency=dict(type="int"),
222 detect_multiplier=dict(type="int"),
223 request_data_size=dict(type="int"),
224 type_of_service=dict(type="int"),
225 operation_timeout=dict(type="int"),
226 threshold=dict(type="int"),
227 ipv6_traffic_class=dict(type="int"),
228 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
229 )
230
231 module = AnsibleModule(
232 argument_spec=argument_spec,
233 supports_check_mode=True,
234 required_if=[
235 ["state", "absent", ["ipsla_monitoring_policy", "ipsla_monitoring_policy_uuid"], True],
236 ["state", "present", ["ipsla_monitoring_policy", "ipsla_monitoring_policy_uuid"], True],
237 ],
238 )
239
240 mso = MSOModule(module)
241
242 template = module.params.get("template")
243 ipsla_monitoring_policy = module.params.get("ipsla_monitoring_policy")
244 description = module.params.get("description")
245 ipsla_monitoring_policy_uuid = module.params.get("ipsla_monitoring_policy_uuid")
246 sla_type = module.params.get("sla_type")
247 destination_port = module.params.get("destination_port")
248 http_version = module.params.get("http_version")
249 http_uri = module.params.get("http_uri")
250 sla_frequency = module.params.get("sla_frequency")
251 detect_multiplier = module.params.get("detect_multiplier")
252 request_data_size = module.params.get("request_data_size")
253 type_of_service = module.params.get("type_of_service")
254 operation_timeout = module.params.get("operation_timeout")
255 threshold = module.params.get("threshold")
256 ipv6_traffic_class = module.params.get("ipv6_traffic_class")
257 state = module.params.get("state")
258
259 if sla_frequency is not None and sla_frequency not in range(1, 301):
260 mso.fail_json(msg="Invalid value provided for sla_frequency: {0}; it must be in the range 1 - 300".format(sla_frequency))
261
262 if detect_multiplier is not None and detect_multiplier not in range(1, 101):
263 mso.fail_json(msg="Invalid value provided for detect_multiplier: {0}; it must be in the range 1 - 100".format(detect_multiplier))
264
265 if request_data_size is not None and request_data_size not in range(0, 17514):
266 mso.fail_json(msg="Invalid value provided for request_data_size: {0}; it must be in the range 0 - 17513".format(request_data_size))
267
268 if type_of_service is not None and type_of_service not in range(0, 256):
269 mso.fail_json(msg="Invalid value provided for type_of_service: {0}; it must be in the range 0 - 255".format(type_of_service))
270
271 if operation_timeout is not None and sla_frequency is not None and operation_timeout > (sla_frequency * 1000):
272 mso.fail_json(
273 msg="Invalid value provided for operation_timeout: {0}; must be less than or equal to: {1}".format(operation_timeout, (sla_frequency * 1000))
274 )
275
276 if threshold is not None and operation_timeout is not None and threshold > operation_timeout:
277 mso.fail_json(
278 msg="Invalid value provided for threshold: {0}; it must be less than or equal to the operation_timeout: {1}".format(threshold, operation_timeout)
279 )
280
281 if ipv6_traffic_class is not None and ipv6_traffic_class not in range(0, 256):
282 mso.fail_json(msg="Invalid value provided for ipv6_traffic_class: {0}; it must be in the range 0 - 255".format(ipv6_traffic_class))
283
284 if sla_type == "http":
285 destination_port = 80
286
287 if sla_type != "http":
288 if http_version or http_uri:
289 mso.fail_json(msg="http_version and http_uri can only be used when sla_type is 'http'")
290
291 ops = []
292 match = None
293
294 mso_template = MSOTemplate(mso, "tenant", template)
295 mso_template.validate_template("tenantPolicy")
296 object_description = "IPSLA Monitoring Policy"
297
298 path = "/tenantPolicyTemplate/template/ipslaMonitoringPolicies"
299 match = mso_template.get_ipsla_monitoring_policy(ipsla_monitoring_policy_uuid, ipsla_monitoring_policy)
300 if isinstance(match, list): # Query all policies
301 mso.existing = mso.previous = match
302 elif match is not None: # Query a specific policy
303 mso.existing = mso.previous = copy.deepcopy(match.details)
304
305 if state == "present":
306 mso_values = dict(
307 name=ipsla_monitoring_policy,
308 description=description,
309 slaType=sla_type,
310 slaPort=destination_port,
311 httpVersion=http_version,
312 httpUri=http_uri,
313 slaFrequency=sla_frequency,
314 slaDetectMultiplier=detect_multiplier,
315 reqDataSize=request_data_size,
316 ipv4ToS=type_of_service,
317 timeout=operation_timeout,
318 threshold=threshold,
319 ipv6TrfClass=ipv6_traffic_class,
320 )
321 if match:
322 update_path = "{0}/{1}".format(path, match.index)
323 append_update_ops_data(ops, match.details, update_path, mso_values)
324 mso.sanitize(match.details, collate=True)
325
326 else:
327 mso.sanitize(mso_values)
328 ops.append(dict(op="add", path="{0}/-".format(path), value=mso.sent))
329
330 elif state == "absent":
331 if match:
332 ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index)))
333
334 if not module.check_mode and ops:
335 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
336 ipsla_policies = response.get("tenantPolicyTemplate", {}).get("template", {}).get("ipslaMonitoringPolicies", [])
337 match = mso_template.get_object_by_key_value_pairs(
338 object_description,
339 ipsla_policies,
340 [KVPair("uuid", ipsla_monitoring_policy_uuid) if ipsla_monitoring_policy_uuid else KVPair("name", ipsla_monitoring_policy)],
341 )
342 if match:
343 mso.existing = match.details
344 else:
345 mso.existing = {}
346 elif module.check_mode and state != "query":
347 mso.existing = mso.proposed if state == "present" else {}
348
349 mso.exit_json()
350
351
352 if __name__ == "__main__":
353 main()