github.com/ricardclau/terraform@v0.6.17-0.20160519222547-283e3ae6b5a9/builtin/providers/aws/resource_aws_api_gateway_method_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 TestAccAWSAPIGatewayMethod_basic(t *testing.T) { 15 var conf apigateway.Method 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSAPIGatewayMethodDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSAPIGatewayMethodConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf), 26 testAccCheckAWSAPIGatewayMethodAttributes(&conf), 27 resource.TestCheckResourceAttr( 28 "aws_api_gateway_method.test", "http_method", "GET"), 29 resource.TestCheckResourceAttr( 30 "aws_api_gateway_method.test", "authorization", "NONE"), 31 resource.TestCheckResourceAttr( 32 "aws_api_gateway_method.test", "request_models.application/json", "Error"), 33 ), 34 }, 35 36 resource.TestStep{ 37 Config: testAccAWSAPIGatewayMethodConfigUpdate, 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf), 40 testAccCheckAWSAPIGatewayMethodAttributesUpdate(&conf), 41 ), 42 }, 43 }, 44 }) 45 } 46 47 func testAccCheckAWSAPIGatewayMethodAttributes(conf *apigateway.Method) resource.TestCheckFunc { 48 return func(s *terraform.State) error { 49 if *conf.HttpMethod != "GET" { 50 return fmt.Errorf("Wrong HttpMethod: %q", *conf.HttpMethod) 51 } 52 if *conf.AuthorizationType != "NONE" { 53 return fmt.Errorf("Wrong Authorization: %q", *conf.AuthorizationType) 54 } 55 56 if val, ok := conf.RequestParameters["method.request.header.Content-Type"]; !ok { 57 return fmt.Errorf("missing Content-Type RequestParameters") 58 } else { 59 if *val != false { 60 return fmt.Errorf("wrong Content-Type RequestParameters value") 61 } 62 } 63 if val, ok := conf.RequestParameters["method.request.querystring.page"]; !ok { 64 return fmt.Errorf("missing page RequestParameters") 65 } else { 66 if *val != true { 67 return fmt.Errorf("wrong query string RequestParameters value") 68 } 69 } 70 71 return nil 72 } 73 } 74 75 func testAccCheckAWSAPIGatewayMethodAttributesUpdate(conf *apigateway.Method) resource.TestCheckFunc { 76 return func(s *terraform.State) error { 77 if *conf.HttpMethod != "GET" { 78 return fmt.Errorf("Wrong HttpMethod: %q", *conf.HttpMethod) 79 } 80 if conf.RequestParameters["method.request.header.Content-Type"] != nil { 81 return fmt.Errorf("Content-Type RequestParameters shouldn't exist") 82 } 83 if val, ok := conf.RequestParameters["method.request.querystring.page"]; !ok { 84 return fmt.Errorf("missing updated page RequestParameters") 85 } else { 86 if *val != false { 87 return fmt.Errorf("wrong query string RequestParameters updated value") 88 } 89 } 90 91 return nil 92 } 93 } 94 95 func testAccCheckAWSAPIGatewayMethodExists(n string, res *apigateway.Method) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 rs, ok := s.RootModule().Resources[n] 98 if !ok { 99 return fmt.Errorf("Not found: %s", n) 100 } 101 102 if rs.Primary.ID == "" { 103 return fmt.Errorf("No API Gateway Method ID is set") 104 } 105 106 conn := testAccProvider.Meta().(*AWSClient).apigateway 107 108 req := &apigateway.GetMethodInput{ 109 HttpMethod: aws.String("GET"), 110 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 111 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 112 } 113 describe, err := conn.GetMethod(req) 114 if err != nil { 115 return err 116 } 117 118 *res = *describe 119 120 return nil 121 } 122 } 123 124 func testAccCheckAWSAPIGatewayMethodDestroy(s *terraform.State) error { 125 conn := testAccProvider.Meta().(*AWSClient).apigateway 126 127 for _, rs := range s.RootModule().Resources { 128 if rs.Type != "aws_api_gateway_method" { 129 continue 130 } 131 132 req := &apigateway.GetMethodInput{ 133 HttpMethod: aws.String("GET"), 134 ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID), 135 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 136 } 137 _, err := conn.GetMethod(req) 138 139 if err == nil { 140 return fmt.Errorf("API Gateway Method still exists") 141 } 142 143 aws2err, ok := err.(awserr.Error) 144 if !ok { 145 return err 146 } 147 if aws2err.Code() != "NotFoundException" { 148 return err 149 } 150 151 return nil 152 } 153 154 return nil 155 } 156 157 const testAccAWSAPIGatewayMethodConfig = ` 158 resource "aws_api_gateway_rest_api" "test" { 159 name = "test" 160 } 161 162 resource "aws_api_gateway_resource" "test" { 163 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 164 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 165 path_part = "test" 166 } 167 168 resource "aws_api_gateway_method" "test" { 169 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 170 resource_id = "${aws_api_gateway_resource.test.id}" 171 http_method = "GET" 172 authorization = "NONE" 173 174 request_models = { 175 "application/json" = "Error" 176 } 177 178 request_parameters_in_json = <<PARAMS 179 { 180 "method.request.header.Content-Type": false, 181 "method.request.querystring.page": true 182 } 183 PARAMS 184 } 185 ` 186 187 const testAccAWSAPIGatewayMethodConfigUpdate = ` 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 request_parameters_in_json = <<PARAMS 209 { 210 "method.request.querystring.page": false 211 } 212 PARAMS 213 } 214 `