Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_schema_template_external_epg_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_external_epg_contract
15 short_description: Manage Extrnal EPG contracts in schema templates
16 description:
17 - Manage External EPG contracts in schema templates on Cisco ACI Multi-Site.
18 author:
19 - Devarshi Shah (@devarshishah3)
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 external_epg:
33 description:
34 - The name of the EPG to manage.
35 type: str
36 required: true
37 contract:
38 description:
39 - A contract associated to this EPG.
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 BD.
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 BD.
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_external_epg
71 - module: cisco.mso.mso_schema_template_contract_filter
72 extends_documentation_fragment: cisco.mso.modules
73 """
74
75 EXAMPLES = r"""
76 - name: Add a contract to an EPG
77 cisco.mso.mso_schema_template_external_epg_contract:
78 host: mso_host
79 username: admin
80 password: SomeSecretPassword
81 schema: Schema 1
82 template: Template 1
83 epg: EPG 1
84 contract:
85 name: Contract 1
86 type: consumer
87 state: present
88
89 - name: Remove a Contract
90 cisco.mso.mso_schema_template_external_epg_contract:
91 host: mso_host
92 username: admin
93 password: SomeSecretPassword
94 schema: Schema 1
95 template: Template 1
96 epg: EPG 1
97 contract:
98 name: Contract 1
99 state: absent
100
101 - name: Query a specific Contract
102 cisco.mso.mso_schema_template_external_epg_contract:
103 host: mso_host
104 username: admin
105 password: SomeSecretPassword
106 schema: Schema 1
107 template: Template 1
108 epg: EPG 1
109 contract:
110 name: Contract 1
111 state: query
112 register: query_result
113
114 - name: Query all Contracts
115 cisco.mso.mso_schema_template_external_epg_contract:
116 host: mso_host
117 username: admin
118 password: SomeSecretPassword
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_contractref_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 external_epg=dict(type="str", required=True),
138 contract=dict(type="dict", options=mso_contractref_spec()),
139 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
140 )
141
142 module = AnsibleModule(
143 argument_spec=argument_spec,
144 supports_check_mode=True,
145 required_if=[
146 ["state", "absent", ["contract"]],
147 ["state", "present", ["contract"]],
148 ],
149 )
150
151 schema = module.params.get("schema")
152 template = module.params.get("template").replace(" ", "")
153 external_epg = module.params.get("external_epg")
154 contract = module.params.get("contract")
155 if contract is not None and contract.get("template") is not None:
156 contract["template"] = contract.get("template").replace(" ", "")
157 state = module.params.get("state")
158
159 mso = MSOModule(module)
160
161 if contract:
162 if contract.get("schema") is None:
163 contract["schema"] = schema
164 contract["schema_id"] = mso.lookup_schema(contract.get("schema"))
165 if contract.get("template") is None:
166 contract["template"] = template
167
168 # Get schema
169 schema_id, schema_path, schema_obj = mso.query_schema(schema)
170
171 # Get template
172 templates = [t.get("name") for t in schema_obj.get("templates")]
173 if template not in templates:
174 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates)))
175 template_idx = templates.index(template)
176
177 # Get EPG
178 epgs = [e.get("name") for e in schema_obj.get("templates")[template_idx]["externalEpgs"]]
179 if external_epg not in epgs:
180 mso.fail_json(msg="Provided epg '{epg}' does not exist. Existing epgs: {epgs}".format(epg=external_epg, epgs=", ".join(epgs)))
181 epg_idx = epgs.index(external_epg)
182
183 # Get Contract
184 contract_path = None
185 if contract:
186 contracts = [
187 (c.get("contractRef"), c.get("relationshipType"))
188 for c in schema_obj.get("templates")[template_idx]["externalEpgs"][epg_idx]["contractRelationships"]
189 ]
190 contract_ref = mso.contract_ref(**contract)
191 if (contract_ref, contract.get("type")) in contracts:
192 contract_idx = contracts.index((contract_ref, contract.get("type")))
193 contract_path = "/templates/{0}/externalEpgs/{1}/contractRelationships/{2}".format(template, external_epg, contract_idx)
194 mso.existing = schema_obj.get("templates")[template_idx]["externalEpgs"][epg_idx]["contractRelationships"][contract_idx]
195
196 if state == "query":
197 if not contract:
198 mso.existing = schema_obj.get("templates")[template_idx]["externalEpgs"][epg_idx]["contractRelationships"]
199 elif not mso.existing:
200 mso.fail_json(msg="Contract '{0}' not found".format(contract_ref))
201
202 if "contractRef" in mso.existing:
203 mso.existing["contractRef"] = mso.dict_from_ref(mso.existing.get("contractRef"))
204 mso.exit_json()
205
206 contracts_path = "/templates/{0}/externalEpgs/{1}/contractRelationships".format(template, external_epg)
207 ops = []
208
209 mso.previous = mso.existing
210 if state == "absent":
211 if mso.existing and contract_path:
212 mso.sent = mso.existing = {}
213 ops.append(dict(op="remove", path=contract_path))
214
215 elif state == "present":
216 payload = dict(
217 relationshipType=contract.get("type"),
218 contractRef=dict(
219 contractName=contract.get("name"),
220 templateName=contract.get("template"),
221 schemaId=contract.get("schema_id"),
222 ),
223 )
224
225 mso.sanitize(payload, collate=True)
226
227 if mso.existing and contract_path:
228 ops.append(dict(op="replace", path=contract_path, value=mso.sent))
229 else:
230 ops.append(dict(op="add", path=contracts_path + "/-", value=mso.sent))
231
232 mso.existing = mso.proposed
233
234 if "contractRef" in mso.previous:
235 mso.previous["contractRef"] = mso.dict_from_ref(mso.previous.get("contractRef"))
236
237 if not module.check_mode and mso.proposed != mso.previous:
238 mso.request(schema_path, method="PATCH", data=ops)
239
240 mso.exit_json()
241
242
243 if __name__ == "__main__":
244 main()