From 02adf61f5e2888a507fb313a4fe189e7694e6d68 Mon Sep 17 00:00:00 2001 From: drexler Date: Wed, 1 Dec 2021 21:54:24 -0500 Subject: [PATCH 1/3] feat: support comment property for athena databases --- internal/service/athena/database.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/service/athena/database.go b/internal/service/athena/database.go index 4827d58866a..9845bd9a265 100644 --- a/internal/service/athena/database.go +++ b/internal/service/athena/database.go @@ -33,6 +33,10 @@ func ResourceDatabase() *schema.Resource { Required: true, ForceNew: true, }, + "comment": { + Type: schema.TypeString, + Optional: true, + }, "force_destroy": { Type: schema.TypeBool, Optional: true, @@ -93,8 +97,15 @@ func expandAthenaResultConfiguration(bucket string, encryptionConfigurationList func resourceDatabaseCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*conns.AWSClient).AthenaConn + var databaseDescription string + if v, ok := d.GetOk("comment"); ok { + databaseDescription = strings.Replace(v.(string), "'", "\\'", -1) + } else { + databaseDescription = "" + } + input := &athena.StartQueryExecutionInput{ - QueryString: aws.String(fmt.Sprintf("create database `%s`;", d.Get("name").(string))), + QueryString: aws.String(fmt.Sprintf("create database `%[1]s` comment '%[2]s';", d.Get("name").(string), databaseDescription)), ResultConfiguration: expandAthenaResultConfiguration(d.Get("bucket").(string), d.Get("encryption_configuration").([]interface{})), } From f6c364d6014d965ab238e7ee1bb21525cb8f47bf Mon Sep 17 00:00:00 2001 From: drexler Date: Wed, 1 Dec 2021 21:54:52 -0500 Subject: [PATCH 2/3] test: add cover test for comment property --- internal/service/athena/database_test.go | 72 ++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/internal/service/athena/database_test.go b/internal/service/athena/database_test.go index 3d96f064e3a..943807129f4 100644 --- a/internal/service/athena/database_test.go +++ b/internal/service/athena/database_test.go @@ -35,6 +35,46 @@ func TestAccAthenaDatabase_basic(t *testing.T) { }) } +func TestAccAthenaDatabase_description(t *testing.T) { + rInt := sdkacctest.RandInt() + dbName := sdkacctest.RandString(8) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAthenaDatabaseCommentConfig(rInt, dbName, false), + Check: resource.ComposeTestCheckFunc( + testAccCheckDatabaseExists("aws_athena_database.hoge"), + resource.TestCheckResourceAttr("aws_athena_database.hoge", "name", dbName), + ), + }, + }, + }) +} + +func TestAccAthenaDatabase_unescaped_description(t *testing.T) { + rInt := sdkacctest.RandInt() + dbName := sdkacctest.RandString(8) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, athena.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAthenaDatabaseUnescapedCommentConfig(rInt, dbName, false), + Check: resource.ComposeTestCheckFunc( + testAccCheckDatabaseExists("aws_athena_database.hoge"), + resource.TestCheckResourceAttr("aws_athena_database.hoge", "name", dbName), + ), + }, + }, + }) +} + func TestAccAthenaDatabase_encryption(t *testing.T) { rInt := sdkacctest.RandInt() dbName := sdkacctest.RandString(8) @@ -354,6 +394,38 @@ resource "aws_athena_database" "hoge" { `, randInt, dbName, forceDestroy) } +func testAccAthenaDatabaseCommentConfig(randInt int, dbName string, forceDestroy bool) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" "hoge" { + bucket = "tf-test-athena-db-%[1]d" + force_destroy = true +} + +resource "aws_athena_database" "hoge" { + name = "%[2]s" + bucket = aws_s3_bucket.hoge.bucket + comment = "athena is a goddess" + force_destroy = %[3]t +} +`, randInt, dbName, forceDestroy) +} + +func testAccAthenaDatabaseUnescapedCommentConfig(randInt int, dbName string, forceDestroy bool) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" "hoge" { + bucket = "tf-test-athena-db-%[1]d" + force_destroy = true +} + +resource "aws_athena_database" "hoge" { + name = "%[2]s" + bucket = aws_s3_bucket.hoge.bucket + comment = "athena's a goddess" + force_destroy = %[3]t +} +`, randInt, dbName, forceDestroy) +} + func testAccAthenaDatabaseWithKMSConfig(randInt int, dbName string, forceDestroy bool) string { return fmt.Sprintf(` resource "aws_kms_key" "hoge" { From 09afa0eb15ba92ee2359638c8a511f6d16b374c6 Mon Sep 17 00:00:00 2001 From: drexler Date: Wed, 1 Dec 2021 21:55:21 -0500 Subject: [PATCH 3/3] docs: add documentation for comment property --- website/docs/r/athena_database.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/r/athena_database.html.markdown b/website/docs/r/athena_database.html.markdown index 53ee0b6d92b..917f834a136 100644 --- a/website/docs/r/athena_database.html.markdown +++ b/website/docs/r/athena_database.html.markdown @@ -29,6 +29,7 @@ The following arguments are supported: * `name` - (Required) Name of the database to create. * `bucket` - (Required) Name of s3 bucket to save the results of the query execution. +* `comment` - (Optional) Description of the database. * `encryption_configuration` - (Optional) The encryption key block AWS Athena uses to decrypt the data in S3, such as an AWS Key Management Service (AWS KMS) key. An `encryption_configuration` block is documented below. * `force_destroy` - (Optional, Default: false) A boolean that indicates all tables should be deleted from the database so that the database can be destroyed without error. The tables are *not* recoverable.