Skip to content

Commit

Permalink
Merge pull request #5141 from terraform-providers/b-aws_glue_catalog_…
Browse files Browse the repository at this point in the history
…database-notfound

resource/aws_glue_catalog_database: Prevent error when deleted outside Terraform
  • Loading branch information
bflad authored Jul 11, 2018
2 parents adf3980 + 9561f26 commit f9cd8c7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 8 additions & 1 deletion aws/resource_aws_glue_catalog_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}
if isAWSErr(err, glue.ErrCodeEntityNotFoundException, "") {
log.Printf("[WARN] Glue Catalog Database (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

return fmt.Errorf("Error reading Glue Catalog Database: %s", err.Error())
Expand Down Expand Up @@ -187,7 +188,13 @@ func resourceAwsGlueCatalogDatabaseExists(d *schema.ResourceData, meta interface
}

_, err = glueconn.GetDatabase(input)
return err == nil, err
if err != nil {
if isAWSErr(err, glue.ErrCodeEntityNotFoundException, "") {
return false, nil
}
return false, err
}
return true, nil
}

func readAwsGlueCatalogID(id string) (catalogID string, name string, err error) {
Expand Down
35 changes: 35 additions & 0 deletions aws/resource_aws_glue_catalog_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,41 @@ func TestAccAWSGlueCatalogDatabase_full(t *testing.T) {
})
}

func TestAccAWSGlueCatalogDatabase_recreates(t *testing.T) {
resourceName := "aws_glue_catalog_database.test"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGlueDatabaseDestroy,
Steps: []resource.TestStep{
{
Config: testAccGlueCatalogDatabase_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGlueCatalogDatabaseExists(resourceName),
),
},
{
// Simulate deleting the database outside Terraform
PreConfig: func() {
conn := testAccProvider.Meta().(*AWSClient).glueconn
input := &glue.DeleteDatabaseInput{
Name: aws.String(fmt.Sprintf("my_test_catalog_database_%d", rInt)),
}
_, err := conn.DeleteDatabase(input)
if err != nil {
t.Fatalf("error deleting Glue Catalog Database: %s", err)
}
},
Config: testAccGlueCatalogDatabase_basic(rInt),
ExpectNonEmptyPlan: true,
PlanOnly: true,
},
},
})
}

func testAccCheckGlueDatabaseDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).glueconn

Expand Down

0 comments on commit f9cd8c7

Please sign in to comment.