diff --git a/aws/resource_aws_api_gateway_rest_api.go b/aws/resource_aws_api_gateway_rest_api.go index ffd553657ec..ee1849222b0 100644 --- a/aws/resource_aws_api_gateway_rest_api.go +++ b/aws/resource_aws_api_gateway_rest_api.go @@ -65,6 +65,13 @@ func resourceAwsApiGatewayRestApi() *schema.Resource { Optional: true, }, + "body_base_path": { + Type: schema.TypeString, + Default: "ignore", + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"ignore", "prepend", "split"}, true), + }, + "minimum_compression_size": { Type: schema.TypeInt, Optional: true, @@ -175,12 +182,20 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{} d.SetId(aws.StringValue(gateway.Id)) + bodyBasePathMode := d.Get("body_base_path").(string) + if body, ok := d.GetOk("body"); ok { log.Printf("[DEBUG] Initializing API Gateway from OpenAPI spec %s", d.Id()) _, err := conn.PutRestApi(&apigateway.PutRestApiInput{ RestApiId: gateway.Id, Mode: aws.String(apigateway.PutModeOverwrite), Body: []byte(body.(string)), + Parameters: map[string]*string{ + // See https://docs.aws.amazon.com/cli/latest/reference/apigateway/import-rest-api.html + // At the moment of writing, according to aws support, the docs are incorrect + // and the parameter should be called 'basepath' and not 'basePath' + "basepath": &bodyBasePathMode, + }, }) if err != nil { return fmt.Errorf("error creating API Gateway specification: %s", err) @@ -411,12 +426,17 @@ func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{} } if d.HasChange("body") { + bodyBasePathMode := d.Get("body_base_path").(string) + if body, ok := d.GetOk("body"); ok { log.Printf("[DEBUG] Updating API Gateway from OpenAPI spec: %s", d.Id()) _, err := conn.PutRestApi(&apigateway.PutRestApiInput{ RestApiId: aws.String(d.Id()), Mode: aws.String(apigateway.PutModeOverwrite), Body: []byte(body.(string)), + Parameters: map[string]*string{ + "basepath": &bodyBasePathMode, + }, }) if err != nil { return fmt.Errorf("error updating API Gateway specification: %s", err) diff --git a/aws/resource_aws_api_gateway_rest_api_test.go b/aws/resource_aws_api_gateway_rest_api_test.go index 7555fe1d856..58072a6318f 100644 --- a/aws/resource_aws_api_gateway_rest_api_test.go +++ b/aws/resource_aws_api_gateway_rest_api_test.go @@ -513,6 +513,105 @@ func TestAccAWSAPIGatewayRestApi_openapi(t *testing.T) { }) } +func TestAccAWSAPIGatewayRestApi_openapi_body_base_path_ignore(t *testing.T) { + var conf apigateway.RestApi + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayRestAPIConfigOpenAPIBasePathIgnore, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf), + testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/test"}), + ), + }, + { + ResourceName: "aws_api_gateway_rest_api.test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"body"}, + }, + { + Config: testAccAWSAPIGatewayRestAPIUpdateConfigOpenAPIBasePathIgnore, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf), + testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"), + testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/update"}), + ), + }, + }, + }) +} + +func TestAccAWSAPIGatewayRestApi_openapi_body_base_path_prepend(t *testing.T) { + var conf apigateway.RestApi + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayRestAPIConfigOpenAPIBasePathPrepend, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf), + testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/foo/bar/baz/test"}), + ), + }, + { + ResourceName: "aws_api_gateway_rest_api.test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"body"}, + }, + { + Config: testAccAWSAPIGatewayRestAPIUpdateConfigOpenAPIBasePathPrepend, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf), + testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"), + testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/foo/bar/baz/update"}), + ), + }, + }, + }) +} + +func TestAccAWSAPIGatewayRestApi_openapi_body_base_path_split(t *testing.T) { + var conf apigateway.RestApi + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayRestAPIConfigOpenAPIBasePathSplit, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf), + testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/bar/baz/test"}), + ), + }, + { + ResourceName: "aws_api_gateway_rest_api.test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"body"}, + }, + { + Config: testAccAWSAPIGatewayRestAPIUpdateConfigOpenAPIBasePathSplit, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf), + testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"), + testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/bar/baz/update"}), + ), + }, + }, + }) +} + func testAccCheckAWSAPIGatewayRestAPINameAttribute(conf *apigateway.RestApi, name string) resource.TestCheckFunc { return func(s *terraform.State) error { if *conf.Name != name { @@ -949,7 +1048,7 @@ resource "aws_api_gateway_rest_api" "test" { "info": { "title": "%s", "version": "2017-04-20T04:08:08Z" - }, + }, "schemes": [ "https" ], @@ -1020,3 +1119,246 @@ EOF } `, rName, rName) } + +const testAccAWSAPIGatewayRestAPIConfigOpenAPIBasePathIgnore = ` +resource "aws_api_gateway_rest_api" "test" { + name = "test" + body_base_path = "ignore" + body = <