Add aws_api_gateway_method_response resource

This commit is contained in:
Raphael Randschau 2016-03-05 23:16:16 +01:00
parent 032e6081cb
commit 91f5206f8d
6 changed files with 435 additions and 0 deletions

View File

@ -117,6 +117,7 @@ func Provider() terraform.ResourceProvider {
"aws_api_gateway_rest_api": resourceAwsApiGatewayRestApi(),
"aws_api_gateway_resource": resourceAwsApiGatewayResource(),
"aws_api_gateway_method": resourceAwsApiGatewayMethod(),
"aws_api_gateway_method_response": resourceAwsApiGatewayMethodResponse(),
"aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(),
"aws_autoscaling_group": resourceAwsAutoscalingGroup(),
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),

View File

@ -0,0 +1,179 @@
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 resourceAwsApiGatewayMethodResponse() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayMethodResponseCreate,
Read: resourceAwsApiGatewayMethodResponseRead,
Update: resourceAwsApiGatewayMethodResponseUpdate,
Delete: resourceAwsApiGatewayMethodResponseDelete,
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,
},
"response_models": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
},
},
}
}
func resourceAwsApiGatewayMethodResponseCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
models := make(map[string]string)
for k, v := range d.Get("response_models").(map[string]interface{}) {
models[k] = v.(string)
}
_, err := conn.PutMethodResponse(&apigateway.PutMethodResponseInput{
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)),
ResponseModels: aws.StringMap(models),
// 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 Method Response: %s", err)
}
d.SetId(fmt.Sprintf("agmr-%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 Method ID: %s", d.Id())
return nil
}
func resourceAwsApiGatewayMethodResponseRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Reading API Gateway Method %s", d.Id())
methodResponse, err := conn.GetMethodResponse(&apigateway.GetMethodResponseInput{
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 Method: %s", methodResponse)
d.Set("response_models", aws.StringValueMap(methodResponse.ResponseModels))
d.SetId(fmt.Sprintf("agmr-%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 resourceAwsApiGatewayMethodResponseUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Updating API Gateway Method Response %s", d.Id())
operations := make([]*apigateway.PatchOperation, 0)
if d.HasChange("response_models") {
operations = append(operations, expandApiGatewayRequestResponseModelOperations(d, "response_models", "responseModels")...)
}
out, err := conn.UpdateMethodResponse(&apigateway.UpdateMethodResponseInput{
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)),
PatchOperations: operations,
})
if err != nil {
return err
}
log.Printf("[DEBUG] Received API Gateway Method Response: %s", out)
return resourceAwsApiGatewayMethodResponseRead(d, meta)
}
func resourceAwsApiGatewayMethodResponseDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway Method 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.DeleteMethodResponse(&apigateway.DeleteMethodResponseInput{
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,149 @@
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 TestAccAWSAPIGatewayMethodResponse_basic(t *testing.T) {
var conf apigateway.MethodResponse
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayMethodResponseDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAPIGatewayMethodResponseConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayMethodResponseExists("aws_api_gateway_method_response.error", &conf),
testAccCheckAWSAPIGatewayMethodResponseAttributes(&conf),
resource.TestCheckResourceAttr(
"aws_api_gateway_method_response.error", "status_code", "400"),
resource.TestCheckResourceAttr(
"aws_api_gateway_method_response.error", "response_models.application/json", "Error"),
),
},
},
})
}
func testAccCheckAWSAPIGatewayMethodResponseAttributes(conf *apigateway.MethodResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.StatusCode == "" {
return fmt.Errorf("empty StatusCode")
}
if val, ok := conf.ResponseModels["application/json"]; !ok {
return fmt.Errorf("missing application/json ResponseModel")
} else {
if *val != "Error" {
return fmt.Errorf("wrong application/json ResponseModel")
}
}
return nil
}
}
func testAccCheckAWSAPIGatewayMethodResponseExists(n string, res *apigateway.MethodResponse) 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.GetMethodResponseInput{
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.GetMethodResponse(req)
if err != nil {
return err
}
*res = *describe
return nil
}
}
func testAccCheckAWSAPIGatewayMethodResponseDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_api_gateway_method_response" {
continue
}
req := &apigateway.GetMethodResponseInput{
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.GetMethodResponse(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 testAccAWSAPIGatewayMethodResponseConfig = `
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"
}
}
`

View File

@ -8,6 +8,7 @@ import (
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/directoryservice"
@ -783,3 +784,46 @@ func flattenAsgEnabledMetrics(list []*autoscaling.EnabledMetric) []string {
}
return strs
}
func expandApiGatewayRequestResponseModelOperations(d *schema.ResourceData, key string, prefix string) []*apigateway.PatchOperation {
operations := make([]*apigateway.PatchOperation, 0)
oldModels, newModels := d.GetChange(key)
oldModelMap := oldModels.(map[string]interface{})
newModelMap := newModels.(map[string]interface{})
for k, _ := range oldModelMap {
operation := apigateway.PatchOperation{
Op: aws.String("remove"),
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(k, "/", "~1", -1))),
}
for nK, nV := range newModelMap {
if nK == k {
operation.Op = aws.String("replace")
operation.Value = aws.String(nV.(string))
}
}
operations = append(operations, &operation)
}
for nK, nV := range newModelMap {
exists := false
for k, _ := range oldModelMap {
if k == nK {
exists = true
}
}
if !exists {
operation := apigateway.PatchOperation{
Op: aws.String("add"),
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(nK, "/", "~1", -1))),
Value: aws.String(nV.(string)),
}
operations = append(operations, &operation)
}
}
return operations
}

View File

@ -0,0 +1,59 @@
---
layout: "aws"
page_title: "AWS: aws_api_gateway_method_response"
sidebar_current: "docs-aws-resource-api-gateway-method-response"
description: |-
Provides an HTTP Method Response for an API Gateway Resource.
---
# aws\_api\_gateway\_method\_response
Provides an HTTP Method 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"
}
```
## 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

@ -22,6 +22,9 @@
<li<%= sidebar_current("docs-aws-resource-api-gateway-method") %>>
<a href="/docs/providers/aws/r/api_gateway_method.html">aws_api_gateway_method</a>
</li>
<li<%= sidebar_current("docs-aws-resource-api-gateway-method-response") %>>
<a href="/docs/providers/aws/r/api_gateway_method_response.html">aws_api_gateway_method_response</a>
</li>
</ul>
</li>