From e016e15d358a388b2b0609c8ae999f71ef429346 Mon Sep 17 00:00:00 2001 From: Paul Hudson Date: Wed, 17 Jul 2019 13:21:51 +0100 Subject: [PATCH 1/3] add workgroup support for Athena named query --- aws/resource_aws_athena_named_query.go | 9 ++++ aws/resource_aws_athena_named_query_test.go | 42 +++++++++++++++++++ .../docs/r/athena_named_query.html.markdown | 26 ++++++++++-- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_athena_named_query.go b/aws/resource_aws_athena_named_query.go index e5bd43f2846..5e9a006e0b2 100644 --- a/aws/resource_aws_athena_named_query.go +++ b/aws/resource_aws_athena_named_query.go @@ -29,6 +29,11 @@ func resourceAwsAthenaNamedQuery() *schema.Resource { Required: true, ForceNew: true, }, + "workgroup": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, "database": { Type: schema.TypeString, Required: true, @@ -51,6 +56,9 @@ func resourceAwsAthenaNamedQueryCreate(d *schema.ResourceData, meta interface{}) Name: aws.String(d.Get("name").(string)), QueryString: aws.String(d.Get("query").(string)), } + if raw, ok := d.GetOk("workgroup"); ok { + input.WorkGroup = aws.String(raw.(string)) + } if raw, ok := d.GetOk("description"); ok { input.Description = aws.String(raw.(string)) } @@ -82,6 +90,7 @@ func resourceAwsAthenaNamedQueryRead(d *schema.ResourceData, meta interface{}) e d.Set("name", resp.NamedQuery.Name) d.Set("query", resp.NamedQuery.QueryString) + d.Set("workgroup", resp.NamedQuery.WorkGroup) d.Set("database", resp.NamedQuery.Database) d.Set("description", resp.NamedQuery.Description) return nil diff --git a/aws/resource_aws_athena_named_query_test.go b/aws/resource_aws_athena_named_query_test.go index dd6aba53c12..d4393285a7e 100644 --- a/aws/resource_aws_athena_named_query_test.go +++ b/aws/resource_aws_athena_named_query_test.go @@ -27,6 +27,22 @@ func TestAccAWSAthenaNamedQuery_basic(t *testing.T) { }) } +func TestAccAWSAthenaNamedQuery_withWorkGroup(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAthenaNamedQueryDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAthenaNamedWorkGroupQueryConfig(acctest.RandInt(), acctest.RandString(5)), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAthenaNamedQueryExists("aws_athena_named_query.foo"), + ), + }, + }, + }) +} + func TestAccAWSAthenaNamedQuery_import(t *testing.T) { resourceName := "aws_athena_named_query.foo" @@ -111,3 +127,29 @@ resource "aws_athena_named_query" "foo" { } `, rName, rInt, rName, rName) } + +func testAccAthenaNamedWorkGroupQueryConfig(rInt int, rName string) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" "hoge" { + bucket = "tf-athena-db-%s-%d" + force_destroy = true +} + +resource "aws_athena_workgroup" "test" { + name = "tf-athena-workgroup-%s-%d" +} + +resource "aws_athena_database" "hoge" { + name = "%s" + bucket = "${aws_s3_bucket.hoge.bucket}" +} + +resource "aws_athena_named_query" "bar" { + name = "tf-athena-named-query-%s" + workgroup = "${aws_athena_workgroup.test.id}" + database = "${aws_athena_database.hoge.name}" + query = "SELECT * FROM ${aws_athena_database.hoge.name} limit 10;" + description = "tf test" +} +`, rName, rInt, rName, rInt, rName, rName) +} diff --git a/website/docs/r/athena_named_query.html.markdown b/website/docs/r/athena_named_query.html.markdown index 6593eaf28ef..7f497d50710 100644 --- a/website/docs/r/athena_named_query.html.markdown +++ b/website/docs/r/athena_named_query.html.markdown @@ -17,15 +17,34 @@ resource "aws_s3_bucket" "hoge" { bucket = "tf-test" } +resource "aws_kms_key" "test" { + deletion_window_in_days = 7 + description = "Athena KMS Key" +} + +resource "aws_athena_workgroup" "test" { + name = "example" + + configuration { + result_configuration { + encryption_configuration { + encryption_option = "SSE_KMS" + kms_key_arn = "${aws_kms_key.test.arn}" + } + } + } +} + resource "aws_athena_database" "hoge" { name = "users" bucket = "${aws_s3_bucket.hoge.bucket}" } resource "aws_athena_named_query" "foo" { - name = "bar" - database = "${aws_athena_database.hoge.name}" - query = "SELECT * FROM ${aws_athena_database.hoge.name} limit 10;" + name = "bar" + workgroup = "${aws_athena_workgroup.test.id}" + database = "${aws_athena_database.hoge.name}" + query = "SELECT * FROM ${aws_athena_database.hoge.name} limit 10;" } ``` @@ -34,6 +53,7 @@ resource "aws_athena_named_query" "foo" { The following arguments are supported: * `name` - (Required) The plain language name for the query. Maximum length of 128. +* `workgroup` - (Optional) The workgroup to which the query belongs. * `database` - (Required) The database to which the query belongs. * `query` - (Required) The text of the query itself. In other words, all query statements. Maximum length of 262144. * `description` - (Optional) A brief explanation of the query. Maximum length of 1024. From 4cd3f81f718a992059ee03f596e967ff4e83f411 Mon Sep 17 00:00:00 2001 From: Paul Hudson Date: Thu, 18 Jul 2019 15:50:21 +0100 Subject: [PATCH 2/3] fix name collision --- aws/resource_aws_athena_named_query_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_athena_named_query_test.go b/aws/resource_aws_athena_named_query_test.go index d4393285a7e..c01b3d268ff 100644 --- a/aws/resource_aws_athena_named_query_test.go +++ b/aws/resource_aws_athena_named_query_test.go @@ -36,7 +36,7 @@ func TestAccAWSAthenaNamedQuery_withWorkGroup(t *testing.T) { { Config: testAccAthenaNamedWorkGroupQueryConfig(acctest.RandInt(), acctest.RandString(5)), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAthenaNamedQueryExists("aws_athena_named_query.foo"), + testAccCheckAWSAthenaNamedQueryExists("aws_athena_named_query.bar"), ), }, }, From f39761203204dcecc32e63ada0fb0de8349d9a16 Mon Sep 17 00:00:00 2001 From: Paul Hudson Date: Thu, 18 Jul 2019 15:51:34 +0100 Subject: [PATCH 3/3] set default workgroup to primary --- aws/resource_aws_athena_named_query.go | 1 + website/docs/r/athena_named_query.html.markdown | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_athena_named_query.go b/aws/resource_aws_athena_named_query.go index 5e9a006e0b2..57a5c04cbae 100644 --- a/aws/resource_aws_athena_named_query.go +++ b/aws/resource_aws_athena_named_query.go @@ -33,6 +33,7 @@ func resourceAwsAthenaNamedQuery() *schema.Resource { Type: schema.TypeString, Optional: true, ForceNew: true, + Default: "primary", }, "database": { Type: schema.TypeString, diff --git a/website/docs/r/athena_named_query.html.markdown b/website/docs/r/athena_named_query.html.markdown index 7f497d50710..333bcd27f85 100644 --- a/website/docs/r/athena_named_query.html.markdown +++ b/website/docs/r/athena_named_query.html.markdown @@ -53,7 +53,7 @@ resource "aws_athena_named_query" "foo" { The following arguments are supported: * `name` - (Required) The plain language name for the query. Maximum length of 128. -* `workgroup` - (Optional) The workgroup to which the query belongs. +* `workgroup` - (Optional) The workgroup to which the query belongs. Defaults to `primary` * `database` - (Required) The database to which the query belongs. * `query` - (Required) The text of the query itself. In other words, all query statements. Maximum length of 262144. * `description` - (Optional) A brief explanation of the query. Maximum length of 1024.