From e0be381b9d5cebe6da3fc33266d8ae70526bcfcc Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Feb 2024 14:58:32 +0100 Subject: [PATCH 01/14] rename CRUD methods --- internal/service/teams/data_source_teams.go | 4 ++-- internal/service/teams/resource_teams.go | 24 ++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/service/teams/data_source_teams.go b/internal/service/teams/data_source_teams.go index ee2294a838..013c4f72a1 100644 --- a/internal/service/teams/data_source_teams.go +++ b/internal/service/teams/data_source_teams.go @@ -15,7 +15,7 @@ import ( func DataSource() *schema.Resource { return &schema.Resource{ - ReadContext: dataSourceMongoDBAtlasTeamRead, + ReadContext: dataSourceRead, Schema: map[string]*schema.Schema{ "org_id": { Type: schema.TypeString, @@ -44,7 +44,7 @@ func DataSource() *schema.Resource { } } -func dataSourceMongoDBAtlasTeamRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { +func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { var ( conn = meta.(*config.MongoDBClient).Atlas orgID = d.Get("org_id").(string) diff --git a/internal/service/teams/resource_teams.go b/internal/service/teams/resource_teams.go index 2d9793e785..7d9e1d12cb 100644 --- a/internal/service/teams/resource_teams.go +++ b/internal/service/teams/resource_teams.go @@ -29,12 +29,12 @@ const ( func Resource() *schema.Resource { return &schema.Resource{ - CreateContext: resourceMongoDBAtlasTeamCreate, - ReadContext: resourceMongoDBAtlasTeamRead, - UpdateContext: resourceMongoDBAtlasTeamUpdate, - DeleteContext: resourceMongoDBAtlasTeamDelete, + CreateContext: resourceCreate, + ReadContext: resourceRead, + UpdateContext: resourceUpdate, + DeleteContext: resourceDelete, Importer: &schema.ResourceImporter{ - StateContext: resourceMongoDBAtlasTeamImportState, + StateContext: resourceImport, }, Schema: map[string]*schema.Schema{ "org_id": { @@ -61,7 +61,7 @@ func Resource() *schema.Resource { } } -func resourceMongoDBAtlasTeamCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { +func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { conn := meta.(*config.MongoDBClient).Atlas orgID := d.Get("org_id").(string) @@ -80,10 +80,10 @@ func resourceMongoDBAtlasTeamCreate(ctx context.Context, d *schema.ResourceData, "id": teamsResp.ID, })) - return resourceMongoDBAtlasTeamRead(ctx, d, meta) + return resourceRead(ctx, d, meta) } -func resourceMongoDBAtlasTeamRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { +func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { conn := meta.(*config.MongoDBClient).Atlas ids := conversion.DecodeStateID(d.Id()) @@ -127,7 +127,7 @@ func resourceMongoDBAtlasTeamRead(ctx context.Context, d *schema.ResourceData, m return nil } -func resourceMongoDBAtlasTeamUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { +func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { conn := meta.(*config.MongoDBClient).Atlas ids := conversion.DecodeStateID(d.Id()) @@ -214,10 +214,10 @@ func resourceMongoDBAtlasTeamUpdate(ctx context.Context, d *schema.ResourceData, } } - return resourceMongoDBAtlasTeamRead(ctx, d, meta) + return resourceRead(ctx, d, meta) } -func resourceMongoDBAtlasTeamDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { +func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { conn := meta.(*config.MongoDBClient).Atlas ids := conversion.DecodeStateID(d.Id()) orgID := ids["org_id"] @@ -249,7 +249,7 @@ func resourceMongoDBAtlasTeamDelete(ctx context.Context, d *schema.ResourceData, return nil } -func resourceMongoDBAtlasTeamImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { +func resourceImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { conn := meta.(*config.MongoDBClient).Atlas parts := strings.SplitN(d.Id(), "-", 2) From f5a687762e107340e49cc47760dffff5be5d16f4 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Feb 2024 15:07:16 +0100 Subject: [PATCH 02/14] rename methods for clarity --- .../service/teams/data_source_teams_test.go | 8 ++--- internal/service/teams/resource_teams_test.go | 30 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/service/teams/data_source_teams_test.go b/internal/service/teams/data_source_teams_test.go index b80c78532f..5c03b95c87 100644 --- a/internal/service/teams/data_source_teams_test.go +++ b/internal/service/teams/data_source_teams_test.go @@ -24,7 +24,7 @@ func TestAccConfigDSTeam_basic(t *testing.T) { CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ { - Config: testAccDataSourceMongoDBAtlasTeamConfig(orgID, name, username), + Config: dataSourceConfigBasic(orgID, name, username), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(dataSourceName, "org_id"), resource.TestCheckResourceAttrSet(dataSourceName, "team_id"), @@ -50,7 +50,7 @@ func TestAccConfigDSTeamByName_basic(t *testing.T) { CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ { - Config: testAccDataSourceMongoDBAtlasTeamConfigByName(orgID, name, username), + Config: dataSourceConfigBasicByName(orgID, name, username), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(dataSourceName, "org_id"), resource.TestCheckResourceAttrSet(dataSourceName, "team_id"), @@ -62,7 +62,7 @@ func TestAccConfigDSTeamByName_basic(t *testing.T) { }) } -func testAccDataSourceMongoDBAtlasTeamConfig(orgID, name, username string) string { +func dataSourceConfigBasic(orgID, name, username string) string { return fmt.Sprintf(` resource "mongodbatlas_teams" "test" { org_id = "%s" @@ -78,7 +78,7 @@ func testAccDataSourceMongoDBAtlasTeamConfig(orgID, name, username string) strin `, orgID, name, username) } -func testAccDataSourceMongoDBAtlasTeamConfigByName(orgID, name, username string) string { +func dataSourceConfigBasicByName(orgID, name, username string) string { return fmt.Sprintf(` resource "mongodbatlas_teams" "test" { org_id = "%s" diff --git a/internal/service/teams/resource_teams_test.go b/internal/service/teams/resource_teams_test.go index e618bb9026..9575e831d6 100644 --- a/internal/service/teams/resource_teams_test.go +++ b/internal/service/teams/resource_teams_test.go @@ -32,42 +32,42 @@ func TestAccConfigRSTeam_basic(t *testing.T) { CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ { - Config: testAccMongoDBAtlasTeamConfig(orgID, name, + Config: configBasic(orgID, name, []string{ username, }, ), Check: resource.ComposeTestCheckFunc( - testAccCheckMongoDBAtlasTeamExists(resourceName, &team), - testAccCheckMongoDBAtlasTeamAttributes(&team, name), + checkExists(resourceName, &team), + checkAttributes(&team, name), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", name), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), ), }, { - Config: testAccMongoDBAtlasTeamConfig(orgID, updatedName, + Config: configBasic(orgID, updatedName, []string{ username, }, ), Check: resource.ComposeTestCheckFunc( - testAccCheckMongoDBAtlasTeamExists(resourceName, &team), - testAccCheckMongoDBAtlasTeamAttributes(&team, updatedName), + checkExists(resourceName, &team), + checkAttributes(&team, updatedName), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", updatedName), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), ), }, { - Config: testAccMongoDBAtlasTeamConfig(orgID, updatedName, + Config: configBasic(orgID, updatedName, []string{ username, }, ), Check: resource.ComposeTestCheckFunc( - testAccCheckMongoDBAtlasTeamExists(resourceName, &team), - testAccCheckMongoDBAtlasTeamAttributes(&team, updatedName), + checkExists(resourceName, &team), + checkAttributes(&team, updatedName), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", updatedName), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), @@ -91,7 +91,7 @@ func TestAccConfigRSTeam_importBasic(t *testing.T) { CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ { - Config: testAccMongoDBAtlasTeamConfig(orgID, name, []string{username}), + Config: configBasic(orgID, name, []string{username}), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttrSet(resourceName, "name"), @@ -105,7 +105,7 @@ func TestAccConfigRSTeam_importBasic(t *testing.T) { }, { ResourceName: resourceName, - ImportStateIdFunc: testAccCheckMongoDBAtlasTeamStateIDFunc(resourceName), + ImportStateIdFunc: importStateIDFunc(resourceName), ImportState: true, ImportStateVerify: true, }, @@ -113,7 +113,7 @@ func TestAccConfigRSTeam_importBasic(t *testing.T) { }) } -func testAccCheckMongoDBAtlasTeamExists(resourceName string, team *matlas.Team) resource.TestCheckFunc { +func checkExists(resourceName string, team *matlas.Team) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -136,7 +136,7 @@ func testAccCheckMongoDBAtlasTeamExists(resourceName string, team *matlas.Team) } } -func testAccCheckMongoDBAtlasTeamAttributes(team *matlas.Team, name string) resource.TestCheckFunc { +func checkAttributes(team *matlas.Team, name string) resource.TestCheckFunc { return func(s *terraform.State) error { if team.Name != name { return fmt.Errorf("bad name: %s", team.Name) @@ -145,7 +145,7 @@ func testAccCheckMongoDBAtlasTeamAttributes(team *matlas.Team, name string) reso } } -func testAccCheckMongoDBAtlasTeamStateIDFunc(resourceName string) resource.ImportStateIdFunc { +func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { return func(s *terraform.State) (string, error) { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -155,7 +155,7 @@ func testAccCheckMongoDBAtlasTeamStateIDFunc(resourceName string) resource.Impor } } -func testAccMongoDBAtlasTeamConfig(orgID, name string, usernames []string) string { +func configBasic(orgID, name string, usernames []string) string { return fmt.Sprintf(` resource "mongodbatlas_teams" "test" { org_id = "%s" From 1fbd745b04ade3f80bc96a11b014670a4e4437f5 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Feb 2024 15:10:14 +0100 Subject: [PATCH 03/14] remove redundant check --- internal/service/teams/resource_teams_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/internal/service/teams/resource_teams_test.go b/internal/service/teams/resource_teams_test.go index 9575e831d6..2ea46ef919 100644 --- a/internal/service/teams/resource_teams_test.go +++ b/internal/service/teams/resource_teams_test.go @@ -39,7 +39,6 @@ func TestAccConfigRSTeam_basic(t *testing.T) { ), Check: resource.ComposeTestCheckFunc( checkExists(resourceName, &team), - checkAttributes(&team, name), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", name), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), @@ -53,7 +52,6 @@ func TestAccConfigRSTeam_basic(t *testing.T) { ), Check: resource.ComposeTestCheckFunc( checkExists(resourceName, &team), - checkAttributes(&team, updatedName), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", updatedName), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), @@ -67,7 +65,6 @@ func TestAccConfigRSTeam_basic(t *testing.T) { ), Check: resource.ComposeTestCheckFunc( checkExists(resourceName, &team), - checkAttributes(&team, updatedName), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", updatedName), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), @@ -136,15 +133,6 @@ func checkExists(resourceName string, team *matlas.Team) resource.TestCheckFunc } } -func checkAttributes(team *matlas.Team, name string) resource.TestCheckFunc { - return func(s *terraform.State) error { - if team.Name != name { - return fmt.Errorf("bad name: %s", team.Name) - } - return nil - } -} - func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { return func(s *terraform.State) (string, error) { rs, ok := s.RootModule().Resources[resourceName] From d54125c3b193a3fe410b61e58eff07b11ef1be15 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 11:17:38 +0100 Subject: [PATCH 04/14] initial migratation to new SDK --- internal/common/conversion/misc.go | 8 ++ internal/service/teams/resource_teams.go | 90 ++++++++++--------- .../teams/resource_teams_migration_test.go | 51 +++++++++++ internal/service/teams/resource_teams_test.go | 11 +-- 4 files changed, 111 insertions(+), 49 deletions(-) create mode 100644 internal/service/teams/resource_teams_migration_test.go diff --git a/internal/common/conversion/misc.go b/internal/common/conversion/misc.go index f13ed2afef..441946768e 100644 --- a/internal/common/conversion/misc.go +++ b/internal/common/conversion/misc.go @@ -45,3 +45,11 @@ func ExpandStringListFromSetSchema(set *schema.Set) []string { } return res } + +func ExpandStringPointerListFromSetSchema(set *schema.Set) *[]string { + res := make([]string, set.Len()) + for i, v := range set.List() { + res[i] = v.(string) + } + return &res +} diff --git a/internal/service/teams/resource_teams.go b/internal/service/teams/resource_teams.go index 7d9e1d12cb..1789e07349 100644 --- a/internal/service/teams/resource_teams.go +++ b/internal/service/teams/resource_teams.go @@ -14,8 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" - - matlas "go.mongodb.org/atlas/mongodbatlas" + "go.mongodb.org/atlas-sdk/v20231115005/admin" ) const ( @@ -62,35 +61,36 @@ func Resource() *schema.Resource { } func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 orgID := d.Get("org_id").(string) // Creating the team - teamsResp, _, err := conn.Teams.Create(ctx, orgID, - &matlas.Team{ + teamsResp, _, err := connV2.TeamsApi.CreateTeam(ctx, orgID, + &admin.Team{ Name: d.Get("name").(string), - Usernames: conversion.ExpandStringListFromSetSchema(d.Get("usernames").(*schema.Set)), - }) + Usernames: conversion.ExpandStringPointerListFromSetSchema(d.Get("usernames").(*schema.Set)), + }).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamCreate, err)) } d.SetId(conversion.EncodeStateID(map[string]string{ "org_id": orgID, - "id": teamsResp.ID, + "id": teamsResp.GetId(), })) return resourceRead(ctx, d, meta) } func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 ids := conversion.DecodeStateID(d.Id()) orgID := ids["org_id"] teamID := ids["id"] - team, resp, err := conn.Teams.Get(context.Background(), orgID, teamID) + // team, resp, err := conn.Teams.Get(context.Background(), orgID, teamID) + team, resp, err := connV2.TeamsApi.GetTeamById(context.Background(), orgID, teamID).Execute() if err != nil { // new resource missing @@ -101,23 +101,23 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(errorTeamRead, err)) } - if err = d.Set("name", team.Name); err != nil { + if err = d.Set("name", team.GetName()); err != nil { return diag.FromErr(fmt.Errorf(errorTeamSetting, "name", teamID, err)) } - if err = d.Set("team_id", team.ID); err != nil { + if err = d.Set("team_id", team.GetId()); err != nil { return diag.FromErr(fmt.Errorf(errorTeamSetting, "team_id", teamID, err)) } // Set Usernames - users, _, err := conn.Teams.GetTeamUsersAssigned(ctx, orgID, teamID) + users, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, teamID).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamRead, err)) } usernames := []string{} - for i := range users { - usernames = append(usernames, users[i].Username) + for i := range users.GetResults() { + usernames = append(usernames, users.GetResults()[i].GetUsername()) } if err := d.Set("usernames", usernames); err != nil { @@ -128,14 +128,18 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di } func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 ids := conversion.DecodeStateID(d.Id()) orgID := ids["org_id"] teamID := ids["id"] if d.HasChange("name") { - _, _, err := conn.Teams.Rename(ctx, orgID, teamID, d.Get("name").(string)) + _, _, err := connV2.TeamsApi.RenameTeam(ctx, orgID, teamID, + &admin.Team{ + Name: d.Get("name").(string), + Usernames: conversion.ExpandStringPointerListFromSetSchema(d.Get("usernames").(*schema.Set)), + }).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamUpdate, err)) } @@ -144,7 +148,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. if d.HasChange("usernames") { // First, we need to remove the current users of the team and later add the new users // Get the current team's users - users, _, err := conn.Teams.GetTeamUsersAssigned(ctx, orgID, teamID) + users, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, teamID).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamRead, err)) @@ -153,14 +157,14 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. // Removing each user - Let's not modify the state before making sure we can continue // existig users - index := make(map[string]matlas.AtlasUser) - for i := range users { - index[users[i].Username] = users[i] + index := make(map[string]admin.CloudAppUser) + for i := range users.GetResults() { + index[users.GetResults()[i].GetUsername()] = users.GetResults()[i] } cleanUsers := func() error { - for i := range users { - _, err := conn.Teams.RemoveUserToTeam(ctx, orgID, teamID, users[i].ID) + for i := range users.GetResults() { + _, err := connV2.TeamsApi.RemoveTeamUser(ctx, orgID, teamID, users.GetResults()[i].GetId()).Execute() if err != nil { return fmt.Errorf("error deleting Atlas User (%s) information: %s", teamID, err) } @@ -171,10 +175,10 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. // existing users // Verify if the gave users exists - var newUsers []string + var newUsers []admin.AddUserToTeam for _, username := range d.Get("usernames").(*schema.Set).List() { - user, _, err := conn.AtlasUsers.GetByName(ctx, username.(string)) + user, _, err := connV2.MongoDBCloudUsersApi.GetUserByUsername(ctx, username.(string)).Execute() updatedUserData := user @@ -199,7 +203,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. } } // if the user exists, we will storage its teamID - newUsers = append(newUsers, updatedUserData.ID) + newUsers = append(newUsers, admin.AddUserToTeam{Id: updatedUserData.GetId()}) } // Update the users, remove the old ones, add the new ones @@ -208,7 +212,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(err) } - _, _, err = conn.Teams.AddUsersToTeam(ctx, orgID, teamID, newUsers) + _, _, err = connV2.TeamsApi.AddTeamUser(ctx, orgID, teamID, &newUsers).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamAddUsers, err)) } @@ -218,22 +222,21 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. } func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 ids := conversion.DecodeStateID(d.Id()) orgID := ids["org_id"] id := ids["id"] err := retry.RetryContext(ctx, 1*time.Hour, func() *retry.RetryError { - _, err := conn.Teams.RemoveTeamFromOrganization(ctx, orgID, id) + _, _, err := connV2.TeamsApi.DeleteTeam(ctx, orgID, id).Execute() if err != nil { - var target *matlas.ErrorResponse - if errors.As(err, &target) && target.ErrorCode == "CANNOT_DELETE_TEAM_ASSIGNED_TO_PROJECT" { - projectID, err := getProjectIDByTeamID(ctx, conn, id) + if errors.Is(err, errors.New("CANNOT_DELETE_TEAM_ASSIGNED_TO_PROJECT")) { + projectID, err := getProjectIDByTeamID(ctx, connV2, id) if err != nil { return retry.NonRetryableError(err) } - _, err = conn.Teams.RemoveTeamFromProject(ctx, projectID, id) + _, err = connV2.TeamsApi.RemoveProjectTeam(ctx, projectID, id).Execute() if err != nil { return retry.NonRetryableError(fmt.Errorf(errorTeamDelete, id, err)) } @@ -250,7 +253,7 @@ func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag. } func resourceImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 parts := strings.SplitN(d.Id(), "-", 2) if len(parts) != 2 { @@ -260,7 +263,7 @@ func resourceImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*s orgID := parts[0] teamID := parts[1] - u, _, err := conn.Teams.Get(ctx, orgID, teamID) + team, _, err := connV2.TeamsApi.GetTeamById(ctx, orgID, teamID).Execute() if err != nil { return nil, fmt.Errorf("couldn't import team (%s) in organization(%s), error: %s", teamID, orgID, err) } @@ -275,28 +278,27 @@ func resourceImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*s d.SetId(conversion.EncodeStateID(map[string]string{ "org_id": orgID, - "id": u.ID, + "id": team.GetId(), })) return []*schema.ResourceData{d}, nil } -func getProjectIDByTeamID(ctx context.Context, conn *matlas.Client, teamID string) (string, error) { - options := &matlas.ListOptions{} - projects, _, err := conn.Projects.GetAllProjects(ctx, options) +func getProjectIDByTeamID(ctx context.Context, connV2 *admin.APIClient, teamID string) (string, error) { + projects, _, err := connV2.ProjectsApi.ListProjects(ctx).Execute() if err != nil { return "", fmt.Errorf("error getting projects information: %s", err) } - for _, project := range projects.Results { - teams, _, err := conn.Projects.GetProjectTeamsAssigned(ctx, project.ID) + for _, project := range projects.GetResults() { + teams, _, err := connV2.TeamsApi.ListProjectTeams(ctx, project.GetId()).Execute() if err != nil { return "", fmt.Errorf("error getting teams from project information: %s", err) } - for _, team := range teams.Results { - if team.TeamID == teamID { - return project.ID, nil + for _, team := range teams.GetResults() { + if team.GetTeamId() == teamID { + return project.GetId(), nil } } } diff --git a/internal/service/teams/resource_teams_migration_test.go b/internal/service/teams/resource_teams_migration_test.go new file mode 100644 index 0000000000..8b2bcb420d --- /dev/null +++ b/internal/service/teams/resource_teams_migration_test.go @@ -0,0 +1,51 @@ +package teams_test + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/plancheck" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig" + "go.mongodb.org/atlas-sdk/v20231115005/admin" +) + +func TestAccMigrationTeams_basic(t *testing.T) { + var ( + team admin.Team + resourceName = "mongodbatlas_teams.test" + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + name = fmt.Sprintf("test-acc-%s", acctest.RandString(10)) + username = os.Getenv("MONGODB_ATLAS_USERNAME") + ) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { mig.PreCheckBasic(t) }, + CheckDestroy: acc.CheckDestroyTeam, + Steps: []resource.TestStep{ + { + ExternalProviders: mig.ExternalProviders(), + Config: configBasic(orgID, name, []string{username}), + Check: resource.ComposeTestCheckFunc( + checkExists(resourceName, &team), + resource.TestCheckResourceAttrSet(resourceName, "org_id"), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), + ), + }, + { + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + Config: configBasic(orgID, name, []string{username}), + ConfigPlanChecks: resource.ConfigPlanChecks{ + PreApply: []plancheck.PlanCheck{ + acc.DebugPlan(), + plancheck.ExpectEmptyPlan(), + }, + }, + }, + }, + }) +} diff --git a/internal/service/teams/resource_teams_test.go b/internal/service/teams/resource_teams_test.go index 2ea46ef919..01b6fabe6d 100644 --- a/internal/service/teams/resource_teams_test.go +++ b/internal/service/teams/resource_teams_test.go @@ -13,12 +13,12 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" - matlas "go.mongodb.org/atlas/mongodbatlas" + "go.mongodb.org/atlas-sdk/v20231115005/admin" ) func TestAccConfigRSTeam_basic(t *testing.T) { var ( - team matlas.Team + team admin.Team resourceName = "mongodbatlas_teams.test" orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") name = fmt.Sprintf("test-acc-%s", acctest.RandString(10)) @@ -110,7 +110,7 @@ func TestAccConfigRSTeam_importBasic(t *testing.T) { }) } -func checkExists(resourceName string, team *matlas.Team) resource.TestCheckFunc { +func checkExists(resourceName string, team *admin.Team) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -124,9 +124,10 @@ func checkExists(resourceName string, team *matlas.Team) resource.TestCheckFunc } log.Printf("[DEBUG] orgID: %s", orgID) log.Printf("[DEBUG] teamID: %s", id) - teamResp, _, err := acc.Conn().Teams.Get(context.Background(), orgID, id) + teamResp, _, err := acc.ConnV2().TeamsApi.GetTeamById(context.Background(), orgID, id).Execute() if err == nil { - *team = *teamResp + team.Id = teamResp.Id + team.Name = teamResp.GetName() return nil } return fmt.Errorf("team(%s) does not exist", id) From a99fa4206520dd79cf3e82e56ba1aa6480cb9425 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 11:34:15 +0100 Subject: [PATCH 05/14] migrate data source --- internal/service/teams/data_source_teams.go | 22 ++++++++++----------- internal/service/teams/resource_teams.go | 1 - 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/internal/service/teams/data_source_teams.go b/internal/service/teams/data_source_teams.go index 013c4f72a1..5ad86ef3dc 100644 --- a/internal/service/teams/data_source_teams.go +++ b/internal/service/teams/data_source_teams.go @@ -10,7 +10,7 @@ import ( "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" - matlas "go.mongodb.org/atlas/mongodbatlas" + "go.mongodb.org/atlas-sdk/v20231115005/admin" ) func DataSource() *schema.Resource { @@ -46,13 +46,13 @@ func DataSource() *schema.Resource { func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { var ( - conn = meta.(*config.MongoDBClient).Atlas + connV2 = meta.(*config.MongoDBClient).AtlasV2 orgID = d.Get("org_id").(string) teamID, teamIDOk = d.GetOk("team_id") name, nameOk = d.GetOk("name") err error - team *matlas.Team + team *admin.TeamResponse ) if !teamIDOk && !nameOk { @@ -60,32 +60,32 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. } if teamIDOk { - team, _, err = conn.Teams.Get(ctx, orgID, teamID.(string)) + team, _, err = connV2.TeamsApi.GetTeamById(ctx, orgID, teamID.(string)).Execute() } else { - team, _, err = conn.Teams.GetOneTeamByName(ctx, orgID, name.(string)) + team, _, err = connV2.TeamsApi.GetTeamByName(ctx, orgID, name.(string)).Execute() } if err != nil { return diag.FromErr(fmt.Errorf(errorTeamRead, err)) } - if err := d.Set("team_id", team.ID); err != nil { + if err := d.Set("team_id", team.GetId()); err != nil { return diag.FromErr(fmt.Errorf(errorTeamSetting, "name", d.Id(), err)) } - if err := d.Set("name", team.Name); err != nil { + if err := d.Set("name", team.GetName()); err != nil { return diag.FromErr(fmt.Errorf(errorTeamSetting, "name", d.Id(), err)) } // Set Usernames - users, _, err := conn.Teams.GetTeamUsersAssigned(ctx, orgID, team.ID) + users, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, team.GetId()).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamRead, err)) } usernames := []string{} - for i := range users { - usernames = append(usernames, users[i].Username) + for i := range users.GetResults() { + usernames = append(usernames, users.GetResults()[i].GetUsername()) } if err := d.Set("usernames", usernames); err != nil { @@ -94,7 +94,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. d.SetId(conversion.EncodeStateID(map[string]string{ "org_id": orgID, - "id": team.ID, + "id": team.GetId(), })) return nil diff --git a/internal/service/teams/resource_teams.go b/internal/service/teams/resource_teams.go index 1789e07349..da4134b6a3 100644 --- a/internal/service/teams/resource_teams.go +++ b/internal/service/teams/resource_teams.go @@ -89,7 +89,6 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di orgID := ids["org_id"] teamID := ids["id"] - // team, resp, err := conn.Teams.Get(context.Background(), orgID, teamID) team, resp, err := connV2.TeamsApi.GetTeamById(context.Background(), orgID, teamID).Execute() if err != nil { From f3c38cb319b95a2f46380d37ae18a8a981c70156 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 11:51:42 +0100 Subject: [PATCH 06/14] fix team rename --- internal/service/teams/resource_teams.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/service/teams/resource_teams.go b/internal/service/teams/resource_teams.go index da4134b6a3..fe3e615e7d 100644 --- a/internal/service/teams/resource_teams.go +++ b/internal/service/teams/resource_teams.go @@ -137,7 +137,6 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. _, _, err := connV2.TeamsApi.RenameTeam(ctx, orgID, teamID, &admin.Team{ Name: d.Get("name").(string), - Usernames: conversion.ExpandStringPointerListFromSetSchema(d.Get("usernames").(*schema.Set)), }).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamUpdate, err)) From 6fe1131e7b0bb073c996227647d7a2f2b0d13665 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 11:58:33 +0100 Subject: [PATCH 07/14] lint --- internal/service/teams/resource_teams.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/service/teams/resource_teams.go b/internal/service/teams/resource_teams.go index fe3e615e7d..20d553d8ef 100644 --- a/internal/service/teams/resource_teams.go +++ b/internal/service/teams/resource_teams.go @@ -135,9 +135,8 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. if d.HasChange("name") { _, _, err := connV2.TeamsApi.RenameTeam(ctx, orgID, teamID, - &admin.Team{ - Name: d.Get("name").(string), - }).Execute() + &admin.Team{Name: d.Get("name").(string)}, + ).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamUpdate, err)) } From a03758ad694440c83a011c945f7ee097cfe663ea Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 14:36:17 +0100 Subject: [PATCH 08/14] check error using SDK v2 way --- internal/service/teams/resource_teams.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/teams/resource_teams.go b/internal/service/teams/resource_teams.go index 20d553d8ef..c3efab97d5 100644 --- a/internal/service/teams/resource_teams.go +++ b/internal/service/teams/resource_teams.go @@ -227,7 +227,7 @@ func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag. err := retry.RetryContext(ctx, 1*time.Hour, func() *retry.RetryError { _, _, err := connV2.TeamsApi.DeleteTeam(ctx, orgID, id).Execute() if err != nil { - if errors.Is(err, errors.New("CANNOT_DELETE_TEAM_ASSIGNED_TO_PROJECT")) { + if admin.IsErrorCode(err, "CANNOT_DELETE_TEAM_ASSIGNED_TO_PROJECT") { projectID, err := getProjectIDByTeamID(ctx, connV2, id) if err != nil { return retry.NonRetryableError(err) From e5b7c3a14cbdd045f553336777a63c2fd1b2187a Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 15:26:34 +0100 Subject: [PATCH 09/14] remove unnecessary comments and method --- internal/common/conversion/misc.go | 8 -------- internal/service/teams/data_source_teams.go | 1 - internal/service/teams/resource_teams.go | 18 ++---------------- 3 files changed, 2 insertions(+), 25 deletions(-) diff --git a/internal/common/conversion/misc.go b/internal/common/conversion/misc.go index 441946768e..f13ed2afef 100644 --- a/internal/common/conversion/misc.go +++ b/internal/common/conversion/misc.go @@ -45,11 +45,3 @@ func ExpandStringListFromSetSchema(set *schema.Set) []string { } return res } - -func ExpandStringPointerListFromSetSchema(set *schema.Set) *[]string { - res := make([]string, set.Len()) - for i, v := range set.List() { - res[i] = v.(string) - } - return &res -} diff --git a/internal/service/teams/data_source_teams.go b/internal/service/teams/data_source_teams.go index 5ad86ef3dc..1b6f484446 100644 --- a/internal/service/teams/data_source_teams.go +++ b/internal/service/teams/data_source_teams.go @@ -77,7 +77,6 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(errorTeamSetting, "name", d.Id(), err)) } - // Set Usernames users, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, team.GetId()).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamRead, err)) diff --git a/internal/service/teams/resource_teams.go b/internal/service/teams/resource_teams.go index c3efab97d5..01959c7414 100644 --- a/internal/service/teams/resource_teams.go +++ b/internal/service/teams/resource_teams.go @@ -64,11 +64,11 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag. connV2 := meta.(*config.MongoDBClient).AtlasV2 orgID := d.Get("org_id").(string) - // Creating the team + usernames := conversion.ExpandStringListFromSetSchema(d.Get("usernames").(*schema.Set)) teamsResp, _, err := connV2.TeamsApi.CreateTeam(ctx, orgID, &admin.Team{ Name: d.Get("name").(string), - Usernames: conversion.ExpandStringPointerListFromSetSchema(d.Get("usernames").(*schema.Set)), + Usernames: &usernames, }).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamCreate, err)) @@ -92,7 +92,6 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di team, resp, err := connV2.TeamsApi.GetTeamById(context.Background(), orgID, teamID).Execute() if err != nil { - // new resource missing if resp != nil && resp.StatusCode == http.StatusNotFound { d.SetId("") return nil @@ -108,7 +107,6 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(errorTeamSetting, "team_id", teamID, err)) } - // Set Usernames users, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, teamID).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamRead, err)) @@ -143,17 +141,12 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. } if d.HasChange("usernames") { - // First, we need to remove the current users of the team and later add the new users - // Get the current team's users users, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, teamID).Execute() if err != nil { return diag.FromErr(fmt.Errorf(errorTeamRead, err)) } - // Removing each user - Let's not modify the state before making sure we can continue - - // existig users index := make(map[string]admin.CloudAppUser) for i := range users.GetResults() { index[users.GetResults()[i].GetUsername()] = users.GetResults()[i] @@ -169,9 +162,6 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. return nil } - // existing users - - // Verify if the gave users exists var newUsers []admin.AddUserToTeam for _, username := range d.Get("usernames").(*schema.Set).List() { @@ -180,9 +170,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. updatedUserData := user if err != nil { - // this must be handle as a soft error if !strings.Contains(err.Error(), "401") { - // In this case is a hard error doing a rollback from the initial operation return diag.FromErr(fmt.Errorf("error getting Atlas User (%s) information: %s", username, err)) } @@ -199,11 +187,9 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. updatedUserData = &cached } } - // if the user exists, we will storage its teamID newUsers = append(newUsers, admin.AddUserToTeam{Id: updatedUserData.GetId()}) } - // Update the users, remove the old ones, add the new ones err = cleanUsers() if err != nil { return diag.FromErr(err) From 5d7a1b2abf5aa5f75fff5f9eeadab7cbeff2dd14 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 15:29:12 +0100 Subject: [PATCH 10/14] use usernames array variable --- internal/service/teams/resource_teams_test.go | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/internal/service/teams/resource_teams_test.go b/internal/service/teams/resource_teams_test.go index 01b6fabe6d..f2ffec591a 100644 --- a/internal/service/teams/resource_teams_test.go +++ b/internal/service/teams/resource_teams_test.go @@ -23,7 +23,7 @@ func TestAccConfigRSTeam_basic(t *testing.T) { orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") name = fmt.Sprintf("test-acc-%s", acctest.RandString(10)) updatedName = fmt.Sprintf("test-acc-%s", acctest.RandString(10)) - username = os.Getenv("MONGODB_ATLAS_USERNAME") + usernames = []string{os.Getenv("MONGODB_ATLAS_USERNAME")} ) resource.ParallelTest(t, resource.TestCase{ @@ -32,11 +32,7 @@ func TestAccConfigRSTeam_basic(t *testing.T) { CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ { - Config: configBasic(orgID, name, - []string{ - username, - }, - ), + Config: configBasic(orgID, name, usernames), Check: resource.ComposeTestCheckFunc( checkExists(resourceName, &team), resource.TestCheckResourceAttrSet(resourceName, "org_id"), @@ -45,11 +41,7 @@ func TestAccConfigRSTeam_basic(t *testing.T) { ), }, { - Config: configBasic(orgID, updatedName, - []string{ - username, - }, - ), + Config: configBasic(orgID, updatedName, usernames), Check: resource.ComposeTestCheckFunc( checkExists(resourceName, &team), resource.TestCheckResourceAttrSet(resourceName, "org_id"), @@ -58,11 +50,7 @@ func TestAccConfigRSTeam_basic(t *testing.T) { ), }, { - Config: configBasic(orgID, updatedName, - []string{ - username, - }, - ), + Config: configBasic(orgID, updatedName, usernames), Check: resource.ComposeTestCheckFunc( checkExists(resourceName, &team), resource.TestCheckResourceAttrSet(resourceName, "org_id"), From 6d353c4b81f3c32bb8c17e56eafd8b95570942f3 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 15:32:35 +0100 Subject: [PATCH 11/14] use PreCheckAtlasUsername in resource tests --- internal/service/teams/resource_teams_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/teams/resource_teams_test.go b/internal/service/teams/resource_teams_test.go index f2ffec591a..40b88eb2f3 100644 --- a/internal/service/teams/resource_teams_test.go +++ b/internal/service/teams/resource_teams_test.go @@ -27,7 +27,7 @@ func TestAccConfigRSTeam_basic(t *testing.T) { ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, + PreCheck: func() { acc.PreCheckAtlasUsername(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ @@ -71,7 +71,7 @@ func TestAccConfigRSTeam_importBasic(t *testing.T) { ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, + PreCheck: func() { acc.PreCheckAtlasUsername(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ From cd358ff8ddb3c5b418d6d057522ca529c08dec14 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 15:36:53 +0100 Subject: [PATCH 12/14] precheck checks atlas user --- internal/service/teams/data_source_teams_test.go | 4 ++-- internal/service/teams/resource_teams_migration_test.go | 2 +- internal/testutil/mig/pre_check.go | 6 ++++++ internal/testutil/mig/provider.go | 7 +++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/service/teams/data_source_teams_test.go b/internal/service/teams/data_source_teams_test.go index 5c03b95c87..26770b527b 100644 --- a/internal/service/teams/data_source_teams_test.go +++ b/internal/service/teams/data_source_teams_test.go @@ -19,7 +19,7 @@ func TestAccConfigDSTeam_basic(t *testing.T) { ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, + PreCheck: func() { acc.PreCheckAtlasUsername(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ @@ -45,7 +45,7 @@ func TestAccConfigDSTeamByName_basic(t *testing.T) { ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, + PreCheck: func() { acc.PreCheckAtlasUsername(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ diff --git a/internal/service/teams/resource_teams_migration_test.go b/internal/service/teams/resource_teams_migration_test.go index 8b2bcb420d..a0a1d4385f 100644 --- a/internal/service/teams/resource_teams_migration_test.go +++ b/internal/service/teams/resource_teams_migration_test.go @@ -23,7 +23,7 @@ func TestAccMigrationTeams_basic(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { mig.PreCheckBasic(t) }, + PreCheck: func() { mig.PreCheckAtlasUsername(t) }, CheckDestroy: acc.CheckDestroyTeam, Steps: []resource.TestStep{ { diff --git a/internal/testutil/mig/pre_check.go b/internal/testutil/mig/pre_check.go index e7574b0bb8..a6c5da177e 100644 --- a/internal/testutil/mig/pre_check.go +++ b/internal/testutil/mig/pre_check.go @@ -22,3 +22,9 @@ func PreCheckBasicOwnerID(tb testing.TB) { tb.Helper() PreCheckBasic(tb) } + +func PreCheckAtlasUsername(tb testing.TB) { + tb.Helper() + checkAtlasUser(tb) + PreCheckBasic(tb) +} diff --git a/internal/testutil/mig/provider.go b/internal/testutil/mig/provider.go index b22f828a37..b8a8f39bcf 100644 --- a/internal/testutil/mig/provider.go +++ b/internal/testutil/mig/provider.go @@ -40,3 +40,10 @@ func checkLastVersion(tb testing.TB) { func versionConstraint() string { return os.Getenv("MONGODB_ATLAS_LAST_VERSION") } + +func checkAtlasUser(tb testing.TB) { + tb.Helper() + if os.Getenv("MONGODB_ATLAS_USERNAME") == "" { + tb.Fatal("`MONGODB_ATLAS_USERNAME` must be set ") + } +} From 05c4820e133eb1d2194cc2ff0dea3c1a2394cf0a Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 15:43:14 +0100 Subject: [PATCH 13/14] remove teams parameter in checkExists --- .../service/teams/resource_teams_migration_test.go | 4 +--- internal/service/teams/resource_teams_test.go | 14 +++++--------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/internal/service/teams/resource_teams_migration_test.go b/internal/service/teams/resource_teams_migration_test.go index a0a1d4385f..84a53357fd 100644 --- a/internal/service/teams/resource_teams_migration_test.go +++ b/internal/service/teams/resource_teams_migration_test.go @@ -10,12 +10,10 @@ import ( "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig" - "go.mongodb.org/atlas-sdk/v20231115005/admin" ) func TestAccMigrationTeams_basic(t *testing.T) { var ( - team admin.Team resourceName = "mongodbatlas_teams.test" orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") name = fmt.Sprintf("test-acc-%s", acctest.RandString(10)) @@ -30,7 +28,7 @@ func TestAccMigrationTeams_basic(t *testing.T) { ExternalProviders: mig.ExternalProviders(), Config: configBasic(orgID, name, []string{username}), Check: resource.ComposeTestCheckFunc( - checkExists(resourceName, &team), + checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", name), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), diff --git a/internal/service/teams/resource_teams_test.go b/internal/service/teams/resource_teams_test.go index 40b88eb2f3..85160360f2 100644 --- a/internal/service/teams/resource_teams_test.go +++ b/internal/service/teams/resource_teams_test.go @@ -13,12 +13,10 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" - "go.mongodb.org/atlas-sdk/v20231115005/admin" ) func TestAccConfigRSTeam_basic(t *testing.T) { var ( - team admin.Team resourceName = "mongodbatlas_teams.test" orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") name = fmt.Sprintf("test-acc-%s", acctest.RandString(10)) @@ -34,7 +32,7 @@ func TestAccConfigRSTeam_basic(t *testing.T) { { Config: configBasic(orgID, name, usernames), Check: resource.ComposeTestCheckFunc( - checkExists(resourceName, &team), + checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", name), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), @@ -43,7 +41,7 @@ func TestAccConfigRSTeam_basic(t *testing.T) { { Config: configBasic(orgID, updatedName, usernames), Check: resource.ComposeTestCheckFunc( - checkExists(resourceName, &team), + checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", updatedName), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), @@ -52,7 +50,7 @@ func TestAccConfigRSTeam_basic(t *testing.T) { { Config: configBasic(orgID, updatedName, usernames), Check: resource.ComposeTestCheckFunc( - checkExists(resourceName, &team), + checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "org_id"), resource.TestCheckResourceAttr(resourceName, "name", updatedName), resource.TestCheckResourceAttr(resourceName, "usernames.#", "1"), @@ -98,7 +96,7 @@ func TestAccConfigRSTeam_importBasic(t *testing.T) { }) } -func checkExists(resourceName string, team *admin.Team) resource.TestCheckFunc { +func checkExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -112,10 +110,8 @@ func checkExists(resourceName string, team *admin.Team) resource.TestCheckFunc { } log.Printf("[DEBUG] orgID: %s", orgID) log.Printf("[DEBUG] teamID: %s", id) - teamResp, _, err := acc.ConnV2().TeamsApi.GetTeamById(context.Background(), orgID, id).Execute() + _, _, err := acc.ConnV2().TeamsApi.GetTeamById(context.Background(), orgID, id).Execute() if err == nil { - team.Id = teamResp.Id - team.Name = teamResp.GetName() return nil } return fmt.Errorf("team(%s) does not exist", id) From 8f13d90f4f6b25ae991c96ef117941ee0fd18a24 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Mon, 5 Feb 2024 15:45:35 +0100 Subject: [PATCH 14/14] refactor check for mig --- internal/testutil/mig/pre_check.go | 4 ++-- internal/testutil/mig/provider.go | 7 ------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/internal/testutil/mig/pre_check.go b/internal/testutil/mig/pre_check.go index 203c32cd3b..261a10ee21 100644 --- a/internal/testutil/mig/pre_check.go +++ b/internal/testutil/mig/pre_check.go @@ -31,6 +31,6 @@ func PreCheckCert(tb testing.TB) { func PreCheckAtlasUsername(tb testing.TB) { tb.Helper() - checkAtlasUser(tb) - PreCheckBasic(tb) + checkLastVersion(tb) + acc.PreCheckAtlasUsername(tb) } diff --git a/internal/testutil/mig/provider.go b/internal/testutil/mig/provider.go index b8a8f39bcf..b22f828a37 100644 --- a/internal/testutil/mig/provider.go +++ b/internal/testutil/mig/provider.go @@ -40,10 +40,3 @@ func checkLastVersion(tb testing.TB) { func versionConstraint() string { return os.Getenv("MONGODB_ATLAS_LAST_VERSION") } - -func checkAtlasUser(tb testing.TB) { - tb.Helper() - if os.Getenv("MONGODB_ATLAS_USERNAME") == "" { - tb.Fatal("`MONGODB_ATLAS_USERNAME` must be set ") - } -}