Add aws_api_gateawy_integration_response resource

This commit is contained in:
Raphael Randschau 2016-03-05 23:17:21 +01:00
parent 1593dbe9c8
commit 4da8b3d03a
5 changed files with 401 additions and 0 deletions

View File

@ -119,6 +119,7 @@ func Provider() terraform.ResourceProvider {
"aws_api_gateway_method": resourceAwsApiGatewayMethod(),
"aws_api_gateway_method_response": resourceAwsApiGatewayMethodResponse(),
"aws_api_gateway_integration": resourceAwsApiGatewayIntegration(),
"aws_api_gateway_integration_response": resourceAwsApiGatewayIntegrationResponse(),
"aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(),
"aws_autoscaling_group": resourceAwsAutoscalingGroup(),
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),

View File

@ -0,0 +1,159 @@
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsApiGatewayIntegrationResponse() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayIntegrationResponseCreate,
Read: resourceAwsApiGatewayIntegrationResponseRead,
Update: resourceAwsApiGatewayIntegrationResponseUpdate,
Delete: resourceAwsApiGatewayIntegrationResponseDelete,
Schema: map[string]*schema.Schema{
"rest_api_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"http_method": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateHTTPMethod,
},
"status_code": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"selection_pattern": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"response_templates": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
},
},
}
}
func resourceAwsApiGatewayIntegrationResponseCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
templates := make(map[string]string)
for k, v := range d.Get("response_templates").(map[string]interface{}) {
templates[k] = v.(string)
}
_, err := conn.PutIntegrationResponse(&apigateway.PutIntegrationResponseInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
StatusCode: aws.String(d.Get("status_code").(string)),
ResponseTemplates: aws.StringMap(templates),
// TODO implement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
ResponseParameters: nil,
})
if err != nil {
return fmt.Errorf("Error creating API Gateway Integration Response: %s", err)
}
d.SetId(fmt.Sprintf("agir-%s-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string), d.Get("status_code").(string)))
log.Printf("[DEBUG] API Gateway Integration Response ID: %s", d.Id())
return nil
}
func resourceAwsApiGatewayIntegrationResponseRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Reading API Gateway Integration Response %s", d.Id())
integrationResponse, err := conn.GetIntegrationResponse(&apigateway.GetIntegrationResponseInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
StatusCode: aws.String(d.Get("status_code").(string)),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
d.SetId("")
return nil
}
return err
}
log.Printf("[DEBUG] Received API Gateway Integration Response: %s", integrationResponse)
d.SetId(fmt.Sprintf("agir-%s-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string), d.Get("status_code").(string)))
return nil
}
func resourceAwsApiGatewayIntegrationResponseUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceAwsApiGatewayIntegrationResponseCreate(d, meta)
}
func resourceAwsApiGatewayIntegrationResponseDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway Integration Response: %s", d.Id())
resourceId := d.Get("resource_id").(string)
if o, n := d.GetChange("resource_id"); o.(string) != n.(string) {
resourceId = o.(string)
}
httpMethod := d.Get("http_method").(string)
if o, n := d.GetChange("http_method"); o.(string) != n.(string) {
httpMethod = o.(string)
}
restApiID := d.Get("rest_api_id").(string)
if o, n := d.GetChange("rest_api_id"); o.(string) != n.(string) {
restApiID = o.(string)
}
statusCode := d.Get("status_code").(string)
if o, n := d.GetChange("status_code"); o.(string) != n.(string) {
statusCode = o.(string)
}
return resource.Retry(5*time.Minute, func() error {
log.Printf("[DEBUG] schema is %#v", d)
_, err := conn.DeleteIntegrationResponse(&apigateway.DeleteIntegrationResponseInput{
HttpMethod: aws.String(httpMethod),
ResourceId: aws.String(resourceId),
RestApiId: aws.String(restApiID),
StatusCode: aws.String(statusCode),
})
if err == nil {
return nil
}
apigatewayErr, ok := err.(awserr.Error)
if apigatewayErr.Code() == "NotFoundException" {
return nil
}
if !ok {
return resource.RetryError{Err: err}
}
return resource.RetryError{Err: err}
})
}

View File

