Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_schema_template_l3out.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_l3out
16 short_description: Manage l3outs in schema templates
17 description:
18 - Manage l3outs 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 l3out:
33 description:
34 - The name of the l3out to manage.
35 type: str
36 aliases: [ name ]
37 description:
38 description:
39 - The description of l3out is supported on versions of MSO that are 3.3 or greater.
40 type: str
41 display_name:
42 description:
43 - The name as displayed on the MSO web interface.
44 type: str
45 vrf:
46 description:
47 - The VRF associated to this L3out.
48 type: dict
49 suboptions:
50 name:
51 description:
52 - The name of the VRF to associate with.
53 required: true
54 type: str
55 schema:
56 description:
57 - The schema that defines the referenced VRF.
58 - If this parameter is unspecified, it defaults to the current schema.
59 type: str
60 template:
61 description:
62 - The template that defines the referenced VRF.
63 - If this parameter is unspecified, it defaults to the current schema.
64 type: str
65 state:
66 description:
67 - Use C(present) or C(absent) for adding or removing.
68 - Use C(query) for listing an object or multiple objects.
69 type: str
70 choices: [ absent, present, query ]
71 default: present
72 extends_documentation_fragment: cisco.mso.modules
73 """
74
75 EXAMPLES = r"""
76 - name: Add a new L3out
77 cisco.mso.mso_schema_template_l3out:
78 host: mso_host
79 username: admin
80 password: SomeSecretPassword
81 validate_certs: false
82 schema: Schema 1
83 template: Template 1
84 l3out: L3out 1
85 vrf:
86 name: vrfName
87 schema: vrfSchema
88 template: vrfTemplate
89 state: present
90
91 - name: Remove an L3out
92 cisco.mso.mso_schema_template_l3out:
93 host: mso_host
94 username: admin
95 password: SomeSecretPassword
96 schema: Schema 1
97 template: Template 1
98 l3out: L3out 1
99 state: absent
100
101 - name: Query a specific L3outs
102 cisco.mso.mso_schema_template_l3out:
103 host: mso_host
104 username: admin
105 password: SomeSecretPassword
106 validate_certs: false
107 schema: Schema 1
108 template: Template 1
109 l3out: L3out 1
110 state: query
111 register: query_result
112
113 - name: Query all L3outs
114 cisco.mso.mso_schema_template_l3out:
115 host: mso_host
116 username: admin
117 password: SomeSecretPassword
118 validate_certs: false
119 schema: Schema 1
120 template: Template 1
121 state: query
122 register: query_result
123 """
124
125 RETURN = r"""
126 """
127
128 from ansible.module_utils.basic import AnsibleModule
129 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_reference_spec
130
131
132 def main():
133 argument_spec = mso_argument_spec()
134 argument_spec.update(
135 schema=dict(type="str", required=True),
136 template=dict(type="str", required=True),
137 l3out=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects
138 description=dict(type="str"),
139 display_name=dict(type="str"),
140 vrf=dict(type="dict", options=mso_reference_spec()),
141 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
142 )
143
144 module = AnsibleModule(
145 argument_spec=argument_spec,
146 supports_check_mode=True,
147 required_if=[
148 ["state", "absent", ["l3out"]],
149 ["state", "present", ["l3out", "vrf"]],
150 ],
151 )
152
153 schema = module.params.get("schema")
154 template = module.params.get("template").replace(" ", "")
155 l3out = module.params.get("l3out")
156 description = module.params.get("description")
157 display_name = module.params.get("display_name")
158 vrf = module.params.get("vrf")
159 if vrf is not None and vrf.get("template") is not None:
160 vrf["template"] = vrf.get("template").replace(" ", "")
161 state = module.params.get("state")
162
163 mso = MSOModule(module)
164
165 # Get schema objects
166 schema_id, schema_path, schema_obj = mso.query_schema(schema)
167
168 # Get template
169 templates = [t.get("name") for t in schema_obj.get("templates")]
170 if template not in templates:
171 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates)))
172 template_idx = templates.index(template)
173
174 # Get L3out
175 l3outs = [l3.get("name") for l3 in schema_obj.get("templates")[template_idx]["intersiteL3outs"]]
176
177 if l3out is not None and l3out in l3outs:
178 l3out_idx = l3outs.index(l3out)
179 mso.existing = schema_obj.get("templates")[template_idx]["intersiteL3outs"][l3out_idx]
180
181 if state == "query":
182 if l3out is None:
183 mso.existing = schema_obj.get("templates")[template_idx]["intersiteL3outs"]
184 elif not mso.existing:
185 mso.fail_json(msg="L3out '{l3out}' not found".format(l3out=l3out))
186 mso.exit_json()
187
188 l3outs_path = "/templates/{0}/intersiteL3outs".format(template)
189 l3out_path = "/templates/{0}/intersiteL3outs/{1}".format(template, l3out)
190 ops = []
191
192 mso.previous = mso.existing
193 if state == "absent":
194 if mso.existing:
195 mso.sent = mso.existing = {}
196 ops.append(dict(op="remove", path=l3out_path))
197
198 elif state == "present":
199 vrf_ref = mso.make_reference(vrf, "vrf", schema_id, template)
200
201 if display_name is None and not mso.existing:
202 display_name = l3out
203
204 payload = dict(
205 name=l3out,
206 displayName=display_name,
207 vrfRef=vrf_ref,
208 )
209
210 if description is not None:
211 payload.update(description=description)
212
213 mso.sanitize(payload, collate=True)
214
215 if mso.existing:
216 ops.append(dict(op="replace", path=l3out_path, value=mso.sent))
217 else:
218 ops.append(dict(op="add", path=l3outs_path + "/-", value=mso.sent))
219
220 mso.existing = mso.proposed
221
222 if not module.check_mode:
223 mso.request(schema_path, method="PATCH", data=ops)
224
225 mso.exit_json()
226
227
228 if __name__ == "__main__":
229 main()