From 2be1b48e84a0fb2d5a0d5908329d190372c849b7 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Mon, 6 Jan 2020 15:17:42 +0200 Subject: [PATCH] resource/aws_api_gateway_usage_plan_key: Support resource import (#11439) Output from acceptance testing: ``` --- PASS: TestAccAWSAPIGatewayUsagePlanKey_disappears (21.37s) --- PASS: TestAccAWSAPIGatewayUsagePlanKey_basic (106.97s) ``` --- ...resource_aws_api_gateway_usage_plan_key.go | 20 +++++- ...rce_aws_api_gateway_usage_plan_key_test.go | 67 ++++++++++++++++++- .../api_gateway_usage_plan_key.html.markdown | 8 +++ 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/aws/resource_aws_api_gateway_usage_plan_key.go b/aws/resource_aws_api_gateway_usage_plan_key.go index 7900f1583a3..dbc41192707 100644 --- a/aws/resource_aws_api_gateway_usage_plan_key.go +++ b/aws/resource_aws_api_gateway_usage_plan_key.go @@ -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" @@ -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": { @@ -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 @@ -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 } @@ -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 { diff --git a/aws/resource_aws_api_gateway_usage_plan_key_test.go b/aws/resource_aws_api_gateway_usage_plan_key_test.go index a0647619128..411681fd01b 100644 --- a/aws/resource_aws_api_gateway_usage_plan_key_test.go +++ b/aws/resource_aws_api_gateway_usage_plan_key_test.go @@ -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{ @@ -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( @@ -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] @@ -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 } @@ -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" { diff --git a/website/docs/r/api_gateway_usage_plan_key.html.markdown b/website/docs/r/api_gateway_usage_plan_key.html.markdown index 62e76fa45fe..31178769505 100644 --- a/website/docs/r/api_gateway_usage_plan_key.html.markdown +++ b/website/docs/r/api_gateway_usage_plan_key.html.markdown @@ -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 +``` \ No newline at end of file