From 19c167d44e32ddea9328c1b1814237df98f1132d Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 1 Sep 2023 21:26:37 +0300 Subject: [PATCH 1/8] r/api_gateway_api_key - add cusomer key and make name updateable --- internal/service/apigateway/api_key.go | 52 +++++++++++++------ internal/service/apigateway/api_key_test.go | 16 +++++- .../docs/r/api_gateway_api_key.html.markdown | 10 ++-- 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/internal/service/apigateway/api_key.go b/internal/service/apigateway/api_key.go index 8ec40c0373f..a65db853897 100644 --- a/internal/service/apigateway/api_key.go +++ b/internal/service/apigateway/api_key.go @@ -38,34 +38,38 @@ func ResourceAPIKey() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "name": { + "arn": { Type: schema.TypeString, - Required: true, - ForceNew: true, + Computed: true, + }, + "created_date": { + Type: schema.TypeString, + Computed: true, + }, + "customer_id": { + Type: schema.TypeString, + Optional: true, }, - "description": { Type: schema.TypeString, Optional: true, Default: "Managed by Terraform", }, - "enabled": { Type: schema.TypeBool, Optional: true, Default: true, }, - - "created_date": { + "last_updated_date": { Type: schema.TypeString, Computed: true, }, - - "last_updated_date": { + "name": { Type: schema.TypeString, - Computed: true, + Required: true, }, - + names.AttrTags: tftags.TagsSchema(), + names.AttrTagsAll: tftags.TagsSchemaComputed(), "value": { Type: schema.TypeString, Optional: true, @@ -74,12 +78,6 @@ func ResourceAPIKey() *schema.Resource { Sensitive: true, ValidateFunc: validation.StringLenBetween(20, 128), }, - "arn": { - Type: schema.TypeString, - Computed: true, - }, - names.AttrTags: tftags.TagsSchema(), - names.AttrTagsAll: tftags.TagsSchemaComputed(), }, CustomizeDiff: verify.SetTagsDiff, @@ -99,6 +97,10 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta inte Value: aws.String(d.Get("value").(string)), } + if v, ok := d.GetOk("customer_id"); ok && v.(string) != "" { + input.CustomerId = aws.String(v.(string)) + } + apiKey, err := conn.CreateApiKeyWithContext(ctx, input) if err != nil { @@ -174,6 +176,22 @@ func resourceAPIKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchO }) } + if d.HasChange("name") { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String(apigateway.OpReplace), + Path: aws.String("/name"), + Value: aws.String(d.Get("name").(string)), + }) + } + + if d.HasChange("customerId") { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String(apigateway.OpReplace), + Path: aws.String("/name"), + Value: aws.String(d.Get("customer_id").(string)), + }) + } + return operations } diff --git a/internal/service/apigateway/api_key_test.go b/internal/service/apigateway/api_key_test.go index 2cb0e0c43c6..d2fdd7b16e2 100644 --- a/internal/service/apigateway/api_key_test.go +++ b/internal/service/apigateway/api_key_test.go @@ -22,9 +22,10 @@ import ( func TestAccAPIGatewayAPIKey_basic(t *testing.T) { ctx := acctest.Context(t) - var apiKey1 apigateway.ApiKey + var apiKey1, apiKey2 apigateway.ApiKey resourceName := "aws_api_gateway_api_key.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + rNameUpdated := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -50,6 +51,19 @@ func TestAccAPIGatewayAPIKey_basic(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + { + Config: testAccAPIKeyConfig_basic(rNameUpdated), + Check: resource.ComposeTestCheckFunc( + testAccCheckAPIKeyExists(ctx, resourceName, &apiKey2), + acctest.MatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "apigateway", regexache.MustCompile(`/apikeys/+.`)), + resource.TestCheckResourceAttrSet(resourceName, "created_date"), + resource.TestCheckResourceAttr(resourceName, "description", "Managed by Terraform"), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "name", rNameUpdated), + resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"), + resource.TestCheckResourceAttrSet(resourceName, "value"), + ), + }, }, }) } diff --git a/website/docs/r/api_gateway_api_key.html.markdown b/website/docs/r/api_gateway_api_key.html.markdown index 6ea047ff1dd..38672b55956 100644 --- a/website/docs/r/api_gateway_api_key.html.markdown +++ b/website/docs/r/api_gateway_api_key.html.markdown @@ -15,8 +15,8 @@ Provides an API Gateway API Key. ## Example Usage ```terraform -resource "aws_api_gateway_api_key" "MyDemoApiKey" { - name = "demo" +resource "aws_api_gateway_api_key" "example" { + name = "example" } ``` @@ -25,6 +25,7 @@ resource "aws_api_gateway_api_key" "MyDemoApiKey" { This resource supports the following arguments: * `name` - (Required) Name of the API key. +* `customer_id` - (Required) An Amazon Web Services Marketplace customer identifier, when integrating with the Amazon Web Services SaaS Marketplace. * `description` - (Optional) API key description. Defaults to "Managed by Terraform". * `enabled` - (Optional) Whether the API key can be used by callers. Defaults to `true`. * `value` - (Optional) Value of the API key. If specified, the value must be an alphanumeric string between 20 and 128 characters. If not specified, it will be automatically generated by AWS on creation. @@ -37,7 +38,6 @@ This resource exports the following attributes in addition to the arguments abov * `id` - ID of the API key * `created_date` - Creation date of the API key * `last_updated_date` - Last update date of the API key -* `value` - Value of the API key * `arn` - ARN * `tags_all` - Map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block). @@ -47,7 +47,7 @@ In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashico ```terraform import { - to = aws_api_gateway_api_key.my_demo_key + to = aws_api_gateway_api_key.example id = "8bklk8bl1k3sB38D9B3l0enyWT8c09B30lkq0blk" } ``` @@ -55,5 +55,5 @@ import { Using `terraform import`, import API Gateway Keys using the `id`. For example: ```console -% terraform import aws_api_gateway_api_key.my_demo_key 8bklk8bl1k3sB38D9B3l0enyWT8c09B30lkq0blk +% terraform import aws_api_gateway_api_key.example 8bklk8bl1k3sB38D9B3l0enyWT8c09B30lkq0blk ``` From d2cdd1a2a02000c35c0cb000333ff1c4228b3428 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 1 Sep 2023 21:29:58 +0300 Subject: [PATCH 2/8] changelog --- .changelog/33281.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changelog/33281.txt diff --git a/.changelog/33281.txt b/.changelog/33281.txt new file mode 100644 index 00000000000..188eb6e827d --- /dev/null +++ b/.changelog/33281.txt @@ -0,0 +1,7 @@ +```release-note:enhancement +resource/aws_api_gateway_api_key: Add support `customer_id` argument +``` + +```release-note:enhancement +resource/aws_api_gateway_api_key: Allow updating `name` +``` \ No newline at end of file From 8ef5d2abcfe27e95405578c59808c4625fa15bf9 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 1 Sep 2023 21:44:14 +0300 Subject: [PATCH 3/8] changelog --- internal/service/apigateway/api_key.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/apigateway/api_key.go b/internal/service/apigateway/api_key.go index a65db853897..775bd8ff9f6 100644 --- a/internal/service/apigateway/api_key.go +++ b/internal/service/apigateway/api_key.go @@ -97,7 +97,7 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta inte Value: aws.String(d.Get("value").(string)), } - if v, ok := d.GetOk("customer_id"); ok && v.(string) != "" { + if v, ok := d.GetOk("customer_id"); ok { input.CustomerId = aws.String(v.(string)) } From 539993f4f67d94f8e06ea78b2f1a455cc09f3ad3 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 4 Sep 2023 14:57:49 -0400 Subject: [PATCH 4/8] Tweak CHANGELOG entry. --- .changelog/33281.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/33281.txt b/.changelog/33281.txt index 188eb6e827d..531931d21b6 100644 --- a/.changelog/33281.txt +++ b/.changelog/33281.txt @@ -1,5 +1,5 @@ ```release-note:enhancement -resource/aws_api_gateway_api_key: Add support `customer_id` argument +resource/aws_api_gateway_api_key: Add `customer_id` argument ``` ```release-note:enhancement From 60603a805feb64e3436ff9ac77a76ba8a3fb3da1 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 4 Sep 2023 15:00:19 -0400 Subject: [PATCH 5/8] 'customerId' -> 'customer_id'. --- internal/service/apigateway/api_key.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/internal/service/apigateway/api_key.go b/internal/service/apigateway/api_key.go index 775bd8ff9f6..1ff9d4b9335 100644 --- a/internal/service/apigateway/api_key.go +++ b/internal/service/apigateway/api_key.go @@ -137,25 +137,19 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interf Resource: fmt.Sprintf("/apikeys/%s", d.Id()), }.String() d.Set("arn", arn) - - d.Set("name", apiKey.Name) + d.Set("created_date", apiKey.CreatedDate.Format(time.RFC3339)) d.Set("description", apiKey.Description) d.Set("enabled", apiKey.Enabled) + d.Set("last_updated_date", apiKey.LastUpdatedDate.Format(time.RFC3339)) + d.Set("name", apiKey.Name) d.Set("value", apiKey.Value) - if err := d.Set("created_date", apiKey.CreatedDate.Format(time.RFC3339)); err != nil { - return sdkdiag.AppendErrorf(diags, "setting created_date: %s", err) - } - - if err := d.Set("last_updated_date", apiKey.LastUpdatedDate.Format(time.RFC3339)); err != nil { - return sdkdiag.AppendErrorf(diags, "setting last_updated_date: %s", err) - } - return diags } func resourceAPIKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { operations := make([]*apigateway.PatchOperation, 0) + if d.HasChange("enabled") { isEnabled := "false" if d.Get("enabled").(bool) { @@ -184,10 +178,10 @@ func resourceAPIKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchO }) } - if d.HasChange("customerId") { + if d.HasChange("customer_id") { operations = append(operations, &apigateway.PatchOperation{ Op: aws.String(apigateway.OpReplace), - Path: aws.String("/name"), + Path: aws.String("/customerId"), Value: aws.String(d.Get("customer_id").(string)), }) } From 252f84c103da3d316b0bf1285137e645ea4298ff Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 4 Sep 2023 15:04:18 -0400 Subject: [PATCH 6/8] Add 'TestAccAPIGatewayAPIKey_customerID'. --- internal/service/apigateway/api_key_test.go | 47 +++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/internal/service/apigateway/api_key_test.go b/internal/service/apigateway/api_key_test.go index d2fdd7b16e2..6dc761d192b 100644 --- a/internal/service/apigateway/api_key_test.go +++ b/internal/service/apigateway/api_key_test.go @@ -39,6 +39,7 @@ func TestAccAPIGatewayAPIKey_basic(t *testing.T) { testAccCheckAPIKeyExists(ctx, resourceName, &apiKey1), acctest.MatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "apigateway", regexache.MustCompile(`/apikeys/+.`)), resource.TestCheckResourceAttrSet(resourceName, "created_date"), + resource.TestCheckResourceAttr(resourceName, "customer_id", ""), resource.TestCheckResourceAttr(resourceName, "description", "Managed by Terraform"), resource.TestCheckResourceAttr(resourceName, "enabled", "true"), resource.TestCheckResourceAttr(resourceName, "name", rName), @@ -57,6 +58,7 @@ func TestAccAPIGatewayAPIKey_basic(t *testing.T) { testAccCheckAPIKeyExists(ctx, resourceName, &apiKey2), acctest.MatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "apigateway", regexache.MustCompile(`/apikeys/+.`)), resource.TestCheckResourceAttrSet(resourceName, "created_date"), + resource.TestCheckResourceAttr(resourceName, "customer_id", ""), resource.TestCheckResourceAttr(resourceName, "description", "Managed by Terraform"), resource.TestCheckResourceAttr(resourceName, "enabled", "true"), resource.TestCheckResourceAttr(resourceName, "name", rNameUpdated), @@ -114,6 +116,42 @@ func TestAccAPIGatewayAPIKey_tags(t *testing.T) { }) } +func TestAccAPIGatewayAPIKey_customerID(t *testing.T) { + ctx := acctest.Context(t) + var apiKey1, apiKey2 apigateway.ApiKey + resourceName := "aws_api_gateway_api_key.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, apigateway.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckAPIKeyDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccAPIKeyConfig_customerID(rName, "cid1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAPIKeyExists(ctx, resourceName, &apiKey1), + resource.TestCheckResourceAttr(resourceName, "customer_id", "cid1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAPIKeyConfig_customerID(rName, "cid2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAPIKeyExists(ctx, resourceName, &apiKey2), + testAccCheckAPIKeyNotRecreated(&apiKey1, &apiKey2), + resource.TestCheckResourceAttr(resourceName, "customer_id", "cid2"), + ), + }, + }, + }) +} + func TestAccAPIGatewayAPIKey_description(t *testing.T) { ctx := acctest.Context(t) var apiKey1, apiKey2 apigateway.ApiKey @@ -332,6 +370,15 @@ resource "aws_api_gateway_api_key" "test" { `, rName, tagKey1, tagValue1, tagKey2, tagValue2) } +func testAccAPIKeyConfig_customerID(rName, customerID string) string { + return fmt.Sprintf(` +resource "aws_api_gateway_api_key" "test" { + customer_id = %[2]q + name = %[1]q +} +`, rName, customerID) +} + func testAccAPIKeyConfig_description(rName, description string) string { return fmt.Sprintf(` resource "aws_api_gateway_api_key" "test" { From 484557ddb9158780b1807db4792f39fd158c7380 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 4 Sep 2023 15:09:31 -0400 Subject: [PATCH 7/8] r/aws_api_gateway_api_key: Read 'customer_id'. --- internal/service/apigateway/api_key.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/service/apigateway/api_key.go b/internal/service/apigateway/api_key.go index 1ff9d4b9335..a9bc1cc418c 100644 --- a/internal/service/apigateway/api_key.go +++ b/internal/service/apigateway/api_key.go @@ -138,6 +138,7 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interf }.String() d.Set("arn", arn) d.Set("created_date", apiKey.CreatedDate.Format(time.RFC3339)) + d.Set("customer_id", apiKey.CustomerId) d.Set("description", apiKey.Description) d.Set("enabled", apiKey.Enabled) d.Set("last_updated_date", apiKey.LastUpdatedDate.Format(time.RFC3339)) From 9df2b292b6de82905f9a86aa9239bc258a5059ea Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 4 Sep 2023 15:14:24 -0400 Subject: [PATCH 8/8] d/aws_api_gateway_api_key: Add 'customer_id' attribute. --- .changelog/33281.txt | 4 +++ .../service/apigateway/api_key_data_source.go | 26 ++++++++++++------- .../apigateway/api_key_data_source_test.go | 15 ++++++----- .../docs/d/api_gateway_api_key.html.markdown | 1 + 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/.changelog/33281.txt b/.changelog/33281.txt index 531931d21b6..16a7ca5e3dd 100644 --- a/.changelog/33281.txt +++ b/.changelog/33281.txt @@ -4,4 +4,8 @@ resource/aws_api_gateway_api_key: Add `customer_id` argument ```release-note:enhancement resource/aws_api_gateway_api_key: Allow updating `name` +``` + +```release-note:enhancement +data-source/aws_api_gateway_api_key: Add `customer_id` attribute ``` \ No newline at end of file diff --git a/internal/service/apigateway/api_key_data_source.go b/internal/service/apigateway/api_key_data_source.go index 7063a55c044..289919d6ea8 100644 --- a/internal/service/apigateway/api_key_data_source.go +++ b/internal/service/apigateway/api_key_data_source.go @@ -19,12 +19,13 @@ import ( func DataSourceAPIKey() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceAPIKeyRead, + Schema: map[string]*schema.Schema{ - "id": { + "created_date": { Type: schema.TypeString, - Required: true, + Computed: true, }, - "name": { + "customer_id": { Type: schema.TypeString, Computed: true, }, @@ -32,24 +33,28 @@ func DataSourceAPIKey() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "created_date": { - Type: schema.TypeString, + "enabled": { + Type: schema.TypeBool, Computed: true, }, + "id": { + Type: schema.TypeString, + Required: true, + }, "last_updated_date": { Type: schema.TypeString, Computed: true, }, - "enabled": { - Type: schema.TypeBool, + "name": { + Type: schema.TypeString, Computed: true, }, + "tags": tftags.TagsSchemaComputed(), "value": { Type: schema.TypeString, Computed: true, Sensitive: true, }, - "tags": tftags.TagsSchemaComputed(), }, } } @@ -67,12 +72,13 @@ func dataSourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta inte } d.SetId(aws.StringValue(apiKey.Id)) - d.Set("name", apiKey.Name) - d.Set("value", apiKey.Value) d.Set("created_date", aws.TimeValue(apiKey.CreatedDate).Format(time.RFC3339)) + d.Set("customer_id", apiKey.CustomerId) d.Set("description", apiKey.Description) d.Set("enabled", apiKey.Enabled) d.Set("last_updated_date", aws.TimeValue(apiKey.LastUpdatedDate).Format(time.RFC3339)) + d.Set("name", apiKey.Name) + d.Set("value", apiKey.Value) if err := d.Set("tags", KeyValueTags(ctx, apiKey.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) diff --git a/internal/service/apigateway/api_key_data_source_test.go b/internal/service/apigateway/api_key_data_source_test.go index f63bf8c1c65..8b5df61874f 100644 --- a/internal/service/apigateway/api_key_data_source_test.go +++ b/internal/service/apigateway/api_key_data_source_test.go @@ -27,21 +27,22 @@ func TestAccAPIGatewayAPIKeyDataSource_basic(t *testing.T) { { Config: testAccAPIKeyDataSourceConfig_basic(rName), Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(resourceName, "created_date", dataSourceName, "created_date"), + resource.TestCheckResourceAttrPair(resourceName, "customer_id", dataSourceName, "customer_id"), + resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceName, "description"), + resource.TestCheckResourceAttrPair(resourceName, "enabled", dataSourceName, "enabled"), resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "id"), + resource.TestCheckResourceAttrPair(resourceName, "last_updated_date", dataSourceName, "last_updated_date"), resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"), + resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"), resource.TestCheckResourceAttrPair(resourceName, "value", dataSourceName, "value"), - resource.TestCheckResourceAttrPair(resourceName, "enabled", dataSourceName, "enabled"), - resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceName, "description"), - resource.TestCheckResourceAttrSet(dataSourceName, "last_updated_date"), - resource.TestCheckResourceAttrSet(dataSourceName, "created_date"), - resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), ), }, }, }) } -func testAccAPIKeyDataSourceConfig_basic(r string) string { +func testAccAPIKeyDataSourceConfig_basic(rName string) string { return fmt.Sprintf(` resource "aws_api_gateway_api_key" "test" { name = %[1]q @@ -50,5 +51,5 @@ resource "aws_api_gateway_api_key" "test" { data "aws_api_gateway_api_key" "test" { id = aws_api_gateway_api_key.test.id } -`, r) +`, rName) } diff --git a/website/docs/d/api_gateway_api_key.html.markdown b/website/docs/d/api_gateway_api_key.html.markdown index 58867f19c46..cda11bc4d2a 100644 --- a/website/docs/d/api_gateway_api_key.html.markdown +++ b/website/docs/d/api_gateway_api_key.html.markdown @@ -32,6 +32,7 @@ This data source exports the following attributes in addition to the arguments a * `value` - Set to the value of the API Key. * `created_date` - Date and time when the API Key was created. * `last_updated_date` - Date and time when the API Key was last updated. +* `customer_id` - Amazon Web Services Marketplace customer identifier, when integrating with the Amazon Web Services SaaS Marketplace. * `description` - Description of the API Key. * `enabled` - Whether the API Key is enabled. * `tags` - Map of tags for the resource.