Skip to content

Commit

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

resource/aws_glue_catalog_database: Properly return error when missing colon during import
  • Loading branch information
bflad authored Jul 9, 2018
2 parents c840b04 + 7d0c7cb commit 8afca02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
31 changes: 23 additions & 8 deletions aws/resource_aws_glue_catalog_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions aws/resource_aws_glue_catalog_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 8afca02

Please sign in to comment.