From 054e20c46881622123ec24d6dcd1ecaa2be4ba21 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Wed, 15 Aug 2018 18:41:44 -0400 Subject: [PATCH] resource/aws_api_gateway_base_path_mapping: Support resource import * Support, test, and document resource import via ID * Fix CheckDestroy function handling to check aws_api_gateway_base_path_mapping resources --- ...ource_aws_api_gateway_base_path_mapping.go | 50 +++++--- ..._aws_api_gateway_base_path_mapping_test.go | 115 ++++++++++++------ ...pi_gateway_base_path_mapping.html.markdown | 16 +++ 3 files changed, 129 insertions(+), 52 deletions(-) diff --git a/aws/resource_aws_api_gateway_base_path_mapping.go b/aws/resource_aws_api_gateway_base_path_mapping.go index ed31dc67b5f..dfbcd704136 100644 --- a/aws/resource_aws_api_gateway_base_path_mapping.go +++ b/aws/resource_aws_api_gateway_base_path_mapping.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -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": { @@ -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{ @@ -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) @@ -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), }) @@ -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 +} diff --git a/aws/resource_aws_api_gateway_base_path_mapping_test.go b/aws/resource_aws_api_gateway_base_path_mapping_test.go index 9cd09b8886f..849ead6c61d 100644 --- a/aws/resource_aws_api_gateway_base_path_mapping_test.go +++ b/aws/resource_aws_api_gateway_base_path_mapping_test.go @@ -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 @@ -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 @@ -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, + }, }, }) } @@ -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 { @@ -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" { diff --git a/website/docs/r/api_gateway_base_path_mapping.html.markdown b/website/docs/r/api_gateway_base_path_mapping.html.markdown index 9a838f490e2..70de3fe7ae5 100644 --- a/website/docs/r/api_gateway_base_path_mapping.html.markdown +++ b/website/docs/r/api_gateway_base_path_mapping.html.markdown @@ -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 +```