github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_integration_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 TestAccAWSAPIGatewayIntegration_basic(t *testing.T) { 15 var conf apigateway.Integration 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSAPIGatewayIntegrationDestroy, 21 Steps: []resource.TestStep{ 22 { 23 Config: testAccAWSAPIGatewayIntegrationConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf), 26 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "type", "HTTP"), 27 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "integration_http_method", "GET"), 28 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "uri", "https://www.google.de"), 29 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "passthrough_behavior", "WHEN_NO_MATCH"), 30 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "content_handling", "CONVERT_TO_TEXT"), 31 resource.TestCheckNoResourceAttr("aws_api_gateway_integration.test", "credentials"), 32 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.%", "2"), 33 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.integration.request.header.X-Authorization", "'static'"), 34 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.integration.request.header.X-Foo", "'Bar'"), 35 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.%", "2"), 36 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/json", ""), 37 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"), 38 ), 39 }, 40 41 { 42 Config: testAccAWSAPIGatewayIntegrationConfigUpdate, 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf), 45 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "type", "HTTP"), 46 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "integration_http_method", "GET"), 47 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "uri", "https://www.google.de"), 48 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "passthrough_behavior", "WHEN_NO_MATCH"), 49 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "content_handling", "CONVERT_TO_TEXT"), 50 resource.TestCheckNoResourceAttr("aws_api_gateway_integration.test", "credentials"), 51 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.%", "2"), 52 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.integration.request.header.X-Authorization", "'updated'"), 53 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.integration.request.header.X-FooBar", "'Baz'"), 54 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.%", "2"), 55 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/json", "{'foobar': 'bar}"), 56 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.text/html", "<html>Foo</html>"), 57 ), 58 }, 59 60 { 61 Config: testAccAWSAPIGatewayIntegrationConfigUpdateNoTemplates, 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf), 64 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "type", "HTTP"), 65 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "integration_http_method", "GET"), 66 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "uri", "https://www.google.de"), 67 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "passthrough_behavior", "WHEN_NO_MATCH"), 68 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "content_handling", "CONVERT_TO_TEXT"), 69 resource.TestCheckNoResourceAttr("aws_api_gateway_integration.test", "credentials"), 70 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.%", "0"), 71 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.%", "0"), 72 ), 73 }, 74 75 { 76 Config: testAccAWSAPIGatewayIntegrationConfig, 77 Check: resource.ComposeTestCheckFunc( 78 testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf), 79 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "type", "HTTP"), 80 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "integration_http_method", "GET"), 81 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "uri", "https://www.google.de"), 82 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "passthrough_behavior", "WHEN_NO_MATCH"), 83 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "content_handling", "CONVERT_TO_TEXT"), 84 resource.TestCheckNoResourceAttr("aws_api_gateway_integration.test", "credentials"), 85 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.%", "2"), 86 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.integration.request.header.X-Authorization", "'static'"), 87 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.%", "2"), 88 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/json", ""), 89 resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"), 90 ), 91 }, 92 }, 93 }) 94 } 95 96 func testAccCheckAWSAPIGatewayIntegrationExists(n string, res *apigateway.Integration) 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.GetIntegrationInput{ 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 } 114 describe, err := conn.GetIntegration(req) 115 if err != nil { 116 return err 117 } 118 119 *res = *describe 120 121 return nil 122 } 123 } 124 125 func testAccCheckAWSAPIGatewayIntegrationDestroy(s *terraform.State) error { 126 conn := testAccProvider.Meta().(*AWSClient).apigateway 127 128 for _, rs := range s.RootModule().Resources { 129 if rs.Type != "aws_api_gateway_integration" { 130 continue 131 } 132 133 req := &apigateway.GetIntegrationInput{ 134 HttpMethod: aws.String("GET"), 135 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 136 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 137 } 138 _, err := conn.GetIntegration(req) 139 140 if err == nil { 141 return fmt.Errorf("API Gateway Method still exists") 142 } 143 144 aws2err, ok := err.(awserr.Error) 145 if !ok { 146 return err 147 } 148 if aws2err.Code() != "NotFoundException" { 149 return err 150 } 151 152 return nil 153 } 154 155 return nil 156 } 157 158 const testAccAWSAPIGatewayIntegrationConfig = ` 159 resource "aws_api_gateway_rest_api" "test" { 160 name = "test" 161 } 162 163 resource "aws_api_gateway_resource" "test" { 164 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 165 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 166 path_part = "test" 167 } 168 169 resource "aws_api_gateway_method" "test" { 170 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 171 resource_id = "${aws_api_gateway_resource.test.id}" 172 http_method = "GET" 173 authorization = "NONE" 174 175 request_models = { 176 "application/json" = "Error" 177 } 178 } 179 180 resource "aws_api_gateway_integration" "test" { 181 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 182 resource_id = "${aws_api_gateway_resource.test.id}" 183 http_method = "${aws_api_gateway_method.test.http_method}" 184 185 request_templates = { 186 "application/json" = "" 187 "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }" 188 } 189 190 request_parameters = { 191 "integration.request.header.X-Authorization" = "'static'" 192 "integration.request.header.X-Foo" = "'Bar'" 193 } 194 195 type = "HTTP" 196 uri = "https://www.google.de" 197 integration_http_method = "GET" 198 passthrough_behavior = "WHEN_NO_MATCH" 199 content_handling = "CONVERT_TO_TEXT" 200 } 201 ` 202 203 const testAccAWSAPIGatewayIntegrationConfigUpdate = ` 204 resource "aws_api_gateway_rest_api" "test" { 205 name = "test" 206 } 207 208 resource "aws_api_gateway_resource" "test" { 209 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 210 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 211 path_part = "test" 212 } 213 214 resource "aws_api_gateway_method" "test" { 215 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 216 resource_id = "${aws_api_gateway_resource.test.id}" 217 http_method = "GET" 218 authorization = "NONE" 219 220 request_models = { 221 "application/json" = "Error" 222 } 223 } 224 225 resource "aws_api_gateway_integration" "test" { 226 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 227 resource_id = "${aws_api_gateway_resource.test.id}" 228 http_method = "${aws_api_gateway_method.test.http_method}" 229 230 request_templates = { 231 "application/json" = "{'foobar': 'bar}" 232 "text/html" = "<html>Foo</html>" 233 } 234 235 request_parameters = { 236 "integration.request.header.X-Authorization" = "'updated'" 237 "integration.request.header.X-FooBar" = "'Baz'" 238 } 239 240 type = "HTTP" 241 uri = "https://www.google.de" 242 integration_http_method = "GET" 243 passthrough_behavior = "WHEN_NO_MATCH" 244 content_handling = "CONVERT_TO_TEXT" 245 } 246 ` 247 248 const testAccAWSAPIGatewayIntegrationConfigUpdateNoTemplates = ` 249 resource "aws_api_gateway_rest_api" "test" { 250 name = "test" 251 } 252 253 resource "aws_api_gateway_resource" "test" { 254 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 255 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 256 path_part = "test" 257 } 258 259 resource "aws_api_gateway_method" "test" { 260 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 261 resource_id = "${aws_api_gateway_resource.test.id}" 262 http_method = "GET" 263 authorization = "NONE" 264 265 request_models = { 266 "application/json" = "Error" 267 } 268 } 269 270 resource "aws_api_gateway_integration" "test" { 271 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 272 resource_id = "${aws_api_gateway_resource.test.id}" 273 http_method = "${aws_api_gateway_method.test.http_method}" 274 275 type = "HTTP" 276 uri = "https://www.google.de" 277 integration_http_method = "GET" 278 passthrough_behavior = "WHEN_NO_MATCH" 279 content_handling = "CONVERT_TO_TEXT" 280 } 281 `