@@ -12,7 +12,8 @@ import (
12
12
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
13
13
)
14
14
15
- var resourceName = "mongodbatlas_cloud_user_project_assignment.test"
15
+ var resourceNamePending = "mongodbatlas_cloud_user_project_assignment.test_pending"
16
+ var resourceNameActive = "mongodbatlas_cloud_user_project_assignment.test_active"
16
17
17
18
func TestAccCloudUserProjectAssignmentRS_basic (t * testing.T ) {
18
19
resource .ParallelTest (t , * basicTestCase (t ))
@@ -22,43 +23,68 @@ func basicTestCase(t *testing.T) *resource.TestCase {
22
23
t .Helper ()
23
24
24
25
orgID := os .Getenv ("MONGODB_ATLAS_ORG_ID" )
25
- username := acc .RandomEmail ()
26
+ activeUsername := os .Getenv ("MONGODB_ATLAS_USERNAME_2" )
27
+ pendingUsername := acc .RandomEmail ()
26
28
projectName := acc .RandomName ()
27
29
roles := []string {"GROUP_OWNER" , "GROUP_CLUSTER_MANAGER" }
28
30
updatedRoles := []string {"GROUP_OWNER" , "GROUP_SEARCH_INDEX_EDITOR" , "GROUP_READ_ONLY" }
29
31
30
32
return & resource.TestCase {
31
- PreCheck : func () { acc .PreCheckBasic (t ) },
33
+ PreCheck : func () { acc .PreCheckBasic (t ); acc . PreCheckAtlasUsernames ( t ) },
32
34
ProtoV6ProviderFactories : acc .TestAccProviderV6Factories ,
33
35
CheckDestroy : checkDestroy ,
34
36
Steps : []resource.TestStep {
35
37
{
36
- Config : configBasic (orgID , username , projectName , roles ),
37
- Check : checks (username , projectName , roles ),
38
+ Config : configBasic (orgID , pendingUsername , activeUsername , projectName , roles ),
39
+ Check : checks (pendingUsername , activeUsername , projectName , roles ),
38
40
},
39
41
{
40
- Config : configBasic (orgID , username , projectName , updatedRoles ),
41
- Check : checks (username , projectName , updatedRoles ),
42
+ Config : configBasic (orgID , pendingUsername , activeUsername , projectName , updatedRoles ),
43
+ Check : checks (pendingUsername , activeUsername , projectName , updatedRoles ),
42
44
},
43
45
{
44
- ResourceName : resourceName ,
46
+ ResourceName : resourceNamePending ,
45
47
ImportState : true ,
46
48
ImportStateVerify : true ,
47
49
ImportStateVerifyIdentifierAttribute : "user_id" ,
48
50
ImportStateIdFunc : func (s * terraform.State ) (string , error ) {
49
- attrs := s .RootModule ().Resources [resourceName ].Primary .Attributes
51
+ attrs := s .RootModule ().Resources [resourceNamePending ].Primary .Attributes
50
52
projectID := attrs ["project_id" ]
51
53
userID := attrs ["user_id" ]
52
54
return projectID + "/" + userID , nil
53
55
},
54
56
},
55
57
{
56
- ResourceName : resourceName ,
58
+ ResourceName : resourceNamePending ,
57
59
ImportState : true ,
58
60
ImportStateVerify : true ,
59
61
ImportStateVerifyIdentifierAttribute : "user_id" ,
60
62
ImportStateIdFunc : func (s * terraform.State ) (string , error ) {
61
- attrs := s .RootModule ().Resources [resourceName ].Primary .Attributes
63
+ attrs := s .RootModule ().Resources [resourceNamePending ].Primary .Attributes
64
+ projectID := attrs ["project_id" ]
65
+ username := attrs ["username" ]
66
+ return projectID + "/" + username , nil
67
+ },
68
+ },
69
+ {
70
+ ResourceName : resourceNameActive ,
71
+ ImportState : true ,
72
+ ImportStateVerify : true ,
73
+ ImportStateVerifyIdentifierAttribute : "user_id" ,
74
+ ImportStateIdFunc : func (s * terraform.State ) (string , error ) {
75
+ attrs := s .RootModule ().Resources [resourceNameActive ].Primary .Attributes
76
+ projectID := attrs ["project_id" ]
77
+ userID := attrs ["user_id" ]
78
+ return projectID + "/" + userID , nil
79
+ },
80
+ },
81
+ {
82
+ ResourceName : resourceNameActive ,
83
+ ImportState : true ,
84
+ ImportStateVerify : true ,
85
+ ImportStateVerifyIdentifierAttribute : "user_id" ,
86
+ ImportStateIdFunc : func (s * terraform.State ) (string , error ) {
87
+ attrs := s .RootModule ().Resources [resourceNameActive ].Primary .Attributes
62
88
projectID := attrs ["project_id" ]
63
89
username := attrs ["username" ]
64
90
return projectID + "/" + username , nil
@@ -68,31 +94,43 @@ func basicTestCase(t *testing.T) *resource.TestCase {
68
94
}
69
95
}
70
96
71
- func configBasic (orgID , username , projectName string , roles []string ) string {
97
+ func configBasic (orgID , pendingUsername , activeUsername , projectName string , roles []string ) string {
72
98
rolesStr := `"` + strings .Join (roles , `", "` ) + `"`
73
99
return fmt .Sprintf (`
74
100
resource "mongodbatlas_project" "test" {
75
101
name = %[1]q
76
102
org_id = %[2]q
77
103
}
78
104
79
- resource "mongodbatlas_cloud_user_project_assignment" "test " {
105
+ resource "mongodbatlas_cloud_user_project_assignment" "test_pending " {
80
106
username = %[3]q
81
107
project_id = mongodbatlas_project.test.id
82
- roles = [%[4]s]
108
+ roles = [%[5]s]
109
+ }
110
+
111
+ resource "mongodbatlas_cloud_user_project_assignment" "test_active" {
112
+ username = %[4]q
113
+ project_id = mongodbatlas_project.test.id
114
+ roles = [%[5]s]
83
115
}` ,
84
- projectName , orgID , username , rolesStr )
116
+ projectName , orgID , pendingUsername , activeUsername , rolesStr )
85
117
}
86
118
87
- func checks (username , projectName string , roles []string ) resource.TestCheckFunc {
119
+ func checks (pendingUsername , activeUsername , projectName string , roles []string ) resource.TestCheckFunc {
88
120
checkFuncs := []resource.TestCheckFunc {
89
- resource .TestCheckResourceAttr (resourceName , "username" , username ),
90
- resource .TestCheckResourceAttrSet (resourceName , "project_id" ),
91
- resource .TestCheckResourceAttr (resourceName , "roles.#" , fmt .Sprintf ("%d" , len (roles ))),
121
+ resource .TestCheckResourceAttr (resourceNamePending , "username" , pendingUsername ),
122
+ resource .TestCheckResourceAttrSet (resourceNamePending , "project_id" ),
123
+ resource .TestCheckResourceAttr (resourceNamePending , "roles.#" , fmt .Sprintf ("%d" , len (roles ))),
124
+ resource .TestCheckResourceAttr (resourceNameActive , "username" , activeUsername ),
125
+ resource .TestCheckResourceAttrSet (resourceNameActive , "project_id" ),
126
+ resource .TestCheckResourceAttr (resourceNameActive , "roles.#" , fmt .Sprintf ("%d" , len (roles ))),
92
127
}
93
128
94
129
for _ , role := range roles {
95
- checkFuncs = append (checkFuncs , resource .TestCheckTypeSetElemAttr (resourceName , "roles.*" , role ))
130
+ checkFuncs = append (checkFuncs ,
131
+ resource .TestCheckTypeSetElemAttr (resourceNamePending , "roles.*" , role ),
132
+ resource .TestCheckTypeSetElemAttr (resourceNameActive , "roles.*" , role ),
133
+ )
96
134
}
97
135
return resource .ComposeAggregateTestCheckFunc (checkFuncs ... )
98
136
}
0 commit comments