Skip to content

Commit

Permalink
resource/athena_named_query: Support import (#3231)
Browse files Browse the repository at this point in the history
* resource/athena_named_query: Support import

* remove &schema.Schema
  • Loading branch information
atsushi-ishibashi authored and Ninir committed Feb 2, 2018
1 parent 1ca27de commit 67baaab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
21 changes: 15 additions & 6 deletions aws/resource_aws_athena_named_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand All @@ -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
}

Expand All @@ -89,6 +98,6 @@ func resourceAwsAthenaNamedQueryDelete(d *schema.ResourceData, meta interface{})
if err != nil {
return err
}
d.SetId("")

return nil
}
34 changes: 33 additions & 1 deletion aws/resource_aws_athena_named_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/athena_named_query.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 67baaab

Please sign in to comment.