github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 resource.TestStep{ 23 Config: testAccAWSAPIGatewayIntegrationConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf), 26 testAccCheckAWSAPIGatewayIntegrationAttributes(&conf), 27 resource.TestCheckResourceAttr( 28 "aws_api_gateway_integration.test", "type", "HTTP"), 29 resource.TestCheckResourceAttr( 30 "aws_api_gateway_integration.test", "integration_http_method", "GET"), 31 resource.TestCheckResourceAttr( 32 "aws_api_gateway_integration.test", "uri", "https://www.google.de"), 33 resource.TestCheckResourceAttr( 34 "aws_api_gateway_integration.test", "request_templates.application/json", ""), 35 resource.TestCheckResourceAttr( 36 "aws_api_gateway_integration.test", "request_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"), 37 resource.TestCheckResourceAttr( 38 "aws_api_gateway_integration.test", "passthrough_behavior", "WHEN_NO_MATCH"), 39 resource.TestCheckNoResourceAttr( 40 "aws_api_gateway_integration.test", "content_handling"), 41 ), 42 }, 43 44 resource.TestStep{ 45 Config: testAccAWSAPIGatewayIntegrationConfigUpdate, 46 Check: resource.ComposeTestCheckFunc( 47 testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf), 48 testAccCheckAWSAPIGatewayMockIntegrationAttributes(&conf), 49 resource.TestCheckResourceAttr( 50 "aws_api_gateway_integration.test", "type", "MOCK"), 51 resource.TestCheckResourceAttr( 52 "aws_api_gateway_integration.test", "integration_http_method", ""), 53 resource.TestCheckResourceAttr( 54 "aws_api_gateway_integration.test", "uri", ""), 55 resource.TestCheckResourceAttr( 56 "aws_api_gateway_integration.test", "passthrough_behavior", "NEVER"), 57 resource.TestCheckResourceAttr( 58 "aws_api_gateway_integration.test", "content_handling", "CONVERT_TO_BINARY"), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func testAccCheckAWSAPIGatewayMockIntegrationAttributes(conf *apigateway.Integration) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 if *conf.Type != "MOCK" { 68 return fmt.Errorf("Wrong Type: %q", *conf.Type) 69 } 70 if *conf.RequestParameters["integration.request.header.X-Authorization"] != "'updated'" { 71 return fmt.Errorf("wrong updated RequestParameters for header.X-Authorization") 72 } 73 if *conf.ContentHandling != "CONVERT_TO_BINARY" { 74 return fmt.Errorf("wrong ContentHandling: %q", *conf.ContentHandling) 75 } 76 return nil 77 } 78 } 79 80 func testAccCheckAWSAPIGatewayIntegrationAttributes(conf *apigateway.Integration) resource.TestCheckFunc { 81 return func(s *terraform.State) error { 82 if *conf.HttpMethod == "" { 83 return fmt.Errorf("empty HttpMethod") 84 } 85 if *conf.Uri != "https://www.google.de" { 86 return fmt.Errorf("wrong Uri") 87 } 88 if *conf.Type != "HTTP" { 89 return fmt.Errorf("wrong Type") 90 } 91 if conf.RequestTemplates["application/json"] != nil { 92 return fmt.Errorf("wrong RequestTemplate for application/json") 93 } 94 if *conf.RequestTemplates["application/xml"] != "#set($inputRoot = $input.path('$'))\n{ }" { 95 return fmt.Errorf("wrong RequestTemplate for application/xml") 96 } 97 if *conf.RequestParameters["integration.request.header.X-Authorization"] != "'static'" { 98 return fmt.Errorf("wrong RequestParameters for header.X-Authorization") 99 } 100 return nil 101 } 102 } 103 104 func testAccCheckAWSAPIGatewayIntegrationExists(n string, res *apigateway.Integration) resource.TestCheckFunc { 105 return func(s *terraform.State) error { 106 rs, ok := s.RootModule().Resources[n] 107 if !ok { 108 return fmt.Errorf("Not found: %s", n) 109 } 110 111 if rs.Primary.ID == "" { 112 return fmt.Errorf("No API Gateway Method ID is set") 113 } 114 115 conn := testAccProvider.Meta().(*AWSClient).apigateway 116 117 req := &apigateway.GetIntegrationInput{ 118 HttpMethod: aws.String("GET"), 119 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 120 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 121 } 122 describe, err := conn.GetIntegration(req) 123 if err != nil { 124 return err 125 } 126 127 *res = *describe 128 129 return nil 130 } 131 } 132 133 func testAccCheckAWSAPIGatewayIntegrationDestroy(s *terraform.State) error { 134 conn := testAccProvider.Meta().(*AWSClient).apigateway 135 136 for _, rs := range s.RootModule().Resources { 137 if rs.Type != "aws_api_gateway_integration" { 138 continue 139 } 140 141 req := &apigateway.GetIntegrationInput{ 142 HttpMethod: aws.String("GET"), 143 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 144 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 145 } 146 _, err := conn.GetIntegration(req) 147 148 if err == nil { 149 return fmt.Errorf("API Gateway Method still exists") 150 } 151 152 aws2err, ok := err.(awserr.Error) 153 if !ok { 154 return err 155 } 156 if aws2err.Code() != "NotFoundException" { 157 return err 158 } 159 160 return nil 161 } 162 163 return nil 164 } 165 166 const testAccAWSAPIGatewayIntegrationConfig = ` 167 resource "aws_api_gateway_rest_api" "test" { 168 name = "test" 169 } 170 171 resource "aws_api_gateway_resource" "test" { 172 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 173 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 174 path_part = "test" 175 } 176 177 resource "aws_api_gateway_method" "test" { 178 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 179 resource_id = "${aws_api_gateway_resource.test.id}" 180 http_method = "GET" 181 authorization = "NONE" 182 183 request_models = { 184 "application/json" = "Error" 185 } 186 } 187 188 resource "aws_api_gateway_integration" "test" { 189 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 190 resource_id = "${aws_api_gateway_resource.test.id}" 191 http_method = "${aws_api_gateway_method.test.http_method}" 192 193 request_templates = { 194 "application/json" = "" 195 "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }" 196 } 197 198 request_parameters = { 199 "integration.request.header.X-Authorization" = "'static'" 200 } 201 202 type = "HTTP" 203 uri = "https://www.google.de" 204 integration_http_method = "GET" 205 passthrough_behavior = "WHEN_NO_MATCH" 206 } 207 ` 208 209 const testAccAWSAPIGatewayIntegrationConfigUpdate = ` 210 resource "aws_api_gateway_rest_api" "test" { 211 name = "test" 212 } 213 214 resource "aws_api_gateway_resource" "test" { 215 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 216 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 217 path_part = "test" 218 } 219 220 resource "aws_api_gateway_method" "test" { 221 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 222 resource_id = "${aws_api_gateway_resource.test.id}" 223 http_method = "GET" 224 authorization = "NONE" 225 226 request_models = { 227 "application/json" = "Error" 228 } 229 } 230 231 resource "aws_api_gateway_integration" "test" { 232 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 233 resource_id = "${aws_api_gateway_resource.test.id}" 234 http_method = "${aws_api_gateway_method.test.http_method}" 235 236 request_parameters = { 237 "integration.request.header.X-Authorization" = "'updated'" 238 } 239 240 type = "MOCK" 241 passthrough_behavior = "NEVER" 242 content_handling = "CONVERT_TO_BINARY" 243 244 } 245 `