Debian Codegraph @

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

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