Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_tenant.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Dag Wieers (@dagwieers) <[email protected]>
5 # Copyright: (c) 2020, Cindy Zhao (@cizhao) <[email protected]>
6 # Copyright: (c) 2023, Anvitha Jain (@anvjain) <[email protected]>
7 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
14
15 DOCUMENTATION = r"""
16 ---
17 module: mso_tenant
18 short_description: Manage tenants
19 description:
20 - Manage tenants on Cisco ACI Multi-Site.
21 author:
22 - Dag Wieers (@dagwieers)
23 options:
24 tenant:
25 description:
26 - The name of the tenant.
27 type: str
28 aliases: [ name ]
29 display_name:
30 description:
31 - The name of the tenant to be displayed in the web UI.
32 type: str
33 description:
34 description:
35 - The description for this tenant.
36 type: str
37 users:
38 description:
39 - A list of associated users for this tenant.
40 - Using this property will replace any existing associated users.
41 - Admin user is always added to the associated user list irrespective of this parameter being used.
42 type: list
43 elements: str
44 remote_users:
45 description:
46 - A list of associated remote users for this tenant.
47 type: list
48 elements: dict
49 suboptions:
50 name:
51 description:
52 - The name of the associated remote user for this tenant.
53 required: true
54 type: str
55 login_domain:
56 description:
57 - Domain name of the associated remote user for this tenant.
58 required: true
59 type: str
60 sites:
61 description:
62 - A list of associated sites for this tenant.
63 - Using this property will replace any existing associated sites.
64 type: list
65 elements: str
66 orchestrator_only:
67 description:
68 - Orchestrator Only C(no) is used to delete the tenant from the MSO and Sites/APIC.
69 - C(yes) is used to remove the tenant only from the MSO.
70 type: str
71 choices: [ 'yes', 'no' ]
72 default: 'yes'
73 state:
74 description:
75 - Use C(present) or C(absent) for adding or removing.
76 - Use C(query) for listing an object or multiple objects.
77 type: str
78 choices: [ absent, present, query ]
79 default: present
80 extends_documentation_fragment: cisco.mso.modules
81 """
82
83 EXAMPLES = r"""
84 - name: Add a new tenant
85 cisco.mso.mso_tenant:
86 host: mso_host
87 username: admin
88 password: SomeSecretPassword
89 tenant: north_europe
90 display_name: North European Datacenter
91 description: This tenant manages the NEDC environment.
92 state: present
93
94 - name: Remove a tenant from MSO and Site/APIC
95 cisco.mso.mso_tenant:
96 host: mso_host
97 username: admin
98 password: SomeSecretPassword
99 tenant: north_europe
100 orchestrator_only: 'no'
101 state: absent
102
103 - name: Remove a tenant from MSO only
104 cisco.mso.mso_tenant:
105 host: mso_host
106 username: admin
107 password: SomeSecretPassword
108 tenant: north_europe
109 orchestrator_only: 'yes'
110 state: absent
111
112 - name: Query a tenant
113 cisco.mso.mso_tenant:
114 host: mso_host
115 username: admin
116 password: SomeSecretPassword
117 tenant: north_europe
118 state: query
119 register: query_result
120
121 - name: Query all tenants
122 cisco.mso.mso_tenant:
123 host: mso_host
124 username: admin
125 password: SomeSecretPassword
126 state: query
127 register: query_result
128 """
129
130 RETURN = r"""
131 """
132
133 from ansible.module_utils.basic import AnsibleModule
134 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, ndo_remote_user_spec
135 from ansible_collections.cisco.mso.plugins.module_utils.constants import YES_OR_NO_TO_BOOL_STRING_MAP
136
137
138 def main():
139 argument_spec = mso_argument_spec()
140 argument_spec.update(
141 description=dict(type="str"),
142 display_name=dict(type="str"),
143 tenant=dict(type="str", aliases=["name"]),
144 users=dict(type="list", elements="str"),
145 remote_users=dict(type="list", elements="dict", options=ndo_remote_user_spec()),
146 sites=dict(type="list", elements="str"),
147 orchestrator_only=dict(type="str", default="yes", choices=["yes", "no"]),
148 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
149 )
150
151 module = AnsibleModule(
152 argument_spec=argument_spec,
153 supports_check_mode=True,
154 required_if=[
155 ["state", "absent", ["tenant"]],
156 ["state", "present", ["tenant"]],
157 ],
158 )
159
160 description = module.params.get("description")
161 display_name = module.params.get("display_name")
162 tenant = module.params.get("tenant")
163 orchestrator_only = module.params.get("orchestrator_only")
164 state = module.params.get("state")
165 remote_users = module.params.get("remote_users")
166
167 mso = MSOModule(module)
168
169 tenant_id = None
170 path = "tenants"
171
172 # Query for existing object(s)
173 if tenant:
174 mso.existing = mso.get_obj(path, name=tenant)
175 if mso.existing:
176 tenant_id = mso.existing.get("id")
177 # If we found an existing object, continue with it
178 path = "tenants/{id}".format(id=tenant_id)
179 else:
180 mso.existing = mso.query_objs(path)
181
182 if state == "query":
183 pass
184
185 elif state == "absent":
186 mso.previous = mso.existing
187 if mso.existing:
188 if module.check_mode:
189 mso.existing = {}
190 else:
191 path = "{0}?msc-only={1}".format(path, YES_OR_NO_TO_BOOL_STRING_MAP.get(orchestrator_only))
192 mso.existing = mso.request(path, method="DELETE")
193
194 elif state == "present":
195 mso.previous = mso.existing
196
197 # Convert sites and users
198 sites = mso.lookup_sites(module.params.get("sites"))
199 users = mso.lookup_users(module.params.get("users"))
200 if remote_users is not None:
201 users += mso.lookup_remote_users(remote_users)
202
203 payload = dict(
204 description=description,
205 id=tenant_id,
206 name=tenant,
207 displayName=display_name,
208 siteAssociations=sites,
209 userAssociations=users,
210 )
211
212 mso.sanitize(payload, collate=True)
213
214 # Ensure displayName is not undefined
215 if mso.sent.get("displayName") is None:
216 mso.sent["displayName"] = tenant
217
218 if mso.existing:
219 if mso.check_changed():
220 if module.check_mode:
221 mso.existing = mso.proposed
222 else:
223 mso.existing = mso.request(path, method="PUT", data=mso.sent)
224 else:
225 if module.check_mode:
226 mso.existing = mso.proposed
227 else:
228 mso.existing = mso.request(path, method="POST", data=mso.sent)
229
230 mso.exit_json()
231
232
233 if __name__ == "__main__":
234 main()