Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Mark project api key resource as destroyed if not present #1351

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions mongodbatlas/resource_mongodbatlas_project_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,35 @@ func resourceMongoDBAtlasProjectAPIKeyRead(ctx context.Context, d *schema.Resour
if err != nil {
return diag.FromErr(fmt.Errorf("error getting api key information: %s", err))
}
apiKeyIsPresent := false
for _, val := range projectAPIKeys {
if val.ID == apiKeyID {
if err := d.Set("api_key_id", val.ID); err != nil {
return diag.FromErr(fmt.Errorf("error setting `api_key_id`: %s", err))
}
if val.ID != apiKeyID {
continue
}

if err := d.Set("description", val.Desc); err != nil {
return diag.FromErr(fmt.Errorf("error setting `description`: %s", err))
}
apiKeyIsPresent = true
if err := d.Set("api_key_id", val.ID); err != nil {
return diag.FromErr(fmt.Errorf("error setting `api_key_id`: %s", err))
}

if err := d.Set("public_key", val.PublicKey); err != nil {
return diag.FromErr(fmt.Errorf("error setting `public_key`: %s", err))
}
if roleOk {
if err := d.Set("role_names", flattenProjectAPIKeyRoles(projectID, val.Roles)); err != nil {
return diag.FromErr(fmt.Errorf("error setting `roles`: %s", err))
}
if err := d.Set("description", val.Desc); err != nil {
return diag.FromErr(fmt.Errorf("error setting `description`: %s", err))
}

if err := d.Set("public_key", val.PublicKey); err != nil {
return diag.FromErr(fmt.Errorf("error setting `public_key`: %s", err))
}
if roleOk {
if err := d.Set("role_names", flattenProjectAPIKeyRoles(projectID, val.Roles)); err != nil {
return diag.FromErr(fmt.Errorf("error setting `roles`: %s", err))
}
}
}
if !apiKeyIsPresent {
// api key has been deleted, marking resource as destroyed
d.SetId("")
return nil
}

if err := d.Set("project_id", projectID); err != nil {
return diag.FromErr(fmt.Errorf("error setting `project_id`: %s", err))
Expand Down
56 changes: 56 additions & 0 deletions mongodbatlas/resource_mongodbatlas_project_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"fmt"
"os"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

func TestAccConfigRSProjectAPIKey_Basic(t *testing.T) {
Expand Down Expand Up @@ -90,6 +92,60 @@ func TestAccConfigRSProjectAPIKey_importBasic(t *testing.T) {
})
}

func TestAccConfigRSProjectAPIKey_RecreateWhenDeletedExternally(t *testing.T) {
var (
resourceName = "mongodbatlas_project_api_key.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
descriptionPrefix = "test-acc-project-to-delete-api-key"
description = fmt.Sprintf("%s-%s", descriptionPrefix, acctest.RandString(5))
roleName = "GROUP_OWNER"
)

projectAPIKeyConfig := testAccMongoDBAtlasProjectAPIKeyConfigBasic(orgID, projectName, description, roleName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckBasic(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasProjectAPIKeyDestroy,
Steps: []resource.TestStep{
{
Config: projectAPIKeyConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "description"),
),
},
{
PreConfig: func() {
if err := deleteAPIKeyManually(orgID, descriptionPrefix); err != nil {
t.Fatalf("failed to manually delete API key resource: %s", err)
}
},
Config: projectAPIKeyConfig,
PlanOnly: true,
ExpectNonEmptyPlan: true, // should detect that api key has to be recreated
},
},
})
}

func deleteAPIKeyManually(orgID, descriptionPrefix string) error {
conn := testAccProvider.Meta().(*MongoDBClient).Atlas
list, _, err := conn.APIKeys.List(context.Background(), orgID, &matlas.ListOptions{})
if err != nil {
return err
}
for _, key := range list {
if strings.HasPrefix(key.Desc, descriptionPrefix) {
if _, err := conn.APIKeys.Delete(context.Background(), orgID, key.ID); err != nil {
return err
}
}
}
return nil
}

func testAccCheckMongoDBAtlasProjectAPIKeyDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*MongoDBClient).Atlas

Expand Down
Loading