From 60418c6d18d1200f4617811b61fa557f5f5779c0 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sun, 20 Oct 2019 17:26:32 +0300 Subject: [PATCH 1/3] Add tag support to api gateway usage plan --- aws/provider_test.go | 20 ++ aws/resource_aws_api_gateway_usage_plan.go | 30 +++ ...esource_aws_api_gateway_usage_plan_test.go | 221 ++++++++++++------ .../r/api_gateway_usage_plan.html.markdown | 2 + 4 files changed, 200 insertions(+), 73 deletions(-) diff --git a/aws/provider_test.go b/aws/provider_test.go index c98da9c9d91d..7ab6632fa66f 100644 --- a/aws/provider_test.go +++ b/aws/provider_test.go @@ -128,6 +128,26 @@ func testAccMatchResourceAttrRegionalARN(resourceName, attributeName, arnService } } +// testAccMatchResourceAttrRegionalARN ensures the Terraform state regexp matches a formatted ARN with region and no account id +func testAccMatchResourceAttrRegionalARNNoAccount(resourceName, attributeName, arnService string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc { + return func(s *terraform.State) error { + arnRegexp := arn.ARN{ + Partition: testAccGetPartition(), + Region: testAccGetRegion(), + Resource: arnResourceRegexp.String(), + Service: arnService, + }.String() + + attributeMatch, err := regexp.Compile(arnRegexp) + + if err != nil { + return fmt.Errorf("Unable to compile ARN regexp (%s): %s", arnRegexp, err) + } + + return resource.TestMatchResourceAttr(resourceName, attributeName, attributeMatch)(s) + } +} + // testAccCheckResourceAttrGlobalARN ensures the Terraform state exactly matches a formatted ARN without region func testAccCheckResourceAttrGlobalARN(resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc { return func(s *terraform.State) error { diff --git a/aws/resource_aws_api_gateway_usage_plan.go b/aws/resource_aws_api_gateway_usage_plan.go index 11f4d2dad5b6..3cfc6e69423c 100644 --- a/aws/resource_aws_api_gateway_usage_plan.go +++ b/aws/resource_aws_api_gateway_usage_plan.go @@ -7,10 +7,12 @@ import ( "strconv" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/apigateway" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsApiGatewayUsagePlan() *schema.Resource { @@ -107,6 +109,11 @@ func resourceAwsApiGatewayUsagePlan() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "tags": tagsSchema(), + "arn": { + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -197,6 +204,10 @@ func resourceAwsApiGatewayUsagePlanCreate(d *schema.ResourceData, meta interface params.Throttle = ts } + if v, ok := d.GetOk("tags"); ok { + params.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().ApigatewayTags() + } + up, err := conn.CreateUsagePlan(params) if err != nil { return fmt.Errorf("Error creating API Gateway Usage Plan: %s", err) @@ -243,6 +254,18 @@ func resourceAwsApiGatewayUsagePlanRead(d *schema.ResourceData, meta interface{} return err } + if err := d.Set("tags", keyvaluetags.ApigatewayKeyValueTags(up.Tags).IgnoreAws().Map()); err != nil { + return fmt.Errorf("error setting tags: %s", err) + } + + arn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Service: "apigateway", + Region: meta.(*AWSClient).region, + Resource: fmt.Sprintf("/usageplans/%s", d.Id()), + }.String() + d.Set("arn", arn) + d.Set("name", up.Name) d.Set("description", up.Description) d.Set("product_code", up.ProductCode) @@ -446,6 +469,13 @@ func resourceAwsApiGatewayUsagePlanUpdate(d *schema.ResourceData, meta interface } } + if d.HasChange("tags") { + o, n := d.GetChange("tags") + if err := keyvaluetags.ApigatewayUpdateTags(conn, d.Get("arn").(string), o, n); err != nil { + return fmt.Errorf("error updating tags: %s", err) + } + } + params := &apigateway.UpdateUsagePlanInput{ UsagePlanId: aws.String(d.Id()), PatchOperations: operations, diff --git a/aws/resource_aws_api_gateway_usage_plan_test.go b/aws/resource_aws_api_gateway_usage_plan_test.go index 1183edfaf140..0c78425f0021 100644 --- a/aws/resource_aws_api_gateway_usage_plan_test.go +++ b/aws/resource_aws_api_gateway_usage_plan_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -38,6 +39,7 @@ func TestAccAWSAPIGatewayUsagePlan_basic(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) updatedName := acctest.RandString(10) + resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -47,24 +49,66 @@ func TestAccAWSAPIGatewayUsagePlan_basic(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + testAccMatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "apigateway", regexp.MustCompile(`/usageplans/+.`)), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "description", ""), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicUpdatedConfig(updatedName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", updatedName), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), + resource.TestCheckResourceAttr(resourceName, "name", updatedName), + resource.TestCheckResourceAttr(resourceName, "description", ""), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "description", ""), + ), + }, + }, + }) +} + +func TestAccAWSAPIGatewayUsagePlan_tags(t *testing.T) { + var conf apigateway.UsagePlan + name := acctest.RandString(10) + resourceName := "aws_api_gateway_usage_plan.main" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayUsagePlanDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSApiGatewayUsagePlanBasicTags1(name, "key1", "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), + ), + }, + { + Config: testAccAWSApiGatewayUsagePlanBasicTags2(name, "key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccAWSApiGatewayUsagePlanBasicTags1(name, "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), ), }, }, @@ -74,6 +118,7 @@ func TestAccAWSAPIGatewayUsagePlan_basic(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_description(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) + resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -83,35 +128,35 @@ func TestAccAWSAPIGatewayUsagePlan_description(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "description", ""), ), }, { Config: testAccAWSApiGatewayUsagePlanDescriptionConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", "This is a description"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "description", "This is a description"), ), }, { Config: testAccAWSApiGatewayUsagePlanDescriptionUpdatedConfig(name), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", "This is a new description"), + resource.TestCheckResourceAttr(resourceName, "description", "This is a new description"), ), }, { Config: testAccAWSApiGatewayUsagePlanDescriptionConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", "This is a description"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "description", "This is a description"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "description", ""), ), }, }, @@ -121,6 +166,7 @@ func TestAccAWSAPIGatewayUsagePlan_description(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_productCode(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) + resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -130,35 +176,35 @@ func TestAccAWSAPIGatewayUsagePlan_productCode(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", ""), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "product_code", ""), ), }, { Config: testAccAWSApiGatewayUsagePlanProductCodeConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", "MYCODE"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "product_code", "MYCODE"), ), }, { Config: testAccAWSApiGatewayUsagePlanProductCodeUpdatedConfig(name), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", "MYCODE2"), + resource.TestCheckResourceAttr(resourceName, "product_code", "MYCODE2"), ), }, { Config: testAccAWSApiGatewayUsagePlanProductCodeConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", "MYCODE"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "product_code", "MYCODE"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", ""), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "product_code", ""), ), }, }, @@ -168,6 +214,7 @@ func TestAccAWSAPIGatewayUsagePlan_productCode(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_throttling(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) + resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -177,35 +224,35 @@ func TestAccAWSAPIGatewayUsagePlan_throttling(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckNoResourceAttr(resourceName, "throttle_settings"), ), }, { Config: testAccAWSApiGatewayUsagePlanThrottlingConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.4173790118.burst_limit", "2"), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.4173790118.rate_limit", "5"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "throttle_settings.4173790118.burst_limit", "2"), + resource.TestCheckResourceAttr(resourceName, "throttle_settings.4173790118.rate_limit", "5"), ), }, { Config: testAccAWSApiGatewayUsagePlanThrottlingModifiedConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.1779463053.burst_limit", "3"), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.1779463053.rate_limit", "6"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "throttle_settings.1779463053.burst_limit", "3"), + resource.TestCheckResourceAttr(resourceName, "throttle_settings.1779463053.rate_limit", "6"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckNoResourceAttr(resourceName, "throttle_settings"), ), }, }, @@ -216,6 +263,7 @@ func TestAccAWSAPIGatewayUsagePlan_throttling(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_throttlingInitialRateLimit(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) + resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -225,8 +273,8 @@ func TestAccAWSAPIGatewayUsagePlan_throttlingInitialRateLimit(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanThrottlingConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.4173790118.rate_limit", "5"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "throttle_settings.4173790118.rate_limit", "5"), ), }, }, @@ -236,6 +284,7 @@ func TestAccAWSAPIGatewayUsagePlan_throttlingInitialRateLimit(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_quota(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) + resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -245,37 +294,37 @@ func TestAccAWSAPIGatewayUsagePlan_quota(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckNoResourceAttr(resourceName, "quota_settings"), ), }, { Config: testAccAWSApiGatewayUsagePlanQuotaConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.1956747625.limit", "100"), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.1956747625.offset", "6"), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.1956747625.period", "WEEK"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "quota_settings.1956747625.limit", "100"), + resource.TestCheckResourceAttr(resourceName, "quota_settings.1956747625.offset", "6"), + resource.TestCheckResourceAttr(resourceName, "quota_settings.1956747625.period", "WEEK"), ), }, { Config: testAccAWSApiGatewayUsagePlanQuotaModifiedConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.3909168194.limit", "200"), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.3909168194.offset", "20"), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.3909168194.period", "MONTH"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "quota_settings.3909168194.limit", "200"), + resource.TestCheckResourceAttr(resourceName, "quota_settings.3909168194.offset", "20"), + resource.TestCheckResourceAttr(resourceName, "quota_settings.3909168194.period", "MONTH"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckNoResourceAttr(resourceName, "quota_settings"), ), }, }, @@ -285,6 +334,7 @@ func TestAccAWSAPIGatewayUsagePlan_quota(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_apiStages(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) + resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -296,44 +346,44 @@ func TestAccAWSAPIGatewayUsagePlan_apiStages(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanApiStagesConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "api_stages.0.stage", "test"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "api_stages.0.stage", "test"), ), }, // Handle api stages removal { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "api_stages"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckNoResourceAttr(resourceName, "api_stages"), ), }, // Handle api stages additions { Config: testAccAWSApiGatewayUsagePlanApiStagesConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "api_stages.0.stage", "test"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "api_stages.0.stage", "test"), ), }, // Handle api stages updates { Config: testAccAWSApiGatewayUsagePlanApiStagesModifiedConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "api_stages.0.stage", "foo"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "api_stages.0.stage", "foo"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), - resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), - resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "api_stages"), + testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckNoResourceAttr(resourceName, "api_stages"), ), }, }, @@ -475,6 +525,31 @@ resource "aws_api_gateway_usage_plan" "main" { `, rName) } +func testAccAWSApiGatewayUsagePlanBasicTags1(rName, tagKey1, tagValue1 string) string { + return fmt.Sprintf(testAccAWSAPIGatewayUsagePlanConfig+` +resource "aws_api_gateway_usage_plan" "main" { + name = "%s" + + tags = { + %q = %q + } +} +`, rName, tagKey1, tagValue1) +} + +func testAccAWSApiGatewayUsagePlanBasicTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(testAccAWSAPIGatewayUsagePlanConfig+` +resource "aws_api_gateway_usage_plan" "main" { + name = "%s" + + tags = { + %q = %q + %q = %q + } +} +`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +} + func testAccAWSApiGatewayUsagePlanDescriptionConfig(rName string) string { return fmt.Sprintf(testAccAWSAPIGatewayUsagePlanConfig+` resource "aws_api_gateway_usage_plan" "main" { diff --git a/website/docs/r/api_gateway_usage_plan.html.markdown b/website/docs/r/api_gateway_usage_plan.html.markdown index afb9b7c9e036..e8b66dc085fd 100644 --- a/website/docs/r/api_gateway_usage_plan.html.markdown +++ b/website/docs/r/api_gateway_usage_plan.html.markdown @@ -68,6 +68,7 @@ The API Gateway Usage Plan argument layout is a structure composed of several su * `quota_settings` - (Optional) The [quota settings](#quota-settings-arguments) of the usage plan. * `throttle_settings` - (Optional) The [throttling limits](#throttling-settings-arguments) of the usage plan. * `product_code` - (Optional) The AWS Markeplace product identifier to associate with the usage plan as a SaaS product on AWS Marketplace. +* `tags` - (Optional) Key-value mapping of resource tags #### Api Stages arguments @@ -96,6 +97,7 @@ In addition to all arguments above, the following attributes are exported: * `quota_settings` - The quota of the usage plan. * `throttle_settings` - The throttling limits of the usage plan. * `product_code` - The AWS Markeplace product identifier to associate with the usage plan as a SaaS product on AWS Marketplace. +* `arn` - Amazon Resource Name (ARN) ## Import From 61eddeee2a95152e85d56ebab97ac24369ddadd6 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sun, 20 Oct 2019 17:50:35 +0300 Subject: [PATCH 2/3] Revert "Add tag support to api gateway usage plan" This reverts commit 60418c6d --- aws/resource_aws_api_gateway_usage_plan.go | 30 --- ...esource_aws_api_gateway_usage_plan_test.go | 221 ++++++------------ .../r/api_gateway_usage_plan.html.markdown | 2 - 3 files changed, 73 insertions(+), 180 deletions(-) diff --git a/aws/resource_aws_api_gateway_usage_plan.go b/aws/resource_aws_api_gateway_usage_plan.go index 3cfc6e69423c..11f4d2dad5b6 100644 --- a/aws/resource_aws_api_gateway_usage_plan.go +++ b/aws/resource_aws_api_gateway_usage_plan.go @@ -7,12 +7,10 @@ import ( "strconv" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/apigateway" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsApiGatewayUsagePlan() *schema.Resource { @@ -109,11 +107,6 @@ func resourceAwsApiGatewayUsagePlan() *schema.Resource { Type: schema.TypeString, Optional: true, }, - "tags": tagsSchema(), - "arn": { - Type: schema.TypeString, - Computed: true, - }, }, } } @@ -204,10 +197,6 @@ func resourceAwsApiGatewayUsagePlanCreate(d *schema.ResourceData, meta interface params.Throttle = ts } - if v, ok := d.GetOk("tags"); ok { - params.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().ApigatewayTags() - } - up, err := conn.CreateUsagePlan(params) if err != nil { return fmt.Errorf("Error creating API Gateway Usage Plan: %s", err) @@ -254,18 +243,6 @@ func resourceAwsApiGatewayUsagePlanRead(d *schema.ResourceData, meta interface{} return err } - if err := d.Set("tags", keyvaluetags.ApigatewayKeyValueTags(up.Tags).IgnoreAws().Map()); err != nil { - return fmt.Errorf("error setting tags: %s", err) - } - - arn := arn.ARN{ - Partition: meta.(*AWSClient).partition, - Service: "apigateway", - Region: meta.(*AWSClient).region, - Resource: fmt.Sprintf("/usageplans/%s", d.Id()), - }.String() - d.Set("arn", arn) - d.Set("name", up.Name) d.Set("description", up.Description) d.Set("product_code", up.ProductCode) @@ -469,13 +446,6 @@ func resourceAwsApiGatewayUsagePlanUpdate(d *schema.ResourceData, meta interface } } - if d.HasChange("tags") { - o, n := d.GetChange("tags") - if err := keyvaluetags.ApigatewayUpdateTags(conn, d.Get("arn").(string), o, n); err != nil { - return fmt.Errorf("error updating tags: %s", err) - } - } - params := &apigateway.UpdateUsagePlanInput{ UsagePlanId: aws.String(d.Id()), PatchOperations: operations, diff --git a/aws/resource_aws_api_gateway_usage_plan_test.go b/aws/resource_aws_api_gateway_usage_plan_test.go index 0c78425f0021..1183edfaf140 100644 --- a/aws/resource_aws_api_gateway_usage_plan_test.go +++ b/aws/resource_aws_api_gateway_usage_plan_test.go @@ -2,7 +2,6 @@ package aws import ( "fmt" - "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -39,7 +38,6 @@ func TestAccAWSAPIGatewayUsagePlan_basic(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) updatedName := acctest.RandString(10) - resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -49,66 +47,24 @@ func TestAccAWSAPIGatewayUsagePlan_basic(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - testAccMatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "apigateway", regexp.MustCompile(`/usageplans/+.`)), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "description", ""), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicUpdatedConfig(updatedName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "name", updatedName), - resource.TestCheckResourceAttr(resourceName, "description", ""), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", updatedName), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "description", ""), - ), - }, - }, - }) -} - -func TestAccAWSAPIGatewayUsagePlan_tags(t *testing.T) { - var conf apigateway.UsagePlan - name := acctest.RandString(10) - resourceName := "aws_api_gateway_usage_plan.main" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSAPIGatewayUsagePlanDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSApiGatewayUsagePlanBasicTags1(name, "key1", "value1"), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), - resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), - ), - }, - { - Config: testAccAWSApiGatewayUsagePlanBasicTags2(name, "key1", "value1updated", "key2", "value2"), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), - resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), - resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), - ), - }, - { - Config: testAccAWSApiGatewayUsagePlanBasicTags1(name, "key2", "value2"), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), - resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), ), }, }, @@ -118,7 +74,6 @@ func TestAccAWSAPIGatewayUsagePlan_tags(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_description(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) - resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -128,35 +83,35 @@ func TestAccAWSAPIGatewayUsagePlan_description(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "description", ""), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), ), }, { Config: testAccAWSApiGatewayUsagePlanDescriptionConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "description", "This is a description"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", "This is a description"), ), }, { Config: testAccAWSApiGatewayUsagePlanDescriptionUpdatedConfig(name), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "description", "This is a new description"), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", "This is a new description"), ), }, { Config: testAccAWSApiGatewayUsagePlanDescriptionConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "description", "This is a description"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", "This is a description"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "description", ""), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "description", ""), ), }, }, @@ -166,7 +121,6 @@ func TestAccAWSAPIGatewayUsagePlan_description(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_productCode(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) - resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -176,35 +130,35 @@ func TestAccAWSAPIGatewayUsagePlan_productCode(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "product_code", ""), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", ""), ), }, { Config: testAccAWSApiGatewayUsagePlanProductCodeConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "product_code", "MYCODE"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", "MYCODE"), ), }, { Config: testAccAWSApiGatewayUsagePlanProductCodeUpdatedConfig(name), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "product_code", "MYCODE2"), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", "MYCODE2"), ), }, { Config: testAccAWSApiGatewayUsagePlanProductCodeConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "product_code", "MYCODE"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", "MYCODE"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "product_code", ""), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "product_code", ""), ), }, }, @@ -214,7 +168,6 @@ func TestAccAWSAPIGatewayUsagePlan_productCode(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_throttling(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) - resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -224,35 +177,35 @@ func TestAccAWSAPIGatewayUsagePlan_throttling(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckNoResourceAttr(resourceName, "throttle_settings"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings"), ), }, { Config: testAccAWSApiGatewayUsagePlanThrottlingConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "throttle_settings.4173790118.burst_limit", "2"), - resource.TestCheckResourceAttr(resourceName, "throttle_settings.4173790118.rate_limit", "5"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.4173790118.burst_limit", "2"), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.4173790118.rate_limit", "5"), ), }, { Config: testAccAWSApiGatewayUsagePlanThrottlingModifiedConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "throttle_settings.1779463053.burst_limit", "3"), - resource.TestCheckResourceAttr(resourceName, "throttle_settings.1779463053.rate_limit", "6"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.1779463053.burst_limit", "3"), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.1779463053.rate_limit", "6"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckNoResourceAttr(resourceName, "throttle_settings"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings"), ), }, }, @@ -263,7 +216,6 @@ func TestAccAWSAPIGatewayUsagePlan_throttling(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_throttlingInitialRateLimit(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) - resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -273,8 +225,8 @@ func TestAccAWSAPIGatewayUsagePlan_throttlingInitialRateLimit(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanThrottlingConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "throttle_settings.4173790118.rate_limit", "5"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "throttle_settings.4173790118.rate_limit", "5"), ), }, }, @@ -284,7 +236,6 @@ func TestAccAWSAPIGatewayUsagePlan_throttlingInitialRateLimit(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_quota(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) - resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -294,37 +245,37 @@ func TestAccAWSAPIGatewayUsagePlan_quota(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckNoResourceAttr(resourceName, "quota_settings"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings"), ), }, { Config: testAccAWSApiGatewayUsagePlanQuotaConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "quota_settings.1956747625.limit", "100"), - resource.TestCheckResourceAttr(resourceName, "quota_settings.1956747625.offset", "6"), - resource.TestCheckResourceAttr(resourceName, "quota_settings.1956747625.period", "WEEK"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.1956747625.limit", "100"), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.1956747625.offset", "6"), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.1956747625.period", "WEEK"), ), }, { Config: testAccAWSApiGatewayUsagePlanQuotaModifiedConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "quota_settings.3909168194.limit", "200"), - resource.TestCheckResourceAttr(resourceName, "quota_settings.3909168194.offset", "20"), - resource.TestCheckResourceAttr(resourceName, "quota_settings.3909168194.period", "MONTH"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.3909168194.limit", "200"), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.3909168194.offset", "20"), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings.3909168194.period", "MONTH"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckNoResourceAttr(resourceName, "quota_settings"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "quota_settings"), ), }, }, @@ -334,7 +285,6 @@ func TestAccAWSAPIGatewayUsagePlan_quota(t *testing.T) { func TestAccAWSAPIGatewayUsagePlan_apiStages(t *testing.T) { var conf apigateway.UsagePlan name := acctest.RandString(10) - resourceName := "aws_api_gateway_usage_plan.main" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -346,44 +296,44 @@ func TestAccAWSAPIGatewayUsagePlan_apiStages(t *testing.T) { { Config: testAccAWSApiGatewayUsagePlanApiStagesConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "api_stages.0.stage", "test"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "api_stages.0.stage", "test"), ), }, // Handle api stages removal { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckNoResourceAttr(resourceName, "api_stages"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "api_stages"), ), }, // Handle api stages additions { Config: testAccAWSApiGatewayUsagePlanApiStagesConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "api_stages.0.stage", "test"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "api_stages.0.stage", "test"), ), }, // Handle api stages updates { Config: testAccAWSApiGatewayUsagePlanApiStagesModifiedConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "api_stages.0.stage", "foo"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "api_stages.0.stage", "foo"), ), }, { Config: testAccAWSApiGatewayUsagePlanBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGatewayUsagePlanExists(resourceName, &conf), - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckNoResourceAttr(resourceName, "api_stages"), + testAccCheckAWSAPIGatewayUsagePlanExists("aws_api_gateway_usage_plan.main", &conf), + resource.TestCheckResourceAttr("aws_api_gateway_usage_plan.main", "name", name), + resource.TestCheckNoResourceAttr("aws_api_gateway_usage_plan.main", "api_stages"), ), }, }, @@ -525,31 +475,6 @@ resource "aws_api_gateway_usage_plan" "main" { `, rName) } -func testAccAWSApiGatewayUsagePlanBasicTags1(rName, tagKey1, tagValue1 string) string { - return fmt.Sprintf(testAccAWSAPIGatewayUsagePlanConfig+` -resource "aws_api_gateway_usage_plan" "main" { - name = "%s" - - tags = { - %q = %q - } -} -`, rName, tagKey1, tagValue1) -} - -func testAccAWSApiGatewayUsagePlanBasicTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { - return fmt.Sprintf(testAccAWSAPIGatewayUsagePlanConfig+` -resource "aws_api_gateway_usage_plan" "main" { - name = "%s" - - tags = { - %q = %q - %q = %q - } -} -`, rName, tagKey1, tagValue1, tagKey2, tagValue2) -} - func testAccAWSApiGatewayUsagePlanDescriptionConfig(rName string) string { return fmt.Sprintf(testAccAWSAPIGatewayUsagePlanConfig+` resource "aws_api_gateway_usage_plan" "main" { diff --git a/website/docs/r/api_gateway_usage_plan.html.markdown b/website/docs/r/api_gateway_usage_plan.html.markdown index e8b66dc085fd..afb9b7c9e036 100644 --- a/website/docs/r/api_gateway_usage_plan.html.markdown +++ b/website/docs/r/api_gateway_usage_plan.html.markdown @@ -68,7 +68,6 @@ The API Gateway Usage Plan argument layout is a structure composed of several su * `quota_settings` - (Optional) The [quota settings](#quota-settings-arguments) of the usage plan. * `throttle_settings` - (Optional) The [throttling limits](#throttling-settings-arguments) of the usage plan. * `product_code` - (Optional) The AWS Markeplace product identifier to associate with the usage plan as a SaaS product on AWS Marketplace. -* `tags` - (Optional) Key-value mapping of resource tags #### Api Stages arguments @@ -97,7 +96,6 @@ In addition to all arguments above, the following attributes are exported: * `quota_settings` - The quota of the usage plan. * `throttle_settings` - The throttling limits of the usage plan. * `product_code` - The AWS Markeplace product identifier to associate with the usage plan as a SaaS product on AWS Marketplace. -* `arn` - Amazon Resource Name (ARN) ## Import From c28c51559f07659984ec31a1900c536a3e551134 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sun, 20 Oct 2019 20:35:02 +0300 Subject: [PATCH 3/3] Add tag support to api gateway domain name - doc + fix tags --- aws/resource_aws_api_gateway_api_key.go | 29 +++++++- aws/resource_aws_api_gateway_api_key_test.go | 71 +++++++++++++++++++ .../docs/r/api_gateway_api_key.html.markdown | 2 + 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_api_gateway_api_key.go b/aws/resource_aws_api_gateway_api_key.go index 8655efe4e692..574576b867fc 100644 --- a/aws/resource_aws_api_gateway_api_key.go +++ b/aws/resource_aws_api_gateway_api_key.go @@ -6,9 +6,11 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/service/apigateway" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsApiGatewayApiKey() *schema.Resource { @@ -77,6 +79,11 @@ func resourceAwsApiGatewayApiKey() *schema.Resource { Sensitive: true, ValidateFunc: validation.StringLenBetween(30, 128), }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchema(), }, } } @@ -90,9 +97,10 @@ func resourceAwsApiGatewayApiKeyCreate(d *schema.ResourceData, meta interface{}) Description: aws.String(d.Get("description").(string)), Enabled: aws.Bool(d.Get("enabled").(bool)), Value: aws.String(d.Get("value").(string)), + Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().ApigatewayTags(), }) if err != nil { - return fmt.Errorf("Error creating API Gateway: %s", err) + return fmt.Errorf("Error creating API Gateway API Key: %s", err) } d.SetId(aws.StringValue(apiKey.Id)) @@ -118,6 +126,18 @@ func resourceAwsApiGatewayApiKeyRead(d *schema.ResourceData, meta interface{}) e return err } + if err := d.Set("tags", keyvaluetags.ApigatewayKeyValueTags(apiKey.Tags).IgnoreAws().Map()); err != nil { + return fmt.Errorf("error setting tags: %s", err) + } + + arn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Service: "apigateway", + Region: meta.(*AWSClient).region, + Resource: fmt.Sprintf("/apikeys/%s", d.Id()), + }.String() + d.Set("arn", arn) + d.Set("name", apiKey.Name) d.Set("description", apiKey.Description) d.Set("enabled", apiKey.Enabled) @@ -164,6 +184,13 @@ func resourceAwsApiGatewayApiKeyUpdate(d *schema.ResourceData, meta interface{}) log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id()) + if d.HasChange("tags") { + o, n := d.GetChange("tags") + if err := keyvaluetags.ApigatewayUpdateTags(conn, d.Get("arn").(string), o, n); err != nil { + return fmt.Errorf("error updating tags: %s", err) + } + } + _, err := conn.UpdateApiKey(&apigateway.UpdateApiKeyInput{ ApiKey: aws.String(d.Id()), PatchOperations: resourceAwsApiGatewayApiKeyUpdateOperations(d), diff --git a/aws/resource_aws_api_gateway_api_key_test.go b/aws/resource_aws_api_gateway_api_key_test.go index bf0d886d5425..1b32bf46da70 100644 --- a/aws/resource_aws_api_gateway_api_key_test.go +++ b/aws/resource_aws_api_gateway_api_key_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -26,6 +27,7 @@ func TestAccAWSAPIGatewayApiKey_basic(t *testing.T) { Config: testAccAWSAPIGatewayApiKeyConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey1), + testAccMatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "apigateway", regexp.MustCompile(`/apikeys/+.`)), resource.TestCheckResourceAttrSet(resourceName, "created_date"), resource.TestCheckResourceAttr(resourceName, "description", "Managed by Terraform"), resource.TestCheckResourceAttr(resourceName, "enabled", "true"), @@ -43,6 +45,50 @@ func TestAccAWSAPIGatewayApiKey_basic(t *testing.T) { }) } +func TestAccAWSAPIGatewayApiKey_Tags(t *testing.T) { + var apiKey1 apigateway.ApiKey + resourceName := "aws_api_gateway_api_key.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayApiKeyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayApiKeyConfigTags1(rName, "key1", "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey1), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), + ), + }, + { + Config: testAccAWSAPIGatewayApiKeyConfigTags2(rName, "key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey1), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccAWSAPIGatewayApiKeyConfigTags1(rName, "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey1), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccAWSAPIGatewayApiKey_Description(t *testing.T) { var apiKey1, apiKey2 apigateway.ApiKey resourceName := "aws_api_gateway_api_key.test" @@ -217,6 +263,31 @@ resource "aws_api_gateway_api_key" "test" { `, rName) } +func testAccAWSAPIGatewayApiKeyConfigTags1(rName, tagKey1, tagValue1 string) string { + return fmt.Sprintf(` +resource "aws_api_gateway_api_key" "test" { + name = %[1]q + + tags = { + %[2]q = %[3]q + } +} +`, rName, tagKey1, tagValue1) +} + +func testAccAWSAPIGatewayApiKeyConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` +resource "aws_api_gateway_api_key" "test" { + name = %[1]q + + tags = { + %[2]q = %[3]q + %[4]q = %[5]q + } +} +`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +} + func testAccAWSAPIGatewayApiKeyConfigDescription(rName, description string) string { return fmt.Sprintf(` resource "aws_api_gateway_api_key" "test" { diff --git a/website/docs/r/api_gateway_api_key.html.markdown b/website/docs/r/api_gateway_api_key.html.markdown index 06a338a45a6a..3281cfb3ff16 100644 --- a/website/docs/r/api_gateway_api_key.html.markdown +++ b/website/docs/r/api_gateway_api_key.html.markdown @@ -27,6 +27,7 @@ The following arguments are supported: * `description` - (Optional) The API key description. Defaults to "Managed by Terraform". * `enabled` - (Optional) Specifies whether the API key can be used by callers. Defaults to `true`. * `value` - (Optional) The value of the API key. If not specified, it will be automatically generated by AWS on creation. +* `tags` - (Optional) Key-value mapping of resource tags ## Attribute Reference @@ -36,6 +37,7 @@ In addition to all arguments above, the following attributes are exported: * `created_date` - The creation date of the API key * `last_updated_date` - The last update date of the API key * `value` - The value of the API key +* `arn` - Amazon Resource Name (ARN) ## Import