diff --git a/aws/resource_aws_athena_named_query.go b/aws/resource_aws_athena_named_query.go index 0df322f99c4..bfbd197c42a 100644 --- a/aws/resource_aws_athena_named_query.go +++ b/aws/resource_aws_athena_named_query.go @@ -14,23 +14,27 @@ func resourceAwsAthenaNamedQuery() *schema.Resource { Read: resourceAwsAthenaNamedQueryRead, Delete: resourceAwsAthenaNamedQueryDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "query": &schema.Schema{ + "query": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "database": &schema.Schema{ + "database": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "description": &schema.Schema{ + "description": { Type: schema.TypeString, Optional: true, ForceNew: true, @@ -66,7 +70,7 @@ func resourceAwsAthenaNamedQueryRead(d *schema.ResourceData, meta interface{}) e NamedQueryId: aws.String(d.Id()), } - _, err := conn.GetNamedQuery(input) + resp, err := conn.GetNamedQuery(input) if err != nil { if isAWSErr(err, athena.ErrCodeInvalidRequestException, d.Id()) { log.Printf("[WARN] Athena Named Query (%s) not found, removing from state", d.Id()) @@ -75,6 +79,11 @@ func resourceAwsAthenaNamedQueryRead(d *schema.ResourceData, meta interface{}) e } return err } + + d.Set("name", resp.NamedQuery.Name) + d.Set("query", resp.NamedQuery.QueryString) + d.Set("database", resp.NamedQuery.Database) + d.Set("description", resp.NamedQuery.Description) return nil } @@ -89,6 +98,6 @@ func resourceAwsAthenaNamedQueryDelete(d *schema.ResourceData, meta interface{}) if err != nil { return err } - d.SetId("") + return nil } diff --git a/aws/resource_aws_athena_named_query_test.go b/aws/resource_aws_athena_named_query_test.go index 7e37aa520d9..b88325a8a75 100644 --- a/aws/resource_aws_athena_named_query_test.go +++ b/aws/resource_aws_athena_named_query_test.go @@ -27,6 +27,27 @@ func TestAccAWSAthenaNamedQuery_basic(t *testing.T) { }) } +func TestAccAwsAthenaNamedQuery_import(t *testing.T) { + resourceName := "aws_athena_named_query.foo" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAthenaNamedQueryDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAthenaNamedQueryConfig(acctest.RandInt(), acctest.RandString(5)), + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckAWSAthenaNamedQueryDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).athenaconn for _, rs := range s.RootModule().Resources { @@ -54,11 +75,22 @@ func testAccCheckAWSAthenaNamedQueryDestroy(s *terraform.State) error { func testAccCheckAWSAthenaNamedQueryExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { - _, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[name] if !ok { return fmt.Errorf("Not found: %s", name) } + conn := testAccProvider.Meta().(*AWSClient).athenaconn + + input := &athena.GetNamedQueryInput{ + NamedQueryId: aws.String(rs.Primary.ID), + } + + _, err := conn.GetNamedQuery(input) + if err != nil { + return err + } + return nil } } diff --git a/website/docs/r/athena_named_query.html.markdown b/website/docs/r/athena_named_query.html.markdown index c56dff866f4..6050d3bf120 100644 --- a/website/docs/r/athena_named_query.html.markdown +++ b/website/docs/r/athena_named_query.html.markdown @@ -43,3 +43,11 @@ The following arguments are supported: The following attributes are exported: * `id` - The unique ID of the query. + +## Import + +Athena Named Query can be imported using the query ID, e.g. + +``` +$ terraform import aws_athena_named_query.example 0123456789 +```