Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Sabari Jaganathan (@sajagana) <[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
14 DOCUMENTATION = r"""
15 ---
16 module: mso_schema_site_contract_service_graph
17 short_description: Manage the service graph association with a contract in schema sites
18 description:
19 - Manage the service graph association with a contract in schema sites on Cisco ACI Multi-Site.
20 - This module is only compatible with NDO versions 3.7 and 4.2+. NDO versions 4.0 and 4.1 are not supported.
21 author:
22 - Sabari Jaganathan (@sajagana)
23 options:
24 tenant:
25 description:
26 - The name of the tenant.
27 type: str
28 schema:
29 description:
30 - The name of the schema.
31 type: str
32 required: true
33 template:
34 description:
35 - The name of the template.
36 type: str
37 required: true
38 contract:
39 description:
40 - The name of the contract.
41 type: str
42 site:
43 description:
44 - The name of the site.
45 type: str
46 required: true
47 service_graph_schema:
48 description:
49 - The name of the schema in which the service graph is located.
50 type: str
51 service_graph_template:
52 description:
53 - The name of the template in which the service graph is located.
54 type: str
55 service_graph:
56 description:
57 - The name of the service graph to associate with the site contract.
58 type: str
59 node_relationship:
60 description:
61 - A list of nodes and their connector details associated with the Service Graph.
62 type: list
63 elements: dict
64 suboptions:
65 cluster_interface_device:
66 description:
67 - The name of the cluster interface device.
68 type: str
69 required: true
70 aliases: [ cluster_device, device, device_name ]
71 provider_connector_cluster_interface:
72 description:
73 - The name of the cluster interface for the provider connector.
74 type: str
75 required: true
76 aliases: [ provider_cluster_interface, provider_interface, provider_interface_name ]
77 provider_connector_redirect_policy_tenant:
78 description:
79 - The name of the tenant for the provider connector redirect policy.
80 type: str
81 aliases: [ provider_redirect_policy_tenant, provider_tenant ]
82 provider_connector_redirect_policy:
83 description:
84 - The name of the redirect policy for the provider connector.
85 type: str
86 aliases: [ provider_redirect_policy, provider_policy ]
87 consumer_connector_cluster_interface:
88 description:
89 - The name of the cluster interface for the consumer connector.
90 type: str
91 required: true
92 aliases: [ consumer_cluster_interface, consumer_interface, consumer_interface_name ]
93 consumer_connector_redirect_policy_tenant:
94 description:
95 - The name of the tenant for the consumer connector redirect policy.
96 type: str
97 aliases: [ consumer_redirect_policy_tenant, consumer_tenant ]
98 consumer_connector_redirect_policy:
99 description:
100 - The name of the redirect policy for the consumer connector.
101 type: str
102 aliases: [ consumer_redirect_policy, consumer_policy ]
103 consumer_subnet_ips:
104 description:
105 - The list of subnet IPs for the consumer connector.
106 - The subnet IPs option is only available for the load balancer devices.
107 type: list
108 elements: str
109 state:
110 description:
111 - Use C(present) or C(absent) for adding or removing.
112 - Use C(query) for listing an object or multiple objects.
113 type: str
114 choices: [ absent, present, query ]
115 default: present
116 seealso:
117 - module: cisco.mso.mso_schema_template_contract_service_graph
118 extends_documentation_fragment: cisco.mso.modules
119 """
120
121
122 EXAMPLES = r"""
123 - name: Associate a service graph with a site contract
124 cisco.mso.mso_schema_site_contract_service_graph:
125 host: mso_host
126 username: admin
127 password: SomeSecretPassword
128 tenant: ansible_tenant
129 schema: ansible_schema
130 template: ansible_template1
131 site: ansible_test
132 contract: Contract1
133 service_graph_schema: ansible_schema
134 service_graph_template: ansible_template1
135 service_graph: sg
136 node_relationship:
137 - cluster_interface_device: ansible_tenant_firewall1
138 provider_connector_cluster_interface: clu_if1
139 provider_connector_redirect_policy: redirect_policy1
140 consumer_connector_cluster_interface: clu_if1
141 consumer_connector_redirect_policy: redirect_policy1
142 - cluster_interface_device: ansible_tenant_adc
143 provider_connector_cluster_interface: clu_if3
144 provider_connector_redirect_policy: redirect_policy1
145 consumer_connector_cluster_interface: clu_if3
146 consumer_connector_redirect_policy: redirect_policy1
147 consumer_subnet_ips: ["1.1.1.1/24", "4.4.4.4/24"]
148 - cluster_interface_device: ansible_tenant_other
149 provider_connector_cluster_interface: clu_if4
150 provider_connector_redirect_policy: redirect_policy1
151 consumer_connector_cluster_interface: clu_if4
152 consumer_connector_redirect_policy: redirect_policy1
153 state: present
154
155 - name: Associate a service graph with a cloud site contract
156 cisco.mso.mso_schema_site_contract_service_graph:
157 host: mso_host
158 username: admin
159 password: SomeSecretPassword
160 tenant: ansible_tenant
161 schema: ansible_schema
162 template: ansible_template1
163 site: ansible_test
164 contract: Contract1
165 service_graph_schema: ansible_schema
166 service_graph_template: ansible_template1
167 service_graph: sg
168 state: present
169
170 - name: Query a site contract service graph with contract name
171 cisco.mso.mso_schema_site_contract_service_graph:
172 host: mso_host
173 username: admin
174 password: SomeSecretPassword
175 schema: ansible_schema
176 template: ansible_template1
177 contract: Contract1
178 site: ansible_test
179 state: query
180 register: query_result
181
182 - name: Query all site contract service graphs associated with a site template
183 cisco.mso.mso_schema_site_contract_service_graph:
184 host: mso_host
185 username: admin
186 password: SomeSecretPassword
187 schema: ansible_schema
188 template: ansible_template1
189 site: ansible_test
190 state: query
191 register: query_result
192
193 - name: Remove a site contract service graph with contract name
194 cisco.mso.mso_schema_site_contract_service_graph:
195 host: mso_host
196 username: admin
197 password: SomeSecretPassword
198 schema: ansible_schema
199 template: ansible_template1
200 site: ansible_test
201 contract: Contract1
202 state: absent
203 """
204
205 RETURN = r"""
206 """
207
208
209 from ansible.module_utils.basic import AnsibleModule
210 from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema
211 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_schema_site_contract_service_graph_spec
212
213
214 def main():
215 argument_spec = mso_argument_spec()
216 argument_spec.update(
217 tenant=dict(type="str"),
218 schema=dict(type="str", required=True),
219 template=dict(type="str", required=True),
220 contract=dict(type="str"),
221 site=dict(type="str", required=True),
222 service_graph_schema=dict(type="str"),
223 service_graph_template=dict(type="str"),
224 service_graph=dict(type="str"),
225 node_relationship=dict(type="list", elements="dict", options=mso_schema_site_contract_service_graph_spec()),
226 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
227 )
228
229 module = AnsibleModule(
230 argument_spec=argument_spec,
231 supports_check_mode=True,
232 required_if=[
233 ["state", "absent", ["contract"]],
234 ["state", "present", ["contract", "service_graph"]],
235 ],
236 )
237
238 tenant = module.params.get("tenant")
239 schema = module.params.get("schema")
240 template = module.params.get("template").replace(" ", "")
241 contract = module.params.get("contract")
242 site = module.params.get("site")
243 service_graph_schema = module.params.get("service_graph_schema")
244 service_graph_template = module.params.get("service_graph_template")
245 service_graph = module.params.get("service_graph")
246 node_relationship = module.params.get("node_relationship")
247 state = module.params.get("state")
248
249 mso = MSOModule(module)
250
251 mso_schema = MSOSchema(mso, schema, template)
252 mso_schema.set_template(template)
253
254 service_graph_schema_id = schema_id = mso.lookup_schema(schema, True)
255 service_graph_reference_schema = service_graph_schema or schema
256
257 # Get service graph reference schema id if the service graph schema is not matching with the schema
258 if service_graph_reference_schema != schema:
259 service_graph_schema_id = mso.lookup_schema(service_graph_reference_schema, True)
260 if service_graph_schema_id is None:
261 mso.fail_json(msg="Provided service_graph_schema: '{0}' does not exist.".format(service_graph_schema))
262
263 service_graph_reference_schema_id = service_graph_schema_id or schema_id
264 service_graph_reference_template = service_graph_template or template
265
266 # Get site id
267 site_id = mso.lookup_site(site)
268 mso_schema.set_site(template, site)
269 mso_schema.set_site_contract(contract, False)
270
271 if mso.site_type == "on-premise" and state == "present":
272 # When site type is on-premise, both node_relationship and tenant are required
273 if node_relationship is None and tenant is None:
274 mso.fail_json(msg="The node_relationship and tenant attributes are required when state is present and site type is on-premise.")
275 elif node_relationship is None:
276 mso.fail_json(msg="The node_relationship attribute is required when state is present and site type is on-premise.")
277 elif tenant is None:
278 mso.fail_json(msg="The tenant attribute is required when state is present and site type is on-premise.")
279
280 if contract and mso_schema.schema_objects["site_contract"] is not None:
281 site_contract_service_graph = mso_schema.schema_objects["site_contract"].details.get("serviceGraphRelationship")
282 if site_contract_service_graph and service_graph:
283 site_contract_service_graph_name = site_contract_service_graph.get("serviceGraphRef").split("/")[-1]
284 if site_contract_service_graph_name == service_graph:
285 mso.existing = site_contract_service_graph
286 else:
287 mso.fail_json(msg="The service graph: {0} does not associated with the site contract: {1}.".format(service_graph, contract))
288 elif site_contract_service_graph and service_graph is None:
289 mso.existing = site_contract_service_graph
290 elif contract is not None and mso_schema.schema_objects["site_contract"] is None:
291 mso.fail_json(msg="The site contract: {0} does not exist.".format(contract))
292 elif contract is None and mso_schema.schema_objects["site"].details.get("contracts"):
293 mso.existing = [
294 contract.get("serviceGraphRelationship")
295 for contract in mso_schema.schema_objects["site"].details.get("contracts")
296 if contract.get("serviceGraphRelationship")
297 ]
298
299 if state == "query":
300 mso.exit_json()
301
302 site_contract_service_graph_path = "/sites/{0}-{1}/contracts/{2}/serviceGraphRelationship".format(site_id, service_graph_reference_template, contract)
303
304 ops = []
305 mso.previous = mso.existing
306
307 if state == "absent":
308 mso.existing = {}
309 ops.append(dict(op="remove", path=site_contract_service_graph_path))
310 elif state == "present":
311 service_graph_ref = dict(schemaId=service_graph_reference_schema_id, serviceGraphName=service_graph, templateName=service_graph_reference_template)
312 service_node_relationship = []
313 if mso.site_type == "on-premise" and node_relationship:
314 for node_index, node in enumerate(node_relationship, 1):
315 service_node_ref = dict(
316 schemaId=service_graph_reference_schema_id,
317 serviceGraphName=service_graph,
318 serviceNodeName="node{0}".format(node_index),
319 templateName=service_graph_reference_template,
320 )
321
322 consumer_subnet_ips_list = [dict(ip=subnet) for subnet in node.get("consumer_subnet_ips")] if node.get("consumer_subnet_ips") else []
323 consumer_connector = dict(
324 clusterInterface=dict(
325 dn="uni/tn-{0}/lDevVip-{1}/lIf-{2}".format(
326 tenant, node.get("cluster_interface_device"), node.get("consumer_connector_cluster_interface")
327 )
328 ),
329 redirectPolicy=dict(
330 dn="uni/tn-{0}/svcCont/svcRedirectPol-{1}".format(
331 node.get("consumer_connector_redirect_policy_tenant") or tenant, node.get("consumer_connector_redirect_policy")
332 )
333 ),
334 subnets=consumer_subnet_ips_list,
335 )
336 provider_connector = dict(
337 clusterInterface=dict(
338 dn="uni/tn-{0}/lDevVip-{1}/lIf-{2}".format(
339 tenant, node.get("cluster_interface_device"), node.get("provider_connector_cluster_interface")
340 )
341 ),
342 redirectPolicy=dict(
343 dn="uni/tn-{0}/svcCont/svcRedirectPol-{1}".format(
344 node.get("provider_connector_redirect_policy_tenant") or tenant, node.get("provider_connector_redirect_policy")
345 )
346 ),
347 )
348 service_node_relationship.append(
349 dict(consumerConnector=consumer_connector, providerConnector=provider_connector, serviceNodeRef=service_node_ref)
350 )
351
352 payload = dict(serviceGraphRef=service_graph_ref, serviceNodesRelationship=service_node_relationship)
353 elif mso.cloud_provider_type == "azure":
354 mso_schema.set_site_service_graph(service_graph)
355 service_nodes = mso_schema.schema_objects["site_service_graph"].details.get("serviceNodes", [])
356 payload = dict(
357 serviceGraphRef=service_graph_ref, serviceNodesRelationship=[dict(serviceNodeRef=sg_node.get("serviceNodeRef")) for sg_node in service_nodes]
358 )
359
360 mso.sanitize(payload, collate=True)
361
362 if mso.existing:
363 ops.append(dict(op="replace", path=site_contract_service_graph_path, value=mso.sent))
364 else:
365 ops.append(dict(op="add", path=site_contract_service_graph_path, value=mso.sent))
366
367 mso.existing = mso.proposed
368
369 if not module.check_mode:
370 mso.request(mso_schema.path, method="PATCH", data=ops)
371
372 mso.exit_json()
373
374
375 if __name__ == "__main__":
376 main()