From 09d5d40ae8a0866f69f93a1ef75d140cee069a90 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 12:03:15 +0100 Subject: [PATCH 01/21] Athena-DataCatalog: Create entry in `internal/provider/provider.go` --- internal/provider/provider.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index efdd8dab1f1..4ec8942d25e 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -971,9 +971,10 @@ func Provider() *schema.Provider { "aws_appsync_graphql_api": appsync.ResourceGraphQLAPI(), "aws_appsync_resolver": appsync.ResourceResolver(), - "aws_athena_database": athena.ResourceDatabase(), - "aws_athena_named_query": athena.ResourceNamedQuery(), - "aws_athena_workgroup": athena.ResourceWorkGroup(), + "aws_athena_database": athena.ResourceDatabase(), + "aws_athena_data_catalog": athena.ResourceDataCatalog(), + "aws_athena_named_query": athena.ResourceNamedQuery(), + "aws_athena_workgroup": athena.ResourceWorkGroup(), "aws_autoscaling_attachment": autoscaling.ResourceAttachment(), "aws_autoscaling_group": autoscaling.ResourceGroup(), From 41f2ef6668540096e94ec2c6a37fc873ce270fb2 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 12:13:23 +0100 Subject: [PATCH 02/21] Athena-DataCatalog: create changelog entry --- .changelog/22968.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/22968.txt diff --git a/.changelog/22968.txt b/.changelog/22968.txt new file mode 100644 index 00000000000..5c36ee38a11 --- /dev/null +++ b/.changelog/22968.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_athena_data_catalog +``` \ No newline at end of file From f6a70bf0ce96e1fa70698cdb6b5ac3669fbbcbaf Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 12:49:47 +0100 Subject: [PATCH 03/21] Athena-DataCatalog: create `data_catalog.go` --- internal/service/athena/data_catalog.go | 226 ++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 internal/service/athena/data_catalog.go diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go new file mode 100644 index 00000000000..a9a6579feef --- /dev/null +++ b/internal/service/athena/data_catalog.go @@ -0,0 +1,226 @@ +package athena + +import ( + "context" + "log" + "regexp" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/athena" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/flex" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/internal/verify" +) + +func ResourceDataCatalog() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceDataCatalogCreate, + ReadContext: resourceDataCatalogRead, + UpdateContext: resourceDataCatalogUpdate, + DeleteContext: resourceDataCatalogDelete, + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + CustomizeDiff: verify.SetTagsDiff, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.All( + validation.StringLenBetween(1, 129), + validation.StringMatch(regexp.MustCompile(`[\w@-]*`), ""), + ), + }, + "description": { + Type: schema.TypeString, + Optional: true, + }, + "type": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + athena.DataCatalogTypeLambda, + athena.DataCatalogTypeGlue, + athena.DataCatalogTypeHive, + }, false), + }, + "parameters": { + Type: schema.TypeMap, + Elem: &schema.Schema{ + Type: schema.TypeString, + //ValidateFunc: validation.StringLenBetween(1, 51200), -- WIP + }, + Optional: true, + }, + "tags": tftags.TagsSchema(), + "tags_all": tftags.TagsSchemaComputed(), + }, + } +} + +func resourceDataCatalogCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).AthenaConn + defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig + tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{}))) + + name := d.Get("name").(string) + + input := &athena.CreateDataCatalogInput{ + Name: aws.String(name), + Description: aws.String(d.Get("description").(string)), + Type: aws.String(d.Get("type").(string)), + } + + if v, ok := d.GetOk("parameters"); ok { + input.Parameters = flex.ExpandStringMap(v.(map[string]interface{})) + } + + if len(tags) > 0 { + input.Tags = Tags(tags.IgnoreAWS()) + } + + if err := input.Validate(); err != nil { + return diag.Errorf("Error validating Athena Data Catalog (%s): %s", name, err) + } + + log.Printf("[DEBUG] Creating Data Catalog: %s", input) + _, err := conn.CreateDataCatalogWithContext(ctx, input) + + if err != nil { + return diag.Errorf("error creating Athena Data Catalog (%s): %s", name, err) + } + + d.SetId(name) + + return resourceDataCatalogRead(ctx, d, meta) +} + +func resourceDataCatalogUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).AthenaConn + + if d.HasChangesExcept("tags", "tags_all") { + input := &athena.UpdateDataCatalogInput{ + Name: aws.String(d.Id()), + } + + if d.HasChange("name") { + input.Name = aws.String(d.Get("name").(string)) + } + + if d.HasChange("description") { + input.Name = aws.String(d.Get("description").(string)) + } + + if d.HasChange("type") { + input.Name = aws.String(d.Get("type").(string)) + } + + if d.HasChange("parameters") { + parameters := map[string]*string{} + if v, ok := d.GetOk("parameters"); ok { + if m, ok := v.(map[string]interface{}); ok { + parameters = flex.ExpandStringMap(m) + } + } + input.Parameters = parameters + } + + log.Printf("[DEBUG] Updating Athena Data Catalog (%s)", d.Id()) + + if err := input.Validate(); err != nil { + return diag.Errorf("Error validating Athena Data Catalog (%s): %s", d.Id(), err) + } + + _, err := conn.UpdateDataCatalogWithContext(ctx, input) + + if err != nil { + return diag.Errorf("error updating Athena Data Catalog (%s): %s", d.Id(), err) + } + } + + if d.HasChange("tags_all") { + o, n := d.GetChange("tags_all") + + log.Printf("[DEBUG] Updating Athena Data Catalog (%s) tags", d.Id()) + if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil { + return diag.Errorf("error updating Athena Data Catalog (%s) tags: %s", d.Id(), err) + } + } + + return resourceDataCatalogRead(ctx, d, meta) +} + +func resourceDataCatalogDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).AthenaConn + + log.Printf("[DEBUG] Deleting Athena Data Catalog: (%s)", d.Id()) + + _, err := conn.DeleteDataCatalogWithContext(ctx, &athena.DeleteDataCatalogInput{ + Name: aws.String(d.Id()), + }) + + if tfawserr.ErrCodeEquals(err, athena.ErrCodeResourceNotFoundException) { + return nil + } + + if err != nil { + return diag.Errorf("error deleting Athena Data Catalog (%s): %s", d.Id(), err) + } + + return nil +} + +func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).AthenaConn + defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + + input := &athena.GetDataCatalogInput{ + Name: aws.String(d.Id()), + } + + dataCatalog, err := conn.GetDataCatalogWithContext(ctx, input) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Athena Data Catalog (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + if err != nil { + return diag.Errorf("error reading Athena Data Catalog (%s): %s", d.Id(), err) + } + + d.Set("description", dataCatalog.DataCatalog.Description) + d.Set("type", dataCatalog.DataCatalog.Type) + d.Set("parameters", aws.StringValueMap(dataCatalog.DataCatalog.Parameters)) + + tags, err := ListTags(conn, d.Id()) + + if err != nil { + return diag.Errorf("error listing tags for Athena Data Catalog (%s): %s", d.Id(), err) + } + + tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig) + + //lintignore:AWSR002 + if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil { + return diag.Errorf("error setting tags for Athena Data Catalog (%s): %s", d.Id(), err) + } + + if err := d.Set("tags_all", tags.Map()); err != nil { + return diag.Errorf("error setting tags_all for Athena Data Catalog (%s): %s", d.Id(), err) + } + + return nil +} From 785e8a62d0c32fcba3d9cc9600716e46f548b10e Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 14:06:29 +0100 Subject: [PATCH 04/21] Athena-DataCatalog: Set ARN to retrieve tags --- internal/service/athena/data_catalog.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index a9a6579feef..4514540077d 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -2,10 +2,12 @@ package athena import ( "context" + "fmt" "log" "regexp" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/service/athena" "github.com/hashicorp/aws-sdk-go-base/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -32,6 +34,10 @@ func ResourceDataCatalog() *schema.Resource { CustomizeDiff: verify.SetTagsDiff, Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, "name": { Type: schema.TypeString, Required: true, @@ -60,7 +66,7 @@ func ResourceDataCatalog() *schema.Resource { Type: schema.TypeString, //ValidateFunc: validation.StringLenBetween(1, 51200), -- WIP }, - Optional: true, + Required: true, }, "tags": tftags.TagsSchema(), "tags_all": tftags.TagsSchemaComputed(), @@ -122,7 +128,7 @@ func resourceDataCatalogUpdate(ctx context.Context, d *schema.ResourceData, meta } if d.HasChange("type") { - input.Name = aws.String(d.Get("type").(string)) + input.Type = aws.String(d.Get("type").(string)) } if d.HasChange("parameters") { @@ -205,7 +211,17 @@ func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta i d.Set("type", dataCatalog.DataCatalog.Type) d.Set("parameters", aws.StringValueMap(dataCatalog.DataCatalog.Parameters)) - tags, err := ListTags(conn, d.Id()) + arn := arn.ARN{ + Partition: meta.(*conns.AWSClient).Partition, + Region: meta.(*conns.AWSClient).Region, + Service: "athena", + AccountID: meta.(*conns.AWSClient).AccountID, + Resource: fmt.Sprintf("datacatalog/%s", d.Id()), + } + + d.Set("arn", arn.String()) + + tags, err := ListTags(conn, arn.String()) if err != nil { return diag.Errorf("error listing tags for Athena Data Catalog (%s): %s", d.Id(), err) From c46b1ad5704d53a1a8c74b88794a8116e37f81d1 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 14:47:22 +0100 Subject: [PATCH 05/21] Athena-DataCatalog: Implement workaround for the `parameters` as API sets default values --- internal/service/athena/data_catalog.go | 37 +++++++++++++------------ 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index 4514540077d..56b86e97949 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -61,12 +61,15 @@ func ResourceDataCatalog() *schema.Resource { }, false), }, "parameters": { - Type: schema.TypeMap, + Type: schema.TypeMap, + Computed: true, + Optional: true, Elem: &schema.Schema{ - Type: schema.TypeString, + Type: schema.TypeString, + Computed: true, + Optional: true, //ValidateFunc: validation.StringLenBetween(1, 51200), -- WIP }, - Required: true, }, "tags": tftags.TagsSchema(), "tags_all": tftags.TagsSchemaComputed(), @@ -116,19 +119,9 @@ func resourceDataCatalogUpdate(ctx context.Context, d *schema.ResourceData, meta if d.HasChangesExcept("tags", "tags_all") { input := &athena.UpdateDataCatalogInput{ - Name: aws.String(d.Id()), - } - - if d.HasChange("name") { - input.Name = aws.String(d.Get("name").(string)) - } - - if d.HasChange("description") { - input.Name = aws.String(d.Get("description").(string)) - } - - if d.HasChange("type") { - input.Type = aws.String(d.Get("type").(string)) + Name: aws.String(d.Id()), + Type: aws.String(d.Get("type").(string)), + Description: aws.String(d.Get("description").(string)), } if d.HasChange("parameters") { @@ -209,7 +202,17 @@ func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta i d.Set("description", dataCatalog.DataCatalog.Description) d.Set("type", dataCatalog.DataCatalog.Type) - d.Set("parameters", aws.StringValueMap(dataCatalog.DataCatalog.Parameters)) + + // NOTE: This is a workaround for the fact that the API sets default values for parameters that are not set. + // Because the API sets default values, what's returned by the API is different than what's set by the user. + parameters := map[string]*string{} + if v, ok := d.GetOk("parameters"); ok { + if m, ok := v.(map[string]interface{}); ok { + parameters = flex.ExpandStringMap(m) + } + } + + d.Set("parameters", aws.StringValueMap(parameters)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, From f69053d89a551fa757d9b50c40ebfc8871e45894 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 15:57:25 +0100 Subject: [PATCH 06/21] Athena-DataCatalog: Added regex validation to match API spec --- internal/service/athena/data_catalog.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index 56b86e97949..eb4d49ddf26 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -10,6 +10,7 @@ import ( "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/service/athena" "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -62,14 +63,13 @@ func ResourceDataCatalog() *schema.Resource { }, "parameters": { Type: schema.TypeMap, - Computed: true, Optional: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - Computed: true, - Optional: true, - //ValidateFunc: validation.StringLenBetween(1, 51200), -- WIP - }, + Elem: &schema.Schema{Type: schema.TypeString}, + ValidateDiagFunc: allDiagFunc( + validation.MapKeyLenBetween(1, 255), + validation.MapValueLenBetween(0, 51200), + validation.MapValueMatch(regexp.MustCompile(`^[\w@_-]+$`), ""), + ), }, "tags": tftags.TagsSchema(), "tags_all": tftags.TagsSchemaComputed(), @@ -243,3 +243,13 @@ func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta i return nil } + +func allDiagFunc(validators ...schema.SchemaValidateDiagFunc) schema.SchemaValidateDiagFunc { + return func(i interface{}, k cty.Path) diag.Diagnostics { + var diags diag.Diagnostics + for _, validator := range validators { + diags = append(diags, validator(i, k)...) + } + return diags + } +} From 7ce599696623306e31a7b4e9b31aa0d5bdecb0ec Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 19:40:52 +0100 Subject: [PATCH 07/21] Athena-DataCatalog: Add `data_catalog_test.go` --- internal/service/athena/data_catalog.go | 5 +- internal/service/athena/data_catalog_test.go | 150 +++++++++++++++++++ 2 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 internal/service/athena/data_catalog_test.go diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index eb4d49ddf26..da5bd35b2e4 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -17,7 +17,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/flex" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" - "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) @@ -68,7 +67,6 @@ func ResourceDataCatalog() *schema.Resource { ValidateDiagFunc: allDiagFunc( validation.MapKeyLenBetween(1, 255), validation.MapValueLenBetween(0, 51200), - validation.MapValueMatch(regexp.MustCompile(`^[\w@_-]+$`), ""), ), }, "tags": tftags.TagsSchema(), @@ -190,7 +188,8 @@ func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta i dataCatalog, err := conn.GetDataCatalogWithContext(ctx, input) - if !d.IsNewResource() && tfresource.NotFound(err) { + // If the resource doesn't exist, the API returns a `ErrCodeInvalidRequestException` error. + if !d.IsNewResource() && tfawserr.ErrMessageContains(err, athena.ErrCodeInvalidRequestException, "was not found") { log.Printf("[WARN] Athena Data Catalog (%s) not found, removing from state", d.Id()) d.SetId("") return nil diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go new file mode 100644 index 00000000000..5eace75be54 --- /dev/null +++ b/internal/service/athena/data_catalog_test.go @@ -0,0 +1,150 @@ +package athena_test + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/athena" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + tfathena "github.com/hashicorp/terraform-provider-aws/internal/service/athena" +) + +func TestAccDataCatalog_basic(t *testing.T) { + rName := "tf-test-" + sdkacctest.RandString(8) + resourceName := "aws_athena_data_catalog.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDataCatalogDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataCatalogConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "athena", fmt.Sprintf("datacatalog/%s", rName)), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "type", "LAMBDA"), + resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog"), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.Test", "test"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "name", + "parameters", + }, + }, + }, + }) +} + +func TestAccDataCatalog_disappears(t *testing.T) { + rName := "tf-test-" + sdkacctest.RandString(8) + resourceName := "aws_athena_data_catalog.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDataCatalogDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataCatalogConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + acctest.CheckResourceDisappears(acctest.Provider, tfathena.ResourceDataCatalog(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccDataCatalogConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "A test data catalog" + type = "LAMBDA" + + parameters = { + "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } + + tags = { + Test = "test" + } +} +`, rName) +} + +func testAccCheckDataCatalogExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No Athena Data Catalog name is set") + } + + conn := acctest.Provider.Meta().(*conns.AWSClient).AthenaConn + + input := &athena.GetDataCatalogInput{ + Name: aws.String(rs.Primary.ID), + } + + _, err := conn.GetDataCatalog(input) + + if err != nil { + return err + } + + return nil + } +} + +func testAccCheckDataCatalogDestroy(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).AthenaConn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_athena_data_catalog" { + continue + } + + input := &athena.GetDataCatalogInput{ + Name: aws.String(rs.Primary.ID), + } + + output, err := conn.GetDataCatalog(input) + + if tfawserr.ErrMessageContains(err, athena.ErrCodeInvalidRequestException, "was not found") { + continue + } + + if err != nil { + return err + } + + if output.DataCatalog != nil { + return fmt.Errorf("Athena Data Catalog (%s) found", rs.Primary.ID) + } + } + + return nil +} From f9b54275481d231a4de2e97ef2f9fd4681f1fe99 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 19:55:34 +0100 Subject: [PATCH 08/21] Athena-DataCatalog: Added per attribute acceptance tests --- internal/service/athena/data_catalog_test.go | 203 +++++++++++++++++++ 1 file changed, 203 insertions(+) diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index 5eace75be54..7b5ef65db1c 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -52,6 +52,140 @@ func TestAccDataCatalog_basic(t *testing.T) { }) } +func TestAccDataCatalog_type_lambda(t *testing.T) { + rName := "tf-test-" + sdkacctest.RandString(8) + resourceName := "aws_athena_data_catalog.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDataCatalogDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataCatalogConfig_type_lambda(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog using Lambda"), + resource.TestCheckResourceAttr(resourceName, "type", "LAMBDA"), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "2"), + resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + resource.TestCheckResourceAttr(resourceName, "parameters.record-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "name", + "parameters", + }, + }, + }, + }) +} + +func TestAccDataCatalog_type_hive(t *testing.T) { + rName := "tf-test-" + sdkacctest.RandString(8) + resourceName := "aws_athena_data_catalog.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDataCatalogDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataCatalogConfig_type_hive(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog using Hive"), + resource.TestCheckResourceAttr(resourceName, "type", "HIVE"), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "name", + "parameters", + }, + }, + }, + }) +} + +func TestAccDataCatalog_type_glue(t *testing.T) { + rName := "tf-test-" + sdkacctest.RandString(8) + resourceName := "aws_athena_data_catalog.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDataCatalogDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataCatalogConfig_type_glue(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog using Glue"), + resource.TestCheckResourceAttr(resourceName, "type", "GLUE"), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters.catalog-id", "123456789012"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "name", + "parameters", + }, + }, + }, + }) +} + +func TestAccDataCatalog_parameters(t *testing.T) { + rName := "tf-test-" + sdkacctest.RandString(8) + resourceName := "aws_athena_data_catalog.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDataCatalogDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataCatalogConfig_parameters(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "type", "LAMBDA"), + resource.TestCheckResourceAttr(resourceName, "description", "Testing parameters attribute"), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "name", + "parameters", + }, + }, + }, + }) +} + func TestAccDataCatalog_disappears(t *testing.T) { rName := "tf-test-" + sdkacctest.RandString(8) resourceName := "aws_athena_data_catalog.test" @@ -92,6 +226,75 @@ resource "aws_athena_data_catalog" "test" { `, rName) } +func testAccDataCatalogConfig_type_lambda(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "A test data catalog using Lambda" + type = "LAMBDA" + + parameters = { + "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + "record-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } + + tags = { + Test = "test" + } +} +`, rName) +} + +func testAccDataCatalogConfig_type_hive(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "A test data catalog using Hive" + type = "HIVE" + + parameters = { + "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } + + tags = { + Test = "test" + } +} +`, rName) +} + +func testAccDataCatalogConfig_type_glue(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "A test data catalog using Glue" + type = "GLUE" + + parameters = { + "catalog-id" = "123456789012" + } + + tags = { + Test = "test" + } +} +`, rName) +} + +func testAccDataCatalogConfig_parameters(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "Testing parameters attribute" + type = "LAMBDA" + + parameters = { + "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } +} +`, rName) +} + func testAccCheckDataCatalogExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] From c7b8dc1e7410d0e4b7fbab362d5373bb38887838 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Sun, 6 Feb 2022 20:45:05 +0100 Subject: [PATCH 09/21] Athena-DataCatalog: Add new resource docs --- .../docs/r/athena_data_catalog.html.markdown | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 website/docs/r/athena_data_catalog.html.markdown diff --git a/website/docs/r/athena_data_catalog.html.markdown b/website/docs/r/athena_data_catalog.html.markdown new file mode 100644 index 00000000000..aeeaa7e58fb --- /dev/null +++ b/website/docs/r/athena_data_catalog.html.markdown @@ -0,0 +1,102 @@ +--- +subcategory: "Athena" +layout: "aws" +page_title: "AWS: aws_athena_data_catalog" +description: |- + Provides an Athena data catalog. +--- + +# Resource: aws_athena_data_catalog + +Provides an Athena data catalog. + +More information about Athena and Athena data catalogs can be found in the [Athena User Guide](https://docs.aws.amazon.com/athena/latest/ug/what-is.html). + +-> **Tip:** for a more detailed explanation on the usage of `parameters`, see the [DataCatalog API documentation](https://docs.aws.amazon.com/athena/latest/APIReference/API_DataCatalog.html) + +## Example Usage + +```terraform +resource "aws_athena_data_catalog" "example" { + name = "athena-data-catalog" + description = "Example Athena data catalog" + type = "LAMBDA" + + parameters = { + "function" = "arn:aws:lambda:eu-central-1:123456789012:function:not-important-lambda-function" + } + + tags = { + Name = "example-athena-data-catalog" + } +} +``` + +### Hive based Data Catalog + +```terraform +resource "aws_athena_data_catalog" "example" { + name = "hive-data-catalog" + description = "Hive based Data Catalog" + type = "HIVE" + + parameters = { + "metadata-function" = "arn:aws:lambda:eu-central-1:123456789012:function:not-important-lambda-function" + } +} +``` + +### Glue based Data Catalog + +```terraform +resource "aws_athena_data_catalog" "example" { + name = "glue-data-catalog" + description = "Glue based Data Catalog" + type = "GLUE" + + parameters = { + "catalog-id" = "123456789012" + } +} +``` + +### Lambda based Data Catalog + +```terraform +resource "aws_athena_data_catalog" "example" { + name = "lambda-data-catalog" + description = "Lambda based Data Catalog" + type = "LAMBDA" + + parameters = { + "metadata-function" = "arn:aws:lambda:eu-central-1:123456789012:function:not-important-lambda-function-1" + "record-function" = "arn:aws:lambda:eu-central-1:123456789012:function:not-important-lambda-function-2" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +- `name` - (Required) The name of the data catalog. The catalog name must be unique for the AWS account and can use a maximum of 128 alphanumeric, underscore, at sign, or hyphen characters. +- `type` - (Required) The type of data catalog: `LAMBDA` for a federated catalog, `GLUE` for AWS Glue Catalog, or `HIVE` for an external hive metastore. +- `parameters` - (Required) Key value pairs that specifies the Lambda function or functions to use for the data catalog. The mapping used depends on the catalog type. +- `description` - (Optional) A description of the data catalog. +- `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +- `id` - Name of the data catalog. +- `arn` - ARN of the data catalog. +- `tags_all` - Map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block). + +## Import + +Data catalogs can be imported using their `name`, e.g., + +``` +$ terraform import aws_athena_data_catalog.example example-data-catalog +``` \ No newline at end of file From b116b50feeec66ac27e5d7e88b2c85633be4d866 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 08:09:40 -0400 Subject: [PATCH 10/21] r/aws_athena_data_catalog: Alphabetize attributes. --- internal/service/athena/data_catalog.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index da5bd35b2e4..e0db8bff792 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -9,7 +9,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/service/athena" - "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -38,6 +38,10 @@ func ResourceDataCatalog() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "description": { + Type: schema.TypeString, + Optional: true, + }, "name": { Type: schema.TypeString, Required: true, @@ -47,19 +51,6 @@ func ResourceDataCatalog() *schema.Resource { validation.StringMatch(regexp.MustCompile(`[\w@-]*`), ""), ), }, - "description": { - Type: schema.TypeString, - Optional: true, - }, - "type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringInSlice([]string{ - athena.DataCatalogTypeLambda, - athena.DataCatalogTypeGlue, - athena.DataCatalogTypeHive, - }, false), - }, "parameters": { Type: schema.TypeMap, Optional: true, @@ -71,6 +62,11 @@ func ResourceDataCatalog() *schema.Resource { }, "tags": tftags.TagsSchema(), "tags_all": tftags.TagsSchemaComputed(), + "type": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice(athena.DataCatalogType_Values(), false), + }, }, } } From 6426e63d54c972626c04630c97c3f7b476d59bd5 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 08:20:52 -0400 Subject: [PATCH 11/21] r/aws_athena_data_catalog: Reorder CRUD handler functions. --- internal/service/athena/data_catalog.go | 156 +++++++++++------------- 1 file changed, 71 insertions(+), 85 deletions(-) diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index e0db8bff792..d704750a564 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -77,14 +77,13 @@ func resourceDataCatalogCreate(ctx context.Context, d *schema.ResourceData, meta tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{}))) name := d.Get("name").(string) - input := &athena.CreateDataCatalogInput{ Name: aws.String(name), Description: aws.String(d.Get("description").(string)), Type: aws.String(d.Get("type").(string)), } - if v, ok := d.GetOk("parameters"); ok { + if v, ok := d.GetOk("parameters"); ok && len(v.(map[string]interface{})) > 0 { input.Parameters = flex.ExpandStringMap(v.(map[string]interface{})) } @@ -92,15 +91,11 @@ func resourceDataCatalogCreate(ctx context.Context, d *schema.ResourceData, meta input.Tags = Tags(tags.IgnoreAWS()) } - if err := input.Validate(); err != nil { - return diag.Errorf("Error validating Athena Data Catalog (%s): %s", name, err) - } - - log.Printf("[DEBUG] Creating Data Catalog: %s", input) + log.Printf("[DEBUG] Creating Athena Data Catalog: %s", input) _, err := conn.CreateDataCatalogWithContext(ctx, input) if err != nil { - return diag.Errorf("error creating Athena Data Catalog (%s): %s", name, err) + return diag.Errorf("creating Athena Data Catalog (%s): %s", name, err) } d.SetId(name) @@ -108,6 +103,70 @@ func resourceDataCatalogCreate(ctx context.Context, d *schema.ResourceData, meta return resourceDataCatalogRead(ctx, d, meta) } +func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).AthenaConn + defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + + input := &athena.GetDataCatalogInput{ + Name: aws.String(d.Id()), + } + + dataCatalog, err := conn.GetDataCatalogWithContext(ctx, input) + + // If the resource doesn't exist, the API returns a `ErrCodeInvalidRequestException` error. + if !d.IsNewResource() && tfawserr.ErrMessageContains(err, athena.ErrCodeInvalidRequestException, "was not found") { + log.Printf("[WARN] Athena Data Catalog (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + if err != nil { + return diag.Errorf("reading Athena Data Catalog (%s): %s", d.Id(), err) + } + + arn := arn.ARN{ + Partition: meta.(*conns.AWSClient).Partition, + Region: meta.(*conns.AWSClient).Region, + Service: "athena", + AccountID: meta.(*conns.AWSClient).AccountID, + Resource: fmt.Sprintf("datacatalog/%s", d.Id()), + }.String() + d.Set("arn", arn) + d.Set("description", dataCatalog.DataCatalog.Description) + d.Set("type", dataCatalog.DataCatalog.Type) + + // NOTE: This is a workaround for the fact that the API sets default values for parameters that are not set. + // Because the API sets default values, what's returned by the API is different than what's set by the user. + parameters := map[string]*string{} + if v, ok := d.GetOk("parameters"); ok { + if m, ok := v.(map[string]interface{}); ok { + parameters = flex.ExpandStringMap(m) + } + } + + d.Set("parameters", aws.StringValueMap(parameters)) + + tags, err := ListTags(conn, arn) + + if err != nil { + return diag.Errorf("listing tags for Athena Data Catalog (%s): %s", d.Id(), err) + } + + tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig) + + //lintignore:AWSR002 + if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil { + return diag.Errorf("setting tags: %s", err) + } + + if err := d.Set("tags_all", tags.Map()); err != nil { + return diag.Errorf("error setting tags_all: %s", err) + } + + return nil +} + func resourceDataCatalogUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).AthenaConn @@ -128,25 +187,19 @@ func resourceDataCatalogUpdate(ctx context.Context, d *schema.ResourceData, meta input.Parameters = parameters } - log.Printf("[DEBUG] Updating Athena Data Catalog (%s)", d.Id()) - - if err := input.Validate(); err != nil { - return diag.Errorf("Error validating Athena Data Catalog (%s): %s", d.Id(), err) - } - + log.Printf("[DEBUG] Updating Athena Data Catalog: %s", input) _, err := conn.UpdateDataCatalogWithContext(ctx, input) if err != nil { - return diag.Errorf("error updating Athena Data Catalog (%s): %s", d.Id(), err) + return diag.Errorf("updating Athena Data Catalog (%s): %s", d.Id(), err) } } if d.HasChange("tags_all") { o, n := d.GetChange("tags_all") - log.Printf("[DEBUG] Updating Athena Data Catalog (%s) tags", d.Id()) if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil { - return diag.Errorf("error updating Athena Data Catalog (%s) tags: %s", d.Id(), err) + return diag.Errorf("updating Athena Data Catalog (%s) tags: %s", d.Id(), err) } } @@ -157,7 +210,6 @@ func resourceDataCatalogDelete(ctx context.Context, d *schema.ResourceData, meta conn := meta.(*conns.AWSClient).AthenaConn log.Printf("[DEBUG] Deleting Athena Data Catalog: (%s)", d.Id()) - _, err := conn.DeleteDataCatalogWithContext(ctx, &athena.DeleteDataCatalogInput{ Name: aws.String(d.Id()), }) @@ -167,73 +219,7 @@ func resourceDataCatalogDelete(ctx context.Context, d *schema.ResourceData, meta } if err != nil { - return diag.Errorf("error deleting Athena Data Catalog (%s): %s", d.Id(), err) - } - - return nil -} - -func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*conns.AWSClient).AthenaConn - defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig - ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - - input := &athena.GetDataCatalogInput{ - Name: aws.String(d.Id()), - } - - dataCatalog, err := conn.GetDataCatalogWithContext(ctx, input) - - // If the resource doesn't exist, the API returns a `ErrCodeInvalidRequestException` error. - if !d.IsNewResource() && tfawserr.ErrMessageContains(err, athena.ErrCodeInvalidRequestException, "was not found") { - log.Printf("[WARN] Athena Data Catalog (%s) not found, removing from state", d.Id()) - d.SetId("") - return nil - } - - if err != nil { - return diag.Errorf("error reading Athena Data Catalog (%s): %s", d.Id(), err) - } - - d.Set("description", dataCatalog.DataCatalog.Description) - d.Set("type", dataCatalog.DataCatalog.Type) - - // NOTE: This is a workaround for the fact that the API sets default values for parameters that are not set. - // Because the API sets default values, what's returned by the API is different than what's set by the user. - parameters := map[string]*string{} - if v, ok := d.GetOk("parameters"); ok { - if m, ok := v.(map[string]interface{}); ok { - parameters = flex.ExpandStringMap(m) - } - } - - d.Set("parameters", aws.StringValueMap(parameters)) - - arn := arn.ARN{ - Partition: meta.(*conns.AWSClient).Partition, - Region: meta.(*conns.AWSClient).Region, - Service: "athena", - AccountID: meta.(*conns.AWSClient).AccountID, - Resource: fmt.Sprintf("datacatalog/%s", d.Id()), - } - - d.Set("arn", arn.String()) - - tags, err := ListTags(conn, arn.String()) - - if err != nil { - return diag.Errorf("error listing tags for Athena Data Catalog (%s): %s", d.Id(), err) - } - - tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig) - - //lintignore:AWSR002 - if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil { - return diag.Errorf("error setting tags for Athena Data Catalog (%s): %s", d.Id(), err) - } - - if err := d.Set("tags_all", tags.Map()); err != nil { - return diag.Errorf("error setting tags_all for Athena Data Catalog (%s): %s", d.Id(), err) + return diag.Errorf("deleting Athena Data Catalog (%s): %s", d.Id(), err) } return nil From 32a5c2ace486955fe46a3731cfda2c7d8d338ab5 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 08:24:29 -0400 Subject: [PATCH 12/21] r/aws_athena_data_catalog: Tidy up acceptance test configurations. --- internal/service/athena/data_catalog_test.go | 184 +++++++++---------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index 7b5ef65db1c..ccd9e01e38f 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -6,7 +6,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/athena" - "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" @@ -63,7 +63,7 @@ func TestAccDataCatalog_type_lambda(t *testing.T) { CheckDestroy: testAccCheckDataCatalogDestroy, Steps: []resource.TestStep{ { - Config: testAccDataCatalogConfig_type_lambda(rName), + Config: testAccDataCatalogTypeLambdaConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckDataCatalogExists(resourceName), resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog using Lambda"), @@ -97,7 +97,7 @@ func TestAccDataCatalog_type_hive(t *testing.T) { CheckDestroy: testAccCheckDataCatalogDestroy, Steps: []resource.TestStep{ { - Config: testAccDataCatalogConfig_type_hive(rName), + Config: testAccDataCatalogTypeHiveConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckDataCatalogExists(resourceName), resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog using Hive"), @@ -130,7 +130,7 @@ func TestAccDataCatalog_type_glue(t *testing.T) { CheckDestroy: testAccCheckDataCatalogDestroy, Steps: []resource.TestStep{ { - Config: testAccDataCatalogConfig_type_glue(rName), + Config: testAccDataCatalogTypeGlueConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckDataCatalogExists(resourceName), resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog using Glue"), @@ -163,7 +163,7 @@ func TestAccDataCatalog_parameters(t *testing.T) { CheckDestroy: testAccCheckDataCatalogDestroy, Steps: []resource.TestStep{ { - Config: testAccDataCatalogConfig_parameters(rName), + Config: testAccDataCatalogParametersConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckDataCatalogExists(resourceName), resource.TestCheckResourceAttr(resourceName, "name", rName), @@ -208,93 +208,6 @@ func TestAccDataCatalog_disappears(t *testing.T) { }) } -func testAccDataCatalogConfig(rName string) string { - return fmt.Sprintf(` -resource "aws_athena_data_catalog" "test" { - name = %[1]q - description = "A test data catalog" - type = "LAMBDA" - - parameters = { - "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" - } - - tags = { - Test = "test" - } -} -`, rName) -} - -func testAccDataCatalogConfig_type_lambda(rName string) string { - return fmt.Sprintf(` -resource "aws_athena_data_catalog" "test" { - name = %[1]q - description = "A test data catalog using Lambda" - type = "LAMBDA" - - parameters = { - "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" - "record-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" - } - - tags = { - Test = "test" - } -} -`, rName) -} - -func testAccDataCatalogConfig_type_hive(rName string) string { - return fmt.Sprintf(` -resource "aws_athena_data_catalog" "test" { - name = %[1]q - description = "A test data catalog using Hive" - type = "HIVE" - - parameters = { - "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" - } - - tags = { - Test = "test" - } -} -`, rName) -} - -func testAccDataCatalogConfig_type_glue(rName string) string { - return fmt.Sprintf(` -resource "aws_athena_data_catalog" "test" { - name = %[1]q - description = "A test data catalog using Glue" - type = "GLUE" - - parameters = { - "catalog-id" = "123456789012" - } - - tags = { - Test = "test" - } -} -`, rName) -} - -func testAccDataCatalogConfig_parameters(rName string) string { - return fmt.Sprintf(` -resource "aws_athena_data_catalog" "test" { - name = %[1]q - description = "Testing parameters attribute" - type = "LAMBDA" - - parameters = { - "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" - } -} -`, rName) -} - func testAccCheckDataCatalogExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -351,3 +264,90 @@ func testAccCheckDataCatalogDestroy(s *terraform.State) error { return nil } + +func testAccDataCatalogConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "A test data catalog" + type = "LAMBDA" + + parameters = { + "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } + + tags = { + Test = "test" + } +} +`, rName) +} + +func testAccDataCatalogTypeLambdaConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "A test data catalog using Lambda" + type = "LAMBDA" + + parameters = { + "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + "record-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } + + tags = { + Test = "test" + } +} +`, rName) +} + +func testAccDataCatalogTypeHiveConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "A test data catalog using Hive" + type = "HIVE" + + parameters = { + "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } + + tags = { + Test = "test" + } +} +`, rName) +} + +func testAccDataCatalogTypeGlueConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "A test data catalog using Glue" + type = "GLUE" + + parameters = { + "catalog-id" = "123456789012" + } + + tags = { + Test = "test" + } +} +`, rName) +} + +func testAccDataCatalogParametersConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "Testing parameters attribute" + type = "LAMBDA" + + parameters = { + "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } +} +`, rName) +} From 08d5c98873abc2ca60dd869d6f07401aed9d6e88 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 08:25:49 -0400 Subject: [PATCH 13/21] r/aws_athena_data_catalog: Rename acceptance test functions. --- internal/service/athena/data_catalog_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index ccd9e01e38f..362c2abc83c 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -15,7 +15,7 @@ import ( tfathena "github.com/hashicorp/terraform-provider-aws/internal/service/athena" ) -func TestAccDataCatalog_basic(t *testing.T) { +func TestAccAthenaDataCatalog_basic(t *testing.T) { rName := "tf-test-" + sdkacctest.RandString(8) resourceName := "aws_athena_data_catalog.test" @@ -52,7 +52,7 @@ func TestAccDataCatalog_basic(t *testing.T) { }) } -func TestAccDataCatalog_type_lambda(t *testing.T) { +func TestAccAthenaDataCatalog_type_lambda(t *testing.T) { rName := "tf-test-" + sdkacctest.RandString(8) resourceName := "aws_athena_data_catalog.test" @@ -86,7 +86,7 @@ func TestAccDataCatalog_type_lambda(t *testing.T) { }) } -func TestAccDataCatalog_type_hive(t *testing.T) { +func TestAccAthenaDataCatalog_type_hive(t *testing.T) { rName := "tf-test-" + sdkacctest.RandString(8) resourceName := "aws_athena_data_catalog.test" @@ -119,7 +119,7 @@ func TestAccDataCatalog_type_hive(t *testing.T) { }) } -func TestAccDataCatalog_type_glue(t *testing.T) { +func TestAccAthenaDataCatalog_type_glue(t *testing.T) { rName := "tf-test-" + sdkacctest.RandString(8) resourceName := "aws_athena_data_catalog.test" @@ -152,7 +152,7 @@ func TestAccDataCatalog_type_glue(t *testing.T) { }) } -func TestAccDataCatalog_parameters(t *testing.T) { +func TestAccAthenaDataCatalog_parameters(t *testing.T) { rName := "tf-test-" + sdkacctest.RandString(8) resourceName := "aws_athena_data_catalog.test" From 82663e4547e5dd8865f58ea3f4cd30da06bc30ac Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 08:55:45 -0400 Subject: [PATCH 14/21] r/aws_athena_data_catalog: Tidup up 'parameters' handling. --- internal/service/athena/data_catalog.go | 25 +++++++++++++++----- internal/service/athena/data_catalog_test.go | 9 ++----- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index d704750a564..5fbffc1ace0 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -138,6 +138,21 @@ func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta i // NOTE: This is a workaround for the fact that the API sets default values for parameters that are not set. // Because the API sets default values, what's returned by the API is different than what's set by the user. + if v, ok := d.GetOk("parameters"); ok && len(v.(map[string]interface{})) > 0 { + parameters := make(map[string]string, 0) + + for key, val := range v.(map[string]interface{}) { + if v, ok := dataCatalog.DataCatalog.Parameters[key]; ok { + parameters[key] = aws.StringValue(v) + } else { + parameters[key] = val.(string) + } + } + + d.Set("parameters", parameters) + } else { + d.Set("parameters", nil) + } parameters := map[string]*string{} if v, ok := d.GetOk("parameters"); ok { if m, ok := v.(map[string]interface{}); ok { @@ -178,13 +193,11 @@ func resourceDataCatalogUpdate(ctx context.Context, d *schema.ResourceData, meta } if d.HasChange("parameters") { - parameters := map[string]*string{} - if v, ok := d.GetOk("parameters"); ok { - if m, ok := v.(map[string]interface{}); ok { - parameters = flex.ExpandStringMap(m) - } + if v, ok := d.GetOk("parameters"); ok && len(v.(map[string]interface{})) > 0 { + input.Parameters = flex.ExpandStringMap(v.(map[string]interface{})) + } else { + input.Parameters = make(map[string]*string, 0) } - input.Parameters = parameters } log.Printf("[DEBUG] Updating Athena Data Catalog: %s", input) diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index 362c2abc83c..19eab6eaa92 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -35,8 +35,7 @@ func TestAccAthenaDataCatalog_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog"), resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), - resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), - resource.TestCheckResourceAttr(resourceName, "tags.Test", "test"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, { @@ -275,10 +274,6 @@ resource "aws_athena_data_catalog" "test" { parameters = { "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" } - - tags = { - Test = "test" - } } `, rName) } @@ -292,7 +287,7 @@ resource "aws_athena_data_catalog" "test" { parameters = { "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" - "record-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + "record-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" } tags = { From 88f7fb11b34346a3eec9d09e5bf22351815c0098 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 09:10:22 -0400 Subject: [PATCH 15/21] r/aws_athena_data_catalog: Test 'parameters' update. --- internal/service/athena/data_catalog.go | 11 +-- internal/service/athena/data_catalog_test.go | 86 +++++++++++--------- 2 files changed, 47 insertions(+), 50 deletions(-) diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index 5fbffc1ace0..dd63436088f 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -134,6 +134,7 @@ func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta i }.String() d.Set("arn", arn) d.Set("description", dataCatalog.DataCatalog.Description) + d.Set("name", dataCatalog.DataCatalog.Name) d.Set("type", dataCatalog.DataCatalog.Type) // NOTE: This is a workaround for the fact that the API sets default values for parameters that are not set. @@ -153,14 +154,6 @@ func resourceDataCatalogRead(ctx context.Context, d *schema.ResourceData, meta i } else { d.Set("parameters", nil) } - parameters := map[string]*string{} - if v, ok := d.GetOk("parameters"); ok { - if m, ok := v.(map[string]interface{}); ok { - parameters = flex.ExpandStringMap(m) - } - } - - d.Set("parameters", aws.StringValueMap(parameters)) tags, err := ListTags(conn, arn) @@ -195,8 +188,6 @@ func resourceDataCatalogUpdate(ctx context.Context, d *schema.ResourceData, meta if d.HasChange("parameters") { if v, ok := d.GetOk("parameters"); ok && len(v.(map[string]interface{})) > 0 { input.Parameters = flex.ExpandStringMap(v.(map[string]interface{})) - } else { - input.Parameters = make(map[string]*string, 0) } } diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index 19eab6eaa92..b57ad94fa5f 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -39,13 +39,10 @@ func TestAccAthenaDataCatalog_basic(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{ - "name", - "parameters", - }, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"parameters"}, }, }, }) @@ -73,13 +70,10 @@ func TestAccAthenaDataCatalog_type_lambda(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{ - "name", - "parameters", - }, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"parameters"}, }, }, }) @@ -106,13 +100,10 @@ func TestAccAthenaDataCatalog_type_hive(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{ - "name", - "parameters", - }, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"parameters"}, }, }, }) @@ -139,13 +130,10 @@ func TestAccAthenaDataCatalog_type_glue(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{ - "name", - "parameters", - }, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"parameters"}, }, }, }) @@ -165,21 +153,24 @@ func TestAccAthenaDataCatalog_parameters(t *testing.T) { Config: testAccDataCatalogParametersConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckDataCatalogExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "type", "LAMBDA"), - resource.TestCheckResourceAttr(resourceName, "description", "Testing parameters attribute"), resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), - resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-1"), ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{ - "name", - "parameters", - }, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"parameters"}, + }, + { + Config: testAccDataCatalogParametersUpdatedConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "2"), + resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-2"), + resource.TestCheckResourceAttr(resourceName, "parameters.record-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-2"), + ), }, }, }) @@ -341,7 +332,22 @@ resource "aws_athena_data_catalog" "test" { type = "LAMBDA" parameters = { - "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function-1" + } +} +`, rName) +} + +func testAccDataCatalogParametersUpdatedConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + description = "Testing parameters attribute" + type = "LAMBDA" + + parameters = { + "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function-2" + "record-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function-2" } } `, rName) From 0f07b9ae709b8aac4be634e5939be33d2e212339 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 09:21:56 -0400 Subject: [PATCH 16/21] r/aws_athena_data_catalog: Add 'TestAccAthenaDataCatalog_tags'. --- internal/service/athena/data_catalog.go | 4 +- internal/service/athena/data_catalog_test.go | 140 +++++++++++++----- .../docs/r/athena_data_catalog.html.markdown | 2 +- 3 files changed, 109 insertions(+), 37 deletions(-) diff --git a/internal/service/athena/data_catalog.go b/internal/service/athena/data_catalog.go index dd63436088f..6bbd8bf7a12 100644 --- a/internal/service/athena/data_catalog.go +++ b/internal/service/athena/data_catalog.go @@ -40,7 +40,7 @@ func ResourceDataCatalog() *schema.Resource { }, "description": { Type: schema.TypeString, - Optional: true, + Required: true, }, "name": { Type: schema.TypeString, @@ -53,7 +53,7 @@ func ResourceDataCatalog() *schema.Resource { }, "parameters": { Type: schema.TypeMap, - Optional: true, + Required: true, Elem: &schema.Schema{Type: schema.TypeString}, ValidateDiagFunc: allDiagFunc( validation.MapKeyLenBetween(1, 255), diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index b57ad94fa5f..027fe63e4d7 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -48,6 +48,73 @@ func TestAccAthenaDataCatalog_basic(t *testing.T) { }) } +func TestAccDataCatalog_disappears(t *testing.T) { + rName := "tf-test-" + sdkacctest.RandString(8) + resourceName := "aws_athena_data_catalog.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDataCatalogDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataCatalogConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + acctest.CheckResourceDisappears(acctest.Provider, tfathena.ResourceDataCatalog(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccAthenaDataCatalog_tags(t *testing.T) { + rName := "tf-test-" + sdkacctest.RandString(8) + resourceName := "aws_athena_data_catalog.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDataCatalogDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataCatalogConfigTags1(rName, "key1", "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"parameters"}, + }, + { + Config: testAccDataCatalogConfigTags2(rName, "key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccDataCatalogConfigTags1(rName, "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckDataCatalogExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + }, + }) +} + func TestAccAthenaDataCatalog_type_lambda(t *testing.T) { rName := "tf-test-" + sdkacctest.RandString(8) resourceName := "aws_athena_data_catalog.test" @@ -176,28 +243,6 @@ func TestAccAthenaDataCatalog_parameters(t *testing.T) { }) } -func TestAccDataCatalog_disappears(t *testing.T) { - rName := "tf-test-" + sdkacctest.RandString(8) - resourceName := "aws_athena_data_catalog.test" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acctest.PreCheck(t) }, - ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), - Providers: acctest.Providers, - CheckDestroy: testAccCheckDataCatalogDestroy, - Steps: []resource.TestStep{ - { - Config: testAccDataCatalogConfig(rName), - Check: resource.ComposeTestCheckFunc( - testAccCheckDataCatalogExists(resourceName), - acctest.CheckResourceDisappears(acctest.Provider, tfathena.ResourceDataCatalog(), resourceName), - ), - ExpectNonEmptyPlan: true, - }, - }, - }) -} - func testAccCheckDataCatalogExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -269,6 +314,45 @@ resource "aws_athena_data_catalog" "test" { `, rName) } +func testAccDataCatalogConfigTags1(rName, tagKey1, tagValue1 string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + type = "LAMBDA" + + description = "Testing tags" + + parameters = { + "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } + + tags = { + %[2]q = %[3]q + } +} +`, rName, tagKey1, tagValue1) +} + +func testAccDataCatalogConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` +resource "aws_athena_data_catalog" "test" { + name = %[1]q + type = "LAMBDA" + + description = "Testing tags" + + parameters = { + "function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" + } + + tags = { + %[2]q = %[3]q + %[4]q = %[5]q + } +} +`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +} + func testAccDataCatalogTypeLambdaConfig(rName string) string { return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { @@ -280,10 +364,6 @@ resource "aws_athena_data_catalog" "test" { "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" "record-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" } - - tags = { - Test = "test" - } } `, rName) } @@ -298,10 +378,6 @@ resource "aws_athena_data_catalog" "test" { parameters = { "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" } - - tags = { - Test = "test" - } } `, rName) } @@ -316,10 +392,6 @@ resource "aws_athena_data_catalog" "test" { parameters = { "catalog-id" = "123456789012" } - - tags = { - Test = "test" - } } `, rName) } diff --git a/website/docs/r/athena_data_catalog.html.markdown b/website/docs/r/athena_data_catalog.html.markdown index aeeaa7e58fb..dab6b0a8865 100644 --- a/website/docs/r/athena_data_catalog.html.markdown +++ b/website/docs/r/athena_data_catalog.html.markdown @@ -82,7 +82,7 @@ The following arguments are supported: - `name` - (Required) The name of the data catalog. The catalog name must be unique for the AWS account and can use a maximum of 128 alphanumeric, underscore, at sign, or hyphen characters. - `type` - (Required) The type of data catalog: `LAMBDA` for a federated catalog, `GLUE` for AWS Glue Catalog, or `HIVE` for an external hive metastore. - `parameters` - (Required) Key value pairs that specifies the Lambda function or functions to use for the data catalog. The mapping used depends on the catalog type. -- `description` - (Optional) A description of the data catalog. +- `description` - (Required) A description of the data catalog. - `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. ## Attributes Reference From 7c3371011d1b3a4e8b8b5325cb5ac58cb336ffbb Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 09:25:08 -0400 Subject: [PATCH 17/21] Fix 'MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]'. --- website/docs/r/athena_data_catalog.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/athena_data_catalog.html.markdown b/website/docs/r/athena_data_catalog.html.markdown index dab6b0a8865..34a16b94aef 100644 --- a/website/docs/r/athena_data_catalog.html.markdown +++ b/website/docs/r/athena_data_catalog.html.markdown @@ -12,7 +12,7 @@ Provides an Athena data catalog. More information about Athena and Athena data catalogs can be found in the [Athena User Guide](https://docs.aws.amazon.com/athena/latest/ug/what-is.html). --> **Tip:** for a more detailed explanation on the usage of `parameters`, see the [DataCatalog API documentation](https://docs.aws.amazon.com/athena/latest/APIReference/API_DataCatalog.html) +-> **Tip:** for a more detailed explanation on the usage of `parameters`, see the [DataCatalog API documentation](https://docs.aws.amazon.com/athena/latest/APIReference/API_DataCatalog.html) ## Example Usage From 2fd96f92fe76ab518b1f4dc6103ee717400ad3f3 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 09:26:07 -0400 Subject: [PATCH 18/21] Fix terrafmt error. --- internal/service/athena/data_catalog_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index 027fe63e4d7..554623e8d31 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -356,9 +356,9 @@ resource "aws_athena_data_catalog" "test" { func testAccDataCatalogTypeLambdaConfig(rName string) string { return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { - name = %[1]q + name = %[1]q description = "A test data catalog using Lambda" - type = "LAMBDA" + type = "LAMBDA" parameters = { "metadata-function" = "arn:aws:lambda:us-east-1:123456789012:function:test-function" From d19467988228cf539ded71f27a0a41518bf8de63 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 09:29:16 -0400 Subject: [PATCH 19/21] Really fix terrafmt error. --- internal/service/athena/data_catalog_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index 554623e8d31..8c9c6a1fc6d 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -356,7 +356,7 @@ resource "aws_athena_data_catalog" "test" { func testAccDataCatalogTypeLambdaConfig(rName string) string { return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { - name = %[1]q + name = %[1]q description = "A test data catalog using Lambda" type = "LAMBDA" From 03850032599b5a10b71a9d1440d89cfcd60bebfa Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 11:00:08 -0400 Subject: [PATCH 20/21] Fix 'AT012: file contains multiple acceptance test name prefixes: [TestAccAthenaDataCatalog TestAccDataCatalog]'. --- internal/service/athena/data_catalog_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index 8c9c6a1fc6d..e0952ca5ba0 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -48,7 +48,7 @@ func TestAccAthenaDataCatalog_basic(t *testing.T) { }) } -func TestAccDataCatalog_disappears(t *testing.T) { +func TestAccAthenaDataCatalog_disappears(t *testing.T) { rName := "tf-test-" + sdkacctest.RandString(8) resourceName := "aws_athena_data_catalog.test" From 64d83f0243cf1fe2fe2203db214e8c52c98c7119 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 22 Apr 2022 11:02:16 -0400 Subject: [PATCH 21/21] Add '//lintignore:AWSAT003,AWSAT005' in multiple places. --- internal/service/athena/data_catalog_test.go | 21 +++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/service/athena/data_catalog_test.go b/internal/service/athena/data_catalog_test.go index e0952ca5ba0..b68b50939e7 100644 --- a/internal/service/athena/data_catalog_test.go +++ b/internal/service/athena/data_catalog_test.go @@ -34,7 +34,7 @@ func TestAccAthenaDataCatalog_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "type", "LAMBDA"), resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog"), resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), - resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), //lintignore:AWSAT003,AWSAT005 resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, @@ -132,8 +132,8 @@ func TestAccAthenaDataCatalog_type_lambda(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog using Lambda"), resource.TestCheckResourceAttr(resourceName, "type", "LAMBDA"), resource.TestCheckResourceAttr(resourceName, "parameters.%", "2"), - resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), - resource.TestCheckResourceAttr(resourceName, "parameters.record-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), //lintignore:AWSAT003,AWSAT005 + resource.TestCheckResourceAttr(resourceName, "parameters.record-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), //lintignore:AWSAT003,AWSAT005 ), }, { @@ -163,7 +163,7 @@ func TestAccAthenaDataCatalog_type_hive(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "description", "A test data catalog using Hive"), resource.TestCheckResourceAttr(resourceName, "type", "HIVE"), resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), - resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), + resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function"), //lintignore:AWSAT003,AWSAT005 ), }, { @@ -221,7 +221,7 @@ func TestAccAthenaDataCatalog_parameters(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckDataCatalogExists(resourceName), resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), - resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-1"), + resource.TestCheckResourceAttr(resourceName, "parameters.function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-1"), //lintignore:AWSAT003,AWSAT005 ), }, { @@ -235,8 +235,8 @@ func TestAccAthenaDataCatalog_parameters(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckDataCatalogExists(resourceName), resource.TestCheckResourceAttr(resourceName, "parameters.%", "2"), - resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-2"), - resource.TestCheckResourceAttr(resourceName, "parameters.record-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-2"), + resource.TestCheckResourceAttr(resourceName, "parameters.metadata-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-2"), //lintignore:AWSAT003,AWSAT005 + resource.TestCheckResourceAttr(resourceName, "parameters.record-function", "arn:aws:lambda:us-east-1:123456789012:function:test-function-2"), //lintignore:AWSAT003,AWSAT005 ), }, }, @@ -301,6 +301,7 @@ func testAccCheckDataCatalogDestroy(s *terraform.State) error { } func testAccDataCatalogConfig(rName string) string { + //lintignore:AWSAT003,AWSAT005 return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { name = %[1]q @@ -315,6 +316,7 @@ resource "aws_athena_data_catalog" "test" { } func testAccDataCatalogConfigTags1(rName, tagKey1, tagValue1 string) string { + //lintignore:AWSAT003,AWSAT005 return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { name = %[1]q @@ -334,6 +336,7 @@ resource "aws_athena_data_catalog" "test" { } func testAccDataCatalogConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + //lintignore:AWSAT003,AWSAT005 return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { name = %[1]q @@ -354,6 +357,7 @@ resource "aws_athena_data_catalog" "test" { } func testAccDataCatalogTypeLambdaConfig(rName string) string { + //lintignore:AWSAT003,AWSAT005 return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { name = %[1]q @@ -369,6 +373,7 @@ resource "aws_athena_data_catalog" "test" { } func testAccDataCatalogTypeHiveConfig(rName string) string { + //lintignore:AWSAT003,AWSAT005 return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { name = %[1]q @@ -397,6 +402,7 @@ resource "aws_athena_data_catalog" "test" { } func testAccDataCatalogParametersConfig(rName string) string { + //lintignore:AWSAT003,AWSAT005 return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { name = %[1]q @@ -411,6 +417,7 @@ resource "aws_athena_data_catalog" "test" { } func testAccDataCatalogParametersUpdatedConfig(rName string) string { + //lintignore:AWSAT003,AWSAT005 return fmt.Sprintf(` resource "aws_athena_data_catalog" "test" { name = %[1]q