@ -0,0 +1,173 @@
package aws
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSAPIGatewayIntegrationResponse_basic(t *testing.T) {
var conf apigateway.IntegrationResponse
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayIntegrationResponseDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAPIGatewayIntegrationResponseConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayIntegrationResponseExists("aws_api_gateway_integration_response.test", &conf),
testAccCheckAWSAPIGatewayIntegrationResponseAttributes(&conf),
resource.TestCheckResourceAttr(
"aws_api_gateway_integration_response.test", "response_templates.application/json", ""),
resource.TestCheckResourceAttr(
"aws_api_gateway_integration_response.test", "response_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"),
),
},
},
})
}
func testAccCheckAWSAPIGatewayIntegrationResponseAttributes(conf *apigateway.IntegrationResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.StatusCode != "400" {
return fmt.Errorf("wrong StatusCode: %q", *conf.StatusCode)
}
if conf.ResponseTemplates["application/json"] != nil {
return fmt.Errorf("wrong ResponseTemplate for application/json")
}
if *conf.ResponseTemplates["application/xml"] != "#set($inputRoot = $input.path('$'))\n{ }" {
return fmt.Errorf("wrong ResponseTemplate for application/xml")
}
return nil
}
}
func testAccCheckAWSAPIGatewayIntegrationResponseExists(n string, res *apigateway.IntegrationResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No API Gateway Method ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).apigateway
req := &apigateway.GetIntegrationResponseInput{
HttpMethod: aws.String("GET"),
ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
}
describe, err := conn.GetIntegrationResponse(req)
if err != nil {
return err
}
*res = *describe
return nil
}
}
func testAccCheckAWSAPIGatewayIntegrationResponseDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_api_gateway_integration_response" {
continue
}
req := &apigateway.GetIntegrationResponseInput{
HttpMethod: aws.String("GET"),
ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
}
_, err := conn.GetIntegrationResponse(req)
if err == nil {
return fmt.Errorf("API Gateway Method still exists")
}
aws2err, ok := err.(awserr.Error)
if !ok {
return err
}
if aws2err.Code() != "NotFoundException" {
return err
}
return nil
}
return nil
}
const testAccAWSAPIGatewayIntegrationResponseConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
}
resource "aws_api_gateway_resource" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
path_part = "test"
}
resource "aws_api_gateway_method" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "GET"
authorization = "NONE"
request_models = {
"application/json" = "Error"
}
}
resource "aws_api_gateway_method_response" "error" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"
status_code = "400"
response_models = {
"application/json" = "Error"
}
}
resource "aws_api_gateway_integration" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"
request_templates = {
"application/json" = ""
"application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
}
type = "MOCK"
}
resource "aws_api_gateway_integration_response" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"
status_code = "${aws_api_gateway_method_response.error.status_code}"
response_templates = {
"application/json" = ""
"application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
}
}
`

View File

@ -0,0 +1,65 @@
---
layout: "aws"
page_title: "AWS: aws_api_gateway_integration_response"
sidebar_current: "docs-aws-resource-api-gateway-integration-response"
description: |-
Provides an HTTP Method Integration Response for an API Gateway Resource.
---
# aws\_api\_gateway\_integration\_response
Provides an HTTP Method Integration Response for an API Gateway Resource.
## Example Usage
```
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
resource "aws_api_gateway_resource" "MyDemoResource" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
parent_resource_id = "${aws_api_gateway_rest_api.MyDemoAPI.root_resource_id}"
path_part = "mydemoresource"
}
resource "aws_api_gateway_method" "MyDemoMethod" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
http_method = "GET"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "MyDemoIntegration" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
http_method = "${aws_api_gateway_method.MyDemoMethod.http_method}"
type = "MOCK"
}
resource "aws_api_gateway_method_response" "200" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
http_method = "${aws_api_gateway_method.MyDemoMethod.http_method}"
status_code = "200"
}
resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
http_method = "${aws_api_gateway_method.MyDemoMethod.http_method}"
status_code = "${aws_api_gateway_method_response.200.status_code}"
}
```
## Argument Reference
The following arguments are supported:
* `rest_api_id` - (Required) API Gateway ID
* `resource_id` - (Required) API Gateway Resource ID
* `http_method` - (Required) HTTP Method (GET, POST, PUT, DELETE, HEAD, OPTION)
* `status_code` - (Required) Specify the HTTP status code.
* `response_models` - (Optional) Specifies the Model resources used for the response's content type
* `response_parameters` - (Optional) Represents response parameters that can be sent back to the caller

View File

@ -28,6 +28,9 @@
<li<%= sidebar_current("docs-aws-resource-api-gateway-integration") %>>
<a href="/docs/providers/aws/r/api_gateway_integration.html">aws_api_gateway_integration</a>
</li>
<li<%= sidebar_current("docs-aws-resource-api-gateway-integration-response") %>>
<a href="/docs/providers/aws/r/api_gateway_integration_response.html">aws_api_gateway_integration_response</a>
</li>
</ul>
</li>