Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]>
5 # Copyright: (c) 2021, Anvitha Jain (@anvitha-jain) <[email protected]>
6 # Copyright: (c) 2022, Akini Ross (@akinross) <[email protected]>
7 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
14
15 DOCUMENTATION = r"""
16 ---
17 module: mso_schema_site_bd_l3out
18 short_description: Manage site-local BD l3out's in schema template
19 description:
20 - Manage site-local BDs l3out's in schema template on Cisco ACI Multi-Site.
21 author:
22 - Dag Wieers (@dagwieers)
23 - Anvitha Jain (@anvitha-jain)
24 - Akini Ross (@akinross)
25 options:
26 schema:
27 description:
28 - The name of the schema.
29 type: str
30 required: true
31 site:
32 description:
33 - The name of the site.
34 type: str
35 required: true
36 template:
37 description:
38 - The name of the template.
39 type: str
40 required: true
41 bd:
42 description:
43 - The name of the BD.
44 type: str
45 required: true
46 l3out:
47 description:
48 - The l3out associated to this BD.
49 aliases: [ name ]
50 type: dict
51 suboptions:
52 name:
53 description:
54 - The name of the l3out to associate with.
55 required: true
56 type: str
57 schema:
58 description:
59 - The schema that defines the referenced l3out.
60 - If this parameter is unspecified, it defaults to the current schema.
61 - Mutually exclusive with O(l3out.tenant).
62 type: str
63 template:
64 description:
65 - The template that defines the referenced l3out.
66 - If this parameter is unspecified, it defaults to the current schema.
67 - Mutually exclusive with O(l3out.tenant).
68 type: str
69 tenant:
70 description:
71 - The tenant name of the referenced l3out.
72 - If this parameter is specified, the constructed l3out reference will refer to a distinguished name (DN) in APIC.
73 - Mutually exclusive with O(l3out.schema) and O(l3out.template).
74 type: str
75 state:
76 description:
77 - Use C(present) or C(absent) for adding or removing.
78 - Use C(query) for listing an object or multiple objects.
79 type: str
80 choices: [ absent, present, query ]
81 default: present
82 notes:
83 - The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index.
84 This can cause silent corruption on concurrent access when changing/removing on object as
85 the wrong object may be referenced. This module is affected by this deficiency.
86 seealso:
87 - module: cisco.mso.mso_schema_site_bd
88 - module: cisco.mso.mso_schema_template_bd
89 extends_documentation_fragment: cisco.mso.modules
90 """
91
92 EXAMPLES = r"""
93 - name: Add a new site BD l3out
94 cisco.mso.mso_schema_site_bd_l3out:
95 host: mso_host
96 username: admin
97 password: SomeSecretPassword
98 schema: Schema1
99 site: Site1
100 template: Template1
101 bd: BD1
102 l3out:
103 name: L3out1
104 state: present
105
106 - name: Add a new site BD l3out with different schema and template
107 cisco.mso.mso_schema_site_bd_l3out:
108 host: mso_host
109 username: admin
110 password: SomeSecretPassword
111 schema: Schema1
112 site: Site1
113 template: Template1
114 bd: BD1
115 l3out:
116 name: L3out1
117 schema: Schema2
118 template: Template2
119 state: present
120
121 - name: Remove a site BD l3out
122 cisco.mso.mso_schema_site_bd_l3out:
123 host: mso_host
124 username: admin
125 password: SomeSecretPassword
126 schema: Schema1
127 site: Site1
128 template: Template1
129 bd: BD1
130 l3out:
131 name: L3out1
132 state: absent
133
134 - name: Query a specific site BD l3out
135 cisco.mso.mso_schema_site_bd_l3out:
136 host: mso_host
137 username: admin
138 password: SomeSecretPassword
139 schema: Schema1
140 site: Site1
141 template: Template1
142 bd: BD1
143 l3out:
144 name: L3out1
145 state: query
146 register: query_result
147
148 - name: Query all site BD l3outs
149 cisco.mso.mso_schema_site_bd_l3out:
150 host: mso_host
151 username: admin
152 password: SomeSecretPassword
153 schema: Schema1
154 site: Site1
155 template: Template1
156 bd: BD1
157 state: query
158 register: query_result
159 """
160
161 RETURN = r"""
162 """
163
164 from ansible.module_utils.basic import AnsibleModule
165 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_l3out_reference_spec
166 from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema
167
168
169 def main():
170 argument_spec = mso_argument_spec()
171 argument_spec.update(
172 schema=dict(type="str", required=True),
173 site=dict(type="str", required=True),
174 template=dict(type="str", required=True),
175 bd=dict(type="str", required=True),
176 l3out=dict(
177 type="dict", options=mso_l3out_reference_spec(), aliases=["name"], mutually_exclusive=[("tenant", "schema"), ("tenant", "template")]
178 ), # This parameter is not required for querying all objects
179 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
180 )
181
182 module = AnsibleModule(
183 argument_spec=argument_spec,
184 supports_check_mode=True,
185 required_if=[
186 ["state", "absent", ["l3out"]],
187 ["state", "present", ["l3out"]],
188 ],
189 )
190
191 schema = module.params.get("schema")
192 site = module.params.get("site")
193 template = module.params.get("template").replace(" ", "")
194 bd = module.params.get("bd")
195 l3out = module.params.get("l3out")
196 state = module.params.get("state")
197
198 mso = MSOModule(module)
199
200 mso_schema = MSOSchema(mso, schema, template, site)
201 mso_objects = mso_schema.schema_objects
202
203 mso_schema.set_template_bd(bd)
204 mso_schema.set_site_bd(bd, fail_module=False)
205
206 bd_path = "/sites/{0}-{1}/bds".format(mso_objects.get("site").details.get("siteId"), template)
207 ops = []
208 payload = dict()
209
210 if l3out:
211
212 if l3out.get("tenant"):
213 l3out_ref = "uni/tn-{0}/out-{1}".format(l3out.get("tenant"), l3out.get("name"))
214 else:
215 l3out_schema_id = mso.lookup_schema(l3out.get("schema")) if l3out.get("schema") else mso_schema.id
216 l3out_template = l3out.get("template") if l3out.get("template") else template
217 l3out_ref = mso.l3out_ref(schema_id=l3out_schema_id, template=l3out_template, l3out=l3out.get("name"))
218
219 if not mso_objects.get("site_bd"):
220 payload = dict(bdRef=dict(schemaId=mso_schema.id, templateName=template, bdName=bd), l3Outs=[l3out.get("name")], l3OutRefs=[l3out_ref])
221 else:
222 mso_objects.get("site_bd").details["bdRef"] = dict(schemaId=mso_schema.id, templateName=template, bdName=bd)
223 l3out_refs = mso_objects.get("site_bd").details.get("l3OutRefs", [])
224 l3outs = mso_objects.get("site_bd").details.get("l3Outs", [])
225 # check on name because refs are handled differently between versions
226 if l3out.get("name") in l3outs:
227 mso.existing = mso.dict_from_ref(l3out_refs[l3outs.index(l3out.get("name"))])
228
229 if state == "query":
230 if l3out is None:
231 if "l3OutRefs" in mso_objects.get("site_bd", {}).details.keys():
232 mso.existing = [mso.dict_from_ref(l3) for l3 in mso_objects.get("site_bd", {}).details.get("l3OutRefs", [])]
233 else:
234 mso.existing = [dict(l3outName=l3) for l3 in mso_objects.get("site_bd", {}).details.get("l3Outs", [])]
235 elif not mso.existing:
236 mso.fail_json(msg="L3out '{0}' not found".format(l3out.get("name")))
237 mso.exit_json()
238
239 mso.previous = mso.existing
240 if state == "absent":
241 if mso.existing:
242 mso.sent = mso.existing = {}
243 if l3out.get("name") in l3outs:
244 del l3outs[l3outs.index(l3out.get("name"))]
245 if l3out_ref in l3out_refs:
246 del l3out_refs[l3out_refs.index(l3out_ref)]
247 ops.append(dict(op="replace", path="{0}/{1}".format(bd_path, bd), value=mso_objects.get("site_bd").details))
248
249 elif state == "present":
250 if payload:
251 ops.append(dict(op="add", path="{0}/-".format(bd_path), value=payload))
252 elif not mso.existing:
253 l3outs.append(l3out.get("name"))
254 l3out_refs.append(l3out_ref)
255 ops.append(dict(op="replace", path="{0}/{1}".format(bd_path, bd), value=mso_objects.get("site_bd").details))
256
257 if l3out.get("tenant"):
258 mso.existing = mso.dict_from_ref(l3out_ref)
259 else:
260 mso.existing = mso.make_reference(l3out, "l3out", l3out_schema_id, l3out_template)
261
262 if not module.check_mode:
263 mso.request(mso_schema.path, method="PATCH", data=ops)
264
265 mso.exit_json()
266
267
268 if __name__ == "__main__":
269 main()