1
1
package clouduserprojectassignment_test
2
2
3
3
import (
4
+ "fmt"
5
+ "os"
6
+ "strings"
4
7
"testing"
5
8
9
+ "github.com/hashicorp/terraform-plugin-testing/helper/resource"
10
+ "github.com/hashicorp/terraform-plugin-testing/plancheck"
11
+ "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
6
12
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig"
7
13
)
8
14
15
+ const (
16
+ resourceInvitationName = "mongodbatlas_project_invitation.mig_test"
17
+ resourceProjectName = "mongodbatlas_project.mig_test"
18
+ resourceUserProjectAssignmentName = "mongodbatlas_cloud_user_project_assignment.user_mig_test"
19
+ )
20
+
9
21
func TestMigCloudUserProjectAssignmentRS_basic (t * testing.T ) {
10
22
mig .SkipIfVersionBelow (t , "2.0.0" ) // when resource 1st released
11
23
mig .CreateAndRunTest (t , basicTestCase (t ))
12
24
}
13
25
14
26
func TestMigCloudUserProjectAssignmentRS_migrationJourney (t * testing.T ) {
27
+ var (
28
+ orgID = os .Getenv ("MONGODB_ATLAS_ORG_ID" )
29
+ username = acc .RandomEmail ()
30
+ projectName = fmt .Sprintf ("mig_user_project_%s" , acc .RandomName ())
31
+ roles = []string {"GROUP_READ_ONLY" , "GROUP_DATA_ACCESS_ADMIN" }
32
+ rolesStr = `"` + strings .Join (roles , `", "` ) + `"`
33
+ )
15
34
35
+ resource .ParallelTest (t , resource.TestCase {
36
+ PreCheck : func () { acc .PreCheckBasic (t ) },
37
+ CheckDestroy : checkDestroy ,
38
+ Steps : []resource.TestStep {
39
+ {
40
+ ExternalProviders : mig .ExternalProviders (),
41
+ Config : originalConfigFirst (username , projectName , orgID , rolesStr ),
42
+ },
43
+ {
44
+ ProtoV6ProviderFactories : acc .TestAccProviderV6Factories ,
45
+ Config : userProjectAssignmentConfigSecond (username , projectName , orgID , rolesStr ),
46
+ Check : checksSecond (username , projectName , roles ),
47
+ },
48
+ {
49
+ ProtoV6ProviderFactories : acc .TestAccProviderV6Factories ,
50
+ ConfigPlanChecks : resource.ConfigPlanChecks {
51
+ PreApply : []plancheck.PlanCheck {
52
+ plancheck .ExpectResourceAction (resourceInvitationName , plancheck .ResourceActionDestroy ),
53
+ },
54
+ },
55
+ Config : removeProjectInvitationConfigThird (username , projectName , orgID , rolesStr ),
56
+ },
57
+ mig .TestStepCheckEmptyPlan (removeProjectInvitationConfigThird (username , projectName , orgID , rolesStr )),
58
+ },
59
+ })
16
60
}
17
61
18
- func originalConfigFirst (t * testing.T ) string {
19
- t .Helper ()
20
- return `
21
- locals {
22
-
23
- roles = [ "GROUP_READ_ONLY", "GROUP_DATA_ACCESS_READ_ONLY" ]
24
- }
62
+ func originalConfigFirst (username , projectName , orgID , roles string ) string {
63
+ return fmt .Sprintf (`
64
+ locals {
65
+ username = %[1]q
66
+ roles = [%[2]q]
67
+ }
25
68
26
- resource "mongodbatlas_project" "mig_test" {
27
- name = "migration_user_project"
28
- org_id = var.org_id
29
- }
69
+ resource "mongodbatlas_project" "mig_test" {
70
+ name = %[3]q
71
+ org_id = %[4]q
72
+ }
30
73
31
- resource "mongodbatlas_project_invitation" "mig_test" {
32
- project_id = mongodbatlas_project.mig_test.id
33
- username = local.username
34
- roles = local.roles
35
- }
36
- `
74
+ resource "mongodbatlas_project_invitation" "mig_test" {
75
+ project_id = mongodbatlas_project.mig_test.id
76
+ username = local.username
77
+ roles = local.roles
78
+ }
79
+ ` , username , roles , projectName , orgID )
37
80
}
38
81
39
- func userProjectAssignmentConfigSecond (t * testing.T ) string {
40
- t .Helper ()
41
- return `
82
+ func userProjectAssignmentConfigSecond (username , projectName , orgID , roles string ) string {
83
+ return fmt .Sprintf (`
42
84
locals {
43
-
44
- roles = [ "GROUP_READ_ONLY", "GROUP_DATA_ACCESS_READ_ONLY" ]
85
+ username = %[1]q
86
+ roles = [%[2]q ]
45
87
}
46
88
47
89
resource "mongodbatlas_project" "mig_test" {
48
- name = "migration_user_project"
49
- org_id = var.org_id
90
+ name = %[3]q
91
+ org_id = %[4]q
50
92
}
51
93
52
94
resource "mongodbatlas_project_invitation" "mig_test" {
@@ -60,5 +102,39 @@ func userProjectAssignmentConfigSecond(t *testing.T) string {
60
102
username = local.username
61
103
roles = local.roles
62
104
}
63
- `
105
+ ` , username , roles , projectName , orgID )
106
+ }
107
+
108
+ func removeProjectInvitationConfigThird (username , projectName , orgID , roles string ) string {
109
+ return fmt .Sprintf (`
110
+ locals {
111
+ username = %[1]q
112
+ roles = [%[2]q]
113
+ }
114
+
115
+ resource "mongodbatlas_project" "mig_test" {
116
+ name = %[3]q
117
+ org_id = %[4]q
118
+ }
119
+
120
+ resource "mongodbatlas_cloud_user_project_assignment" "user_mig_test" {
121
+ project_id = mongodbatlas_project.mig_test.id
122
+ username = local.username
123
+ roles = local.roles
124
+ }
125
+ ` , username , roles , projectName , orgID )
126
+ }
127
+
128
+ func checksSecond (username , projectName string , roles []string ) resource.TestCheckFunc {
129
+ checkFuncs := []resource.TestCheckFunc {
130
+ resource .TestCheckResourceAttr (resourceUserProjectAssignmentName , "username" , username ),
131
+ resource .TestCheckResourceAttrSet (resourceUserProjectAssignmentName , "project_id" ),
132
+ resource .TestCheckResourceAttr (resourceUserProjectAssignmentName , "roles.#" , fmt .Sprintf ("%d" , len (roles ))),
133
+ }
134
+
135
+ for i , role := range roles {
136
+ checkFuncs = append (checkFuncs , resource .TestCheckResourceAttr (resourceUserProjectAssignmentName , fmt .Sprintf ("roles.%d" , i ), role ))
137
+ }
138
+
139
+ return resource .ComposeAggregateTestCheckFunc (checkFuncs ... )
64
140
}
0 commit comments