From 232e8af2b37018b88f29348065de9c61d71376c1 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Thu, 21 May 2020 16:03:11 +0300 Subject: [PATCH 1/4] add arn attribute add disappears test --- aws/resource_aws_glue_catalog_table.go | 30 +++-- aws/resource_aws_glue_catalog_table_test.go | 123 +++++++++--------- .../docs/r/glue_catalog_table.html.markdown | 8 ++ 3 files changed, 95 insertions(+), 66 deletions(-) diff --git a/aws/resource_aws_glue_catalog_table.go b/aws/resource_aws_glue_catalog_table.go index 5542adb7c6d2..f99f5a9474db 100644 --- a/aws/resource_aws_glue_catalog_table.go +++ b/aws/resource_aws_glue_catalog_table.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" ) @@ -21,6 +22,10 @@ func resourceAwsGlueCatalogTable() *schema.Resource { }, Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, "catalog_id": { Type: schema.TypeString, ForceNew: true, @@ -224,7 +229,7 @@ func readAwsGlueTableID(id string) (catalogID string, dbName string, name string } func resourceAwsGlueCatalogTableCreate(d *schema.ResourceData, meta interface{}) error { - glueconn := meta.(*AWSClient).glueconn + conn := meta.(*AWSClient).glueconn catalogID := createAwsGlueCatalogID(d, meta.(*AWSClient).accountid) dbName := d.Get("database_name").(string) name := d.Get("name").(string) @@ -235,7 +240,7 @@ func resourceAwsGlueCatalogTableCreate(d *schema.ResourceData, meta interface{}) TableInput: expandGlueTableInput(d), } - _, err := glueconn.CreateTable(input) + _, err := conn.CreateTable(input) if err != nil { return fmt.Errorf("Error creating Catalog Table: %s", err) } @@ -246,7 +251,7 @@ func resourceAwsGlueCatalogTableCreate(d *schema.ResourceData, meta interface{}) } func resourceAwsGlueCatalogTableRead(d *schema.ResourceData, meta interface{}) error { - glueconn := meta.(*AWSClient).glueconn + conn := meta.(*AWSClient).glueconn catalogID, dbName, name, err := readAwsGlueTableID(d.Id()) if err != nil { @@ -259,7 +264,7 @@ func resourceAwsGlueCatalogTableRead(d *schema.ResourceData, meta interface{}) e Name: aws.String(name), } - out, err := glueconn.GetTable(input) + out, err := conn.GetTable(input) if err != nil { if isAWSErr(err, glue.ErrCodeEntityNotFoundException, "") { @@ -271,6 +276,15 @@ func resourceAwsGlueCatalogTableRead(d *schema.ResourceData, meta interface{}) e return fmt.Errorf("Error reading Glue Catalog Table: %s", err) } + tableArn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Service: "glue", + Region: meta.(*AWSClient).region, + AccountID: meta.(*AWSClient).accountid, + Resource: fmt.Sprintf("table/%s/%s", dbName, aws.StringValue(out.Table.Name)), + }.String() + d.Set("arn", tableArn) + d.Set("name", out.Table.Name) d.Set("catalog_id", catalogID) d.Set("database_name", dbName) @@ -298,7 +312,7 @@ func resourceAwsGlueCatalogTableRead(d *schema.ResourceData, meta interface{}) e } func resourceAwsGlueCatalogTableUpdate(d *schema.ResourceData, meta interface{}) error { - glueconn := meta.(*AWSClient).glueconn + conn := meta.(*AWSClient).glueconn catalogID, dbName, _, err := readAwsGlueTableID(d.Id()) if err != nil { @@ -311,7 +325,7 @@ func resourceAwsGlueCatalogTableUpdate(d *schema.ResourceData, meta interface{}) TableInput: expandGlueTableInput(d), } - if _, err := glueconn.UpdateTable(updateTableInput); err != nil { + if _, err := conn.UpdateTable(updateTableInput); err != nil { return fmt.Errorf("Error updating Glue Catalog Table: %s", err) } @@ -319,7 +333,7 @@ func resourceAwsGlueCatalogTableUpdate(d *schema.ResourceData, meta interface{}) } func resourceAwsGlueCatalogTableDelete(d *schema.ResourceData, meta interface{}) error { - glueconn := meta.(*AWSClient).glueconn + conn := meta.(*AWSClient).glueconn catalogID, dbName, name, tableIdErr := readAwsGlueTableID(d.Id()) if tableIdErr != nil { @@ -327,7 +341,7 @@ func resourceAwsGlueCatalogTableDelete(d *schema.ResourceData, meta interface{}) } log.Printf("[DEBUG] Glue Catalog Table: %s:%s:%s", catalogID, dbName, name) - _, err := glueconn.DeleteTable(&glue.DeleteTableInput{ + _, err := conn.DeleteTable(&glue.DeleteTableInput{ CatalogId: aws.String(catalogID), Name: aws.String(name), DatabaseName: aws.String(dbName), diff --git a/aws/resource_aws_glue_catalog_table_test.go b/aws/resource_aws_glue_catalog_table_test.go index 3a02033365b5..4881ed3a61b9 100644 --- a/aws/resource_aws_glue_catalog_table_test.go +++ b/aws/resource_aws_glue_catalog_table_test.go @@ -14,7 +14,7 @@ import ( func TestAccAWSGlueCatalogTable_recreates(t *testing.T) { resourceName := "aws_glue_catalog_table.test" - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -22,7 +22,7 @@ func TestAccAWSGlueCatalogTable_recreates(t *testing.T) { CheckDestroy: testAccCheckGlueTableDestroy, Steps: []resource.TestStep{ { - Config: testAccGlueCatalogTable_basic(rInt), + Config: testAccGlueCatalogTable_basic(rName), Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogTableExists(resourceName), ), @@ -31,15 +31,15 @@ func TestAccAWSGlueCatalogTable_recreates(t *testing.T) { PreConfig: func() { conn := testAccProvider.Meta().(*AWSClient).glueconn input := &glue.DeleteTableInput{ - Name: aws.String(fmt.Sprintf("my_test_catalog_table_%d", rInt)), - DatabaseName: aws.String(fmt.Sprintf("my_test_catalog_database_%d", rInt)), + Name: aws.String(fmt.Sprintf("my_test_catalog_table_%d", rName)), + DatabaseName: aws.String(fmt.Sprintf("my_test_catalog_database_%d", rName)), } _, err := conn.DeleteTable(input) if err != nil { t.Fatalf("error deleting Glue Catalog Table: %s", err) } }, - Config: testAccGlueCatalogTable_basic(rInt), + Config: testAccGlueCatalogTable_basic(rName), ExpectNonEmptyPlan: true, PlanOnly: true, }, @@ -48,7 +48,7 @@ func TestAccAWSGlueCatalogTable_recreates(t *testing.T) { } func TestAccAWSGlueCatalogTable_basic(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_glue_catalog_table.test" resource.ParallelTest(t, resource.TestCase{ @@ -57,20 +57,13 @@ func TestAccAWSGlueCatalogTable_basic(t *testing.T) { CheckDestroy: testAccCheckGlueTableDestroy, Steps: []resource.TestStep{ { - Config: testAccGlueCatalogTable_basic(rInt), + Config: testAccGlueCatalogTable_basic(rName), Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogTableExists(resourceName), - resource.TestCheckResourceAttr( - resourceName, - "name", - fmt.Sprintf("my_test_catalog_table_%d", rInt), - ), - resource.TestCheckResourceAttr( - resourceName, - "database_name", - fmt.Sprintf("my_test_catalog_database_%d", rInt), - ), + testAccCheckResourceAttrRegionalARN(resourceName, "arn", "glue", fmt.Sprintf("table/%s", rName)), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "database_name", rName), ), }, { @@ -83,7 +76,7 @@ func TestAccAWSGlueCatalogTable_basic(t *testing.T) { } func TestAccAWSGlueCatalogTable_full(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") description := "A test table from terraform" resourceName := "aws_glue_catalog_table.test" @@ -93,12 +86,12 @@ func TestAccAWSGlueCatalogTable_full(t *testing.T) { CheckDestroy: testAccCheckGlueTableDestroy, Steps: []resource.TestStep{ { - Config: testAccGlueCatalogTable_full(rInt, description), + Config: testAccGlueCatalogTable_full(rName, description), Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogTableExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("my_test_catalog_table_%d", rInt)), - resource.TestCheckResourceAttr(resourceName, "database_name", fmt.Sprintf("my_test_catalog_database_%d", rInt)), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "database_name", rName), resource.TestCheckResourceAttr(resourceName, "description", description), resource.TestCheckResourceAttr(resourceName, "owner", "my_owner"), resource.TestCheckResourceAttr(resourceName, "retention", "1"), @@ -146,7 +139,7 @@ func TestAccAWSGlueCatalogTable_full(t *testing.T) { } func TestAccAWSGlueCatalogTable_update_addValues(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") description := "A test table from terraform" resourceName := "aws_glue_catalog_table.test" @@ -156,20 +149,12 @@ func TestAccAWSGlueCatalogTable_update_addValues(t *testing.T) { CheckDestroy: testAccCheckGlueTableDestroy, Steps: []resource.TestStep{ { - Config: testAccGlueCatalogTable_basic(rInt), + Config: testAccGlueCatalogTable_basic(rName), Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogTableExists(resourceName), - resource.TestCheckResourceAttr( - resourceName, - "name", - fmt.Sprintf("my_test_catalog_table_%d", rInt), - ), - resource.TestCheckResourceAttr( - resourceName, - "database_name", - fmt.Sprintf("my_test_catalog_database_%d", rInt), - ), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "database_name", rName), ), }, { @@ -178,12 +163,12 @@ func TestAccAWSGlueCatalogTable_update_addValues(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccGlueCatalogTable_full(rInt, description), + Config: testAccGlueCatalogTable_full(rName, description), Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogTableExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("my_test_catalog_table_%d", rInt)), - resource.TestCheckResourceAttr(resourceName, "database_name", fmt.Sprintf("my_test_catalog_database_%d", rInt)), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "database_name", rName), resource.TestCheckResourceAttr(resourceName, "description", description), resource.TestCheckResourceAttr(resourceName, "owner", "my_owner"), resource.TestCheckResourceAttr(resourceName, "retention", "1"), @@ -226,7 +211,7 @@ func TestAccAWSGlueCatalogTable_update_addValues(t *testing.T) { } func TestAccAWSGlueCatalogTable_update_replaceValues(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") description := "A test table from terraform" resourceName := "aws_glue_catalog_table.test" @@ -236,12 +221,12 @@ func TestAccAWSGlueCatalogTable_update_replaceValues(t *testing.T) { CheckDestroy: testAccCheckGlueTableDestroy, Steps: []resource.TestStep{ { - Config: testAccGlueCatalogTable_full(rInt, description), + Config: testAccGlueCatalogTable_full(rName, description), Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogTableExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("my_test_catalog_table_%d", rInt)), - resource.TestCheckResourceAttr(resourceName, "database_name", fmt.Sprintf("my_test_catalog_database_%d", rInt)), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "database_name", rName), resource.TestCheckResourceAttr(resourceName, "description", description), resource.TestCheckResourceAttr(resourceName, "owner", "my_owner"), resource.TestCheckResourceAttr(resourceName, "retention", "1"), @@ -285,12 +270,12 @@ func TestAccAWSGlueCatalogTable_update_replaceValues(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccGlueCatalogTable_full_replacedValues(rInt), + Config: testAccGlueCatalogTable_full_replacedValues(rName), Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogTableExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("my_test_catalog_table_%d", rInt)), - resource.TestCheckResourceAttr(resourceName, "database_name", fmt.Sprintf("my_test_catalog_database_%d", rInt)), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "database_name", rName), resource.TestCheckResourceAttr(resourceName, "description", "A test table from terraform2"), resource.TestCheckResourceAttr(resourceName, "owner", "my_owner2"), resource.TestCheckResourceAttr(resourceName, "retention", "2"), @@ -404,29 +389,51 @@ func TestAccAWSGlueCatalogTable_StorageDescriptor_SkewedInfo_EmptyConfigurationB }) } -func testAccGlueCatalogTable_basic(rInt int) string { +func TestAccAWSGlueCatalogTable_disappears(t *testing.T) { + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_glue_catalog_table.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGlueTableDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGlueCatalogTable_basic(rName), + Destroy: false, + Check: resource.ComposeTestCheckFunc( + testAccCheckGlueCatalogTableExists(resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsGlueCatalogTable(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccGlueCatalogTable_basic(rName string) string { return fmt.Sprintf(` resource "aws_glue_catalog_database" "test" { - name = "my_test_catalog_database_%d" + name = %[1]q } resource "aws_glue_catalog_table" "test" { - name = "my_test_catalog_table_%d" + name = %[1]q database_name = "${aws_glue_catalog_database.test.name}" } -`, rInt, rInt) +`, rName) } -func testAccGlueCatalogTable_full(rInt int, desc string) string { +func testAccGlueCatalogTable_full(rName, desc string) string { return fmt.Sprintf(` resource "aws_glue_catalog_database" "test" { - name = "my_test_catalog_database_%d" + name = %[1]q } resource "aws_glue_catalog_table" "test" { - name = "my_test_catalog_table_%d" + name = %[1]q database_name = "${aws_glue_catalog_database.test.name}" - description = "%s" + description = %[2]q owner = "my_owner" retention = 1 table_type = "VIRTUAL_VIEW" @@ -504,17 +511,17 @@ resource "aws_glue_catalog_table" "test" { param1 = "param1_val" } } -`, rInt, rInt, desc) +`, rName, desc) } -func testAccGlueCatalogTable_full_replacedValues(rInt int) string { +func testAccGlueCatalogTable_full_replacedValues(rName string) string { return fmt.Sprintf(` resource "aws_glue_catalog_database" "test" { - name = "my_test_catalog_database_%d" + name = %[1]q } resource "aws_glue_catalog_table" "test" { - name = "my_test_catalog_table_%d" + name = %[1]q database_name = "${aws_glue_catalog_database.test.name}" description = "A test table from terraform2" owner = "my_owner2" @@ -599,7 +606,7 @@ resource "aws_glue_catalog_table" "test" { param2 = "param1_val2" } } -`, rInt, rInt) +`, rName) } func testAccGlueCatalogTableConfigStorageDescriptorEmptyConfigurationBlock(rName string) string { @@ -702,8 +709,8 @@ func testAccCheckGlueCatalogTableExists(name string) resource.TestCheckFunc { return err } - glueconn := testAccProvider.Meta().(*AWSClient).glueconn - out, err := glueconn.GetTable(&glue.GetTableInput{ + conn := testAccProvider.Meta().(*AWSClient).glueconn + out, err := conn.GetTable(&glue.GetTableInput{ CatalogId: aws.String(catalogId), DatabaseName: aws.String(dbName), Name: aws.String(resourceName), diff --git a/website/docs/r/glue_catalog_table.html.markdown b/website/docs/r/glue_catalog_table.html.markdown index 449449d0d505..bd6d621ea8cd 100644 --- a/website/docs/r/glue_catalog_table.html.markdown +++ b/website/docs/r/glue_catalog_table.html.markdown @@ -135,6 +135,14 @@ The following arguments are supported: * `skewed_column_value_location_maps` - (Optional) A list of values that appear so frequently as to be considered skewed. * `skewed_column_values` - (Optional) A map of skewed values to the columns that contain them. +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - Catalog ID, Database name and of the name table. +* `arn` - The ARN of the Glue Table. + + ## Import Glue Tables can be imported with their catalog ID (usually AWS account ID), database name, and table name, e.g. From 4938ebfc54dfdb8b8212c7de3103d020ad79ea2a Mon Sep 17 00:00:00 2001 From: Ilia Date: Thu, 21 May 2020 17:48:15 +0300 Subject: [PATCH 2/4] fix --- aws/resource_aws_glue_catalog_table_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_glue_catalog_table_test.go b/aws/resource_aws_glue_catalog_table_test.go index 4881ed3a61b9..333ec09192f5 100644 --- a/aws/resource_aws_glue_catalog_table_test.go +++ b/aws/resource_aws_glue_catalog_table_test.go @@ -31,8 +31,8 @@ func TestAccAWSGlueCatalogTable_recreates(t *testing.T) { PreConfig: func() { conn := testAccProvider.Meta().(*AWSClient).glueconn input := &glue.DeleteTableInput{ - Name: aws.String(fmt.Sprintf("my_test_catalog_table_%d", rName)), - DatabaseName: aws.String(fmt.Sprintf("my_test_catalog_database_%d", rName)), + Name: aws.String(rName), + DatabaseName: aws.String(rName), } _, err := conn.DeleteTable(input) if err != nil { @@ -61,7 +61,7 @@ func TestAccAWSGlueCatalogTable_basic(t *testing.T) { Destroy: false, Check: resource.ComposeTestCheckFunc( testAccCheckGlueCatalogTableExists(resourceName), - testAccCheckResourceAttrRegionalARN(resourceName, "arn", "glue", fmt.Sprintf("table/%s", rName)), + testAccCheckResourceAttrRegionalARN(resourceName, "arn", "glue", fmt.Sprintf("table/%s/%s", rName, rName)), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "database_name", rName), ), From 6d868e23a5b31e96da89135c2ef188e0948e93da Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 12:02:23 +0300 Subject: [PATCH 3/4] add index --- website/docs/index.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index a4f1dc649f25..891f7884b877 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_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) - [`aws_glue_job` resource](/docs/providers/aws/r/glue_job.html) From c76a72a903a0ddb04b8cf82dc79a41249f2a3aec Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 12:04:11 +0300 Subject: [PATCH 4/4] sdk wrapper --- aws/resource_aws_glue_catalog_table_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_glue_catalog_table_test.go b/aws/resource_aws_glue_catalog_table_test.go index 333ec09192f5..97d38d6cd192 100644 --- a/aws/resource_aws_glue_catalog_table_test.go +++ b/aws/resource_aws_glue_catalog_table_test.go @@ -724,9 +724,9 @@ func testAccCheckGlueCatalogTableExists(name string) resource.TestCheckFunc { return fmt.Errorf("No Glue Table Found") } - if *out.Table.Name != resourceName { + if aws.StringValue(out.Table.Name) != resourceName { return fmt.Errorf("Glue Table Mismatch - existing: %q, state: %q", - *out.Table.Name, resourceName) + aws.StringValue(out.Table.Name), resourceName) } return nil