Skip to content

Commit

Permalink
resource/aws_api_gateway_usage_plan_key: Support resource import (#11439
Browse files Browse the repository at this point in the history
)

Output from acceptance testing:

```
--- PASS: TestAccAWSAPIGatewayUsagePlanKey_disappears (21.37s)
--- PASS: TestAccAWSAPIGatewayUsagePlanKey_basic (106.97s)
```
  • Loading branch information
DrFaust92 authored and bflad committed Jan 6, 2020
1 parent 67b3a26 commit 2be1b48
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
20 changes: 18 additions & 2 deletions aws/resource_aws_api_gateway_usage_plan_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -15,6 +16,20 @@ func resourceAwsApiGatewayUsagePlanKey() *schema.Resource {
Create: resourceAwsApiGatewayUsagePlanKeyCreate,
Read: resourceAwsApiGatewayUsagePlanKeyRead,
Delete: resourceAwsApiGatewayUsagePlanKeyDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected USAGE-PLAN-ID/USAGE-PLAN-KEY-ID", d.Id())
}
usagePlanId := idParts[0]
usagePlanKeyId := idParts[1]
d.Set("usage_plan_id", usagePlanId)
d.Set("key_id", usagePlanKeyId)
d.SetId(usagePlanKeyId)
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"key_id": {
Expand Down Expand Up @@ -77,7 +92,7 @@ func resourceAwsApiGatewayUsagePlanKeyRead(d *schema.ResourceData, meta interfac
KeyId: aws.String(d.Get("key_id").(string)),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == apigateway.ErrCodeNotFoundException {
log.Printf("[WARN] API Gateway Usage Plan Key (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
Expand All @@ -87,6 +102,7 @@ func resourceAwsApiGatewayUsagePlanKeyRead(d *schema.ResourceData, meta interfac

d.Set("name", up.Name)
d.Set("value", up.Value)
d.Set("key_type", up.Type)

return nil
}
Expand All @@ -99,7 +115,7 @@ func resourceAwsApiGatewayUsagePlanKeyDelete(d *schema.ResourceData, meta interf
UsagePlanId: aws.String(d.Get("usage_plan_id").(string)),
KeyId: aws.String(d.Get("key_id").(string)),
})
if isAWSErr(err, "NotFoundException", "") {
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") {
return nil
}
if err != nil {
Expand Down
67 changes: 64 additions & 3 deletions aws/resource_aws_api_gateway_usage_plan_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

func TestAccAWSAPIGatewayUsagePlanKey_basic(t *testing.T) {
var conf apigateway.UsagePlanKey
rName := acctest.RandString(10)
updatedName := acctest.RandString(10)
rName := acctest.RandomWithPrefix("tf-acc-test")
updatedName := acctest.RandomWithPrefix("tf-acc-test-updated")
resourceName := "aws_api_gateway_usage_plan_key.main"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -36,6 +36,12 @@ func TestAccAWSAPIGatewayUsagePlanKey_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "value", ""),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccCheckAWSAPIGatewayUsagePlanKeyImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
{
Config: testAccAWSApiGatewayUsagePlanKeyBasicUpdatedConfig(updatedName),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -64,6 +70,28 @@ func TestAccAWSAPIGatewayUsagePlanKey_basic(t *testing.T) {
})
}

func TestAccAWSAPIGatewayUsagePlanKey_disappears(t *testing.T) {
var conf apigateway.UsagePlanKey
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_api_gateway_usage_plan_key.main"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayUsagePlanKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSApiGatewayUsagePlanKeyBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayUsagePlanKeyExists(resourceName, &conf),
testAccCheckAWSAPIGatewayUsagePlanKeyDisappears(resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckAWSAPIGatewayUsagePlanKeyExists(n string, res *apigateway.UsagePlanKey) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -122,7 +150,7 @@ func testAccCheckAWSAPIGatewayUsagePlanKeyDestroy(s *terraform.State) error {
if !ok {
return err
}
if aws2err.Code() != "NotFoundException" {
if aws2err.Code() != apigateway.ErrCodeNotFoundException {
return err
}

Expand All @@ -132,6 +160,39 @@ func testAccCheckAWSAPIGatewayUsagePlanKeyDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSAPIGatewayUsagePlanKeyDisappears(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No resource ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).apigateway

input := &apigateway.DeleteUsagePlanKeyInput{
KeyId: aws.String(rs.Primary.ID),
UsagePlanId: aws.String(rs.Primary.Attributes["usage_plan_id"]),
}
_, err := conn.DeleteUsagePlanKey(input)

return err
}
}

func testAccCheckAWSAPIGatewayUsagePlanKeyImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s/%s", rs.Primary.Attributes["usage_plan_id"], rs.Primary.ID), nil
}
}

func testAccAWSAPIGatewayUsagePlanKeyConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/api_gateway_usage_plan_key.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ In addition to all arguments above, the following attributes are exported:
* `usage_plan_id` - The ID of the API resource
* `name` - The name of a usage plan key.
* `value` - The value of a usage plan key.

## Import

AWS API Gateway Usage Plan Key can be imported using the `USAGE-PLAN-ID/USAGE-PLAN-KEY-ID`, e.g.

```sh
$ terraform import aws_api_gateway_usage_plan_key.key 12345abcde/zzz
```

0 comments on commit 2be1b48

Please sign in to comment.