github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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 }) 35 } 36 37 func testAccCheckAWSAPIGatewayMethodResponseAttributes(conf *apigateway.MethodResponse) resource.TestCheckFunc { 38 return func(s *terraform.State) error { 39 if *conf.StatusCode == "" { 40 return fmt.Errorf("empty StatusCode") 41 } 42 if val, ok := conf.ResponseModels["application/json"]; !ok { 43 return fmt.Errorf("missing application/json ResponseModel") 44 } else { 45 if *val != "Error" { 46 return fmt.Errorf("wrong application/json ResponseModel") 47 } 48 } 49 return nil 50 } 51 } 52 53 func testAccCheckAWSAPIGatewayMethodResponseExists(n string, res *apigateway.MethodResponse) resource.TestCheckFunc { 54 return func(s *terraform.State) error { 55 rs, ok := s.RootModule().Resources[n] 56 if !ok { 57 return fmt.Errorf("Not found: %s", n) 58 } 59 60 if rs.Primary.ID == "" { 61 return fmt.Errorf("No API Gateway Method ID is set") 62 } 63 64 conn := testAccProvider.Meta().(*AWSClient).apigateway 65 66 req := &apigateway.GetMethodResponseInput{ 67 HttpMethod: aws.String("GET"), 68 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 69 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 70 StatusCode: aws.String(rs.Primary.Attributes["status_code"]), 71 } 72 describe, err := conn.GetMethodResponse(req) 73 if err != nil { 74 return err 75 } 76 77 *res = *describe 78 79 return nil 80 } 81 } 82 83 func testAccCheckAWSAPIGatewayMethodResponseDestroy(s *terraform.State) error { 84 conn := testAccProvider.Meta().(*AWSClient).apigateway 85 86 for _, rs := range s.RootModule().Resources { 87 if rs.Type != "aws_api_gateway_method_response" { 88 continue 89 } 90 91 req := &apigateway.GetMethodResponseInput{ 92 HttpMethod: aws.String("GET"), 93 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 94 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 95 StatusCode: aws.String(rs.Primary.Attributes["status_code"]), 96 } 97 _, err := conn.GetMethodResponse(req) 98 99 if err == nil { 100 return fmt.Errorf("API Gateway Method still exists") 101 } 102 103 aws2err, ok := err.(awserr.Error) 104 if !ok { 105 return err 106 } 107 if aws2err.Code() != "NotFoundException" { 108 return err 109 } 110 111 return nil 112 } 113 114 return nil 115 } 116 117 const testAccAWSAPIGatewayMethodResponseConfig = ` 118 resource "aws_api_gateway_rest_api" "test" { 119 name = "test" 120 } 121 122 resource "aws_api_gateway_resource" "test" { 123 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 124 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 125 path_part = "test" 126 } 127 128 resource "aws_api_gateway_method" "test" { 129 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 130 resource_id = "${aws_api_gateway_resource.test.id}" 131 http_method = "GET" 132 authorization = "NONE" 133 134 request_models = { 135 "application/json" = "Error" 136 } 137 } 138 139 resource "aws_api_gateway_method_response" "error" { 140 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 141 resource_id = "${aws_api_gateway_resource.test.id}" 142 http_method = "${aws_api_gateway_method.test.http_method}" 143 status_code = "400" 144 145 response_models = { 146 "application/json" = "Error" 147 } 148 } 149 `