Skip to content

Commit

Permalink
add workgroup support for Athena named query
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhudson committed Jul 18, 2019
1 parent f945137 commit e016e15
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
9 changes: 9 additions & 0 deletions aws/resource_aws_athena_named_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
}
Expand Down Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions aws/resource_aws_athena_named_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
26 changes: 23 additions & 3 deletions website/docs/r/athena_named_query.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
}
```

Expand All @@ -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.
Expand Down

0 comments on commit e016e15

Please sign in to comment.