From b2e8a2f4f48d83b6b37878a1c7ea566228848bf0 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Thu, 21 May 2020 13:38:52 +0300 Subject: [PATCH 1/5] add arn field --- aws/resource_aws_glue_catalog_database.go | 30 +++++-- ...resource_aws_glue_catalog_database_test.go | 84 ++++--------------- .../r/glue_catalog_database.html.markdown | 7 ++ 3 files changed, 43 insertions(+), 78 deletions(-) diff --git a/aws/resource_aws_glue_catalog_database.go b/aws/resource_aws_glue_catalog_database.go index ed919e1b490..e4d942c1cd2 100644 --- a/aws/resource_aws_glue_catalog_database.go +++ b/aws/resource_aws_glue_catalog_database.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/service/glue" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -22,6 +23,10 @@ func resourceAwsGlueCatalogDatabase() *schema.Resource { }, Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, "catalog_id": { Type: schema.TypeString, ForceNew: true, @@ -51,7 +56,7 @@ func resourceAwsGlueCatalogDatabase() *schema.Resource { } func resourceAwsGlueCatalogDatabaseCreate(d *schema.ResourceData, meta interface{}) error { - glueconn := meta.(*AWSClient).glueconn + conn := meta.(*AWSClient).glueconn catalogID := createAwsGlueCatalogID(d, meta.(*AWSClient).accountid) name := d.Get("name").(string) @@ -62,7 +67,7 @@ func resourceAwsGlueCatalogDatabaseCreate(d *schema.ResourceData, meta interface }, } - _, err := glueconn.CreateDatabase(input) + _, err := conn.CreateDatabase(input) if err != nil { return fmt.Errorf("Error creating Catalog Database: %s", err) } @@ -73,7 +78,7 @@ func resourceAwsGlueCatalogDatabaseCreate(d *schema.ResourceData, meta interface } func resourceAwsGlueCatalogDatabaseUpdate(d *schema.ResourceData, meta interface{}) error { - glueconn := meta.(*AWSClient).glueconn + conn := meta.(*AWSClient).glueconn catalogID, name, err := readAwsGlueCatalogID(d.Id()) if err != nil { @@ -108,7 +113,7 @@ func resourceAwsGlueCatalogDatabaseUpdate(d *schema.ResourceData, meta interface dbUpdateInput.DatabaseInput = dbInput if d.HasChanges("description", "location_uri", "parameters") { - if _, err := glueconn.UpdateDatabase(dbUpdateInput); err != nil { + if _, err := conn.UpdateDatabase(dbUpdateInput); err != nil { return err } } @@ -117,7 +122,7 @@ func resourceAwsGlueCatalogDatabaseUpdate(d *schema.ResourceData, meta interface } func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}) error { - glueconn := meta.(*AWSClient).glueconn + conn := meta.(*AWSClient).glueconn catalogID, name, err := readAwsGlueCatalogID(d.Id()) if err != nil { @@ -129,7 +134,7 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{} Name: aws.String(name), } - out, err := glueconn.GetDatabase(input) + out, err := conn.GetDatabase(input) if err != nil { if isAWSErr(err, glue.ErrCodeEntityNotFoundException, "") { @@ -141,6 +146,15 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{} return fmt.Errorf("Error reading Glue Catalog Database: %s", err.Error()) } + 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)), + }.String() + d.Set("arn", databaseArn) + d.Set("name", out.Database.Name) d.Set("catalog_id", catalogID) d.Set("description", out.Database.Description) @@ -158,14 +172,14 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{} } func resourceAwsGlueCatalogDatabaseDelete(d *schema.ResourceData, meta interface{}) error { - glueconn := meta.(*AWSClient).glueconn + 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 = glueconn.DeleteDatabase(&glue.DeleteDatabaseInput{ + _, err = conn.DeleteDatabase(&glue.DeleteDatabaseInput{ Name: aws.String(name), }) if err != nil { diff --git a/aws/resource_aws_glue_catalog_database_test.go b/aws/resource_aws_glue_catalog_database_test.go index d1cc91ae5b7..ec4429e6fcb 100644 --- a/aws/resource_aws_glue_catalog_database_test.go +++ b/aws/resource_aws_glue_catalog_database_test.go @@ -26,26 +26,10 @@ func TestAccAWSGlueCatalogDatabase_full(t *testing.T) { Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogDatabaseExists(resourceName), - resource.TestCheckResourceAttr( - resourceName, - "name", - rName, - ), - resource.TestCheckResourceAttr( - resourceName, - "description", - "", - ), - resource.TestCheckResourceAttr( - resourceName, - "location_uri", - "", - ), - resource.TestCheckResourceAttr( - resourceName, - "parameters.%", - "0", - ), + testAccCheckResourceAttrRegionalARN(resourceName, "arn", "glue", fmt.Sprintf("database/%s", rName)), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "description", ""), resource.TestCheckResourceAttr(resourceName, "location_uri", ""), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "0"), ), }, { @@ -58,62 +42,22 @@ func TestAccAWSGlueCatalogDatabase_full(t *testing.T) { Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogDatabaseExists(resourceName), - resource.TestCheckResourceAttr( - resourceName, - "description", - "A test catalog from terraform", - ), - resource.TestCheckResourceAttr( - resourceName, - "location_uri", - "my-location", - ), - resource.TestCheckResourceAttr( - resourceName, - "parameters.param1", - "value1", - ), - resource.TestCheckResourceAttr( - resourceName, - "parameters.param2", - "true", - ), - resource.TestCheckResourceAttr( - resourceName, - "parameters.param3", - "50", - ), + resource.TestCheckResourceAttr(resourceName, "description", "A test catalog from terraform"), + resource.TestCheckResourceAttr(resourceName, "location_uri", "my-location"), + resource.TestCheckResourceAttr(resourceName, "parameters.param1", "value1"), + resource.TestCheckResourceAttr(resourceName, "parameters.param2", "true"), + resource.TestCheckResourceAttr(resourceName, "parameters.param3", "50"), ), }, { Config: testAccGlueCatalogDatabase_full(rName, "An updated test catalog from terraform"), Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogDatabaseExists(resourceName), - resource.TestCheckResourceAttr( - resourceName, - "description", - "An updated test catalog from terraform", - ), - resource.TestCheckResourceAttr( - resourceName, - "location_uri", - "my-location", - ), - resource.TestCheckResourceAttr( - resourceName, - "parameters.param1", - "value1", - ), - resource.TestCheckResourceAttr( - resourceName, - "parameters.param2", - "true", - ), - resource.TestCheckResourceAttr( - resourceName, - "parameters.param3", - "50", - ), + resource.TestCheckResourceAttr(resourceName, "description", "An updated test catalog from terraform"), + resource.TestCheckResourceAttr(resourceName, "location_uri", "my-location"), + resource.TestCheckResourceAttr(resourceName, "parameters.param1", "value1"), + resource.TestCheckResourceAttr(resourceName, "parameters.param2", "true"), + resource.TestCheckResourceAttr(resourceName, "parameters.param3", "50"), ), }, }, diff --git a/website/docs/r/glue_catalog_database.html.markdown b/website/docs/r/glue_catalog_database.html.markdown index 17a5634f368..f2ac8299454 100644 --- a/website/docs/r/glue_catalog_database.html.markdown +++ b/website/docs/r/glue_catalog_database.html.markdown @@ -28,6 +28,13 @@ The following arguments are supported: * `location_uri` - (Optional) The location of the database (for example, an HDFS path). * `parameters` - (Optional) A list of key-value pairs that define parameters and properties of the database. +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - Catalog ID and name of the database +* `arn` - The ARN of the Glue Connection. + ## Import Glue Catalog Databases can be imported using the `catalog_id:name`. If you have not set a Catalog ID specify the AWS Account ID that the database is in, e.g. From 8dbfd88aabf68f5a495814d0b67b867a7c3ac147 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Thu, 21 May 2020 13:51:55 +0300 Subject: [PATCH 2/5] add disappears test --- ...resource_aws_glue_catalog_database_test.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/aws/resource_aws_glue_catalog_database_test.go b/aws/resource_aws_glue_catalog_database_test.go index ec4429e6fcb..c176912f56c 100644 --- a/aws/resource_aws_glue_catalog_database_test.go +++ b/aws/resource_aws_glue_catalog_database_test.go @@ -99,6 +99,27 @@ func TestAccAWSGlueCatalogDatabase_recreates(t *testing.T) { }) } +func TestAccAWSGlueCatalogDatabase_disappears(t *testing.T) { + resourceName := "aws_glue_catalog_database.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGlueDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGlueCatalogDatabase_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckGlueCatalogDatabaseExists(resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsGlueCatalogDatabase(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckGlueDatabaseDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).glueconn From 318d48cb8e8ce197a0f50317ed17fb5c11907cf0 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 12:17:39 +0300 Subject: [PATCH 3/5] add to index.html.markdown + sdk wrapper --- aws/resource_aws_glue_catalog_database_test.go | 4 ++-- website/docs/index.html.markdown | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_glue_catalog_database_test.go b/aws/resource_aws_glue_catalog_database_test.go index c176912f56c..cce5c5d6a3a 100644 --- a/aws/resource_aws_glue_catalog_database_test.go +++ b/aws/resource_aws_glue_catalog_database_test.go @@ -204,9 +204,9 @@ func testAccCheckGlueCatalogDatabaseExists(name string) resource.TestCheckFunc { return fmt.Errorf("No Glue Database Found") } - if *out.Database.Name != dbName { + if aws.StringValue(out.Database.Name) != dbName { return fmt.Errorf("Glue Database Mismatch - existing: %q, state: %q", - *out.Database.Name, dbName) + aws.StringValue(out.Database.Name), dbName) } return nil diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index 891f7884b87..1fdd93e58bd 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -263,7 +263,8 @@ for more information about connecting to alternate AWS endpoints or AWS compatib - [`aws_elasticache_cluster` resource](/docs/providers/aws/r/elasticache_cluster.html) - [`aws_elb` data source](/docs/providers/aws/d/elb.html) - [`aws_elb` resource](/docs/providers/aws/r/elb.html) - - [`aws_flow_log` resource](/docs/providers/aws/r/flow_log.html) + - [`aws_flow_log` resource](/docs/providers/aws/r/flow_log.html) + - [`aws_glue_catalog_database` resource](/docs/providers/aws/r/glue_catalog_database.html) - [`aws_glue_catalog_table` resource](/docs/providers/aws/r/glue_catalog_table.html) - [`aws_glue_connection` resource](/docs/providers/aws/r/glue_connection.html) - [`aws_glue_crawler` resource](/docs/providers/aws/r/glue_crawler.html) From 9c09ec7df792ba33a094675212605536390f4780 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 13:50:43 +0300 Subject: [PATCH 4/5] Update website/docs/r/glue_catalog_database.html.markdown Co-authored-by: Brian Flad --- website/docs/r/glue_catalog_database.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/glue_catalog_database.html.markdown b/website/docs/r/glue_catalog_database.html.markdown index f2ac8299454..2d967d7587a 100644 --- a/website/docs/r/glue_catalog_database.html.markdown +++ b/website/docs/r/glue_catalog_database.html.markdown @@ -33,7 +33,7 @@ The following arguments are supported: In addition to all arguments above, the following attributes are exported: * `id` - Catalog ID and name of the database -* `arn` - The ARN of the Glue Connection. +* `arn` - The ARN of the Glue Catalog Database. ## Import From e40d312a6e3b9b8f8963d1665aba8cd19f1dca2c Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Sat, 20 Jun 2020 11:12:02 +0300 Subject: [PATCH 5/5] remove recreates in favor of disappears --- ...resource_aws_glue_catalog_database_test.go | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/aws/resource_aws_glue_catalog_database_test.go b/aws/resource_aws_glue_catalog_database_test.go index cce5c5d6a3a..70071fb86a6 100644 --- a/aws/resource_aws_glue_catalog_database_test.go +++ b/aws/resource_aws_glue_catalog_database_test.go @@ -64,41 +64,6 @@ func TestAccAWSGlueCatalogDatabase_full(t *testing.T) { }) } -func TestAccAWSGlueCatalogDatabase_recreates(t *testing.T) { - resourceName := "aws_glue_catalog_database.test" - rName := acctest.RandomWithPrefix("tf-acc-test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGlueDatabaseDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGlueCatalogDatabase_basic(rName), - Check: resource.ComposeTestCheckFunc( - testAccCheckGlueCatalogDatabaseExists(resourceName), - ), - }, - { - // Simulate deleting the database outside Terraform - PreConfig: func() { - conn := testAccProvider.Meta().(*AWSClient).glueconn - input := &glue.DeleteDatabaseInput{ - Name: aws.String(rName), - } - _, err := conn.DeleteDatabase(input) - if err != nil { - t.Fatalf("error deleting Glue Catalog Database: %s", err) - } - }, - Config: testAccGlueCatalogDatabase_basic(rName), - ExpectNonEmptyPlan: true, - PlanOnly: true, - }, - }, - }) -} - func TestAccAWSGlueCatalogDatabase_disappears(t *testing.T) { resourceName := "aws_glue_catalog_database.test" rName := acctest.RandomWithPrefix("tf-acc-test")