Debian Codegraph @

ansible_collections/community/mongodb/plugins/modules/mongodb_atlas_user.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (c) 2020 T-Systems MMS
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 #
7 # This module is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This software is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this software. If not, see <http://www.gnu.org/licenses/>.
19
20 from __future__ import absolute_import, division, print_function
21
22 __metaclass__ = type
23
24 DOCUMENTATION = '''
25 ---
26 module: mongodb_atlas_user
27 short_description: Manage database users in Atlas
28 description:
29 - The mongodb_atlas_user module lets you create, modify and delete the database users in your cluster.
30 - Each user has a set of roles that provide access to the project's databases.
31 - A user's roles apply to all the clusters in the project
32 - if two clusters have a products database and a user has a role granting read access on the products database,
33 - the user has that access on both clusters.
34 - L(API Documentation,https://docs.atlas.mongodb.com/reference/api/database-users/)
35 author: "Martin Schurz (@schurzi)"
36 extends_documentation_fragment: community.mongodb.atlas_options
37 options:
38 database_name:
39 description:
40 - Database against which Atlas authenticates the user.
41 choices: ["admin", "$external"]
42 default: "admin"
43 type: str
44 aliases: [ "databaseName" ]
45 username:
46 description:
47 - Username for authenticating to MongoDB.
48 required: true
49 type: str
50 password:
51 description:
52 - User's password.
53 required: true
54 type: str
55 roles:
56 description:
57 - Array of this user's roles and the databases / collections on which the roles apply.
58 - A role must include following elements
59 suboptions:
60 database_name:
61 required: true
62 type: str
63 description:
64 - Database on which the user has the specified role.
65 - A role on the admin database can include privileges that apply to the other databases.
66 aliases: [ "databaseName" ]
67 role_name:
68 required: true
69 type: str
70 description:
71 - Name of the role. This value can either be a built-in role or a custom role.
72 aliases: [ "roleName" ]
73 required: true
74 type: list
75 elements: dict
76 scopes:
77 description:
78 - List of clusters and Atlas Data Lakes that this user can access.
79 - Atlas grants database users access to all resources by default.
80 suboptions:
81 name:
82 required: true
83 type: str
84 description:
85 - Name of the cluster or Atlas Data Lake that the database user can access.
86 type:
87 type: str
88 choices: ["CLUSTER", "DATA_LAKE"]
89 default: "CLUSTER"
90 description:
91 - Type of resource that the database user can access.
92 required: false
93 default: []
94 type: list
95 elements: dict
96 '''
97
98 EXAMPLES = '''
99 - name: test user
100 community.mongodb.mongodb_atlas_user:
101 api_username: "API_user"
102 api_password: "API_passwort_or_token"
103 group_id: "GROUP_ID"
104 username: my_app_user
105 password: SuperSecret!
106 roles:
107 - database_name: private_info
108 role_name: read
109 - database_name: public_info
110 role_name: readWrite
111 '''
112
113 from ansible.module_utils.basic import AnsibleModule
114 from ansible_collections.community.mongodb.plugins.module_utils.mongodb_atlas import (
115 AtlasAPIObject,
116 )
117
118
119 # ===========================================
120 # Module execution.
121 #
122 def main():
123 # add our own arguments
124 argument_spec = dict(
125 state=dict(default="present", choices=["absent", "present"]),
126 api_username=dict(required=True, aliases=['apiUsername']),
127 api_password=dict(required=True, no_log=True, aliases=['apiPassword']),
128 group_id=dict(required=True, aliases=['groupId']),
129 database_name=dict(default="admin", choices=["admin", "$external"], aliases=["databaseName"]),
130 username=dict(required=True),
131 password=dict(required=True, no_log=True),
132 roles=dict(
133 required=True,
134 type="list",
135 elements="dict",
136 options=dict(
137 database_name=dict(required=True, aliases=["databaseName"]),
138 role_name=dict(required=True, aliases=["roleName"]),
139 ),
140 ),
141 scopes=dict(
142 required=False,
143 type="list",
144 elements="dict",
145 options=dict(
146 name=dict(required=True),
147 type=dict(default="CLUSTER", choices=["CLUSTER", "DATA_LAKE"]),
148 ),
149 default=[],
150 ),
151 )
152
153 # Define the main module
154 module = AnsibleModule(
155 argument_spec=argument_spec, supports_check_mode=True
156 )
157
158 data = {
159 "databaseName": module.params["database_name"],
160 "username": module.params["username"],
161 "password": module.params["password"],
162 "roles": [],
163 "scopes": module.params["scopes"],
164 }
165
166 # remap keys to API format
167 for role in module.params.get("roles"):
168 data["roles"].append({
169 "databaseName": role.get("database_name"),
170 "roleName": role.get("role_name")
171 })
172
173 try:
174 atlas = AtlasAPIObject(
175 module=module,
176 path="/databaseUsers",
177 object_name="username",
178 group_id=module.params["group_id"],
179 data=data,
180 )
181 except Exception as e:
182 module.fail_json(
183 msg="unable to connect to Atlas API. Exception message: %s" % e
184 )
185
186 changed, diff = atlas.update(module.params["state"])
187 module.exit_json(
188 changed=changed,
189 data=atlas.data,
190 diff=diff,
191 )
192
193
194 # import module snippets
195 if __name__ == "__main__":
196 main()