provider/aws: Fixed deletion of aws_api_gateway_base_path_mapping with empty path (#10177)

This commit is contained in:
Ninir 2016-12-12 23:09:25 +01:00 committed by Paul Stack
parent c1637f25c4
commit 16660a4f60
2 changed files with 123 additions and 10 deletions

View File

@ -12,6 +12,8 @@ import (
"github.com/hashicorp/terraform/helper/schema"
)
const emptyBasePathMappingValue = "(none)"
func resourceAwsApiGatewayBasePathMapping() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayBasePathMappingCreate,
@ -19,22 +21,22 @@ func resourceAwsApiGatewayBasePathMapping() *schema.Resource {
Delete: resourceAwsApiGatewayBasePathMappingDelete,
Schema: map[string]*schema.Schema{
"api_id": &schema.Schema{
"api_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"base_path": &schema.Schema{
"base_path": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"stage_name": &schema.Schema{
"stage_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"domain_name": &schema.Schema{
"domain_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
@ -87,6 +89,10 @@ func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta inter
return nil
}
if basePath == "" {
basePath = emptyBasePathMappingValue
}
mapping, err := conn.GetBasePathMapping(&apigateway.GetBasePathMappingInput{
DomainName: aws.String(domainName),
BasePath: aws.String(basePath),
@ -101,7 +107,13 @@ func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta inter
return fmt.Errorf("Error reading Gateway base path mapping: %s", err)
}
d.Set("base_path", mapping.BasePath)
mappingBasePath := *mapping.BasePath
if mappingBasePath == emptyBasePathMappingValue {
mappingBasePath = ""
}
d.Set("base_path", mappingBasePath)
d.Set("api_id", mapping.RestApiId)
d.Set("stage_name", mapping.Stage)
@ -111,9 +123,15 @@ func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta inter
func resourceAwsApiGatewayBasePathMappingDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
basePath := d.Get("base_path").(string)
if basePath == "" {
basePath = emptyBasePathMappingValue
}
_, err := conn.DeleteBasePathMapping(&apigateway.DeleteBasePathMappingInput{
DomainName: aws.String(d.Get("domain_name").(string)),
BasePath: aws.String(d.Get("base_path").(string)),
BasePath: aws.String(basePath),
})
if err != nil {

View File

@ -22,7 +22,7 @@ func TestAccAWSAPIGatewayBasePath_basic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayBasePathDestroy(name),
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSAPIGatewayBasePathConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayBasePathExists("aws_api_gateway_base_path_mapping.test", name, &conf),
@ -32,6 +32,28 @@ func TestAccAWSAPIGatewayBasePath_basic(t *testing.T) {
})
}
// https://github.com/hashicorp/terraform/issues/9212
func TestAccAWSAPIGatewayEmptyBasePath_basic(t *testing.T) {
var conf apigateway.BasePathMapping
// Our test cert is for a wildcard on this domain
name := fmt.Sprintf("%s.tf-acc.invalid", resource.UniqueId())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayBasePathDestroy(name),
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayEmptyBasePathConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayEmptyBasePathExists("aws_api_gateway_base_path_mapping.test", name, &conf),
),
},
},
})
}
func testAccCheckAWSAPIGatewayBasePathExists(n string, name string, res *apigateway.BasePathMapping) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
@ -64,6 +86,34 @@ func testAccCheckAWSAPIGatewayBasePathExists(n string, name string, res *apigate
}
}
func testAccCheckAWSAPIGatewayEmptyBasePathExists(n string, name string, res *apigateway.BasePathMapping) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No API Gateway ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).apigateway
req := &apigateway.GetBasePathMappingInput{
DomainName: aws.String(name),
BasePath: aws.String(""),
}
describe, err := conn.GetBasePathMapping(req)
if err != nil {
return err
}
*res = *describe
return nil
}
}
func testAccCheckAWSAPIGatewayBasePathDestroy(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway
@ -130,9 +180,54 @@ resource "aws_api_gateway_base_path_mapping" "test" {
resource "aws_api_gateway_domain_name" "test" {
domain_name = "%s"
certificate_name = "tf-apigateway-base-path-mapping-test"
certificate_body = "%s"
certificate_chain = "%s"
certificate_private_key = "%s"
certificate_body = <<EOF
%vEOF
certificate_chain = <<EOF
%vEOF
certificate_private_key = <<EOF
%vEOF
}
`, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey)
}
func testAccAWSAPIGatewayEmptyBasePathConfig(name string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
name = "tf-acc-apigateway-base-path-mapping"
description = "Terraform Acceptance Tests"
}
resource "aws_api_gateway_method" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
http_method = "GET"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
http_method = "${aws_api_gateway_method.test.http_method}"
type = "MOCK"
}
resource "aws_api_gateway_deployment" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
stage_name = "test"
depends_on = ["aws_api_gateway_integration.test"]
}
resource "aws_api_gateway_base_path_mapping" "test" {
api_id = "${aws_api_gateway_rest_api.test.id}"
base_path = ""
stage_name = "${aws_api_gateway_deployment.test.stage_name}"
domain_name = "${aws_api_gateway_domain_name.test.domain_name}"
}
resource "aws_api_gateway_domain_name" "test" {
domain_name = "%s"
certificate_name = "tf-apigateway-base-path-mapping-test"
certificate_body = <<EOF
%vEOF
certificate_chain = <<EOF
%vEOF
certificate_private_key = <<EOF
%vEOF
}
`, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey)
}