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