ansible  2.9.27
About: Ansible is an IT Configuration Management, Deployment \
About: Ansible (2.x) is an IT Configuration Management, Deployment & Orchestration tool.
ansible download page.
  Fossies Dox: ansible-2.9.27.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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