4
4
"context"
5
5
"fmt"
6
6
"os"
7
+ "regexp"
7
8
"strings"
8
9
"testing"
9
10
@@ -14,19 +15,25 @@ import (
14
15
15
16
var resourceNamePending = "mongodbatlas_cloud_user_project_assignment.test_pending"
16
17
var resourceNameActive = "mongodbatlas_cloud_user_project_assignment.test_active"
18
+ var DSNameUsername = "data.mongodbatlas_cloud_user_project_assignment.test_username"
19
+ var DSNameUserID = "data.mongodbatlas_cloud_user_project_assignment.test_user_id"
17
20
18
- func TestAccCloudUserProjectAssignmentRS_basic (t * testing.T ) {
21
+ func TestAccCloudUserProjectAssignment_basic (t * testing.T ) {
19
22
resource .ParallelTest (t , * basicTestCase (t ))
20
23
}
21
24
25
+ func TestAccCloudUserProjectAssignmentDS_error (t * testing.T ) {
26
+ resource .ParallelTest (t , * errorTestCase (t ))
27
+ }
28
+
22
29
func basicTestCase (t * testing.T ) * resource.TestCase {
23
30
t .Helper ()
24
31
25
32
// Use MONGODB_ATLAS_USERNAME_2 to avoid USER_ALREADY_IN_GROUP.
26
33
// The default MONGODB_ATLAS_USERNAME (Org Owner) is auto-assigned if no ProjectOwner is set.
27
34
activeUsername := os .Getenv ("MONGODB_ATLAS_USERNAME_2" )
28
35
pendingUsername := acc .RandomEmail ()
29
- projectName := acc .RandomName ( )
36
+ projectID := acc .ProjectIDExecution ( t )
30
37
orgID := os .Getenv ("MONGODB_ATLAS_ORG_ID" )
31
38
roles := []string {"GROUP_OWNER" , "GROUP_CLUSTER_MANAGER" }
32
39
updatedRoles := []string {"GROUP_OWNER" , "GROUP_SEARCH_INDEX_EDITOR" , "GROUP_READ_ONLY" }
@@ -37,12 +44,12 @@ func basicTestCase(t *testing.T) *resource.TestCase {
37
44
CheckDestroy : checkDestroy ,
38
45
Steps : []resource.TestStep {
39
46
{
40
- Config : configBasic (orgID , pendingUsername , activeUsername , projectName , roles ),
41
- Check : checks (pendingUsername , activeUsername , projectName , roles ),
47
+ Config : configBasic (orgID , pendingUsername , activeUsername , projectID , roles ),
48
+ Check : checks (pendingUsername , activeUsername , projectID , roles ),
42
49
},
43
50
{
44
- Config : configBasic (orgID , pendingUsername , activeUsername , projectName , updatedRoles ),
45
- Check : checks (pendingUsername , activeUsername , projectName , updatedRoles ),
51
+ Config : configBasic (orgID , pendingUsername , activeUsername , projectID , updatedRoles ),
52
+ Check : checks (pendingUsername , activeUsername , projectID , updatedRoles ),
46
53
},
47
54
{
48
55
ResourceName : resourceNamePending ,
@@ -96,7 +103,31 @@ func basicTestCase(t *testing.T) *resource.TestCase {
96
103
}
97
104
}
98
105
99
- func configBasic (orgID , pendingUsername , activeUsername , projectName string , roles []string ) string {
106
+ func errorTestCase (t * testing.T ) * resource.TestCase {
107
+ t .Helper ()
108
+ orgID := os .Getenv ("MONGODB_ATLAS_ORG_ID" )
109
+ projectID := acc .ProjectIDExecution (t )
110
+ return & resource.TestCase {
111
+ PreCheck : func () { acc .PreCheckBasic (t ) },
112
+ ProtoV6ProviderFactories : acc .TestAccProviderV6Factories ,
113
+ Steps : []resource.TestStep {
114
+ {
115
+ Config : configError (orgID , projectID ),
116
+ ExpectError : regexp .MustCompile ("either username or user_id must be provided" ),
117
+ },
118
+ },
119
+ }
120
+ }
121
+
122
+ func configError (orgID , projectID string ) string {
123
+ return fmt .Sprintf (`
124
+ data "mongodbatlas_cloud_user_project_assignment" "test" {
125
+ project_id = %[1]q
126
+ }
127
+ ` , projectID , orgID )
128
+ }
129
+
130
+ func configBasic (orgID , pendingUsername , activeUsername , projectID string , roles []string ) string {
100
131
rolesStr := `"` + strings .Join (roles , `", "` ) + `"`
101
132
return fmt .Sprintf (`
102
133
resource "mongodbatlas_project" "test" {
@@ -106,26 +137,41 @@ func configBasic(orgID, pendingUsername, activeUsername, projectName string, rol
106
137
107
138
resource "mongodbatlas_cloud_user_project_assignment" "test_pending" {
108
139
username = %[3]q
109
- project_id = mongodbatlas_project.test.id
140
+ project_id = %[1]q
110
141
roles = [%[5]s]
111
142
}
112
143
113
144
resource "mongodbatlas_cloud_user_project_assignment" "test_active" {
114
145
username = %[4]q
115
- project_id = mongodbatlas_project.test.id
146
+ project_id = %[1]q
116
147
roles = [%[5]s]
148
+ }
149
+
150
+ data "mongodbatlas_cloud_user_project_assignment" "test_username" {
151
+ project_id = %[1]q
152
+ username = mongodbatlas_cloud_user_project_assignment.test_pending.username
153
+ }
154
+
155
+ data "mongodbatlas_cloud_user_project_assignment" "test_user_id" {
156
+ project_id = %[1]q
157
+ user_id = mongodbatlas_cloud_user_project_assignment.test_pending.user_id
117
158
}` ,
118
- projectName , orgID , pendingUsername , activeUsername , rolesStr )
159
+ projectID , orgID , pendingUsername , activeUsername , rolesStr )
119
160
}
120
161
121
- func checks (pendingUsername , activeUsername , projectName string , roles []string ) resource.TestCheckFunc {
162
+ func checks (pendingUsername , activeUsername , projectID string , roles []string ) resource.TestCheckFunc {
122
163
checkFuncs := []resource.TestCheckFunc {
123
164
resource .TestCheckResourceAttr (resourceNamePending , "username" , pendingUsername ),
124
- resource .TestCheckResourceAttrSet (resourceNamePending , "project_id" ),
165
+ resource .TestCheckResourceAttr (resourceNamePending , "project_id" , projectID ),
125
166
resource .TestCheckResourceAttr (resourceNamePending , "roles.#" , fmt .Sprintf ("%d" , len (roles ))),
126
167
resource .TestCheckResourceAttr (resourceNameActive , "username" , activeUsername ),
127
- resource .TestCheckResourceAttrSet (resourceNameActive , "project_id" ),
168
+ resource .TestCheckResourceAttr (resourceNameActive , "project_id" , projectID ),
128
169
resource .TestCheckResourceAttr (resourceNameActive , "roles.#" , fmt .Sprintf ("%d" , len (roles ))),
170
+ resource .TestCheckResourceAttr (DSNameUserID , "username" , pendingUsername ),
171
+ resource .TestCheckResourceAttrPair (DSNameUserID , "username" , resourceNamePending , "username" ),
172
+ resource .TestCheckResourceAttrPair (DSNameUsername , "user_id" , DSNameUserID , "user_id" ),
173
+ resource .TestCheckResourceAttrPair (DSNameUsername , "project_id" , DSNameUserID , "project_id" ),
174
+ resource .TestCheckResourceAttrPair (DSNameUsername , "roles.#" , DSNameUserID , "roles.#" ),
129
175
}
130
176
131
177
for _ , role := range roles {
@@ -134,6 +180,7 @@ func checks(pendingUsername, activeUsername, projectName string, roles []string)
134
180
resource .TestCheckTypeSetElemAttr (resourceNameActive , "roles.*" , role ),
135
181
)
136
182
}
183
+
137
184
return resource .ComposeAggregateTestCheckFunc (checkFuncs ... )
138
185
}
139
186
0 commit comments