Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Dag Wieers (@dagwieers) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: mso_schema_template_external_epg
16 short_description: Manage external EPGs in schema templates
17 description:
18 - Manage external EPGs in schema templates on Cisco ACI Multi-Site.
19 author:
20 - Dag Wieers (@dagwieers)
21 options:
22 schema:
23 description:
24 - The name of the schema.
25 type: str
26 required: true
27 template:
28 description:
29 - The name of the template.
30 type: str
31 required: true
32 external_epg:
33 description:
34 - The name of the external EPG to manage.
35 type: str
36 aliases: [ name, externalepg ]
37 description:
38 description:
39 - The description of external EPG is supported on versions of MSO that are 3.3 or greater.
40 type: str
41 type:
42 description:
43 - The type of external epg.
44 - anp needs to be associated with external epg when the type is cloud.
45 - l3out can be associated with external epg when the type is on-premise.
46 type: str
47 choices: [ on-premise, cloud ]
48 default: on-premise
49 display_name:
50 description:
51 - The name as displayed on the MSO web interface.
52 type: str
53 vrf:
54 description:
55 - The VRF associated with the external epg.
56 type: dict
57 suboptions:
58 name:
59 description:
60 - The name of the VRF to associate with.
61 required: true
62 type: str
63 schema:
64 description:
65 - The schema that defines the referenced VRF.
66 - If this parameter is unspecified, it defaults to the current schema.
67 type: str
68 template:
69 description:
70 - The template that defines the referenced VRF.
71 - If this parameter is unspecified, it defaults to the current template.
72 type: str
73 l3out:
74 description:
75 - The L3Out associated with the external epg.
76 type: dict
77 suboptions:
78 name:
79 description:
80 - The name of the L3Out to associate with.
81 required: true
82 type: str
83 schema:
84 description:
85 - The schema that defines the referenced L3Out.
86 - If this parameter is unspecified, it defaults to the current schema.
87 type: str
88 template:
89 description:
90 - The template that defines the referenced L3Out.
91 - If this parameter is unspecified, it defaults to the current template.
92 type: str
93 anp:
94 description:
95 - The anp associated with the external epg.
96 type: dict
97 suboptions:
98 name:
99 description:
100 - The name of the anp to associate with.
101 required: true
102 type: str
103 schema:
104 description:
105 - The schema that defines the referenced anp.
106 - If this parameter is unspecified, it defaults to the current schema.
107 type: str
108 template:
109 description:
110 - The template that defines the referenced anp.
111 - If this parameter is unspecified, it defaults to the current template.
112 type: str
113 preferred_group:
114 description:
115 - Preferred Group is enabled for this External EPG or not.
116 type: bool
117 qos_level:
118 description:
119 - The QoS Level parameter is supported on versions of MSO/NDO that are 4.3 or greater.
120 - Defaults to C(unspecified) when unset during creation.
121 type: str
122 choices: [ unspecified, level1, level2, level3, level4, level5, level6 ]
123 state:
124 description:
125 - Use C(present) or C(absent) for adding or removing.
126 - Use C(query) for listing an object or multiple objects.
127 type: str
128 choices: [ absent, present, query ]
129 default: present
130 extends_documentation_fragment: cisco.mso.modules
131 """
132
133 EXAMPLES = r"""
134 - name: Add a new external EPG
135 cisco.mso.mso_schema_template_external_epg:
136 host: mso_host
137 username: admin
138 password: SomeSecretPassword
139 schema: Schema 1
140 template: Template 1
141 external_epg: External EPG 1
142 vrf:
143 name: VRF
144 schema: Schema 1
145 template: Template 1
146 state: present
147
148 - name: Add a new external EPG with external epg in cloud
149 cisco.mso.mso_schema_template_external_epg:
150 host: mso_host
151 username: admin
152 password: SomeSecretPassword
153 schema: Schema 1
154 template: Template 1
155 external_epg: External EPG 1
156 type: cloud
157 vrf:
158 name: VRF
159 schema: Schema 1
160 template: Template 1
161 anp:
162 name: ANP1
163 schema: Schema 1
164 template: Template 1
165 state: present
166
167 - name: Remove an external EPG
168 cisco.mso.mso_schema_template_external_epg:
169 host: mso_host
170 username: admin
171 password: SomeSecretPassword
172 schema: Schema 1
173 template: Template 1
174 external_epg: external EPG1
175 state: absent
176
177 - name: Query a specific external EPGs
178 cisco.mso.mso_schema_template_external_epg:
179 host: mso_host
180 username: admin
181 password: SomeSecretPassword
182 schema: Schema 1
183 template: Template 1
184 external_epg: external EPG1
185 state: query
186 register: query_result
187
188 - name: Query all external EPGs
189 cisco.mso.mso_schema_template_external_epg:
190 host: mso_host
191 username: admin
192 password: SomeSecretPassword
193 schema: Schema 1
194 template: Template 1
195 state: query
196 register: query_result
197 """
198
199 RETURN = r"""
200 """
201
202 from ansible.module_utils.basic import AnsibleModule
203 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_reference_spec
204 from ansible_collections.cisco.mso.plugins.module_utils.constants import QOS_LEVEL
205
206
207 def main():
208 argument_spec = mso_argument_spec()
209 argument_spec.update(
210 schema=dict(type="str", required=True),
211 template=dict(type="str", required=True),
212 external_epg=dict(type="str", aliases=["name", "externalepg"]), # This parameter is not required for querying all objects
213 description=dict(type="str"),
214 display_name=dict(type="str"),
215 vrf=dict(type="dict", options=mso_reference_spec()),
216 l3out=dict(type="dict", options=mso_reference_spec()),
217 anp=dict(type="dict", options=mso_reference_spec()),
218 preferred_group=dict(type="bool"),
219 type=dict(type="str", default="on-premise", choices=["on-premise", "cloud"]),
220 qos_level=dict(type="str", choices=QOS_LEVEL),
221 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
222 )
223
224 module = AnsibleModule(
225 argument_spec=argument_spec,
226 supports_check_mode=True,
227 required_if=[
228 ["state", "absent", ["external_epg"]],
229 ["state", "present", ["external_epg", "vrf"]],
230 ["type", "cloud", ["anp"]],
231 ],
232 )
233
234 schema = module.params.get("schema")
235 template = module.params.get("template").replace(" ", "")
236 external_epg = module.params.get("external_epg")
237 description = module.params.get("description")
238 display_name = module.params.get("display_name")
239 vrf = module.params.get("vrf")
240 if vrf is not None and vrf.get("template") is not None:
241 vrf["template"] = vrf.get("template").replace(" ", "")
242 l3out = module.params.get("l3out")
243 if l3out is not None and l3out.get("template") is not None:
244 l3out["template"] = l3out.get("template").replace(" ", "")
245 anp = module.params.get("anp")
246 if anp is not None and anp.get("template") is not None:
247 anp["template"] = anp.get("template").replace(" ", "")
248 preferred_group = module.params.get("preferred_group")
249 type_ext_epg = module.params.get("type")
250 qos_level = module.params.get("qos_level")
251 state = module.params.get("state")
252
253 mso = MSOModule(module)
254
255 # Get schema objects
256 schema_id, schema_path, schema_obj = mso.query_schema(schema)
257
258 # Get template
259 templates = [t.get("name") for t in schema_obj.get("templates")]
260 if template not in templates:
261 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates)))
262 template_idx = templates.index(template)
263
264 # Get external EPGs
265 external_epgs = [e.get("name") for e in schema_obj.get("templates")[template_idx]["externalEpgs"]]
266
267 if external_epg is not None and external_epg in external_epgs:
268 external_epg_idx = external_epgs.index(external_epg)
269 mso.existing = schema_obj.get("templates")[template_idx]["externalEpgs"][external_epg_idx]
270 if "externalEpgRef" in mso.existing:
271 del mso.existing["externalEpgRef"]
272 if "vrfRef" in mso.existing:
273 mso.existing["vrfRef"] = mso.dict_from_ref(mso.existing.get("vrfRef"))
274 if "l3outRef" in mso.existing:
275 mso.existing["l3outRef"] = mso.dict_from_ref(mso.existing.get("l3outRef"))
276 if "anpRef" in mso.existing:
277 mso.existing["anpRef"] = mso.dict_from_ref(mso.existing.get("anpRef"))
278
279 if state == "query":
280 if external_epg is None:
281 mso.existing = schema_obj.get("templates")[template_idx]["externalEpgs"]
282 elif not mso.existing:
283 mso.fail_json(msg="External EPG '{external_epg}' not found".format(external_epg=external_epg))
284 mso.exit_json()
285
286 eepgs_path = "/templates/{0}/externalEpgs".format(template)
287 eepg_path = "/templates/{0}/externalEpgs/{1}".format(template, external_epg)
288 ops = []
289
290 mso.previous = mso.existing
291 if state == "absent":
292 if mso.existing:
293 mso.sent = mso.existing = {}
294 ops.append(dict(op="remove", path=eepg_path))
295
296 elif state == "present":
297 vrf_ref = mso.make_reference(vrf, "vrf", schema_id, template)
298 l3out_ref = mso.make_reference(l3out, "l3out", schema_id, template)
299 anp_ref = mso.make_reference(anp, "anp", schema_id, template)
300 if display_name is None and not mso.existing:
301 display_name = external_epg
302
303 payload = dict(
304 name=external_epg,
305 displayName=display_name,
306 vrfRef=vrf_ref,
307 preferredGroup=preferred_group,
308 )
309
310 if description is not None:
311 payload.update(description=description)
312
313 if type_ext_epg == "cloud":
314 payload["extEpgType"] = "cloud"
315 payload["anpRef"] = anp_ref
316 else:
317 payload["l3outRef"] = l3out_ref
318
319 if qos_level:
320 payload["qosPriority"] = qos_level
321
322 mso.sanitize(payload, collate=True)
323
324 if mso.existing:
325 # clean anpRef when anpRef is null
326 if "anpRef" in mso.existing and mso.existing.get("anpRef") is None:
327 del mso.existing["anpRef"]
328 # clean contractRef to fix api issue
329 for contract in mso.sent.get("contractRelationships"):
330 contract["contractRef"] = mso.dict_from_ref(contract.get("contractRef"))
331 ops.append(dict(op="replace", path=eepg_path, value=mso.sent))
332 else:
333 ops.append(dict(op="add", path=eepgs_path + "/-", value=mso.sent))
334
335 mso.existing = mso.proposed
336
337 if not module.check_mode and mso.proposed != mso.previous:
338 mso.request(schema_path, method="PATCH", data=ops)
339
340 mso.exit_json()
341
342
343 if __name__ == "__main__":
344 main()