github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_integration_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 TestAccAWSAPIGatewayIntegrationResponse_basic(t *testing.T) { 15 var conf apigateway.IntegrationResponse 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSAPIGatewayIntegrationResponseDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSAPIGatewayIntegrationResponseConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSAPIGatewayIntegrationResponseExists("aws_api_gateway_integration_response.test", &conf), 26 testAccCheckAWSAPIGatewayIntegrationResponseAttributes(&conf), 27 resource.TestCheckResourceAttr( 28 "aws_api_gateway_integration_response.test", "response_templates.application/json", ""), 29 resource.TestCheckResourceAttr( 30 "aws_api_gateway_integration_response.test", "response_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"), 31 resource.TestCheckNoResourceAttr( 32 "aws_api_gateway_integration_response.test", "content_handling"), 33 ), 34 }, 35 36 resource.TestStep{ 37 Config: testAccAWSAPIGatewayIntegrationResponseConfigUpdate, 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckAWSAPIGatewayIntegrationResponseExists("aws_api_gateway_integration_response.test", &conf), 40 testAccCheckAWSAPIGatewayIntegrationResponseAttributesUpdate(&conf), 41 resource.TestCheckResourceAttr( 42 "aws_api_gateway_integration_response.test", "response_templates.application/json", "$input.path('$')"), 43 resource.TestCheckResourceAttr( 44 "aws_api_gateway_integration_response.test", "response_templates.application/xml", ""), 45 resource.TestCheckResourceAttr( 46 "aws_api_gateway_integration_response.test", "content_handling", "CONVERT_TO_BINARY"), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func testAccCheckAWSAPIGatewayIntegrationResponseAttributes(conf *apigateway.IntegrationResponse) resource.TestCheckFunc { 54 return func(s *terraform.State) error { 55 if *conf.StatusCode != "400" { 56 return fmt.Errorf("wrong StatusCode: %q", *conf.StatusCode) 57 } 58 if conf.ResponseTemplates["application/json"] != nil { 59 return fmt.Errorf("wrong ResponseTemplate for application/json") 60 } 61 if *conf.ResponseTemplates["application/xml"] != "#set($inputRoot = $input.path('$'))\n{ }" { 62 return fmt.Errorf("wrong ResponseTemplate for application/xml") 63 } 64 if conf.SelectionPattern == nil || *conf.SelectionPattern != ".*" { 65 return fmt.Errorf("wrong SelectionPattern (expected .*)") 66 } 67 if *conf.ResponseParameters["method.response.header.Content-Type"] != "integration.response.body.type" { 68 return fmt.Errorf("wrong ResponseParameters for header.Content-Type") 69 } 70 return nil 71 } 72 } 73 74 func testAccCheckAWSAPIGatewayIntegrationResponseAttributesUpdate(conf *apigateway.IntegrationResponse) resource.TestCheckFunc { 75 return func(s *terraform.State) error { 76 if *conf.StatusCode != "400" { 77 return fmt.Errorf("wrong StatusCode: %q", *conf.StatusCode) 78 } 79 if *conf.ResponseTemplates["application/json"] != "$input.path('$')" { 80 return fmt.Errorf("wrong ResponseTemplate for application/json") 81 } 82 if conf.ResponseTemplates["application/xml"] != nil { 83 return fmt.Errorf("wrong ResponseTemplate for application/xml") 84 } 85 if conf.SelectionPattern != nil { 86 return fmt.Errorf("wrong SelectionPattern (expected nil)") 87 } 88 if conf.ResponseParameters["method.response.header.Content-Type"] != nil { 89 return fmt.Errorf("ResponseParameters for header.Content-Type shouldnt exist") 90 } 91 92 return nil 93 } 94 } 95 96 func testAccCheckAWSAPIGatewayIntegrationResponseExists(n string, res *apigateway.IntegrationResponse) resource.TestCheckFunc { 97 return func(s *terraform.State) error { 98 rs, ok := s.RootModule().Resources[n] 99 if !ok { 100 return fmt.Errorf("Not found: %s", n) 101 } 102 103 if rs.Primary.ID == "" { 104 return fmt.Errorf("No API Gateway Method ID is set") 105 } 106 107 conn := testAccProvider.Meta().(*AWSClient).apigateway 108 109 req := &apigateway.GetIntegrationResponseInput{ 110 HttpMethod: aws.String("GET"), 111 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 112 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 113 StatusCode: aws.String(rs.Primary.Attributes["status_code"]), 114 } 115 describe, err := conn.GetIntegrationResponse(req) 116 if err != nil { 117 return err 118 } 119 120 *res = *describe 121 122 return nil 123 } 124 } 125 126 func testAccCheckAWSAPIGatewayIntegrationResponseDestroy(s *terraform.State) error { 127 conn := testAccProvider.Meta().(*AWSClient).apigateway 128 129 for _, rs := range s.RootModule().Resources { 130 if rs.Type != "aws_api_gateway_integration_response" { 131 continue 132 } 133 134 req := &apigateway.GetIntegrationResponseInput{ 135 HttpMethod: aws.String("GET"), 136 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 137 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 138 StatusCode: aws.String(rs.Primary.Attributes["status_code"]), 139 } 140 _, err := conn.GetIntegrationResponse(req) 141 142 if err == nil { 143 return fmt.Errorf("API Gateway Method still exists") 144 } 145 146 aws2err, ok := err.(awserr.Error) 147 if !ok { 148 return err 149 } 150 if aws2err.Code() != "NotFoundException" { 151 return err 152 } 153 154 return nil 155 } 156 157 return nil 158 } 159 160 const testAccAWSAPIGatewayIntegrationResponseConfig = ` 161 resource "aws_api_gateway_rest_api" "test" { 162 name = "test" 163 } 164 165 resource "aws_api_gateway_resource" "test" { 166 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 167 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 168 path_part = "test" 169 } 170 171 resource "aws_api_gateway_method" "test" { 172 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 173 resource_id = "${aws_api_gateway_resource.test.id}" 174 http_method = "GET" 175 authorization = "NONE" 176 177 request_models = { 178 "application/json" = "Error" 179 } 180 } 181 182 resource "aws_api_gateway_method_response" "error" { 183 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 184 resource_id = "${aws_api_gateway_resource.test.id}" 185 http_method = "${aws_api_gateway_method.test.http_method}" 186 status_code = "400" 187 188 response_models = { 189 "application/json" = "Error" 190 } 191 192 response_parameters = { 193 "method.response.header.Content-Type" = true 194 } 195 } 196 197 resource "aws_api_gateway_integration" "test" { 198 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 199 resource_id = "${aws_api_gateway_resource.test.id}" 200 http_method = "${aws_api_gateway_method.test.http_method}" 201 202 request_templates = { 203 "application/json" = "" 204 "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }" 205 } 206 207 type = "MOCK" 208 } 209 210 resource "aws_api_gateway_integration_response" "test" { 211 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 212 resource_id = "${aws_api_gateway_resource.test.id}" 213 http_method = "${aws_api_gateway_method.test.http_method}" 214 status_code = "${aws_api_gateway_method_response.error.status_code}" 215 selection_pattern = ".*" 216 217 response_templates = { 218 "application/json" = "" 219 "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }" 220 } 221 222 response_parameters = { 223 "method.response.header.Content-Type" = "integration.response.body.type" 224 } 225 } 226 ` 227 228 const testAccAWSAPIGatewayIntegrationResponseConfigUpdate = ` 229 resource "aws_api_gateway_rest_api" "test" { 230 name = "test" 231 } 232 233 resource "aws_api_gateway_resource" "test" { 234 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 235 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 236 path_part = "test" 237 } 238 239 resource "aws_api_gateway_method" "test" { 240 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 241 resource_id = "${aws_api_gateway_resource.test.id}" 242 http_method = "GET" 243 authorization = "NONE" 244 245 request_models = { 246 "application/json" = "Error" 247 } 248 } 249 250 resource "aws_api_gateway_method_response" "error" { 251 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 252 resource_id = "${aws_api_gateway_resource.test.id}" 253 http_method = "${aws_api_gateway_method.test.http_method}" 254 status_code = "400" 255 256 response_models = { 257 "application/json" = "Error" 258 } 259 260 response_parameters = { 261 "method.response.header.Content-Type" = true 262 } 263 } 264 265 resource "aws_api_gateway_integration" "test" { 266 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 267 resource_id = "${aws_api_gateway_resource.test.id}" 268 http_method = "${aws_api_gateway_method.test.http_method}" 269 270 request_templates = { 271 "application/json" = "" 272 "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }" 273 } 274 275 type = "MOCK" 276 } 277 278 resource "aws_api_gateway_integration_response" "test" { 279 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 280 resource_id = "${aws_api_gateway_resource.test.id}" 281 http_method = "${aws_api_gateway_method.test.http_method}" 282 status_code = "${aws_api_gateway_method_response.error.status_code}" 283 284 response_templates = { 285 "application/json" = "$input.path('$')" 286 "application/xml" = "" 287 } 288 289 content_handling = "CONVERT_TO_BINARY" 290 291 } 292 `