Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
11
12 DOCUMENTATION = r"""
13 ---
14 module: mso_schema_template_vrf_contract
15 short_description: Manage vrf contracts in schema templates
16 description:
17 - Manage vrf contracts in schema templates on Cisco ACI Multi-Site.
18 author:
19 - Cindy Zhao (@cizhao)
20 version_added: '0.0.8'
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 to change.
30 type: str
31 required: true
32 vrf:
33 description:
34 - The name of the VRF.
35 type: str
36 required: true
37 contract:
38 description:
39 - A contract associated to this VRF.
40 type: dict
41 suboptions:
42 name:
43 description:
44 - The name of the Contract to associate with.
45 required: true
46 type: str
47 schema:
48 description:
49 - The schema that defines the referenced contract.
50 - If this parameter is unspecified, it defaults to the current schema.
51 type: str
52 template:
53 description:
54 - The template that defines the referenced contract.
55 type: str
56 type:
57 description:
58 - The type of contract.
59 type: str
60 required: true
61 choices: [ consumer, provider ]
62 state:
63 description:
64 - Use C(present) or C(absent) for adding or removing.
65 - Use C(query) for listing an object or multiple objects.
66 type: str
67 choices: [ absent, present, query ]
68 default: present
69 seealso:
70 - module: cisco.mso.mso_schema_template_vrf
71 extends_documentation_fragment: cisco.mso.modules
72 """
73
74 EXAMPLES = r"""
75 - name: Add a contract to a VRF
76 cisco.mso.mso_schema_template_vrf_contract:
77 host: mso_host
78 username: admin
79 password: SomeSecretPassword
80 schema: Schema 1
81 template: Template 1
82 vrf: VRF 1
83 contract:
84 name: Contract 1
85 type: consumer
86 state: present
87
88 - name: Remove a Contract
89 cisco.mso.mso_schema_template_vrf_contract:
90 host: mso_host
91 username: admin
92 password: SomeSecretPassword
93 schema: Schema 1
94 template: Template 1
95 vrf: VRF 1
96 contract:
97 name: Contract 1
98 type: consumer
99 state: absent
100
101 - name: Query a specific Contract
102 cisco.mso.mso_schema_template_vrf_contract:
103 host: mso_host
104 username: admin
105 password: SomeSecretPassword
106 schema: Schema 1
107 template: Template 1
108 vrf: VRF 1
109 contract:
110 name: Contract 1
111 type: consumer
112 state: query
113 register: query_result
114
115 - name: Query all Contracts
116 cisco.mso.mso_schema_template_vrf_contract:
117 host: mso_host
118 username: admin
119 password: SomeSecretPassword
120 schema: Schema 1
121 template: Template 1
122 vrf: VRF 1
123 state: query
124 register: query_result
125 """
126
127 RETURN = r"""
128 """
129
130 from ansible.module_utils.basic import AnsibleModule
131 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_contractref_spec
132
133
134 def main():
135 argument_spec = mso_argument_spec()
136 argument_spec.update(
137 schema=dict(type="str", required=True),
138 template=dict(type="str", required=True),
139 vrf=dict(type="str", required=True),
140 contract=dict(type="dict", options=mso_contractref_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", ["contract"]],
149 ["state", "present", ["contract"]],
150 ],
151 )
152
153 schema = module.params.get("schema")
154 template = module.params.get("template").replace(" ", "")
155 vrf = module.params.get("vrf")
156 contract = module.params.get("contract")
157 if contract is not None and contract.get("template") is not None:
158 contract["template"] = contract.get("template").replace(" ", "")
159 state = module.params.get("state")
160
161 mso = MSOModule(module)
162 if contract:
163 if contract.get("schema") is None:
164 contract["schema"] = schema
165 contract["schema_id"] = mso.lookup_schema(contract.get("schema"))
166 if contract.get("template") is None:
167 contract["template"] = template
168
169 # Get schema
170 schema_id, schema_path, schema_obj = mso.query_schema(schema)
171
172 # Get template
173 templates = [t.get("name") for t in schema_obj.get("templates")]
174 if template not in templates:
175 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates)))
176 template_idx = templates.index(template)
177
178 # Get VRF
179 vrfs = [e.get("name") for e in schema_obj.get("templates")[template_idx]["vrfs"]]
180 if vrf not in vrfs:
181 mso.fail_json(msg="Provided vrf '{vrf}' does not exist. Existing vrfs: {vrfs}".format(vrf=vrf, vrfs=", ".join(vrfs)))
182 vrf_idx = vrfs.index(vrf)
183 vrf_obj = schema_obj.get("templates")[template_idx]["vrfs"][vrf_idx]
184
185 if not vrf_obj.get("vzAnyEnabled"):
186 mso.fail_json(msg="vzAny attribute on vrf '{0}' is disabled.".format(vrf))
187
188 # Get Contract
189 contract_path = None
190 if contract:
191 provider_contracts = [c.get("contractRef") for c in schema_obj.get("templates")[template_idx]["vrfs"][vrf_idx]["vzAnyProviderContracts"]]
192 consumer_contracts = [c.get("contractRef") for c in schema_obj.get("templates")[template_idx]["vrfs"][vrf_idx]["vzAnyConsumerContracts"]]
193 contract_ref = mso.contract_ref(**contract)
194 if contract_ref in provider_contracts and contract.get("type") == "provider":
195 contract_idx = provider_contracts.index(contract_ref)
196 contract_path = "/templates/{0}/vrfs/{1}/vzAnyProviderContracts/{2}".format(template, vrf, contract_idx)
197 mso.existing = schema_obj.get("templates")[template_idx]["vrfs"][vrf_idx]["vzAnyProviderContracts"][contract_idx]
198 if contract_ref in consumer_contracts and contract.get("type") == "consumer":
199 contract_idx = consumer_contracts.index(contract_ref)
200 contract_path = "/templates/{0}/vrfs/{1}/vzAnyConsumerContracts/{2}".format(template, vrf, contract_idx)
201 mso.existing = schema_obj.get("templates")[template_idx]["vrfs"][vrf_idx]["vzAnyConsumerContracts"][contract_idx]
202 if mso.existing.get("contractRef"):
203 mso.existing["contractRef"] = mso.dict_from_ref(mso.existing.get("contractRef"))
204 mso.existing["relationshipType"] = contract.get("type")
205
206 if state == "query":
207 if not contract:
208 provider_contracts = [
209 dict(contractRef=mso.dict_from_ref(c.get("contractRef")), relationshipType="provider")
210 for c in schema_obj.get("templates")[template_idx]["vrfs"][vrf_idx]["vzAnyProviderContracts"]
211 ]
212 consumer_contracts = [
213 dict(contractRef=mso.dict_from_ref(c.get("contractRef")), relationshipType="consumer")
214 for c in schema_obj.get("templates")[template_idx]["vrfs"][vrf_idx]["vzAnyConsumerContracts"]
215 ]
216 mso.existing = provider_contracts + consumer_contracts
217 elif not mso.existing:
218 mso.fail_json(msg="Contract '{0}' not found".format(contract.get("name")))
219
220 mso.exit_json()
221
222 if contract.get("type") == "provider":
223 contracts_path = "/templates/{0}/vrfs/{1}/vzAnyProviderContracts/-".format(template, vrf)
224 if contract.get("type") == "consumer":
225 contracts_path = "/templates/{0}/vrfs/{1}/vzAnyConsumerContracts/-".format(template, vrf)
226 ops = []
227
228 mso.previous = mso.existing
229 if state == "absent":
230 if mso.existing and contract_path:
231 mso.sent = mso.existing = {}
232 ops.append(dict(op="remove", path=contract_path))
233
234 elif state == "present":
235 payload = dict(
236 contractRef=dict(
237 contractName=contract.get("name"),
238 templateName=contract.get("template"),
239 schemaId=contract.get("schema_id"),
240 ),
241 )
242
243 mso.sanitize(payload, collate=True)
244
245 if mso.existing and contract_path:
246 ops.append(dict(op="replace", path=contract_path, value=mso.sent))
247 else:
248 ops.append(dict(op="add", path=contracts_path, value=mso.sent))
249
250 mso.existing = mso.proposed
251 mso.existing["relationshipType"] = contract.get("type")
252
253 if not module.check_mode and mso.proposed != mso.previous:
254 mso.request(schema_path, method="PATCH", data=ops)
255
256 mso.exit_json()
257
258
259 if __name__ == "__main__":
260 main()