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

r/glue_catalog_database - Add sweeper + use catalog_id when deleting #17489

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
3 changes: 3 additions & 0 deletions .changelog/17489.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_glue_catalog_database: Use Catalog Id when deleting Databases.
```
27 changes: 12 additions & 15 deletions aws/resource_aws_glue_catalog_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,37 +162,34 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("Error reading Glue Catalog Database: %s", err.Error())
}

database := out.Database
databaseArn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "glue",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("database/%s", aws.StringValue(out.Database.Name)),
Resource: fmt.Sprintf("database/%s", aws.StringValue(database.Name)),
}.String()
d.Set("arn", databaseArn)

d.Set("name", out.Database.Name)
d.Set("catalog_id", catalogID)
d.Set("description", out.Database.Description)
d.Set("location_uri", out.Database.LocationUri)
d.Set("parameters", aws.StringValueMap(out.Database.Parameters))
d.Set("name", database.Name)
d.Set("catalog_id", database.CatalogId)
d.Set("description", database.Description)
d.Set("location_uri", database.LocationUri)
d.Set("parameters", aws.StringValueMap(database.Parameters))

return nil
}

func resourceAwsGlueCatalogDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).glueconn
catalogID, name, err := readAwsGlueCatalogID(d.Id())
if err != nil {
return err
}

log.Printf("[DEBUG] Glue Catalog Database: %s:%s", catalogID, name)
_, err = conn.DeleteDatabase(&glue.DeleteDatabaseInput{
Name: aws.String(name),
log.Printf("[DEBUG] Glue Catalog Database: %s", d.Id())
_, err := conn.DeleteDatabase(&glue.DeleteDatabaseInput{
Name: aws.String(d.Get("name").(string)),
CatalogId: aws.String(d.Get("catalog_id").(string)),
})
if err != nil {
return fmt.Errorf("Error deleting Glue Catalog Database: %s", err.Error())
return fmt.Errorf("Error deleting Glue Catalog Database: %w", err)
}
return nil
}
Expand Down
50 changes: 50 additions & 0 deletions aws/resource_aws_glue_catalog_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"log"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -11,6 +12,55 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func init() {
resource.AddTestSweepers("aws_glue_catalog_database", &resource.Sweeper{
Name: "aws_glue_catalog_database",
F: testSweepGlueCatalogDatabases,
})
}

func testSweepGlueCatalogDatabases(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*AWSClient).glueconn

input := &glue.GetDatabasesInput{}
err = conn.GetDatabasesPages(input, func(page *glue.GetDatabasesOutput, lastPage bool) bool {
if len(page.DatabaseList) == 0 {
log.Printf("[INFO] No Glue Catalog Databases to sweep")
return false
}
for _, database := range page.DatabaseList {
name := aws.StringValue(database.Name)

log.Printf("[INFO] Deleting Glue Catalog Database: %s", name)

r := resourceAwsGlueCatalogDatabase()
d := r.Data(nil)
d.SetId("???")
d.Set("name", name)
d.Set("catalog_id", database.CatalogId)

err := r.Delete(d, client)
if err != nil {
log.Printf("[ERROR] Failed to delete Glue Catalog Database %s: %s", name, err)
}
}
return !lastPage
})
if err != nil {
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping Glue Catalog Database sweep for %s: %s", region, err)
return nil
}
return fmt.Errorf("Error retrieving Glue Catalog Databases: %s", err)
}

return nil
}

func TestAccAWSGlueCatalogDatabase_full(t *testing.T) {
resourceName := "aws_glue_catalog_database.test"
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down