Skip to content

Commit

Permalink
Merge pull request #5567 from terraform-providers/f-aws_api_gateway_g…
Browse files Browse the repository at this point in the history
…ateway_response-import

resource/aws_api_gateway_gateway_response: Support resource import
  • Loading branch information
bflad authored Aug 21, 2018
2 parents 10c65de + 0da6dd8 commit 1cfc9aa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions aws/resource_aws_api_gateway_gateway_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -18,6 +19,20 @@ func resourceAwsApiGatewayGatewayResponse() *schema.Resource {
Read: resourceAwsApiGatewayGatewayResponseRead,
Update: resourceAwsApiGatewayGatewayResponsePut,
Delete: resourceAwsApiGatewayGatewayResponseDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected REST-API-ID/RESPONSE-TYPE", d.Id())
}
restApiID := idParts[0]
responseType := idParts[1]
d.Set("response_type", responseType)
d.Set("rest_api_id", restApiID)
d.SetId(fmt.Sprintf("aggr-%s-%s", restApiID, responseType))
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"rest_api_id": {
Expand Down
17 changes: 17 additions & 0 deletions aws/resource_aws_api_gateway_gateway_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func TestAccAWSAPIGatewayGatewayResponse_basic(t *testing.T) {
resource.TestCheckNoResourceAttr("aws_api_gateway_gateway_response.test", "response_parameters.gatewayresponse.header.Authorization"),
),
},
{
ResourceName: "aws_api_gateway_gateway_response.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayGatewayResponseImportStateIdFunc("aws_api_gateway_gateway_response.test"),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -107,6 +113,17 @@ func testAccCheckAWSAPIGatewayGatewayResponseDestroy(s *terraform.State) error {
return nil
}

func testAccAWSAPIGatewayGatewayResponseImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s/%s", rs.Primary.Attributes["rest_api_id"], rs.Primary.Attributes["response_type"]), nil
}
}

func testAccAWSAPIGatewayGatewayResponseConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "main" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/api_gateway_gateway_response.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ The following arguments are supported:
* `status_code` - (Optional) The HTTP status code of the Gateway Response.
* `response_parameters` - (Optional) A map specifying the templates used to transform the response body.
* `response_templates` - (Optional) A map specifying the parameters (paths, query strings and headers) of the Gateway Response.

## Import

`aws_api_gateway_gateway_response` can be imported using `REST-API-ID/RESPONSE-TYPE`, e.g.

```
$ terraform import aws_api_gateway_gateway_response.example 12345abcde/UNAUTHORIZED
```

0 comments on commit 1cfc9aa

Please sign in to comment.