Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_physical_domain.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_physical_domain
17 short_description: Manage Physical Domains on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage Physical Domains 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 template.
27 - The template must be a fabric policy template.
28 type: str
29 required: true
30 physical_domain:
31 description:
32 - The name of the Physical Domain.
33 type: str
34 aliases: [ name ]
35 physical_domain_uuid:
36 description:
37 - The uuid of the Physical Domain.
38 - This parameter is required when the O(physical_domain) needs to be updated.
39 type: str
40 aliases: [ uuid ]
41 description:
42 description:
43 - The description of the Physical Domain.
44 type: str
45 pool:
46 description:
47 - The name of the VLAN Pool.
48 - Providing an empty string will remove the pool from the Physical Domain.
49 type: str
50 aliases: [ vlan_pool ]
51 state:
52 description:
53 - Use C(absent) for removing.
54 - Use C(query) for listing an object or multiple objects.
55 - Use C(present) for creating or updating.
56 type: str
57 choices: [ absent, query, present ]
58 default: query
59 extends_documentation_fragment: cisco.mso.modules
60 """
61
62 EXAMPLES = r"""
63 - name: Create a new physical domain
64 cisco.mso.ndo_physical_domain:
65 host: mso_host
66 username: admin
67 password: SomeSecretPassword
68 template: ansible_tenant_template
69 physical_domain: ansible_test_physical_domain
70 pool: ansible_test_vlan_pool
71 state: present
72 register: create
73
74 - name: Query a physical domain with name
75 cisco.mso.ndo_physical_domain:
76 host: mso_host
77 username: admin
78 password: SomeSecretPassword
79 template: ansible_tenant_template
80 physical_domain: ansible_test_physical_domain
81 state: query
82 register: query_one
83
84 - name: Query a physical domain with UUID
85 cisco.mso.ndo_physical_domain:
86 host: mso_host
87 username: admin
88 password: SomeSecretPassword
89 template: ansible_tenant_template
90 physical_domain_uuid: '{{ create.current.uuid }}'
91 state: query
92 register: query_with_uuid
93
94 - name: Query all physical domains in the template
95 cisco.mso.ndo_physical_domain:
96 host: mso_host
97 username: admin
98 password: SomeSecretPassword
99 template: ansible_tenant_template
100 state: query
101 register: query_all
102
103 - name: Delete a vlan pool from a physical domain
104 cisco.mso.ndo_physical_domain:
105 host: mso_host
106 username: admin
107 password: SomeSecretPassword
108 template: ansible_tenant_template
109 physical_domain: ansible_test_physical_domain
110 pool: ""
111 state: present
112
113 - name: Delete a physical domain
114 cisco.mso.ndo_physical_domain:
115 host: mso_host
116 username: admin
117 password: SomeSecretPassword
118 template: ansible_tenant_template
119 physical_domain: ansible_test_physical_domain
120 state: absent
121 """
122
123 RETURN = r"""
124 """
125
126
127 import copy
128 from ansible.module_utils.basic import AnsibleModule
129 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
130 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
131
132
133 def main():
134 argument_spec = mso_argument_spec()
135 argument_spec.update(
136 template=dict(type="str", required=True),
137 physical_domain=dict(type="str", aliases=["name"]),
138 physical_domain_uuid=dict(type="str", aliases=["uuid"]),
139 description=dict(type="str"),
140 pool=dict(type="str", aliases=["vlan_pool"]),
141 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
142 )
143
144 module = AnsibleModule(
145 argument_spec=argument_spec,
146 supports_check_mode=True,
147 required_if=[
148 ["state", "absent", ["physical_domain"]],
149 ["state", "present", ["physical_domain"]],
150 ],
151 )
152
153 mso = MSOModule(module)
154
155 template = module.params.get("template")
156 physical_domain = module.params.get("physical_domain")
157 physical_domain_uuid = module.params.get("physical_domain_uuid")
158 pool = module.params.get("pool")
159 description = module.params.get("description")
160 state = module.params.get("state")
161
162 ops = []
163 match = None
164
165 mso_template = MSOTemplate(mso, "fabric_policy", template)
166 mso_template.validate_template("fabricPolicy")
167
168 path = "/fabricPolicyTemplate/template/domains"
169 match = get_physical_domain(mso_template, physical_domain_uuid, physical_domain)
170
171 if physical_domain_uuid or physical_domain:
172 if match:
173 if match.details.get("pool"):
174 match.details["poolRef"] = match.details.get("pool")
175 match.details["pool"] = mso_template.get_vlan_pool_name(match.details.get("pool"))
176 mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object
177 elif match:
178 for physical_domain in match:
179 if physical_domain.get("pool"):
180 physical_domain["poolRef"] = physical_domain.get("pool")
181 physical_domain["pool"] = mso_template.get_vlan_pool_name(physical_domain.get("pool"))
182 mso.existing = match # Query all objects
183
184 if state == "present":
185 mso.existing = {}
186
187 if match:
188 if physical_domain and match.details.get("name") != physical_domain:
189 ops.append(dict(op="replace", path="{0}/{1}/name".format(path, match.index), value=physical_domain))
190 match.details["name"] = physical_domain
191
192 if description is not None and match.details.get("description") != description:
193 ops.append(dict(op="replace", path="{0}/{1}/description".format(path, match.index), value=description))
194 match.details["description"] = description
195
196 if pool and match.details.get("pool") != pool:
197 ops.append(dict(op="replace", path="{0}/{1}/pool".format(path, match.index), value=mso_template.get_vlan_pool_uuid(pool)))
198 match.details["pool"] = pool
199 elif pool == "" and match.details.get("pool"):
200 ops.append(dict(op="remove", path="{0}/{1}/pool".format(path, match.index)))
201 match.details.pop("pool")
202
203 mso.sanitize(match.details)
204
205 else:
206 payload = {"name": physical_domain, "templateId": mso_template.template.get("templateId"), "schemaId": mso_template.template.get("schemaId")}
207 if description:
208 payload["description"] = description
209
210 if pool:
211 pool_uuid = mso_template.get_vlan_pool_uuid(pool)
212 payload["pool"] = pool_uuid
213
214 ops.append(dict(op="add", path="{0}/-".format(path), value=copy.deepcopy(payload)))
215
216 if pool:
217 payload["pool"] = pool
218 payload["poolRef"] = pool_uuid
219
220 mso.sanitize(payload)
221
222 mso.existing = mso.proposed
223
224 elif state == "absent":
225 if match:
226 ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index)))
227 mso.existing = {}
228
229 if not module.check_mode and ops:
230 mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops)
231 match = get_physical_domain(mso_template, physical_domain_uuid, physical_domain)
232 if match:
233 if match.details.get("pool"):
234 match.details["poolRef"] = match.details.get("pool")
235 match.details["pool"] = mso_template.get_vlan_pool_name(match.details.get("pool"))
236 mso.existing = match.details # When the state is present
237 else:
238 mso.existing = {} # When the state is absent
239 elif module.check_mode and state != "query": # When the state is present/absent with check mode
240 mso.proposed["poolRef"] = mso_template.get_vlan_pool_uuid(pool)
241 mso.existing = mso.proposed if state == "present" else {}
242
243 mso.exit_json()
244
245
246 def get_physical_domain(mso_template, uuid=None, name=None, fail_module=False):
247 match = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("domains", [])
248 if uuid or name: # Query a specific object
249 return mso_template.get_object_by_key_value_pairs("Physical Domain", match, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module)
250 return match # Query all objects
251
252
253 if __name__ == "__main__":
254 main()