|
1 | 1 | package clouduserteamassignment_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "os" |
4 | 6 | "testing"
|
5 | 7 |
|
| 8 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 9 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" |
6 | 10 | "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig"
|
7 | 11 | )
|
8 | 12 |
|
| 13 | +const ( |
| 14 | + resourceTeamName = "mongodbatlas_team.test" |
| 15 | + resourceTeamAssignmentName = "mongodbatlas_cloud_user_team_assignment.test" |
| 16 | +) |
| 17 | + |
9 | 18 | func TestMigCloudUserTeamAssignmentRS_basic(t *testing.T) {
|
10 | 19 | mig.SkipIfVersionBelow(t, "2.0.0") // when resource 1st released
|
11 | 20 | mig.CreateAndRunTest(t, basicTestCase(t))
|
12 | 21 | }
|
| 22 | + |
| 23 | +func TestMigCloudUserTeamAssignmentRS_migrationJourney(t *testing.T) { |
| 24 | + var ( |
| 25 | + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") |
| 26 | + teamName = fmt.Sprintf("team-test-%s", acc.RandomName()) |
| 27 | + username = os.Getenv("MONGODB_ATLAS_USERNAME") |
| 28 | + ) |
| 29 | + |
| 30 | + resource.ParallelTest(t, resource.TestCase{ |
| 31 | + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckAtlasUsername(t) }, |
| 32 | + CheckDestroy: checkDestroy, |
| 33 | + Steps: []resource.TestStep{ |
| 34 | + { |
| 35 | + // NOTE: 'usernames' attribute (available v1.39.0, deprecated in v2.0.0) is used in this test in team resource, |
| 36 | + // which may be removed in future versions. This could cause the test to break - keep for version tracking. |
| 37 | + ExternalProviders: mig.ExternalProviders(), |
| 38 | + Config: configTeamWithUsernamesFirst(orgID, teamName, username), |
| 39 | + }, |
| 40 | + { |
| 41 | + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, |
| 42 | + Config: configWithTeamAssignmentsSecond(orgID, teamName, username), // expected to see 1 import in the plan |
| 43 | + Check: resource.ComposeTestCheckFunc( |
| 44 | + resource.TestCheckResourceAttr(resourceTeamName, "name", teamName), |
| 45 | + |
| 46 | + resource.TestCheckResourceAttrSet(resourceTeamAssignmentName, "user_id"), |
| 47 | + resource.TestCheckResourceAttr(resourceTeamAssignmentName, "username", username), |
| 48 | + ), |
| 49 | + }, |
| 50 | + mig.TestStepCheckEmptyPlan(configWithTeamAssignmentsSecond(orgID, teamName, username)), |
| 51 | + }, |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +// Step 1: Original configuration with usernames attribute |
| 56 | +func configTeamWithUsernamesFirst(orgID, teamName, username string) string { |
| 57 | + return fmt.Sprintf(` |
| 58 | + resource "mongodbatlas_team" "test" { |
| 59 | + org_id = %[2]q |
| 60 | + name = %[3]q |
| 61 | + usernames = [%[1]q] |
| 62 | + } |
| 63 | + `, username, orgID, teamName) |
| 64 | +} |
| 65 | + |
| 66 | +// Step 2: Configuration with team assignments using import blocks |
| 67 | + |
| 68 | +// NOTE: Using static resource assignment instead of for_each with multiple usernames |
| 69 | +// due to a known limitation in Terraform's acceptance testing framework with indexed resources. |
| 70 | +// The actual migration using for_each works correctly (verified locally). |
| 71 | +func configWithTeamAssignmentsSecond(orgID, teamName, username string) string { |
| 72 | + return fmt.Sprintf(` |
| 73 | + resource "mongodbatlas_team" "test" { |
| 74 | + org_id = %[1]q |
| 75 | + name = %[2]q |
| 76 | + } |
| 77 | +
|
| 78 | + data "mongodbatlas_team" "test" { |
| 79 | + org_id = %[1]q |
| 80 | + team_id = mongodbatlas_team.test.team_id |
| 81 | + } |
| 82 | + |
| 83 | + resource "mongodbatlas_cloud_user_team_assignment" "test" { |
| 84 | + org_id = %[1]q |
| 85 | + team_id = mongodbatlas_team.test.team_id |
| 86 | + user_id = data.mongodbatlas_team.test.users[0].id |
| 87 | + } |
| 88 | +
|
| 89 | + import { |
| 90 | + to = mongodbatlas_cloud_user_team_assignment.test |
| 91 | + id = "%[1]s/${mongodbatlas_team.test.team_id}/${data.mongodbatlas_team.test.users[0].id}" |
| 92 | + } |
| 93 | +
|
| 94 | + `, orgID, teamName) |
| 95 | +} |
0 commit comments