Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_role.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_role
16 short_description: Manage roles
17 description:
18 - Manage roles on Cisco ACI Multi-Site.
19 author:
20 - Dag Wieers (@dagwieers)
21 options:
22 role:
23 description:
24 - The name of the role.
25 type: str
26 aliases: [ name ]
27 display_name:
28 description:
29 - The name of the role to be displayed in the web UI.
30 type: str
31 description:
32 description:
33 - The description of the role.
34 type: str
35 read_permissions:
36 description:
37 - A list of read permissions tied to this role.
38 type: list
39 elements: str
40 choices:
41 - backup-db
42 - manage-audit-records
43 - manage-labels
44 - manage-roles
45 - manage-schemas
46 - manage-sites
47 - manage-tenants
48 - manage-tenant-schemas
49 - manage-users
50 - platform-logs
51 - view-all-audit-records
52 - view-labels
53 - view-roles
54 - view-schemas
55 - view-sites
56 - view-tenants
57 - view-tenant-schemas
58 - view-users
59 write_permissions:
60 description:
61 - A list of write permissions tied to this role.
62 type: list
63 elements: str
64 aliases: [ permissions ]
65 choices:
66 - backup-db
67 - manage-audit-records
68 - manage-labels
69 - manage-roles
70 - manage-schemas
71 - manage-sites
72 - manage-tenants
73 - manage-tenant-schemas
74 - manage-users
75 - platform-logs
76 - view-all-audit-records
77 - view-labels
78 - view-roles
79 - view-schemas
80 - view-sites
81 - view-tenants
82 - view-tenant-schemas
83 - view-users
84 state:
85 description:
86 - Use C(present) or C(absent) for adding or removing.
87 - Use C(query) for listing an object or multiple objects.
88 type: str
89 choices: [ absent, present, query ]
90 default: present
91 extends_documentation_fragment: cisco.mso.modules
92 """
93
94 EXAMPLES = r"""
95 - name: Add a new role
96 cisco.mso.mso_role:
97 host: mso_host
98 username: admin
99 password: SomeSecretPassword
100 role: readOnly
101 display_name: Read Only
102 description: Read-only access for troubleshooting
103 read_permissions:
104 - view-roles
105 - view-schemas
106 - view-sites
107 - view-tenants
108 - view-tenant-schemas
109 - view-users
110 write_permissions:
111 - manage-roles
112 - manage-schemas
113 - manage-sites
114 - manage-tenants
115 - manage-tenant-schemas
116 - manage-users
117 state: present
118
119 - name: Remove a role
120 cisco.mso.mso_role:
121 host: mso_host
122 username: admin
123 password: SomeSecretPassword
124 role: readOnly
125 state: absent
126
127 - name: Query a role
128 cisco.mso.mso_role:
129 host: mso_host
130 username: admin
131 password: SomeSecretPassword
132 role: readOnly
133 state: query
134 register: query_result
135
136 - name: Query all roles
137 cisco.mso.mso_role:
138 host: mso_host
139 username: admin
140 password: SomeSecretPassword
141 state: query
142 register: query_result
143 """
144
145 RETURN = r"""
146 """
147
148 from ansible.module_utils.basic import AnsibleModule
149 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
150
151
152 def main():
153 argument_spec = mso_argument_spec()
154 argument_spec.update(
155 role=dict(type="str", aliases=["name"]),
156 display_name=dict(type="str"),
157 description=dict(type="str"),
158 read_permissions=dict(
159 type="list",
160 elements="str",
161 choices=[
162 "backup-db",
163 "manage-audit-records",
164 "manage-labels",
165 "manage-roles",
166 "manage-schemas",
167 "manage-sites",
168 "manage-tenants",
169 "manage-tenant-schemas",
170 "manage-users",
171 "platform-logs",
172 "view-all-audit-records",
173 "view-labels",
174 "view-roles",
175 "view-schemas",
176 "view-sites",
177 "view-tenants",
178 "view-tenant-schemas",
179 "view-users",
180 ],
181 ),
182 write_permissions=dict(
183 type="list",
184 elements="str",
185 aliases=["permissions"],
186 choices=[
187 "backup-db",
188 "manage-audit-records",
189 "manage-labels",
190 "manage-roles",
191 "manage-schemas",
192 "manage-sites",
193 "manage-tenants",
194 "manage-tenant-schemas",
195 "manage-users",
196 "platform-logs",
197 "view-all-audit-records",
198 "view-labels",
199 "view-roles",
200 "view-schemas",
201 "view-sites",
202 "view-tenants",
203 "view-tenant-schemas",
204 "view-users",
205 ],
206 ),
207 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
208 )
209
210 module = AnsibleModule(
211 argument_spec=argument_spec,
212 supports_check_mode=True,
213 required_if=[
214 ["state", "absent", ["role"]],
215 ["state", "present", ["role"]],
216 ],
217 )
218
219 role = module.params.get("role")
220 description = module.params.get("description")
221 read_permissions = module.params.get("read_permissions")
222 write_permissions = module.params.get("write_permissions")
223 state = module.params.get("state")
224
225 mso = MSOModule(module)
226
227 role_id = None
228 path = "roles"
229
230 # Query for existing object(s)
231 if role:
232 mso.existing = mso.get_obj(path, name=role)
233 if mso.existing:
234 role_id = mso.existing.get("id")
235 # If we found an existing object, continue with it
236 path = "roles/{id}".format(id=role_id)
237 else:
238 mso.existing = mso.query_objs(path)
239
240 if state == "query":
241 pass
242
243 elif state == "absent":
244 mso.previous = mso.existing
245 if mso.existing:
246 if module.check_mode:
247 mso.existing = {}
248 else:
249 mso.existing = mso.request(path, method="DELETE")
250
251 elif state == "present":
252 mso.previous = mso.existing
253
254 payload = dict(
255 id=role_id,
256 name=role,
257 displayName=role,
258 description=description,
259 readPermissions=read_permissions,
260 writePermissions=write_permissions,
261 )
262
263 mso.sanitize(payload, collate=True)
264
265 if mso.existing:
266 if mso.check_changed():
267 if module.check_mode:
268 mso.existing = mso.proposed
269 else:
270 mso.existing = mso.request(path, method="PUT", data=mso.sent)
271 else:
272 if module.check_mode:
273 mso.existing = mso.proposed
274 else:
275 mso.existing = mso.request(path, method="POST", data=mso.sent)
276
277 mso.exit_json()
278
279
280 if __name__ == "__main__":
281 main()