Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Anvitha Jain (@anvjain) <[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 = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: ndo_tenant_igmp_snooping_policy
17 short_description: Manage Internet Group Management Protocol (IGMP) Snooping Policies in Tenant Policy Templates on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage IGMP Snooping Policies in Tenant Policy Templates on Cisco Nexus Dashboard Orchestrator (NDO).
20 - This module is only supported on ND v3.1 (NDO v4.3) and later.
21 author:
22 - Anvitha Jain (@anvjain)
23 options:
24 template:
25 description:
26 - The name of the tenant template.
27 type: str
28 aliases: [ tenant_template ]
29 required: true
30 name:
31 description:
32 - The name of the IGMP Snooping Policy.
33 type: str
34 aliases: [ igmp_snooping_policy ]
35 uuid:
36 description:
37 - The UUID of the IGMP Snooping Policy.
38 - This parameter is required when the IGMP Snooping Policy O(name) needs to be updated.
39 type: str
40 description:
41 description:
42 - The description of the IGMP Snooping Policy.
43 - Providing an empty string will remove the O(description="") from the IGMP Snooping Policy.
44 type: str
45 admin_state:
46 description:
47 - The administrative state of the IGMP Snooping Policy.
48 - Defaults to C(enabled) when unset during creation.
49 type: str
50 choices: [ enabled, disabled ]
51 fast_leave_control:
52 description:
53 - The fast leave control of the IGMP Snooping Policy.
54 - Defaults to C(disabled) when unset during creation.
55 type: str
56 choices: [ enabled, disabled ]
57 querier_control:
58 description:
59 - The querier control of the IGMP Snooping Policy.
60 - Defaults to C(disabled) when unset during creation.
61 type: str
62 choices: [ enabled, disabled ]
63 querier_version:
64 description:
65 - The querier version of the IGMP Snooping Policy.
66 - Defaults to C(v3) when unset during creation.
67 type: str
68 choices: [ v2, v3 ]
69 query_interval:
70 description:
71 - The query interval value in seconds.
72 - The query interval of the IGMP Snooping Policy.
73 - Defaults to 125 when unset during creation.
74 - The value must be between 1 and 18000.
75 type: int
76 query_response_interval:
77 description:
78 - The query response interval value in seconds.
79 - The query response interval of the IGMP Snooping Policy.
80 - Defaults to 10 when unset during creation.
81 - The value must be between 1 and 25.
82 type: int
83 last_member_query_interval:
84 description:
85 - The last member query interval value in seconds.
86 - The last member query interval of the IGMP Snooping Policy.
87 - Defaults to 1 when unset during creation.
88 - The value must be between 1 and 25.
89 type: int
90 start_query_interval:
91 description:
92 - The start query interval value in seconds.
93 - The start query interval of the IGMP Snooping Policy.
94 - Defaults to 31 when unset during creation.
95 - The value must be between 1 and 18000.
96 type: int
97 start_query_count:
98 description:
99 - The start query count of the IGMP Snooping Policy.
100 - Defaults to 2 when unset during creation.
101 - The value must be between 1 and 10.
102 type: int
103 state:
104 description:
105 - Use C(absent) for removing.
106 - Use C(query) for listing an object or multiple objects.
107 - Use C(present) for creating or updating.
108 type: str
109 choices: [ absent, query, present ]
110 default: query
111 notes:
112 - The O(template) must exist before using this module in your playbook.
113 Use M(cisco.mso.ndo_template) to create the Tenant template.
114 seealso:
115 - module: cisco.mso.ndo_template
116 extends_documentation_fragment: cisco.mso.modules
117 """
118
119 EXAMPLES = r"""
120 - name: Create a new IGMP Snooping Policy object
121 cisco.mso.ndo_tenant_igmp_snooping_policy:
122 host: mso_host
123 username: admin
124 password: SomeSecretPassword
125 template: tenant_template
126 name: igmp_snooping_policy_1
127 admin_state: enabled
128 fast_leave_control: enabled
129 querier_control: enabled
130 querier_version: v2
131 query_interval: 100
132 query_response_interval: 5
133 last_member_query_interval: 2
134 start_query_interval: 25
135 start_query_count: 1
136 state: present
137 register: igmp_snooping_policy_1
138
139 - name: Update a IGMP Snooping Policy object name with UUID
140 cisco.mso.ndo_tenant_igmp_snooping_policy:
141 host: mso_host
142 username: admin
143 password: SomeSecretPassword
144 template: tenant_template
145 name: igmp_snooping_policy_2
146 uuid: "{{ igmp_snooping_policy_1.current.uuid }}"
147 state: present
148
149 - name: Query a IGMP Snooping Policy object with name
150 cisco.mso.ndo_tenant_igmp_snooping_policy:
151 host: mso_host
152 username: admin
153 password: SomeSecretPassword
154 template: tenant_template
155 name: igmp_snooping_policy_1
156 state: query
157 register: query_uuid
158
159 - name: Query a IGMP Snooping Policy object with UUID
160 cisco.mso.ndo_tenant_igmp_snooping_policy:
161 host: mso_host
162 username: admin
163 password: SomeSecretPassword
164 template: tenant_template
165 uuid: "{{ igmp_snooping_policy_1.current.uuid }}"
166 state: query
167 register: query_uuid
168
169 - name: Query all IGMP Snooping Policy objects in a Tenant Template
170 cisco.mso.ndo_tenant_igmp_snooping_policy:
171 host: mso_host
172 username: admin
173 password: SomeSecretPassword
174 template: tenant_template
175 state: query
176 register: query_all
177
178 - name: Delete a IGMP Snooping Policy object with name
179 cisco.mso.ndo_tenant_igmp_snooping_policy:
180 host: mso_host
181 username: admin
182 password: SomeSecretPassword
183 template: tenant_template
184 name: igmp_snooping_policy_1
185 state: absent
186
187 - name: Delete a IGMP Snooping Policy object with UUID
188 cisco.mso.ndo_tenant_igmp_snooping_policy:
189 host: mso_host
190 username: admin
191 password: SomeSecretPassword
192 template: tenant_template
193 uuid: "{{ igmp_snooping_policy_1.current.uuid }}"
194 state: absent
195 """
196
197 RETURN = r"""
198 """
199
200 import copy
201 from ansible.module_utils.basic import AnsibleModule
202 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
203 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
204 from ansible_collections.cisco.mso.plugins.module_utils.constants import ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP
205 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
206
207
208 def main():
209 argument_spec = mso_argument_spec()
210 argument_spec.update(
211 template=dict(type="str", required=True, aliases=["tenant_template"]),
212 name=dict(type="str", aliases=["igmp_snooping_policy"]),
213 uuid=dict(type="str"),
214 description=dict(type="str"),
215 admin_state=dict(type="str", choices=["enabled", "disabled"]),
216 fast_leave_control=dict(type="str", choices=["enabled", "disabled"]),
217 querier_control=dict(type="str", choices=["enabled", "disabled"]),
218 querier_version=dict(type="str", choices=["v2", "v3"]),
219 query_interval=dict(type="int"),
220 query_response_interval=dict(type="int"),
221 last_member_query_interval=dict(type="int"),
222 start_query_interval=dict(type="int"),
223 start_query_count=dict(type="int"),
224 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
225 )
226
227 module = AnsibleModule(
228 argument_spec=argument_spec,
229 supports_check_mode=True,
230 required_if=[
231 ["state", "absent", ["name", "uuid"], True],
232 ["state", "present", ["name", "uuid"], True],
233 ],
234 )
235
236 mso = MSOModule(module)
237
238 template = module.params.get("template")
239 name = module.params.get("name")
240 uuid = module.params.get("uuid")
241 description = module.params.get("description")
242 admin_state = module.params.get("admin_state")
243 fast_leave_control = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(module.params.get("fast_leave_control"))
244 querier_control = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(module.params.get("querier_control"))
245 querier_version = module.params.get("querier_version")
246 query_interval = module.params.get("query_interval")
247 query_response_interval = module.params.get("query_response_interval")
248 last_member_query_interval = module.params.get("last_member_query_interval")
249 start_query_interval = module.params.get("start_query_interval")
250 start_query_count = module.params.get("start_query_count")
251 state = module.params.get("state")
252
253 mso_template = MSOTemplate(mso, "tenant", template)
254 mso_template.validate_template("tenantPolicy")
255
256 existing_mld_snooping_policies = mso_template.template.get("tenantPolicyTemplate", {}).get("template", {}).get("igmpSnoopPolicies", [])
257 object_description = "IGMP Snooping Policy"
258 igmp_snooping_policy_attrs_path = None
259 match = None
260
261 if name or uuid:
262 match = mso_template.get_object_by_key_value_pairs(
263 object_description,
264 existing_mld_snooping_policies,
265 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
266 )
267 if match:
268 igmp_snooping_policy_attrs_path = "/tenantPolicyTemplate/template/igmpSnoopPolicies/{0}".format(match.index)
269 mso.existing = mso.previous = copy.deepcopy(match.details)
270 else:
271 mso.existing = mso.previous = existing_mld_snooping_policies
272
273 ops = []
274
275 if state == "present":
276 if uuid and not mso.existing:
277 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
278
279 mso_values = dict(
280 name=name,
281 description=description,
282 enableAdminState=admin_state,
283 enableFastLeaveControl=fast_leave_control,
284 enableQuerierControl=querier_control,
285 igmpQuerierVersion=querier_version,
286 queryInterval=query_interval,
287 queryResponseInterval=query_response_interval,
288 lastMemberQueryInterval=last_member_query_interval,
289 startQueryInterval=start_query_interval,
290 startQueryCount=start_query_count,
291 )
292
293 if mso.existing and match:
294 append_update_ops_data(ops, match.details, igmp_snooping_policy_attrs_path, mso_values)
295 mso.sanitize(match.details, collate=True)
296 else:
297 mso.sanitize(mso_values)
298 ops.append(dict(op="add", path="/tenantPolicyTemplate/template/igmpSnoopPolicies/-", value=mso.sent))
299
300 elif state == "absent":
301 if mso.existing and match:
302 ops.append(dict(op="remove", path=igmp_snooping_policy_attrs_path))
303
304 if not module.check_mode and ops:
305 response_object = mso.request(mso_template.template_path, method="PATCH", data=ops)
306 existing_mld_snooping_policies = response_object.get("tenantPolicyTemplate", {}).get("template", {}).get("igmpSnoopPolicies", [])
307 match = mso_template.get_object_by_key_value_pairs(
308 object_description, existing_mld_snooping_policies, [KVPair("uuid", uuid) if uuid else KVPair("name", name)]
309 )
310 if match:
311 mso.existing = match.details # When the state is present
312 else:
313 mso.existing = {} # When the state is absent
314 elif module.check_mode and state != "query": # When the state is present/absent with check mode
315 mso.existing = mso.proposed if state == "present" else {}
316
317 mso.exit_json()
318
319
320 if __name__ == "__main__":
321 main()