Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_epg_to_contract_master.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, Cindy Zhao <[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": "certified"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_epg_to_contract_master
16 short_description: Manage End Point Group (EPG) contract master relationships (fv:RsSecInherited)
17 description:
18 - Manage End Point Groups (EPG) contract master relationships on Cisco ACI fabrics.
19 options:
20 tenant:
21 description:
22 - Name of an existing tenant.
23 type: str
24 aliases: [ tenant_name ]
25 required: true
26 ap:
27 description:
28 - Name of an existing application network profile, that will contain the EPGs.
29 type: str
30 required: true
31 aliases: [ app_profile, app_profile_name ]
32 epg:
33 description:
34 - Name of the end point group.
35 type: str
36 required: true
37 aliases: [ epg_name, name ]
38 contract_master_ap:
39 description:
40 - Name of the application profile where the contract master EPG is.
41 type: str
42 contract_master_epg:
43 description:
44 - Name of the end point group which serves as contract master.
45 type: str
46 state:
47 description:
48 - Use C(present) or C(absent) for adding or removing.
49 - Use C(query) for listing an object or multiple objects.
50 type: str
51 choices: [ absent, present, query ]
52 default: present
53 extends_documentation_fragment:
54 - cisco.aci.aci
55 - cisco.aci.annotation
56
57 notes:
58 - The C(tenant) and C(app_profile) used must exist before using this module in your playbook.
59 The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_ap) modules can be used for this.
60 seealso:
61 - module: cisco.aci.aci_epg
62 - name: APIC Management Information Model reference
63 description: More information about the internal APIC class B(fv:AEPg).
64 link: https://developer.cisco.com/docs/apic-mim-ref/
65 author:
66 - Cindy Zhao (@cizhao)
67 """
68
69 EXAMPLES = r"""
70 - name: Add contract master
71 cisco.aci.aci_epg_to_contract_master:
72 host: apic_host
73 username: admin
74 password: SomeSecretPassword
75 tenant: anstest
76 ap: apName
77 epg: epgName
78 contract_master_ap: ap
79 contract_master_epg: epg
80 state: present
81 delegate_to: localhost
82
83 - name: Remove contract master
84 cisco.aci.aci_epg_to_contract_master:
85 host: apic_host
86 username: admin
87 password: SomeSecretPassword
88 tenant: anstest
89 ap: apName
90 epg: epgName
91 contract_master_ap: ap
92 contract_master_epg: epg
93 state: absent
94 delegate_to: localhost
95
96 - name: Query contract master
97 cisco.aci.aci_epg_to_contract_master:
98 host: apic_host
99 username: admin
100 password: SomeSecretPassword
101 tenant: anstest
102 ap: apName
103 epg: epgName
104 contract_master_ap: ap
105 contract_master_epg: epg
106 state: query
107 delegate_to: localhost
108 """
109
110 RETURN = r"""
111 current:
112 description: The existing configuration from the APIC after the module has finished
113 returned: success
114 type: list
115 sample:
116 [
117 {
118 "fvTenant": {
119 "attributes": {
120 "descr": "Production environment",
121 "dn": "uni/tn-production",
122 "name": "production",
123 "nameAlias": "",
124 "ownerKey": "",
125 "ownerTag": ""
126 }
127 }
128 }
129 ]
130 error:
131 description: The error information as returned from the APIC
132 returned: failure
133 type: dict
134 sample:
135 {
136 "code": "122",
137 "text": "unknown managed object class foo"
138 }
139 raw:
140 description: The raw output returned by the APIC REST API (xml or json)
141 returned: parse error
142 type: str
143 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
144 sent:
145 description: The actual/minimal configuration pushed to the APIC
146 returned: info
147 type: list
148 sample:
149 {
150 "fvTenant": {
151 "attributes": {
152 "descr": "Production environment"
153 }
154 }
155 }
156 previous:
157 description: The original configuration from the APIC before the module has started
158 returned: info
159 type: list
160 sample:
161 [
162 {
163 "fvTenant": {
164 "attributes": {
165 "descr": "Production",
166 "dn": "uni/tn-production",
167 "name": "production",
168 "nameAlias": "",
169 "ownerKey": "",
170 "ownerTag": ""
171 }
172 }
173 }
174 ]
175 proposed:
176 description: The assembled configuration from the user-provided parameters
177 returned: info
178 type: dict
179 sample:
180 {
181 "fvTenant": {
182 "attributes": {
183 "descr": "Production environment",
184 "name": "production"
185 }
186 }
187 }
188 filter_string:
189 description: The filter string used for the request
190 returned: failure or debug
191 type: str
192 sample: ?rsp-prop-include=config-only
193 method:
194 description: The HTTP method used for the request to the APIC
195 returned: failure or debug
196 type: str
197 sample: POST
198 response:
199 description: The HTTP response from the APIC
200 returned: failure or debug
201 type: str
202 sample: OK (30 bytes)
203 status:
204 description: The HTTP status from the APIC
205 returned: failure or debug
206 type: int
207 sample: 200
208 url:
209 description: The HTTP url used for the request to the APIC
210 returned: failure or debug
211 type: str
212 sample: https://10.11.12.13/api/mo/uni/tn-production.json
213 """
214
215 from ansible.module_utils.basic import AnsibleModule
216 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
217
218
219 def main():
220 argument_spec = aci_argument_spec()
221 argument_spec.update(aci_annotation_spec())
222 argument_spec.update(
223 tenant=dict(type="str", aliases=["tenant_name"], required=True),
224 ap=dict(type="str", aliases=["app_profile", "app_profile_name"], required=True),
225 epg=dict(type="str", aliases=["epg_name", "name"], required=True),
226 contract_master_ap=dict(type="str"),
227 contract_master_epg=dict(type="str"),
228 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
229 )
230
231 module = AnsibleModule(
232 argument_spec=argument_spec,
233 supports_check_mode=True,
234 required_if=[
235 ["state", "absent", ["contract_master_ap", "contract_master_epg"]],
236 ["state", "present", ["contract_master_ap", "contract_master_epg"]],
237 ],
238 )
239
240 aci = ACIModule(module)
241
242 tenant = module.params.get("tenant")
243 ap = module.params.get("ap")
244 epg = module.params.get("epg")
245 contract_master_ap = module.params.get("contract_master_ap")
246 contract_master_epg = module.params.get("contract_master_epg")
247 state = module.params.get("state")
248
249 contract_master = "uni/tn-{0}/ap-{1}/epg-{2}".format(tenant, contract_master_ap, contract_master_epg)
250
251 child_configs = []
252
253 aci.construct_url(
254 root_class=dict(
255 aci_class="fvTenant",
256 aci_rn="tn-{0}".format(tenant),
257 module_object=tenant,
258 target_filter={"name": tenant},
259 ),
260 subclass_1=dict(
261 aci_class="fvAp",
262 aci_rn="ap-{0}".format(ap),
263 module_object=ap,
264 target_filter={"name": ap},
265 ),
266 subclass_2=dict(
267 aci_class="fvAEPg",
268 aci_rn="epg-{0}".format(epg),
269 module_object=epg,
270 target_filter={"name": epg},
271 ),
272 subclass_3=dict(
273 aci_class="fvRsSecInherited",
274 aci_rn="rssecInherited-[{0}]".format(contract_master),
275 module_object=contract_master,
276 target_filter={"tDn": contract_master},
277 ),
278 child_classes=[],
279 )
280
281 aci.get_existing()
282
283 if state == "present":
284 aci.payload(aci_class="fvRsSecInherited", class_config=dict(tDn=contract_master), child_configs=child_configs)
285
286 aci.get_diff(aci_class="fvRsSecInherited")
287
288 aci.post_config()
289
290 elif state == "absent":
291 aci.delete_config()
292
293 aci.exit_json()
294
295
296 if __name__ == "__main__":
297 main()