Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see LICENSE 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": "certified"}
11
12 DOCUMENTATION = r"""
13 ---
14 module: aci_tenant
15 short_description: Manage tenants (fv:Tenant)
16 description:
17 - Manage tenants on Cisco ACI fabrics.
18 options:
19 tenant:
20 description:
21 - The name of the tenant.
22 type: str
23 aliases: [ name, tenant_name ]
24 description:
25 description:
26 - Description for the tenant.
27 type: str
28 aliases: [ descr ]
29 state:
30 description:
31 - Use C(present) or C(absent) for adding or removing.
32 - Use C(query) for listing an object or multiple objects.
33 type: str
34 choices: [ absent, present, query ]
35 default: present
36 name_alias:
37 description:
38 - The alias for the current object. This relates to the nameAlias field in ACI.
39 type: str
40 extends_documentation_fragment:
41 - cisco.aci.aci
42 - cisco.aci.annotation
43 - cisco.aci.owner
44
45 seealso:
46 - module: cisco.aci.aci_ap
47 - module: cisco.aci.aci_bd
48 - module: cisco.aci.aci_contract
49 - module: cisco.aci.aci_filter
50 - module: cisco.aci.aci_vrf
51 - name: APIC Management Information Model reference
52 description: More information about the internal APIC class B(fv:Tenant).
53 link: https://developer.cisco.com/docs/apic-mim-ref/
54 author:
55 - Jacob McGill (@jmcgill298)
56 """
57
58 EXAMPLES = r"""
59 - name: Add a new tenant
60 cisco.aci.aci_tenant:
61 host: apic
62 username: admin
63 password: SomeSecretPassword
64 tenant: production
65 description: Production tenant
66 state: present
67 delegate_to: localhost
68
69 - name: Remove a tenant
70 cisco.aci.aci_tenant:
71 host: apic
72 username: admin
73 password: SomeSecretPassword
74 tenant: production
75 state: absent
76 delegate_to: localhost
77
78 - name: Query a tenant
79 cisco.aci.aci_tenant:
80 host: apic
81 username: admin
82 password: SomeSecretPassword
83 tenant: production
84 state: query
85 delegate_to: localhost
86 register: query_result
87
88 - name: Query all tenants
89 cisco.aci.aci_tenant:
90 host: apic
91 username: admin
92 password: SomeSecretPassword
93 state: query
94 delegate_to: localhost
95 register: query_result
96 """
97
98 RETURN = r"""
99 current:
100 description: The existing configuration from the APIC after the module has finished
101 returned: success
102 type: list
103 sample:
104 [
105 {
106 "fvTenant": {
107 "attributes": {
108 "descr": "Production environment",
109 "dn": "uni/tn-production",
110 "name": "production",
111 "nameAlias": "",
112 "ownerKey": "",
113 "ownerTag": ""
114 }
115 }
116 }
117 ]
118 error:
119 description: The error information as returned from the APIC
120 returned: failure
121 type: dict
122 sample:
123 {
124 "code": "122",
125 "text": "unknown managed object class foo"
126 }
127 raw:
128 description: The raw output returned by the APIC REST API (xml or json)
129 returned: parse error
130 type: str
131 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
132 sent:
133 description: The actual/minimal configuration pushed to the APIC
134 returned: info
135 type: list
136 sample:
137 {
138 "fvTenant": {
139 "attributes": {
140 "descr": "Production environment"
141 }
142 }
143 }
144 previous:
145 description: The original configuration from the APIC before the module has started
146 returned: info
147 type: list
148 sample:
149 [
150 {
151 "fvTenant": {
152 "attributes": {
153 "descr": "Production",
154 "dn": "uni/tn-production",
155 "name": "production",
156 "nameAlias": "",
157 "ownerKey": "",
158 "ownerTag": ""
159 }
160 }
161 }
162 ]
163 proposed:
164 description: The assembled configuration from the user-provided parameters
165 returned: info
166 type: dict
167 sample:
168 {
169 "fvTenant": {
170 "attributes": {
171 "descr": "Production environment",
172 "name": "production"
173 }
174 }
175 }
176 filter_string:
177 description: The filter string used for the request
178 returned: failure or debug
179 type: str
180 sample: ?rsp-prop-include=config-only
181 method:
182 description: The HTTP method used for the request to the APIC
183 returned: failure or debug
184 type: str
185 sample: POST
186 response:
187 description: The HTTP response from the APIC
188 returned: failure or debug
189 type: str
190 sample: OK (30 bytes)
191 status:
192 description: The HTTP status from the APIC
193 returned: failure or debug
194 type: int
195 sample: 200
196 url:
197 description: The HTTP url used for the request to the APIC
198 returned: failure or debug
199 type: str
200 sample: https://10.11.12.13/api/mo/uni/tn-production.json
201 """
202
203 from ansible.module_utils.basic import AnsibleModule
204 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
205
206
207 def main():
208 argument_spec = aci_argument_spec()
209 argument_spec.update(aci_annotation_spec())
210 argument_spec.update(aci_owner_spec())
211 argument_spec.update(
212 tenant=dict(type="str", aliases=["name", "tenant_name"]), # Not required for querying all objects
213 description=dict(type="str", aliases=["descr"]),
214 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
215 name_alias=dict(type="str"),
216 )
217
218 module = AnsibleModule(
219 argument_spec=argument_spec,
220 supports_check_mode=True,
221 required_if=[
222 ["state", "absent", ["tenant"]],
223 ["state", "present", ["tenant"]],
224 ],
225 )
226
227 description = module.params.get("description")
228 state = module.params.get("state")
229 tenant = module.params.get("tenant")
230 name_alias = module.params.get("name_alias")
231
232 aci = ACIModule(module)
233 aci.construct_url(
234 root_class=dict(
235 aci_class="fvTenant",
236 aci_rn="tn-{0}".format(tenant),
237 module_object=tenant,
238 target_filter={"name": tenant},
239 ),
240 )
241 aci.get_existing()
242
243 if state == "present":
244 aci.payload(
245 aci_class="fvTenant",
246 class_config=dict(
247 name=tenant,
248 descr=description,
249 nameAlias=name_alias,
250 ),
251 )
252
253 aci.get_diff(aci_class="fvTenant")
254
255 aci.post_config()
256
257 elif state == "absent":
258 aci.delete_config()
259
260 aci.exit_json()
261
262
263 if __name__ == "__main__":
264 main()