-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathresource_mongodbatlas_database_user.go
293 lines (257 loc) · 8.21 KB
/
resource_mongodbatlas_database_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package mongodbatlas
import (
"context"
"errors"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
matlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
)
func resourceMongoDBAtlasDatabaseUser() *schema.Resource {
return &schema.Resource{
Create: resourceMongoDBAtlasDatabaseUserCreate,
Read: resourceMongoDBAtlasDatabaseUserRead,
Update: resourceMongoDBAtlasDatabaseUserUpdate,
Delete: resourceMongoDBAtlasDatabaseUserDelete,
Importer: &schema.ResourceImporter{
State: resourceMongoDBAtlasDatabaseUserImportState,
},
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"database_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"auth_database_name"},
Deprecated: "use auth_database_name instead",
},
"auth_database_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"database_name"},
},
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ConflictsWith: []string{"x509_type"},
},
"x509_type": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
},
"roles": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"role_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"collection_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"database_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
"labels": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"value": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
},
}
}
func resourceMongoDBAtlasDatabaseUserRead(d *schema.ResourceData, meta interface{}) error {
//Get client connection.
conn := meta.(*matlas.Client)
ids := decodeStateID(d.Id())
projectID := ids["project_id"]
username := ids["username"]
authDatabaseName := ids["auth_database_name"]
dbUser, _, err := conn.DatabaseUsers.Get(context.Background(), authDatabaseName, projectID, username)
if err != nil {
return fmt.Errorf("error getting database user information: %s", err)
}
if err := d.Set("username", dbUser.Username); err != nil {
return fmt.Errorf("error setting `username` for database user (%s): %s", d.Id(), err)
}
if _, ok := d.GetOk("auth_database_name"); ok {
if err := d.Set("auth_database_name", dbUser.DatabaseName); err != nil {
return fmt.Errorf("error setting `auth_database_name` for database user (%s): %s", d.Id(), err)
}
} else {
if err := d.Set("database_name", dbUser.DatabaseName); err != nil {
return fmt.Errorf("error setting `database_name` for database user (%s): %s", d.Id(), err)
}
}
if err := d.Set("x509_type", dbUser.X509Type); err != nil {
return fmt.Errorf("error setting `x509_type` for database user (%s): %s", d.Id(), err)
}
if err := d.Set("roles", flattenRoles(dbUser.Roles)); err != nil {
return fmt.Errorf("error setting `roles` for database user (%s): %s", d.Id(), err)
}
if err := d.Set("labels", flattenLabels(dbUser.Labels)); err != nil {
return fmt.Errorf("error setting `labels` for database user (%s): %s", d.Id(), err)
}
return nil
}
func resourceMongoDBAtlasDatabaseUserCreate(d *schema.ResourceData, meta interface{}) error {
//Get client connection.
conn := meta.(*matlas.Client)
projectID := d.Get("project_id").(string)
dbName, dbNameOk := d.GetOk("database_name")
authDBName, authDBNameOk := d.GetOk("auth_database_name")
if !dbNameOk && !authDBNameOk {
return errors.New("one of database_name or auth_database_name must be configured")
}
var authDatabaseName string
if dbNameOk {
authDatabaseName = dbName.(string)
} else {
authDatabaseName = authDBName.(string)
}
dbUserReq := &matlas.DatabaseUser{
Roles: expandRoles(d),
GroupID: projectID,
Username: d.Get("username").(string),
Password: d.Get("password").(string),
X509Type: d.Get("x509_type").(string),
DatabaseName: authDatabaseName,
Labels: expandLabelSliceFromSetSchema(d),
}
dbUserRes, _, err := conn.DatabaseUsers.Create(context.Background(), projectID, dbUserReq)
if err != nil {
return fmt.Errorf("error creating database user: %s", err)
}
d.SetId(encodeStateID(map[string]string{
"project_id": projectID,
"username": dbUserRes.Username,
"auth_database_name": authDatabaseName,
}))
return resourceMongoDBAtlasDatabaseUserRead(d, meta)
}
func resourceMongoDBAtlasDatabaseUserUpdate(d *schema.ResourceData, meta interface{}) error {
//Get client connection.
conn := meta.(*matlas.Client)
ids := decodeStateID(d.Id())
projectID := ids["project_id"]
username := ids["username"]
authDatabaseName := ids["auth_database_name"]
dbUser, _, err := conn.DatabaseUsers.Get(context.Background(), authDatabaseName, projectID, username)
if err != nil {
return fmt.Errorf("error getting database user information: %s", err)
}
if d.HasChange("password") {
dbUser.Password = d.Get("password").(string)
}
if d.HasChange("roles") {
dbUser.Roles = expandRoles(d)
}
if d.HasChange("labels") {
dbUser.Labels = expandLabelSliceFromSetSchema(d)
}
_, _, err = conn.DatabaseUsers.Update(context.Background(), projectID, username, dbUser)
if err != nil {
return fmt.Errorf("error updating database user(%s): %s", username, err)
}
return resourceMongoDBAtlasDatabaseUserRead(d, meta)
}
func resourceMongoDBAtlasDatabaseUserDelete(d *schema.ResourceData, meta interface{}) error {
//Get client connection.
conn := meta.(*matlas.Client)
ids := decodeStateID(d.Id())
projectID := ids["project_id"]
username := ids["username"]
authDatabaseName := ids["auth_database_name"]
_, err := conn.DatabaseUsers.Delete(context.Background(), authDatabaseName, projectID, username)
if err != nil {
return fmt.Errorf("error deleting database user (%s): %s", username, err)
}
return nil
}
func resourceMongoDBAtlasDatabaseUserImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
conn := meta.(*matlas.Client)
parts := strings.SplitN(d.Id(), "-", 3)
if len(parts) != 3 {
return nil, errors.New("import format error: to import a database user, use the format {project_id}-{username}-{auth_database_name}")
}
projectID := parts[0]
username := parts[1]
authDatabaseName := parts[2]
u, _, err := conn.DatabaseUsers.Get(context.Background(), authDatabaseName, projectID, username)
if err != nil {
return nil, fmt.Errorf("couldn't import user(%s) in project(%s), error: %s", username, projectID, err)
}
if err := d.Set("project_id", u.GroupID); err != nil {
log.Printf("[WARN] Error setting project_id for (%s): %s", d.Id(), err)
}
d.SetId(encodeStateID(map[string]string{
"project_id": projectID,
"username": username,
"auth_database_name": authDatabaseName,
}))
return []*schema.ResourceData{d}, nil
}
func expandRoles(d *schema.ResourceData) []matlas.Role {
var roles []matlas.Role
if v, ok := d.GetOk("roles"); ok {
if rs := v.([]interface{}); len(rs) > 0 {
roles = make([]matlas.Role, len(rs))
for k, r := range rs {
roleMap := r.(map[string]interface{})
roles[k] = matlas.Role{
RoleName: roleMap["role_name"].(string),
DatabaseName: roleMap["database_name"].(string),
CollectionName: roleMap["collection_name"].(string),
}
}
}
}
return roles
}
func flattenRoles(roles []matlas.Role) []map[string]interface{} {
roleList := make([]map[string]interface{}, 0)
for _, v := range roles {
roleList = append(roleList, map[string]interface{}{
"role_name": v.RoleName,
"database_name": v.DatabaseName,
"collection_name": v.CollectionName,
})
}
return roleList
}