|
1 | 1 | package clouduserprojectassignment_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
4 | 8 | "testing"
|
5 | 9 |
|
6 | 10 | "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
| 11 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
7 | 12 | "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
|
8 | 13 | )
|
9 | 14 |
|
10 |
| -// TODO: if acceptance test will be run in an existing CI group of resources, the name should include the group in the prefix followed by the name of the resource e.i. TestAccStreamRSStreamInstance_basic |
11 |
| -// In addition, if acceptance test contains testing of both resource and data sources, the RS/DS can be omitted. |
| 15 | +var resourceName = "mongodbatlas_cloud_user_project_assignment.test" |
| 16 | + |
12 | 17 | func TestAccCloudUserProjectAssignmentRS_basic(t *testing.T) {
|
13 |
| - resource.ParallelTest(t, resource.TestCase{ |
| 18 | + resource.ParallelTest(t, *basicTestCase(t)) |
| 19 | +} |
| 20 | + |
| 21 | +func basicTestCase(t *testing.T) *resource.TestCase { |
| 22 | + t.Helper() |
| 23 | + |
| 24 | + orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") |
| 25 | + username := acc.RandomEmail() |
| 26 | + projectName := acc.RandomName() |
| 27 | + roles := []string{"GROUP_OWNER", "GROUP_CLUSTER_MANAGER"} |
| 28 | + updatedRoles := []string{"GROUP_OWNER", "GROUP_SEARCH_INDEX_EDITOR", "GROUP_READ_ONLY"} |
| 29 | + |
| 30 | + return &resource.TestCase{ |
14 | 31 | PreCheck: func() { acc.PreCheckBasic(t) },
|
15 | 32 | ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
|
16 |
| - // CheckDestroy: checkDestroyCloudUserProjectAssignment, |
17 |
| - Steps: []resource.TestStep{ // TODO: verify updates and import in case of resources |
18 |
| - // { |
19 |
| - // Config: cloudUserProjectAssignmentConfig(), |
20 |
| - // Check: cloudUserProjectAssignmentAttributeChecks(), |
21 |
| - // }, |
22 |
| - // { |
23 |
| - // Config: cloudUserProjectAssignmentConfig(), |
24 |
| - // Check: cloudUserProjectAssignmentAttributeChecks(), |
25 |
| - // }, |
26 |
| - // { |
27 |
| - // Config: cloudUserProjectAssignmentConfig(), |
28 |
| - // ResourceName: resourceName, |
29 |
| - // ImportStateIdFunc: checkCloudUserProjectAssignmentImportStateIDFunc, |
30 |
| - // ImportState: true, |
31 |
| - // ImportStateVerify: true, |
| 33 | + CheckDestroy: checkDestroy, |
| 34 | + Steps: []resource.TestStep{ |
| 35 | + { |
| 36 | + Config: configBasic(orgID, username, projectName, roles), |
| 37 | + Check: checks(username, projectName, roles), |
| 38 | + }, |
| 39 | + { |
| 40 | + Config: configBasic(orgID, username, projectName, updatedRoles), |
| 41 | + Check: checks(username, projectName, updatedRoles), |
| 42 | + }, |
| 43 | + { |
| 44 | + ResourceName: resourceName, |
| 45 | + ImportState: true, |
| 46 | + ImportStateVerify: true, |
| 47 | + ImportStateVerifyIdentifierAttribute: "user_id", |
| 48 | + ImportStateIdFunc: func(s *terraform.State) (string, error) { |
| 49 | + attrs := s.RootModule().Resources[resourceName].Primary.Attributes |
| 50 | + projectID := attrs["project_id"] |
| 51 | + userID := attrs["user_id"] |
| 52 | + return projectID + "/" + userID, nil |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + ResourceName: resourceName, |
| 57 | + ImportState: true, |
| 58 | + ImportStateVerify: true, |
| 59 | + ImportStateVerifyIdentifierAttribute: "user_id", |
| 60 | + ImportStateIdFunc: func(s *terraform.State) (string, error) { |
| 61 | + attrs := s.RootModule().Resources[resourceName].Primary.Attributes |
| 62 | + projectID := attrs["project_id"] |
| 63 | + username := attrs["username"] |
| 64 | + return projectID + "/" + username, nil |
| 65 | + }, |
| 66 | + }, |
32 | 67 | },
|
33 |
| - }, |
34 |
| - ) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func configBasic(orgID, username, projectName string, roles []string) string { |
| 72 | + rolesStr := `"` + strings.Join(roles, `", "`) + `"` |
| 73 | + return fmt.Sprintf(` |
| 74 | + resource "mongodbatlas_project" "test" { |
| 75 | + name = %[1]q |
| 76 | + org_id = %[2]q |
| 77 | + } |
| 78 | +
|
| 79 | + resource "mongodbatlas_cloud_user_project_assignment" "test" { |
| 80 | + username = %[3]q |
| 81 | + project_id = mongodbatlas_project.test.id |
| 82 | + roles = [%[4]s] |
| 83 | + }`, |
| 84 | + projectName, orgID, username, rolesStr) |
| 85 | +} |
| 86 | + |
| 87 | +func checks(username, projectName string, roles []string) resource.TestCheckFunc { |
| 88 | + 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))), |
| 92 | + } |
| 93 | + |
| 94 | + for _, role := range roles { |
| 95 | + checkFuncs = append(checkFuncs, resource.TestCheckTypeSetElemAttr(resourceName, "roles.*", role)) |
| 96 | + } |
| 97 | + return resource.ComposeAggregateTestCheckFunc(checkFuncs...) |
| 98 | +} |
| 99 | + |
| 100 | +func checkDestroy(s *terraform.State) error { |
| 101 | + for _, r := range s.RootModule().Resources { |
| 102 | + if r.Type != "mongodbatlas_cloud_user_project_assignment" { |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + userID := r.Primary.Attributes["user_id"] |
| 107 | + username := r.Primary.Attributes["username"] |
| 108 | + projectID := r.Primary.Attributes["project_id"] |
| 109 | + conn := acc.ConnV2() |
| 110 | + |
| 111 | + userListResp, _, err := conn.MongoDBCloudUsersApi.ListProjectUsers(context.Background(), projectID).Execute() |
| 112 | + if err != nil { |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + if userListResp != nil { |
| 117 | + results := userListResp.GetResults() |
| 118 | + for i := range results { |
| 119 | + if userID != "" && results[i].GetId() == userID { |
| 120 | + return fmt.Errorf("cloud user project assignment for user (%s) in project (%s) still exists", r.Primary.Attributes["username"], projectID) |
| 121 | + } |
| 122 | + if username != "" && results[i].GetUsername() == username { |
| 123 | + return fmt.Errorf("cloud user project assignment for user (%s) in project (%s) still exists", r.Primary.Attributes["username"], projectID) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return nil |
35 | 129 | }
|
0 commit comments