Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_api_gateway_gateway_response: Support resource import #5567

Merged
merged 1 commit into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```