Debian Codegraph @

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

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