github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_api_gateway_method_response_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/apigateway"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSAPIGatewayMethodResponse_basic(t *testing.T) {
    15  	var conf apigateway.MethodResponse
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAPIGatewayMethodResponseDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAPIGatewayMethodResponseConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSAPIGatewayMethodResponseExists("aws_api_gateway_method_response.error", &conf),
    26  					testAccCheckAWSAPIGatewayMethodResponseAttributes(&conf),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_api_gateway_method_response.error", "status_code", "400"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_api_gateway_method_response.error", "response_models.application/json", "Error"),
    31  				),
    32  			},
    33  
    34  			resource.TestStep{
    35  				Config: testAccAWSAPIGatewayMethodResponseConfigUpdate,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testAccCheckAWSAPIGatewayMethodResponseExists("aws_api_gateway_method_response.error", &conf),
    38  					testAccCheckAWSAPIGatewayMethodResponseAttributesUpdate(&conf),
    39  					resource.TestCheckResourceAttr(
    40  						"aws_api_gateway_method_response.error", "status_code", "400"),
    41  					resource.TestCheckResourceAttr(
    42  						"aws_api_gateway_method_response.error", "response_models.application/json", "Empty"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testAccCheckAWSAPIGatewayMethodResponseAttributes(conf *apigateway.MethodResponse) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		if *conf.StatusCode == "" {
    52  			return fmt.Errorf("empty StatusCode")
    53  		}
    54  		if val, ok := conf.ResponseModels["application/json"]; !ok {
    55  			return fmt.Errorf("missing application/json ResponseModel")
    56  		} else {
    57  			if *val != "Error" {
    58  				return fmt.Errorf("wrong application/json ResponseModel")
    59  			}
    60  		}
    61  		if val, ok := conf.ResponseParameters["method.response.header.Content-Type"]; !ok {
    62  			return fmt.Errorf("missing Content-Type ResponseParameters")
    63  		} else {
    64  			if *val != true {
    65  				return fmt.Errorf("wrong ResponseParameters value")
    66  			}
    67  		}
    68  		return nil
    69  	}
    70  }
    71  
    72  func testAccCheckAWSAPIGatewayMethodResponseAttributesUpdate(conf *apigateway.MethodResponse) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		if *conf.StatusCode == "" {
    75  			return fmt.Errorf("empty StatusCode")
    76  		}
    77  		if val, ok := conf.ResponseModels["application/json"]; !ok {
    78  			return fmt.Errorf("missing application/json ResponseModel")
    79  		} else {
    80  			if *val != "Empty" {
    81  				return fmt.Errorf("wrong application/json ResponseModel")
    82  			}
    83  		}
    84  		if conf.ResponseParameters["method.response.header.Content-Type"] != nil {
    85  			return fmt.Errorf("Content-Type ResponseParameters shouldn't exist")
    86  		}
    87  		return nil
    88  	}
    89  }
    90  
    91  func testAccCheckAWSAPIGatewayMethodResponseExists(n string, res *apigateway.MethodResponse) resource.TestCheckFunc {
    92  	return func(s *terraform.State) error {
    93  		rs, ok := s.RootModule().Resources[n]
    94  		if !ok {
    95  			return fmt.Errorf("Not found: %s", n)
    96  		}
    97  
    98  		if rs.Primary.ID == "" {
    99  			return fmt.Errorf("No API Gateway Method ID is set")
   100  		}
   101  
   102  		conn := testAccProvider.Meta().(*AWSClient).apigateway
   103  
   104  		req := &apigateway.GetMethodResponseInput{
   105  			HttpMethod: aws.String("GET"),
   106  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
   107  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   108  			StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
   109  		}
   110  		describe, err := conn.GetMethodResponse(req)
   111  		if err != nil {
   112  			return err
   113  		}
   114  
   115  		*res = *describe
   116  
   117  		return nil
   118  	}
   119  }
   120  
   121  func testAccCheckAWSAPIGatewayMethodResponseDestroy(s *terraform.State) error {
   122  	conn := testAccProvider.Meta().(*AWSClient).apigateway
   123  
   124  	for _, rs := range s.RootModule().Resources {
   125  		if rs.Type != "aws_api_gateway_method_response" {
   126  			continue
   127  		}
   128  
   129  		req := &apigateway.GetMethodResponseInput{
   130  			HttpMethod: aws.String("GET"),
   131  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
   132  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   133  			StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
   134  		}
   135  		_, err := conn.GetMethodResponse(req)
   136  
   137  		if err == nil {
   138  			return fmt.Errorf("API Gateway Method still exists")
   139  		}
   140  
   141  		aws2err, ok := err.(awserr.Error)
   142  		if !ok {
   143  			return err
   144  		}
   145  		if aws2err.Code() != "NotFoundException" {
   146  			return err
   147  		}
   148  
   149  		return nil
   150  	}
   151  
   152  	return nil
   153  }
   154  
   155  const testAccAWSAPIGatewayMethodResponseConfig = `
   156  resource "aws_api_gateway_rest_api" "test" {
   157    name = "test"
   158  }
   159  
   160  resource "aws_api_gateway_resource" "test" {
   161    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   162    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   163    path_part = "test"
   164  }
   165  
   166  resource "aws_api_gateway_method" "test" {
   167    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   168    resource_id = "${aws_api_gateway_resource.test.id}"
   169    http_method = "GET"
   170    authorization = "NONE"
   171  
   172    request_models = {
   173      "application/json" = "Error"
   174    }
   175  }
   176  
   177  resource "aws_api_gateway_method_response" "error" {
   178    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   179    resource_id = "${aws_api_gateway_resource.test.id}"
   180    http_method = "${aws_api_gateway_method.test.http_method}"
   181    status_code = "400"
   182  
   183    response_models = {
   184      "application/json" = "Error"
   185    }
   186  
   187    response_parameters = {
   188      "method.response.header.Content-Type" = true
   189    }
   190  }
   191  `
   192  
   193  const testAccAWSAPIGatewayMethodResponseConfigUpdate = `
   194  resource "aws_api_gateway_rest_api" "test" {
   195    name = "test"
   196  }
   197  
   198  resource "aws_api_gateway_resource" "test" {
   199    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   200    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   201    path_part = "test"
   202  }
   203  
   204  resource "aws_api_gateway_method" "test" {
   205    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   206    resource_id = "${aws_api_gateway_resource.test.id}"
   207    http_method = "GET"
   208    authorization = "NONE"
   209  
   210    request_models = {
   211      "application/json" = "Error"
   212    }
   213  }
   214  
   215  resource "aws_api_gateway_method_response" "error" {
   216    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   217    resource_id = "${aws_api_gateway_resource.test.id}"
   218    http_method = "${aws_api_gateway_method.test.http_method}"
   219    status_code = "400"
   220  
   221    response_models = {
   222      "application/json" = "Empty"
   223    }
   224  
   225    response_parameters = {
   226      "method.response.header.Host" = true
   227    }
   228  }
   229  `