From 97896cb827aac472c0d41182764987ad08242403 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:00:36 +0100 Subject: [PATCH 01/11] rename --- .../data_source_project_invitation.go | 4 +- .../data_source_project_invitation_test.go | 2 +- .../resource_project_invitation.go | 96 +++++++++---------- .../resource_project_invitation_test.go | 24 ++--- 4 files changed, 63 insertions(+), 63 deletions(-) diff --git a/internal/service/projectinvitation/data_source_project_invitation.go b/internal/service/projectinvitation/data_source_project_invitation.go index 132fbddb52..83044e4691 100644 --- a/internal/service/projectinvitation/data_source_project_invitation.go +++ b/internal/service/projectinvitation/data_source_project_invitation.go @@ -12,7 +12,7 @@ import ( func DataSource() *schema.Resource { return &schema.Resource{ - ReadContext: dataSourceMongoDBAtlasProjectInvitationRead, + ReadContext: dataSourceRead, Schema: map[string]*schema.Schema{ "project_id": { Type: schema.TypeString, @@ -49,7 +49,7 @@ func DataSource() *schema.Resource { } } -func dataSourceMongoDBAtlasProjectInvitationRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { +func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { // Get client connection. conn := meta.(*config.MongoDBClient).Atlas projectID := d.Get("project_id").(string) diff --git a/internal/service/projectinvitation/data_source_project_invitation_test.go b/internal/service/projectinvitation/data_source_project_invitation_test.go index 9875cedd63..9556458673 100644 --- a/internal/service/projectinvitation/data_source_project_invitation_test.go +++ b/internal/service/projectinvitation/data_source_project_invitation_test.go @@ -23,7 +23,7 @@ func TestAccProjectDSProjectInvitation_basic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - CheckDestroy: testAccCheckMongoDBAtlasProjectInvitationDestroy, + CheckDestroy: checkDestroy, Steps: []resource.TestStep{ { Config: testAccDataSourceMongoDBAtlasProjectInvitationConfig(orgID, projectName, name, initialRole), diff --git a/internal/service/projectinvitation/resource_project_invitation.go b/internal/service/projectinvitation/resource_project_invitation.go index c7aaa53198..3278b60347 100644 --- a/internal/service/projectinvitation/resource_project_invitation.go +++ b/internal/service/projectinvitation/resource_project_invitation.go @@ -16,12 +16,12 @@ import ( func Resource() *schema.Resource { return &schema.Resource{ - CreateContext: resourceMongoDBAtlasProjectInvitationCreate, - ReadContext: resourceMongoDBAtlasProjectInvitationRead, - DeleteContext: resourceMongoDBAtlasProjectInvitationDelete, - UpdateContext: resourceMongoDBAtlasProjectInvitationUpdate, + CreateContext: resourceCreate, + ReadContext: resourceRead, + UpdateContext: resourceUpdate, + DeleteContext: resourceDelete, Importer: &schema.ResourceImporter{ - StateContext: resourceMongoDBAtlasProjectInvitationImportState, + StateContext: resourceImport, }, Schema: map[string]*schema.Schema{ "project_id": { @@ -61,7 +61,46 @@ func Resource() *schema.Resource { } } -func resourceMongoDBAtlasProjectInvitationRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { +func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { + // Get client connection. + conn := meta.(*config.MongoDBClient).Atlas + projectID := d.Get("project_id").(string) + + invitationReq := &matlas.Invitation{ + Roles: createProjectStringListFromSetSchema(d.Get("roles").(*schema.Set)), + Username: d.Get("username").(string), + } + + invitationRes, _, err := conn.Projects.InviteUser(ctx, projectID, invitationReq) + if err != nil { + return diag.FromErr(fmt.Errorf("error creating Project invitation for user %s: %w", d.Get("username").(string), err)) + } + + d.SetId(conversion.EncodeStateID(map[string]string{ + "username": invitationRes.Username, + "project_id": invitationRes.GroupID, + "invitation_id": invitationRes.ID, + })) + + return resourceRead(ctx, d, meta) +} + +func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { + conn := meta.(*config.MongoDBClient).Atlas + ids := conversion.DecodeStateID(d.Id()) + projectID := ids["project_id"] + username := ids["username"] + invitationID := ids["invitation_id"] + + _, err := conn.Projects.DeleteInvitation(ctx, projectID, invitationID) + if err != nil { + return diag.FromErr(fmt.Errorf("error deleting Project invitation for user %s: %w", username, err)) + } + + return nil +} + +func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { // Get client connection. conn := meta.(*config.MongoDBClient).Atlas ids := conversion.DecodeStateID(d.Id()) @@ -118,46 +157,7 @@ func resourceMongoDBAtlasProjectInvitationRead(ctx context.Context, d *schema.Re return nil } -func resourceMongoDBAtlasProjectInvitationCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - // Get client connection. - conn := meta.(*config.MongoDBClient).Atlas - projectID := d.Get("project_id").(string) - - invitationReq := &matlas.Invitation{ - Roles: createProjectStringListFromSetSchema(d.Get("roles").(*schema.Set)), - Username: d.Get("username").(string), - } - - invitationRes, _, err := conn.Projects.InviteUser(ctx, projectID, invitationReq) - if err != nil { - return diag.FromErr(fmt.Errorf("error creating Project invitation for user %s: %w", d.Get("username").(string), err)) - } - - d.SetId(conversion.EncodeStateID(map[string]string{ - "username": invitationRes.Username, - "project_id": invitationRes.GroupID, - "invitation_id": invitationRes.ID, - })) - - return resourceMongoDBAtlasProjectInvitationRead(ctx, d, meta) -} - -func resourceMongoDBAtlasProjectInvitationDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - conn := meta.(*config.MongoDBClient).Atlas - ids := conversion.DecodeStateID(d.Id()) - projectID := ids["project_id"] - username := ids["username"] - invitationID := ids["invitation_id"] - - _, err := conn.Projects.DeleteInvitation(ctx, projectID, invitationID) - if err != nil { - return diag.FromErr(fmt.Errorf("error deleting Project invitation for user %s: %w", username, err)) - } - - return nil -} - -func resourceMongoDBAtlasProjectInvitationUpdate(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()) projectID := ids["project_id"] @@ -173,10 +173,10 @@ func resourceMongoDBAtlasProjectInvitationUpdate(ctx context.Context, d *schema. return diag.FromErr(fmt.Errorf("error updating Project invitation for user %s: %w", username, err)) } - return resourceMongoDBAtlasProjectInvitationRead(ctx, d, meta) + return resourceRead(ctx, d, meta) } -func resourceMongoDBAtlasProjectInvitationImportState(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 projectID, username, err := splitProjectInvitationImportID(d.Id()) if err != nil { diff --git a/internal/service/projectinvitation/resource_project_invitation_test.go b/internal/service/projectinvitation/resource_project_invitation_test.go index bc7eb5b936..ab1356086e 100644 --- a/internal/service/projectinvitation/resource_project_invitation_test.go +++ b/internal/service/projectinvitation/resource_project_invitation_test.go @@ -29,12 +29,12 @@ func TestAccProjectRSProjectInvitation_basic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - CheckDestroy: testAccCheckMongoDBAtlasProjectInvitationDestroy, + CheckDestroy: checkDestroy, Steps: []resource.TestStep{ { - Config: testAccMongoDBAtlasProjectInvitationConfig(orgID, projectName, name, initialRole), + Config: configBasic(orgID, projectName, name, initialRole), Check: resource.ComposeTestCheckFunc( - testAccCheckMongoDBAtlasProjectInvitationExists(t, resourceName, &invitation), + checkExists(t, resourceName, &invitation), testAccCheckMongoDBAtlasProjectInvitationUsernameAttribute(&invitation, name), testAccCheckMongoDBAtlasProjectInvitationRoleAttribute(&invitation, initialRole), resource.TestCheckResourceAttrSet(resourceName, "project_id"), @@ -46,9 +46,9 @@ func TestAccProjectRSProjectInvitation_basic(t *testing.T) { ), }, { - Config: testAccMongoDBAtlasProjectInvitationConfig(orgID, projectName, name, updateRoles), + Config: configBasic(orgID, projectName, name, updateRoles), Check: resource.ComposeTestCheckFunc( - testAccCheckMongoDBAtlasProjectInvitationExists(t, resourceName, &invitation), + checkExists(t, resourceName, &invitation), testAccCheckMongoDBAtlasProjectInvitationUsernameAttribute(&invitation, name), testAccCheckMongoDBAtlasProjectInvitationRoleAttribute(&invitation, updateRoles), resource.TestCheckResourceAttrSet(resourceName, "project_id"), @@ -75,10 +75,10 @@ func TestAccProjectRSProjectInvitation_importBasic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - CheckDestroy: testAccCheckMongoDBAtlasProjectInvitationDestroy, + CheckDestroy: checkDestroy, Steps: []resource.TestStep{ { - Config: testAccMongoDBAtlasProjectInvitationConfig(orgID, projectName, name, initialRole), + Config: configBasic(orgID, projectName, name, initialRole), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(resourceName, "project_id"), resource.TestCheckResourceAttrSet(resourceName, "username"), @@ -90,7 +90,7 @@ func TestAccProjectRSProjectInvitation_importBasic(t *testing.T) { }, { ResourceName: resourceName, - ImportStateIdFunc: testAccCheckMongoDBAtlasProjectInvitationStateIDFunc(resourceName), + ImportStateIdFunc: importStateIDFunc(resourceName), ImportState: true, ImportStateVerify: true, }, @@ -98,7 +98,7 @@ func TestAccProjectRSProjectInvitation_importBasic(t *testing.T) { }) } -func testAccCheckMongoDBAtlasProjectInvitationExists(t *testing.T, resourceName string, invitation *matlas.Invitation) resource.TestCheckFunc { +func checkExists(t *testing.T, resourceName string, invitation *matlas.Invitation) resource.TestCheckFunc { t.Helper() return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] @@ -149,7 +149,7 @@ func testAccCheckMongoDBAtlasProjectInvitationRoleAttribute(invitation *matlas.I } } -func testAccCheckMongoDBAtlasProjectInvitationDestroy(s *terraform.State) error { +func checkDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "mongodbatlas_invitations" { continue @@ -167,7 +167,7 @@ func testAccCheckMongoDBAtlasProjectInvitationDestroy(s *terraform.State) error return nil } -func testAccCheckMongoDBAtlasProjectInvitationStateIDFunc(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 { @@ -177,7 +177,7 @@ func testAccCheckMongoDBAtlasProjectInvitationStateIDFunc(resourceName string) r } } -func testAccMongoDBAtlasProjectInvitationConfig(orgID, projectName, username string, roles []string) string { +func configBasic(orgID, projectName, username string, roles []string) string { return fmt.Sprintf(` resource "mongodbatlas_project" "test" { name = %[2]q From c1956dbc6d0e9691c35c99376fe75aa86dfc414c Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:04:06 +0100 Subject: [PATCH 02/11] remove unneeded test checked functions --- .../resource_project_invitation_test.go | 39 ++----------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/internal/service/projectinvitation/resource_project_invitation_test.go b/internal/service/projectinvitation/resource_project_invitation_test.go index ab1356086e..adce71f1a7 100644 --- a/internal/service/projectinvitation/resource_project_invitation_test.go +++ b/internal/service/projectinvitation/resource_project_invitation_test.go @@ -35,28 +35,23 @@ func TestAccProjectRSProjectInvitation_basic(t *testing.T) { Config: configBasic(orgID, projectName, name, initialRole), Check: resource.ComposeTestCheckFunc( checkExists(t, resourceName, &invitation), - testAccCheckMongoDBAtlasProjectInvitationUsernameAttribute(&invitation, name), - testAccCheckMongoDBAtlasProjectInvitationRoleAttribute(&invitation, initialRole), resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "username"), resource.TestCheckResourceAttrSet(resourceName, "invitation_id"), - resource.TestCheckResourceAttrSet(resourceName, "roles.#"), resource.TestCheckResourceAttr(resourceName, "username", name), resource.TestCheckResourceAttr(resourceName, "roles.#", "1"), + resource.TestCheckTypeSetElemAttr(resourceName, "roles.*", initialRole[0]), ), }, { Config: configBasic(orgID, projectName, name, updateRoles), Check: resource.ComposeTestCheckFunc( checkExists(t, resourceName, &invitation), - testAccCheckMongoDBAtlasProjectInvitationUsernameAttribute(&invitation, name), - testAccCheckMongoDBAtlasProjectInvitationRoleAttribute(&invitation, updateRoles), resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "username"), resource.TestCheckResourceAttrSet(resourceName, "invitation_id"), - resource.TestCheckResourceAttrSet(resourceName, "roles.#"), resource.TestCheckResourceAttr(resourceName, "username", name), resource.TestCheckResourceAttr(resourceName, "roles.#", "2"), + resource.TestCheckTypeSetElemAttr(resourceName, "roles.*", updateRoles[0]), + resource.TestCheckTypeSetElemAttr(resourceName, "roles.*", updateRoles[0]), ), }, }, @@ -81,11 +76,10 @@ func TestAccProjectRSProjectInvitation_importBasic(t *testing.T) { Config: configBasic(orgID, projectName, name, initialRole), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "username"), - resource.TestCheckResourceAttrSet(resourceName, "roles.#"), resource.TestCheckResourceAttrSet(resourceName, "invitation_id"), resource.TestCheckResourceAttr(resourceName, "username", name), resource.TestCheckResourceAttr(resourceName, "roles.#", "1"), + resource.TestCheckTypeSetElemAttr(resourceName, "roles.*", initialRole[0]), ), }, { @@ -124,31 +118,6 @@ func checkExists(t *testing.T, resourceName string, invitation *matlas.Invitatio } } -func testAccCheckMongoDBAtlasProjectInvitationUsernameAttribute(invitation *matlas.Invitation, username string) resource.TestCheckFunc { - return func(s *terraform.State) error { - if invitation.Username != username { - return fmt.Errorf("bad name: %s", invitation.Username) - } - return nil - } -} - -func testAccCheckMongoDBAtlasProjectInvitationRoleAttribute(invitation *matlas.Invitation, roles []string) resource.TestCheckFunc { - return func(s *terraform.State) error { - if len(roles) > 0 { - for _, role := range roles { - for _, currentRole := range invitation.Roles { - if currentRole == role { - return nil - } - } - } - } - - return fmt.Errorf("bad role: %s", invitation.Roles) - } -} - func checkDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "mongodbatlas_invitations" { From 7633b27b675a32c79d1e92997f91fe0bf9998e0f Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:32:10 +0100 Subject: [PATCH 03/11] remove matlas in tests --- .../resource_project_invitation_test.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/internal/service/projectinvitation/resource_project_invitation_test.go b/internal/service/projectinvitation/resource_project_invitation_test.go index adce71f1a7..3e370bbd5e 100644 --- a/internal/service/projectinvitation/resource_project_invitation_test.go +++ b/internal/service/projectinvitation/resource_project_invitation_test.go @@ -12,12 +12,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" - matlas "go.mongodb.org/atlas/mongodbatlas" ) func TestAccProjectRSProjectInvitation_basic(t *testing.T) { var ( - invitation matlas.Invitation resourceName = "mongodbatlas_project_invitation.test" orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") projectName = acctest.RandomWithPrefix("test-acc") @@ -34,7 +32,7 @@ func TestAccProjectRSProjectInvitation_basic(t *testing.T) { { Config: configBasic(orgID, projectName, name, initialRole), Check: resource.ComposeTestCheckFunc( - checkExists(t, resourceName, &invitation), + checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "project_id"), resource.TestCheckResourceAttrSet(resourceName, "invitation_id"), resource.TestCheckResourceAttr(resourceName, "username", name), @@ -45,7 +43,7 @@ func TestAccProjectRSProjectInvitation_basic(t *testing.T) { { Config: configBasic(orgID, projectName, name, updateRoles), Check: resource.ComposeTestCheckFunc( - checkExists(t, resourceName, &invitation), + checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "project_id"), resource.TestCheckResourceAttrSet(resourceName, "invitation_id"), resource.TestCheckResourceAttr(resourceName, "username", name), @@ -92,8 +90,7 @@ func TestAccProjectRSProjectInvitation_importBasic(t *testing.T) { }) } -func checkExists(t *testing.T, resourceName string, invitation *matlas.Invitation) resource.TestCheckFunc { - t.Helper() +func checkExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -106,12 +103,8 @@ func checkExists(t *testing.T, resourceName string, invitation *matlas.Invitatio if projectID == "" && username == "" && invitationID == "" { return fmt.Errorf("no ID is set") } - t.Logf("projectID: %s", projectID) - t.Logf("username: %s", username) - t.Logf("invitationID: %s", invitationID) - invitationResp, _, err := acc.Conn().Projects.Invitation(context.Background(), projectID, invitationID) + _, _, err := acc.Conn().Projects.Invitation(context.Background(), projectID, invitationID) if err == nil { - *invitation = *invitationResp return nil } return fmt.Errorf("invitation(%s) does not exist", invitationID) From 49f3a9a19005f5672ade9d8d947eb08f4452fa3d Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:38:21 +0100 Subject: [PATCH 04/11] new sdk in tests --- .../projectinvitation/resource_project_invitation_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/service/projectinvitation/resource_project_invitation_test.go b/internal/service/projectinvitation/resource_project_invitation_test.go index 3e370bbd5e..7b433cccc9 100644 --- a/internal/service/projectinvitation/resource_project_invitation_test.go +++ b/internal/service/projectinvitation/resource_project_invitation_test.go @@ -103,11 +103,11 @@ func checkExists(resourceName string) resource.TestCheckFunc { if projectID == "" && username == "" && invitationID == "" { return fmt.Errorf("no ID is set") } - _, _, err := acc.Conn().Projects.Invitation(context.Background(), projectID, invitationID) + _, _, err := acc.ConnV2().ProjectsApi.GetProjectInvitation(context.Background(), projectID, invitationID).Execute() if err == nil { return nil } - return fmt.Errorf("invitation(%s) does not exist", invitationID) + return fmt.Errorf("invitation (%s) does not exist", invitationID) } } @@ -120,8 +120,7 @@ func checkDestroy(s *terraform.State) error { projectID := ids["project_id"] invitationID := ids["invitation_id"] - // Try to find the invitation - _, _, err := acc.Conn().Projects.Invitation(context.Background(), projectID, invitationID) + _, _, err := acc.ConnV2().ProjectsApi.GetProjectInvitation(context.Background(), projectID, invitationID).Execute() if err == nil { return fmt.Errorf("invitation (%s) still exists", invitationID) } From 19a58780ee1598a333b3bf3a2123f673c752baaa Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:51:00 +0100 Subject: [PATCH 05/11] migration test --- ...ource_project_invitation_migration_test.go | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 internal/service/projectinvitation/resource_project_invitation_migration_test.go diff --git a/internal/service/projectinvitation/resource_project_invitation_migration_test.go b/internal/service/projectinvitation/resource_project_invitation_migration_test.go new file mode 100644 index 0000000000..74976fea68 --- /dev/null +++ b/internal/service/projectinvitation/resource_project_invitation_migration_test.go @@ -0,0 +1,53 @@ +package projectinvitation_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" +) + +func TestAccMigrationProjectInvitation_basic(t *testing.T) { + var ( + resourceName = "mongodbatlas_project_invitation.test" + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + projectName = acctest.RandomWithPrefix("test-acc") + name = fmt.Sprintf("test-acc-%s@mongodb.com", acctest.RandString(10)) + roles = []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_CLUSTER_MANAGER"} + ) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { mig.PreCheckBasic(t) }, + CheckDestroy: checkDestroy, + Steps: []resource.TestStep{ + { + ExternalProviders: mig.ExternalProviders(), + Config: configBasic(orgID, projectName, name, roles), + Check: resource.ComposeTestCheckFunc( + checkExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "project_id"), + resource.TestCheckResourceAttrSet(resourceName, "invitation_id"), + resource.TestCheckResourceAttr(resourceName, "username", name), + resource.TestCheckResourceAttr(resourceName, "roles.#", "2"), + resource.TestCheckTypeSetElemAttr(resourceName, "roles.*", roles[0]), + resource.TestCheckTypeSetElemAttr(resourceName, "roles.*", roles[1]), + ), + }, + { + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + Config: configBasic(orgID, projectName, name, roles), + ConfigPlanChecks: resource.ConfigPlanChecks{ + PreApply: []plancheck.PlanCheck{ + acc.DebugPlan(), + plancheck.ExpectEmptyPlan(), + }, + }, + }, + }, + }) +} From 960bc77f449e8da84cd6b31d1b60726e3e59891b Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:53:39 +0100 Subject: [PATCH 06/11] data source --- .../data_source_project_invitation.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/internal/service/projectinvitation/data_source_project_invitation.go b/internal/service/projectinvitation/data_source_project_invitation.go index 83044e4691..4757d719c8 100644 --- a/internal/service/projectinvitation/data_source_project_invitation.go +++ b/internal/service/projectinvitation/data_source_project_invitation.go @@ -50,42 +50,41 @@ func DataSource() *schema.Resource { } func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - // Get client connection. - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 projectID := d.Get("project_id").(string) username := d.Get("username").(string) invitationID := d.Get("invitation_id").(string) - projectInvitation, _, err := conn.Projects.Invitation(ctx, projectID, invitationID) + projectInvitation, _, err := connV2.ProjectsApi.GetProjectInvitation(ctx, projectID, invitationID).Execute() if err != nil { return diag.FromErr(fmt.Errorf("error getting Project Invitation information: %w", err)) } - if err := d.Set("username", projectInvitation.Username); err != nil { + if err := d.Set("username", projectInvitation.GetUsername()); err != nil { return diag.FromErr(fmt.Errorf("error getting `username` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("project_id", projectInvitation.GroupID); err != nil { + if err := d.Set("project_id", projectInvitation.GetGroupId()); err != nil { return diag.FromErr(fmt.Errorf("error getting `username` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("invitation_id", projectInvitation.ID); err != nil { + if err := d.Set("invitation_id", projectInvitation.GetId()); err != nil { return diag.FromErr(fmt.Errorf("error getting `invitation_id` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("expires_at", projectInvitation.ExpiresAt); err != nil { + if err := d.Set("expires_at", conversion.TimePtrToStringPtr(projectInvitation.ExpiresAt)); err != nil { return diag.FromErr(fmt.Errorf("error getting `expires_at` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("created_at", projectInvitation.CreatedAt); err != nil { + if err := d.Set("created_at", conversion.TimePtrToStringPtr(projectInvitation.CreatedAt)); err != nil { return diag.FromErr(fmt.Errorf("error getting `created_at` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("inviter_username", projectInvitation.InviterUsername); err != nil { + if err := d.Set("inviter_username", projectInvitation.GetInviterUsername()); err != nil { return diag.FromErr(fmt.Errorf("error getting `inviter_username` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("roles", projectInvitation.Roles); err != nil { + if err := d.Set("roles", projectInvitation.GetRoles()); err != nil { return diag.FromErr(fmt.Errorf("error getting `roles` for Project Invitation (%s): %s", d.Id(), err)) } From f7f9b9ad15f9edbeaf50040fbd231e0330c285ff Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 13:00:22 +0100 Subject: [PATCH 07/11] read --- .../resource_project_invitation.go | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/internal/service/projectinvitation/resource_project_invitation.go b/internal/service/projectinvitation/resource_project_invitation.go index 3278b60347..78f52efa3e 100644 --- a/internal/service/projectinvitation/resource_project_invitation.go +++ b/internal/service/projectinvitation/resource_project_invitation.go @@ -101,18 +101,15 @@ func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag. } func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - // Get client connection. - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 ids := conversion.DecodeStateID(d.Id()) projectID := ids["project_id"] username := ids["username"] invitationID := ids["invitation_id"] - projectInvitation, resp, err := conn.Projects.Invitation(ctx, projectID, invitationID) + projectInvitation, resp, err := connV2.ProjectsApi.GetProjectInvitation(ctx, projectID, invitationID).Execute() if err != nil { - // case 404 - // deleted in the backend case - if resp != nil && resp.StatusCode == http.StatusNotFound { + if resp != nil && resp.StatusCode == http.StatusNotFound { // case 404: deleted in the backend case d.SetId("") return nil } @@ -120,31 +117,31 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf("error getting Project Invitation information: %w", err)) } - if err := d.Set("username", projectInvitation.Username); err != nil { + if err := d.Set("username", projectInvitation.GetUsername()); err != nil { return diag.FromErr(fmt.Errorf("error getting `username` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("project_id", projectInvitation.GroupID); err != nil { + if err := d.Set("project_id", projectInvitation.GetGroupId()); err != nil { return diag.FromErr(fmt.Errorf("error getting `project_id` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("invitation_id", projectInvitation.ID); err != nil { + if err := d.Set("invitation_id", projectInvitation.GetId()); err != nil { return diag.FromErr(fmt.Errorf("error getting `invitation_id` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("expires_at", projectInvitation.ExpiresAt); err != nil { + if err := d.Set("expires_at", conversion.TimePtrToStringPtr(projectInvitation.ExpiresAt)); err != nil { return diag.FromErr(fmt.Errorf("error getting `expires_at` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("created_at", projectInvitation.CreatedAt); err != nil { + if err := d.Set("created_at", conversion.TimePtrToStringPtr(projectInvitation.CreatedAt)); err != nil { return diag.FromErr(fmt.Errorf("error getting `created_at` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("inviter_username", projectInvitation.InviterUsername); err != nil { + if err := d.Set("inviter_username", projectInvitation.GetInviterUsername()); err != nil { return diag.FromErr(fmt.Errorf("error getting `inviter_username` for Project Invitation (%s): %w", d.Id(), err)) } - if err := d.Set("roles", projectInvitation.Roles); err != nil { + if err := d.Set("roles", projectInvitation.GetRoles()); err != nil { return diag.FromErr(fmt.Errorf("error getting `roles` for Project Invitation (%s): %w", d.Id(), err)) } From cc135e223336beb449c2b01b226aee11ae9914c0 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 13:04:08 +0100 Subject: [PATCH 08/11] create --- .../resource_project_invitation.go | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/internal/service/projectinvitation/resource_project_invitation.go b/internal/service/projectinvitation/resource_project_invitation.go index 78f52efa3e..32788c41cb 100644 --- a/internal/service/projectinvitation/resource_project_invitation.go +++ b/internal/service/projectinvitation/resource_project_invitation.go @@ -11,6 +11,7 @@ import ( "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "go.mongodb.org/atlas-sdk/v20231115005/admin" matlas "go.mongodb.org/atlas/mongodbatlas" ) @@ -62,44 +63,29 @@ func Resource() *schema.Resource { } func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - // Get client connection. - conn := meta.(*config.MongoDBClient).Atlas + connV2 := meta.(*config.MongoDBClient).AtlasV2 projectID := d.Get("project_id").(string) - invitationReq := &matlas.Invitation{ - Roles: createProjectStringListFromSetSchema(d.Get("roles").(*schema.Set)), - Username: d.Get("username").(string), + roles := createProjectStringListFromSetSchema(d.Get("roles").(*schema.Set)) + invitationReq := &admin.GroupInvitationRequest{ + Roles: &roles, + Username: conversion.StringPtr(d.Get("username").(string)), } - invitationRes, _, err := conn.Projects.InviteUser(ctx, projectID, invitationReq) + invitationRes, _, err := connV2.ProjectsApi.CreateProjectInvitation(ctx, projectID, invitationReq).Execute() if err != nil { return diag.FromErr(fmt.Errorf("error creating Project invitation for user %s: %w", d.Get("username").(string), err)) } d.SetId(conversion.EncodeStateID(map[string]string{ - "username": invitationRes.Username, - "project_id": invitationRes.GroupID, - "invitation_id": invitationRes.ID, + "username": invitationRes.GetUsername(), + "project_id": invitationRes.GetGroupId(), + "invitation_id": invitationRes.GetId(), })) return resourceRead(ctx, d, meta) } -func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - conn := meta.(*config.MongoDBClient).Atlas - ids := conversion.DecodeStateID(d.Id()) - projectID := ids["project_id"] - username := ids["username"] - invitationID := ids["invitation_id"] - - _, err := conn.Projects.DeleteInvitation(ctx, projectID, invitationID) - if err != nil { - return diag.FromErr(fmt.Errorf("error deleting Project invitation for user %s: %w", username, err)) - } - - return nil -} - func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { connV2 := meta.(*config.MongoDBClient).AtlasV2 ids := conversion.DecodeStateID(d.Id()) @@ -173,6 +159,21 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. return resourceRead(ctx, d, meta) } +func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { + conn := meta.(*config.MongoDBClient).Atlas + ids := conversion.DecodeStateID(d.Id()) + projectID := ids["project_id"] + username := ids["username"] + invitationID := ids["invitation_id"] + + _, err := conn.Projects.DeleteInvitation(ctx, projectID, invitationID) + if err != nil { + return diag.FromErr(fmt.Errorf("error deleting Project invitation for user %s: %w", username, err)) + } + + return nil +} + func resourceImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { conn := meta.(*config.MongoDBClient).Atlas projectID, username, err := splitProjectInvitationImportID(d.Id()) From 3dff0994bb01554405c0810f4eb221babcf8f4dd Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 13:05:36 +0100 Subject: [PATCH 09/11] delete --- .../projectinvitation/resource_project_invitation.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/service/projectinvitation/resource_project_invitation.go b/internal/service/projectinvitation/resource_project_invitation.go index 32788c41cb..932bb79f87 100644 --- a/internal/service/projectinvitation/resource_project_invitation.go +++ b/internal/service/projectinvitation/resource_project_invitation.go @@ -160,17 +160,15 @@ 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()) projectID := ids["project_id"] username := ids["username"] invitationID := ids["invitation_id"] - - _, err := conn.Projects.DeleteInvitation(ctx, projectID, invitationID) + _, _, err := connV2.ProjectsApi.DeleteProjectInvitation(ctx, projectID, invitationID).Execute() if err != nil { return diag.FromErr(fmt.Errorf("error deleting Project invitation for user %s: %w", username, err)) } - return nil } From 3cf64e10a191b171833acbd59ba3e485518d124c Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 13:07:36 +0100 Subject: [PATCH 10/11] update --- .../projectinvitation/resource_project_invitation.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/internal/service/projectinvitation/resource_project_invitation.go b/internal/service/projectinvitation/resource_project_invitation.go index 932bb79f87..432c913896 100644 --- a/internal/service/projectinvitation/resource_project_invitation.go +++ b/internal/service/projectinvitation/resource_project_invitation.go @@ -12,7 +12,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "go.mongodb.org/atlas-sdk/v20231115005/admin" - matlas "go.mongodb.org/atlas/mongodbatlas" ) func Resource() *schema.Resource { @@ -141,21 +140,20 @@ 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()) projectID := ids["project_id"] username := ids["username"] invitationID := ids["invitation_id"] - invitationReq := &matlas.Invitation{ - Roles: conversion.ExpandStringListFromSetSchema(d.Get("roles").(*schema.Set)), + roles := conversion.ExpandStringListFromSetSchema(d.Get("roles").(*schema.Set)) + invitationReq := &admin.GroupInvitationUpdateRequest{ + Roles: &roles, } - - _, _, err := conn.Projects.UpdateInvitationByID(ctx, projectID, invitationID, invitationReq) + _, _, err := connV2.ProjectsApi.UpdateProjectInvitationById(ctx, projectID, invitationID, invitationReq).Execute() if err != nil { return diag.FromErr(fmt.Errorf("error updating Project invitation for user %s: %w", username, err)) } - return resourceRead(ctx, d, meta) } From e51f324c2a838b80075fc19ce33421e415438cdb Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Thu, 1 Feb 2024 13:11:01 +0100 Subject: [PATCH 11/11] import --- .../resource_project_invitation.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/service/projectinvitation/resource_project_invitation.go b/internal/service/projectinvitation/resource_project_invitation.go index 432c913896..e57220444c 100644 --- a/internal/service/projectinvitation/resource_project_invitation.go +++ b/internal/service/projectinvitation/resource_project_invitation.go @@ -171,35 +171,35 @@ 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 projectID, username, err := splitProjectInvitationImportID(d.Id()) if err != nil { return nil, err } - projectInvitations, _, err := conn.Projects.Invitations(ctx, projectID, nil) + projectInvitations, _, err := connV2.ProjectsApi.ListProjectInvitations(ctx, projectID).Execute() if err != nil { return nil, fmt.Errorf("couldn't import Project invitations, error: %s", err) } for _, projectInvitation := range projectInvitations { - if projectInvitation.Username != username { + if conversion.SafeString(projectInvitation.Username) != username { continue } - if err := d.Set("username", projectInvitation.Username); err != nil { + if err := d.Set("username", projectInvitation.GetUsername()); err != nil { return nil, fmt.Errorf("error getting `username` for Project Invitation (%s): %w", username, err) } - if err := d.Set("project_id", projectInvitation.GroupID); err != nil { + if err := d.Set("project_id", projectInvitation.GetGroupId()); err != nil { return nil, fmt.Errorf("error getting `project_id` for Project Invitation (%s): %w", username, err) } - if err := d.Set("invitation_id", projectInvitation.ID); err != nil { + if err := d.Set("invitation_id", projectInvitation.GetId()); err != nil { return nil, fmt.Errorf("error getting `invitation_id` for Project Invitation (%s): %w", username, err) } d.SetId(conversion.EncodeStateID(map[string]string{ "username": username, "project_id": projectID, - "invitation_id": projectInvitation.ID, + "invitation_id": projectInvitation.GetId(), })) return []*schema.ResourceData{d}, nil }