Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Gaspard Micol (@gmicol) <[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_match_rule_policy
17 version_added: "2.12.0"
18 short_description: Manage Match Rule Policies on Cisco Nexus Dashboard Orchestrator (NDO).
19 description:
20 - Manage Match Rule Policies on Cisco Nexus Dashboard Orchestrator (NDO).
21 - This module is only supported on ND v4.1 (NDO v5.1) and later.
22 author:
23 - Gaspard Micol (@gmicol)
24 options:
25 template:
26 description:
27 - The name of the template.
28 - The template must be a Tenant Policy template.
29 - This parameter or O(template_id) is required.
30 type: str
31 template_id:
32 description:
33 - The ID of the template.
34 - The template must be a Tenant Policy template.
35 - This parameter or O(template) is required.
36 type: str
37 name:
38 description:
39 - The name of the Match Rule Policy.
40 type: str
41 aliases: [ match_rule_policy ]
42 uuid:
43 description:
44 - The UUID of the Match Rule Policy.
45 - This parameter is required when the O(name) needs to be updated.
46 type: str
47 aliases: [ match_rule_policy_uuid ]
48 description:
49 description:
50 - The description of the Match Rule Policy.
51 type: str
52 state:
53 description:
54 - Use C(absent) for removing.
55 - Use C(query) for listing an object or multiple objects.
56 - Use C(present) for creating or updating.
57 type: str
58 choices: [ absent, query, present ]
59 default: query
60 notes:
61 - The O(template) must exist before using this module in your playbook.
62 Use M(cisco.mso.ndo_template) to create the Tenant Policy template.
63 seealso:
64 - module: cisco.mso.ndo_template
65 extends_documentation_fragment: cisco.mso.modules
66 """
67
68 EXAMPLES = r"""
69 - name: Create a Match Rule Policy
70 cisco.mso.ndo_match_rule_policy:
71 host: mso_host
72 username: admin
73 password: SomeSecretPassword
74 template: ansible_test
75 name: ansible_match_rule_policy
76 description: Ansible Match Rule Policy
77 state: present
78 register: create_match_rule_policy
79
80 - name: Update a Match Rule Policy using name
81 cisco.mso.ndo_match_rule_policy:
82 host: mso_host
83 username: admin
84 password: SomeSecretPassword
85 template: ansible_test
86 name: ansible_match_rule_policy
87 description: Updated Ansible Match Rule Policy
88 state: present
89
90 - name: Update a Match Rule Policy using UUID
91 cisco.mso.ndo_match_rule_policy:
92 host: mso_host
93 username: admin
94 password: SomeSecretPassword
95 template: ansible_test
96 name: ansible_match_rule_policy_changed
97 uuid: "{{ create_match_rule_policy.current.uuid }}"
98 state: present
99
100 - name: Query a Match Rule Policy using name
101 cisco.mso.ndo_match_rule_policy:
102 host: mso_host
103 username: admin
104 password: SomeSecretPassword
105 template: ansible_test
106 name: ansible_match_rule_policy
107 state: query
108 register: query_with_name
109
110 - name: Query a Match Rule Policy using UUID
111 cisco.mso.ndo_match_rule_policy:
112 host: mso_host
113 username: admin
114 password: SomeSecretPassword
115 template: ansible_test
116 uuid: "{{ create_match_rule_policy.current.uuid }}"
117 state: query
118 register: query_with_uuid
119
120 - name: Query all Match Rule Policies in a template
121 cisco.mso.ndo_match_rule_policy:
122 host: mso_host
123 username: admin
124 password: SomeSecretPassword
125 template: ansible_test
126 state: query
127 register: query_all_objects
128
129 - name: Delete a Match Rule Policy using its name
130 cisco.mso.ndo_match_rule_policy:
131 host: mso_host
132 username: admin
133 password: SomeSecretPassword
134 template: ansible_test
135 name: ansible_match_rule_policy
136 state: absent
137
138 - name: Delete a Match Rule Policy using UUID
139 cisco.mso.ndo_match_rule_policy:
140 host: mso_host
141 username: admin
142 password: SomeSecretPassword
143 template_id: ansible_test
144 uuid: "{{ create_match_rule_policy.current.uuid }}"
145 state: absent
146 """
147
148 RETURN = r"""
149 """
150
151 from ansible.module_utils.basic import AnsibleModule
152 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
153 from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates
154 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
155 import copy
156
157
158 def main():
159 argument_spec = mso_argument_spec()
160 argument_spec.update(
161 template=dict(type="str"),
162 template_id=dict(type="str"),
163 name=dict(type="str", aliases=["match_rule_policy"]),
164 uuid=dict(type="str", aliases=["match_rule_policy_uuid"]),
165 description=dict(type="str"),
166 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
167 )
168
169 module = AnsibleModule(
170 argument_spec=argument_spec,
171 supports_check_mode=True,
172 mutually_exclusive=[
173 ("template", "template_id"),
174 ],
175 required_if=[
176 ["state", "absent", ["name", "uuid"], True],
177 ["state", "present", ["name", "uuid"], True],
178 ],
179 required_one_of=[
180 ["template", "template_id"],
181 ],
182 )
183
184 mso = MSOModule(module)
185 mso_templates = MSOTemplates(mso)
186
187 template_name = module.params.get("template")
188 template_id = module.params.get("template_id")
189 name = module.params.get("name")
190 uuid = module.params.get("uuid")
191 description = module.params.get("description")
192 state = module.params.get("state")
193
194 mso_template = mso_templates.get_template("tenant", template_name, template_id)
195 mso_template.validate_template("tenantPolicy")
196
197 match = mso_template.get_match_rule_policy_object(uuid, name)
198
199 if (uuid or name) and match:
200 mso.existing = mso.previous = copy.deepcopy(mso_template.update_config_with_template_and_references(match.details)) # Query a specific object
201 elif match:
202 mso.existing = [mso_template.update_config_with_template_and_references(obj) for obj in match] # Query all objects
203
204 match_rule_policy_path = "/tenantPolicyTemplate/template/matchRulePolicies/{0}".format(match.index if match else "-")
205
206 ops = []
207
208 if state == "present":
209
210 mso_values = {
211 "name": name,
212 "description": description,
213 }
214
215 if match:
216 append_update_ops_data(ops, match.details, match_rule_policy_path, mso_values)
217 mso.sanitize(mso_values, collate=True)
218 else:
219 mso.sanitize(mso_values)
220 ops.append(dict(op="add", path=match_rule_policy_path, value=mso.sent))
221
222 elif state == "absent":
223 if match:
224 ops.append(dict(op="remove", path=match_rule_policy_path))
225
226 if not module.check_mode and ops:
227 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
228 match = mso_template.get_match_rule_policy_object(uuid, name, search_object=response)
229 if match:
230 mso.existing = mso_template.update_config_with_template_and_references(match.details) # When the state is present
231 else:
232 mso.existing = {} # When the state is absent
233 elif module.check_mode and state != "query": # When the state is present/absent with check mode
234 if mso.proposed:
235 mso_template.update_config_with_template_and_references(mso.proposed)
236 mso.existing = mso.proposed if state == "present" else {}
237 mso.exit_json()
238
239
240 if __name__ == "__main__":
241 main()