Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Dag Wieers (@dagwieers) <[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": "community"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: mso_user
16 short_description: Manage users
17 description:
18 - Manage users on Cisco ACI Multi-Site.
19 author:
20 - Dag Wieers (@dagwieers)
21 options:
22 user:
23 description:
24 - The name of the user.
25 type: str
26 aliases: [ name ]
27 user_password:
28 description:
29 - The password of the user.
30 type: str
31 first_name:
32 description:
33 - The first name of the user.
34 - This parameter is required when creating new users.
35 type: str
36 last_name:
37 description:
38 - The last name of the user.
39 - This parameter is required when creating new users.
40 type: str
41 email:
42 description:
43 - The email address of the user.
44 - This parameter is required when creating new users.
45 type: str
46 phone:
47 description:
48 - The phone number of the user.
49 - This parameter is required when creating new users.
50 type: str
51 account_status:
52 description:
53 - The status of the user account.
54 type: str
55 choices: [ active, inactive ]
56 domain:
57 description:
58 - The domain this user belongs to.
59 - When creating new users, this defaults to C(Local).
60 type: str
61 roles:
62 description:
63 - The roles for this user and their access types (read or write).
64 - Access type defaults to C(write).
65 type: list
66 elements: str
67 state:
68 description:
69 - Use C(present) or C(absent) for adding or removing.
70 - Use C(query) for listing an object or multiple objects.
71 type: str
72 choices: [ absent, present, query ]
73 default: present
74 notes:
75 - A default installation of ACI Multi-Site ships with admin password 'we1come!' which requires a password change on first login.
76 See the examples of how to change the 'admin' password using Ansible.
77 extends_documentation_fragment: cisco.mso.modules
78 """
79
80 EXAMPLES = r"""
81 - name: Update initial admin password
82 cisco.mso.mso_user:
83 host: mso_host
84 username: admin
85 password: initialPassword
86 validate_certs: false
87 user: admin
88 user_password: newPassword
89 state: present
90
91 - name: Add a new user
92 cisco.mso.mso_user:
93 host: mso_host
94 username: admin
95 password: SomeSecretPassword
96 validate_certs: false
97 user: dag
98 user_password: userPassword
99 first_name: Dag
100 last_name: Wieers
101 email: [email protected]
102 phone: +32 478 436 299
103 roles:
104 - name: siteManager
105 access_type: write
106 - name: schemaManager
107 access_type: read
108 state: present
109
110 - name: Add a new user
111 cisco.mso.mso_user:
112 host: mso_host
113 username: admin
114 password: SomeSecretPassword
115 validate_certs: false
116 user: dag
117 first_name: Dag
118 last_name: Wieers
119 email: [email protected]
120 phone: +32 478 436 299
121 roles:
122 - powerUser
123
124 - name: Remove a user
125 cisco.mso.mso_user:
126 host: mso_host
127 username: admin
128 password: SomeSecretPassword
129 validate_certs: false
130 user: dag
131 state: absent
132
133 - name: Query a user
134 cisco.mso.mso_user:
135 host: mso_host
136 username: admin
137 password: SomeSecretPassword
138 validate_certs: false
139 user: dag
140 state: query
141 register: query_result
142
143 - name: Query all users
144 cisco.mso.mso_user:
145 host: mso_host
146 username: admin
147 password: SomeSecretPassword
148 validate_certs: false
149 state: query
150 register: query_result
151 """
152
153 RETURN = r""" # """
154
155 from ansible.module_utils.basic import AnsibleModule
156 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, issubset
157
158
159 def main():
160 argument_spec = mso_argument_spec()
161 argument_spec.update(
162 user=dict(type="str", aliases=["name"]),
163 user_password=dict(type="str", no_log=True),
164 first_name=dict(type="str"),
165 last_name=dict(type="str"),
166 email=dict(type="str"),
167 phone=dict(type="str"),
168 # TODO: What possible options do we have ?
169 account_status=dict(type="str", choices=["active", "inactive"]),
170 domain=dict(type="str"),
171 roles=dict(type="list", elements="str"),
172 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
173 )
174
175 module = AnsibleModule(
176 argument_spec=argument_spec,
177 supports_check_mode=True,
178 required_if=[
179 ["state", "absent", ["user"]],
180 ["state", "present", ["user"]],
181 ],
182 )
183
184 user_name = module.params.get("user")
185 user_password = module.params.get("user_password")
186 first_name = module.params.get("first_name")
187 last_name = module.params.get("last_name")
188 email = module.params.get("email")
189 phone = module.params.get("phone")
190 account_status = module.params.get("account_status")
191 state = module.params.get("state")
192
193 mso = MSOModule(module)
194
195 roles = mso.lookup_roles(module.params.get("roles"))
196 domain = mso.lookup_domain(module.params.get("domain"))
197
198 user_id = None
199 path = "users"
200
201 # Query for existing object(s)
202 if user_name:
203 if mso.module._socket_path and mso.connection.get_platform() == "cisco.nd":
204 mso.existing = mso.get_obj(path, loginID=user_name, api_version="v2")
205 if mso.existing:
206 mso.existing["id"] = mso.existing.get("userID")
207 mso.existing["username"] = mso.existing.get("loginID")
208 else:
209 mso.existing = mso.get_obj(path, username=user_name)
210 if mso.existing:
211 user_id = mso.existing.get("id")
212 # If we found an existing object, continue with it
213 path = "users/{id}".format(id=user_id)
214 else:
215 mso.existing = mso.query_objs(path)
216
217 if state == "query":
218 pass
219
220 elif state == "absent":
221 mso.previous = mso.existing
222 if mso.existing:
223 if module.check_mode:
224 mso.existing = {}
225 else:
226 mso.existing = mso.request(path, method="DELETE")
227
228 elif state == "present":
229 mso.previous = mso.existing
230
231 payload = dict(
232 id=user_id,
233 username=user_name,
234 firstName=first_name,
235 lastName=last_name,
236 emailAddress=email,
237 phoneNumber=phone,
238 accountStatus=account_status,
239 domainId=domain,
240 roles=roles,
241 # active=True,
242 # remote=True,
243 )
244
245 if user_password is not None:
246 payload.update(password=user_password)
247
248 mso.sanitize(payload, collate=True)
249
250 if mso.sent.get("accountStatus") is None:
251 mso.sent["accountStatus"] = "active"
252
253 if mso.existing:
254 if not issubset(mso.sent, mso.existing):
255 # NOTE: Since MSO always returns '******' as password, we need to assume a change
256 if "password" in mso.proposed:
257 mso.module.warn("A password change is assumed, as the MSO REST API does not return passwords we do not know.")
258 mso.result["changed"] = True
259
260 if module.check_mode:
261 mso.existing = mso.proposed
262 else:
263 mso.existing = mso.request(path, method="PUT", data=mso.sent)
264
265 else:
266 if user_password is None:
267 mso.fail_json("The user {0} does not exist. The 'user_password' attribute is required to create a new user.".format(user_name))
268 if module.check_mode:
269 mso.existing = mso.proposed
270 else:
271 mso.existing = mso.request(path, method="POST", data=mso.sent)
272
273 mso.exit_json()
274
275
276 if __name__ == "__main__":
277 main()