Skip to content

Commit

Permalink
chore: Upgrades teams resource to auto-generated SDK (#1905)
Browse files Browse the repository at this point in the history
* rename CRUD methods

* rename methods for clarity

* remove redundant check

* initial migratation to new SDK

* migrate data source

* fix team rename

* lint

* check error using SDK v2 way

* remove unnecessary comments and method

* use usernames array variable

* use PreCheckAtlasUsername in resource tests

* precheck checks atlas user

* remove teams parameter in checkExists

* refactor check for mig
  • Loading branch information
oarbusi authored Feb 5, 2024
1 parent 026150c commit e519c3c
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 133 deletions.
27 changes: 13 additions & 14 deletions internal/service/teams/data_source_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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 {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasTeamRead,
ReadContext: dataSourceRead,
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -44,48 +44,47 @@ 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
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 {
return diag.FromErr(errors.New("either team_id or name must be configured"))
}

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 {
Expand All @@ -94,7 +93,7 @@ func dataSourceMongoDBAtlasTeamRead(ctx context.Context, d *schema.ResourceData,

d.SetId(conversion.EncodeStateID(map[string]string{
"org_id": orgID,
"id": team.ID,
"id": team.GetId(),
}))

return nil
Expand Down
12 changes: 6 additions & 6 deletions internal/service/teams/data_source_teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ 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{
{
Config: testAccDataSourceMongoDBAtlasTeamConfig(orgID, name, username),
Config: dataSourceConfigBasic(orgID, name, username),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "team_id"),
Expand All @@ -45,12 +45,12 @@ 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{
{
Config: testAccDataSourceMongoDBAtlasTeamConfigByName(orgID, name, username),
Config: dataSourceConfigBasicByName(orgID, name, username),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "team_id"),
Expand All @@ -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"
Expand All @@ -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"
Expand Down
Loading

0 comments on commit e519c3c

Please sign in to comment.