From 7d0c7cb860506320dd2554902cb527985daac72a Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Mon, 9 Jul 2018 07:45:30 -0400 Subject: [PATCH] resource/aws_glue_catalog_database: Properly return error when missing colon during import --- aws/resource_aws_glue_catalog_database.go | 31 ++++++++++++++----- ...resource_aws_glue_catalog_database_test.go | 10 ++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/aws/resource_aws_glue_catalog_database.go b/aws/resource_aws_glue_catalog_database.go index 9fde86994c1..bc035a9390e 100644 --- a/aws/resource_aws_glue_catalog_database.go +++ b/aws/resource_aws_glue_catalog_database.go @@ -76,7 +76,10 @@ func resourceAwsGlueCatalogDatabaseCreate(d *schema.ResourceData, meta interface func resourceAwsGlueCatalogDatabaseUpdate(d *schema.ResourceData, meta interface{}) error { glueconn := meta.(*AWSClient).glueconn - catalogID, name := readAwsGlueCatalogID(d.Id()) + catalogID, name, err := readAwsGlueCatalogID(d.Id()) + if err != nil { + return err + } dbUpdateInput := &glue.UpdateDatabaseInput{ CatalogId: aws.String(catalogID), @@ -117,7 +120,10 @@ func resourceAwsGlueCatalogDatabaseUpdate(d *schema.ResourceData, meta interface func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}) error { glueconn := meta.(*AWSClient).glueconn - catalogID, name := readAwsGlueCatalogID(d.Id()) + catalogID, name, err := readAwsGlueCatalogID(d.Id()) + if err != nil { + return err + } input := &glue.GetDatabaseInput{ CatalogId: aws.String(catalogID), @@ -153,10 +159,13 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{} func resourceAwsGlueCatalogDatabaseDelete(d *schema.ResourceData, meta interface{}) error { glueconn := meta.(*AWSClient).glueconn - catalogID, name := readAwsGlueCatalogID(d.Id()) + catalogID, name, err := readAwsGlueCatalogID(d.Id()) + if err != nil { + return err + } log.Printf("[DEBUG] Glue Catalog Database: %s:%s", catalogID, name) - _, err := glueconn.DeleteDatabase(&glue.DeleteDatabaseInput{ + _, err = glueconn.DeleteDatabase(&glue.DeleteDatabaseInput{ Name: aws.String(name), }) if err != nil { @@ -167,20 +176,26 @@ func resourceAwsGlueCatalogDatabaseDelete(d *schema.ResourceData, meta interface func resourceAwsGlueCatalogDatabaseExists(d *schema.ResourceData, meta interface{}) (bool, error) { glueconn := meta.(*AWSClient).glueconn - catalogID, name := readAwsGlueCatalogID(d.Id()) + catalogID, name, err := readAwsGlueCatalogID(d.Id()) + if err != nil { + return false, err + } input := &glue.GetDatabaseInput{ CatalogId: aws.String(catalogID), Name: aws.String(name), } - _, err := glueconn.GetDatabase(input) + _, err = glueconn.GetDatabase(input) return err == nil, err } -func readAwsGlueCatalogID(id string) (catalogID string, name string) { +func readAwsGlueCatalogID(id string) (catalogID string, name string, err error) { idParts := strings.Split(id, ":") - return idParts[0], idParts[1] + if len(idParts) != 2 { + return "", "", fmt.Errorf("Unexpected format of ID (%q), expected CATALOG-ID:DATABASE-NAME", id) + } + return idParts[0], idParts[1], nil } func createAwsGlueCatalogID(d *schema.ResourceData, accountid string) (catalogID string) { diff --git a/aws/resource_aws_glue_catalog_database_test.go b/aws/resource_aws_glue_catalog_database_test.go index 14ddcca007f..e7dbe4c8b5e 100644 --- a/aws/resource_aws_glue_catalog_database_test.go +++ b/aws/resource_aws_glue_catalog_database_test.go @@ -121,7 +121,10 @@ func testAccCheckGlueDatabaseDestroy(s *terraform.State) error { continue } - catalogId, dbName := readAwsGlueCatalogID(rs.Primary.ID) + catalogId, dbName, err := readAwsGlueCatalogID(rs.Primary.ID) + if err != nil { + return err + } input := &glue.GetDatabaseInput{ CatalogId: aws.String(catalogId), @@ -174,7 +177,10 @@ func testAccCheckGlueCatalogDatabaseExists(name string) resource.TestCheckFunc { return fmt.Errorf("No ID is set") } - catalogId, dbName := readAwsGlueCatalogID(rs.Primary.ID) + catalogId, dbName, err := readAwsGlueCatalogID(rs.Primary.ID) + if err != nil { + return err + } glueconn := testAccProvider.Meta().(*AWSClient).glueconn out, err := glueconn.GetDatabase(&glue.GetDatabaseInput{