Skip to content

Commit

Permalink
Merge pull request #5566 from terraform-providers/f-aws_api_gateway_b…
Browse files Browse the repository at this point in the history
…ase_path_mapping-import

resource/aws_api_gateway_base_path_mapping: Support resource import
  • Loading branch information
bflad authored Aug 20, 2018
2 parents a6ce69a + 054e20c commit e9c0ccb
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 52 deletions.
50 changes: 35 additions & 15 deletions aws/resource_aws_api_gateway_base_path_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -19,6 +20,9 @@ func resourceAwsApiGatewayBasePathMapping() *schema.Resource {
Create: resourceAwsApiGatewayBasePathMappingCreate,
Read: resourceAwsApiGatewayBasePathMappingRead,
Delete: resourceAwsApiGatewayBasePathMappingDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"api_id": {
Expand Down Expand Up @@ -82,15 +86,9 @@ func resourceAwsApiGatewayBasePathMappingCreate(d *schema.ResourceData, meta int
func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway

domainName := d.Get("domain_name").(string)
basePath := d.Get("base_path").(string)

if domainName == "" {
return nil
}

if basePath == "" {
basePath = emptyBasePathMappingValue
domainName, basePath, err := decodeApiGatewayBasePathMappingId(d.Id())
if err != nil {
return err
}

mapping, err := conn.GetBasePathMapping(&apigateway.GetBasePathMappingInput{
Expand All @@ -114,6 +112,7 @@ func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta inter
}

d.Set("base_path", mappingBasePath)
d.Set("domain_name", domainName)
d.Set("api_id", mapping.RestApiId)
d.Set("stage_name", mapping.Stage)

Expand All @@ -123,14 +122,13 @@ 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
domainName, basePath, err := decodeApiGatewayBasePathMappingId(d.Id())
if err != nil {
return err
}

_, err := conn.DeleteBasePathMapping(&apigateway.DeleteBasePathMappingInput{
DomainName: aws.String(d.Get("domain_name").(string)),
_, err = conn.DeleteBasePathMapping(&apigateway.DeleteBasePathMappingInput{
DomainName: aws.String(domainName),
BasePath: aws.String(basePath),
})

Expand All @@ -144,3 +142,25 @@ func resourceAwsApiGatewayBasePathMappingDelete(d *schema.ResourceData, meta int

return nil
}

func decodeApiGatewayBasePathMappingId(id string) (string, string, error) {
idFormatErr := fmt.Errorf("Unexpected format of ID (%q), expected DOMAIN/BASEPATH", id)

parts := strings.SplitN(id, "/", 2)
if len(parts) != 2 {
return "", "", idFormatErr
}

domainName := parts[0]
basePath := parts[1]

if domainName == "" {
return "", "", idFormatErr
}

if basePath == "" {
basePath = emptyBasePathMappingValue
}

return domainName, basePath, nil
}
115 changes: 78 additions & 37 deletions aws/resource_aws_api_gateway_base_path_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,59 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSAPIGatewayBasePath_basic(t *testing.T) {
func TestDecodeApiGatewayBasePathMappingId(t *testing.T) {
var testCases = []struct {
Input string
DomainName string
BasePath string
ErrCount int
}{
{
Input: "no-slash",
ErrCount: 1,
},
{
Input: "/missing-domain-name",
ErrCount: 1,
},
{
Input: "domain-name/base-path",
DomainName: "domain-name",
BasePath: "base-path",
ErrCount: 0,
},
{
Input: "domain-name/base/path",
DomainName: "domain-name",
BasePath: "base/path",
ErrCount: 0,
},
{
Input: "domain-name/",
DomainName: "domain-name",
BasePath: emptyBasePathMappingValue,
ErrCount: 0,
},
}

for _, tc := range testCases {
domainName, basePath, err := decodeApiGatewayBasePathMappingId(tc.Input)
if tc.ErrCount == 0 && err != nil {
t.Fatalf("expected %q not to trigger an error, received: %s", tc.Input, err)
}
if tc.ErrCount > 0 && err == nil {
t.Fatalf("expected %q to trigger an error", tc.Input)
}
if domainName != tc.DomainName {
t.Fatalf("expected domain name %q to be %q", domainName, tc.DomainName)
}
if basePath != tc.BasePath {
t.Fatalf("expected base path %q to be %q", basePath, tc.BasePath)
}
}
}

func TestAccAWSAPIGatewayBasePathMapping_basic(t *testing.T) {
var conf apigateway.BasePathMapping

// Our test cert is for a wildcard on this domain
Expand All @@ -29,12 +81,17 @@ func TestAccAWSAPIGatewayBasePath_basic(t *testing.T) {
testAccCheckAWSAPIGatewayBasePathExists("aws_api_gateway_base_path_mapping.test", name, &conf),
),
},
{
ResourceName: "aws_api_gateway_base_path_mapping.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

// https://github.com/hashicorp/terraform/issues/9212
func TestAccAWSAPIGatewayEmptyBasePath_basic(t *testing.T) {
func TestAccAWSAPIGatewayBasePathMapping_BasePath_Empty(t *testing.T) {
var conf apigateway.BasePathMapping

// Our test cert is for a wildcard on this domain
Expand All @@ -48,9 +105,14 @@ func TestAccAWSAPIGatewayEmptyBasePath_basic(t *testing.T) {
{
Config: testAccAWSAPIGatewayEmptyBasePathConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayEmptyBasePathExists("aws_api_gateway_base_path_mapping.test", name, &conf),
testAccCheckAWSAPIGatewayBasePathExists("aws_api_gateway_base_path_mapping.test", name, &conf),
),
},
{
ResourceName: "aws_api_gateway_base_path_mapping.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -68,41 +130,14 @@ func testAccCheckAWSAPIGatewayBasePathExists(n string, name string, res *apigate

conn := testAccProvider.Meta().(*AWSClient).apigateway

req := &apigateway.GetBasePathMappingInput{
DomainName: aws.String(name),
BasePath: aws.String("tf-acc"),
}
describe, err := conn.GetBasePathMapping(req)
domainName, basePath, err := decodeApiGatewayBasePathMappingId(rs.Primary.ID)
if err != nil {
return err
}

if *describe.BasePath != "tf-acc" {
return fmt.Errorf("base path mapping not found")
}

*res = *describe

return nil
}
}

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(""),
DomainName: aws.String(domainName),
BasePath: aws.String(basePath),
}
describe, err := conn.GetBasePathMapping(req)
if err != nil {
Expand All @@ -120,14 +155,20 @@ func testAccCheckAWSAPIGatewayBasePathDestroy(name string) resource.TestCheckFun
conn := testAccProvider.Meta().(*AWSClient).apigateway

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_api_gateway_rest_api" {
if rs.Type != "aws_api_gateway_base_path_mapping" {
continue
}

req := &apigateway.GetBasePathMappingsInput{
DomainName: aws.String(name),
domainName, basePath, err := decodeApiGatewayBasePathMappingId(rs.Primary.ID)
if err != nil {
return err
}

req := &apigateway.GetBasePathMappingInput{
DomainName: aws.String(domainName),
BasePath: aws.String(basePath),
}
_, err := conn.GetBasePathMappings(req)
_, err = conn.GetBasePathMapping(req)

if err != nil {
if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" {
Expand Down
16 changes: 16 additions & 0 deletions website/docs/r/api_gateway_base_path_mapping.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ The following arguments are supported:
* `api_id` - (Required) The id of the API to connect.
* `stage_name` - (Optional) The name of a specific deployment stage to expose at the given path. If omitted, callers may select any stage by including its name as a path element after the base path.
* `base_path` - (Optional) Path segment that must be prepended to the path when accessing the API via this mapping. If omitted, the API is exposed at the root of the given domain.

## Import

`aws_api_gateway_base_path_mapping` can be imported by using the domain name and base path, e.g.

For empty `base_path` (e.g. root path (`/`)):

```
$ terraform import aws_api_gateway_base_path_mapping.example example.com/
```

Otherwise:

```
$ terraform import aws_api_gateway_base_path_mapping.example example.com/base-path
```

0 comments on commit e9c0ccb

Please sign in to comment.