From 306bb4c63792731f0e3ed94eba9054dca6b400ad Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 22 Nov 2022 07:45:09 +1100 Subject: [PATCH 1/4] resource/cloudflare_workers_kv: `key` to force creation of a new resource Closes #1389 --- .changelog/2044.txt | 3 + docs/resources/workers_kv.md | 45 ++++---- .../resources/cloudflare_workers_kv/import.sh | 1 + .../cloudflare_workers_kv/resource.tf | 9 ++ .../provider/resource_cloudflare_worker_kv.go | 104 ------------------ .../resource_cloudflare_worker_kv_test.go | 91 --------------- ..._kv.go => schema_cloudflare_workers_kv.go} | 1 + templates/resources/workers_kv.md | 43 -------- templates/resources/workers_kv.md.tmpl | 25 +++++ 9 files changed, 64 insertions(+), 258 deletions(-) create mode 100644 .changelog/2044.txt create mode 100644 examples/resources/cloudflare_workers_kv/import.sh create mode 100644 examples/resources/cloudflare_workers_kv/resource.tf delete mode 100644 internal/provider/resource_cloudflare_worker_kv.go delete mode 100644 internal/provider/resource_cloudflare_worker_kv_test.go rename internal/provider/{schema_cloudflare_worker_kv.go => schema_cloudflare_workers_kv.go} (95%) delete mode 100644 templates/resources/workers_kv.md create mode 100644 templates/resources/workers_kv.md.tmpl diff --git a/.changelog/2044.txt b/.changelog/2044.txt new file mode 100644 index 0000000000..e02b664a6f --- /dev/null +++ b/.changelog/2044.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/cloudflare_workers_kv: `key` changes force creation of a new resource +``` diff --git a/docs/resources/workers_kv.md b/docs/resources/workers_kv.md index 5ae8481ab1..8fa0cbf20a 100644 --- a/docs/resources/workers_kv.md +++ b/docs/resources/workers_kv.md @@ -1,43 +1,48 @@ --- -layout: "cloudflare" -page_title: "Cloudflare: cloudflare_workers_kv" -description: Provides the ability to manage Cloudflare Workers KV features. +page_title: "cloudflare_workers_kv Resource - Cloudflare" +subcategory: "" +description: |- + Provides a Workers KV Pair. --- -# cloudflare_workers_kv +# cloudflare_workers_kv (Resource) -Provides a Workers KV Pair. _NOTE:_ This resource uses the Cloudflare account APIs. This requires setting the `CLOUDFLARE_ACCOUNT_ID` environment variable or `account_id` provider argument. +Provides a Workers KV Pair. + +~> This resource uses the Cloudflare account APIs. This requires setting the +`CLOUDFLARE_ACCOUNT_ID` environment variable or `account_id` provider argument. ## Example Usage -```hcl +```terraform resource "cloudflare_workers_kv_namespace" "example_ns" { title = "test-namespace" } resource "cloudflare_workers_kv" "example" { namespace_id = cloudflare_workers_kv_namespace.example_ns.id - key = "test-key" - value = "test value" + key = "test-key" + value = "test value" } ``` -## Argument Reference + +## Schema -The following arguments are supported: +### Required -- `namespace_id` - (Required) The ID of the Workers KV namespace in which you want to create the KV pair -- `key` - (Required) The key name -- `value` - (Required) The string value to be stored in the key +- `key` (String) **Modifying this attribute will force creation of a new resource.** +- `namespace_id` (String) **Modifying this attribute will force creation of a new resource.** +- `value` (String) -## Import +### Read-Only -Workers KV Namespace settings can be imported using it's ID. **Note:** Because the same key can exist in multiple namespaces, the ID is a composite key of the format /. +- `id` (String) The ID of this resource. -``` -$ terraform import cloudflare_workers_kv.example beaeb6716c9443eaa4deef11763ccca6/test-key -``` +## Import -where: +Import is supported using the following syntax: -- `beaeb6716c9443eaa4deef11763ccca6` is the ID of the namespace and `test-key` is the key +```shell +$ terraform import cloudflare_workers_kv.example / +``` diff --git a/examples/resources/cloudflare_workers_kv/import.sh b/examples/resources/cloudflare_workers_kv/import.sh new file mode 100644 index 0000000000..7d6d8e33b7 --- /dev/null +++ b/examples/resources/cloudflare_workers_kv/import.sh @@ -0,0 +1 @@ +$ terraform import cloudflare_workers_kv.example / diff --git a/examples/resources/cloudflare_workers_kv/resource.tf b/examples/resources/cloudflare_workers_kv/resource.tf new file mode 100644 index 0000000000..fe578681f5 --- /dev/null +++ b/examples/resources/cloudflare_workers_kv/resource.tf @@ -0,0 +1,9 @@ +resource "cloudflare_workers_kv_namespace" "example_ns" { + title = "test-namespace" +} + +resource "cloudflare_workers_kv" "example" { + namespace_id = cloudflare_workers_kv_namespace.example_ns.id + key = "test-key" + value = "test value" +} diff --git a/internal/provider/resource_cloudflare_worker_kv.go b/internal/provider/resource_cloudflare_worker_kv.go deleted file mode 100644 index 2a5abdb404..0000000000 --- a/internal/provider/resource_cloudflare_worker_kv.go +++ /dev/null @@ -1,104 +0,0 @@ -package provider - -import ( - "context" - "fmt" - "strings" - - cloudflare "github.com/cloudflare/cloudflare-go" - "github.com/hashicorp/terraform-plugin-log/tflog" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/pkg/errors" -) - -func resourceCloudflareWorkerKV() *schema.Resource { - return &schema.Resource{ - Schema: resourceCloudflareWorkerKVSchema(), - CreateContext: resourceCloudflareWorkersKVUpdate, - ReadContext: resourceCloudflareWorkersKVRead, - UpdateContext: resourceCloudflareWorkersKVUpdate, - DeleteContext: resourceCloudflareWorkersKVDelete, - Importer: &schema.ResourceImporter{ - StateContext: resourceCloudflareWorkersKVImport, - }, - } -} - -func resourceCloudflareWorkersKVRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(*cloudflare.API) - namespaceID, key, err := parseId(d.Id()) - if err != nil { - return diag.FromErr(err) - } - - value, err := client.ReadWorkersKV(ctx, namespaceID, key) - if err != nil { - return diag.FromErr(errors.Wrap(err, "error reading workers kv")) - } - - if value == nil { - d.SetId("") - return nil - } - - d.Set("value", string(value)) - return nil -} - -func resourceCloudflareWorkersKVUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(*cloudflare.API) - namespaceID := d.Get("namespace_id").(string) - key := d.Get("key").(string) - value := d.Get("value").(string) - - _, err := client.WriteWorkersKV(ctx, namespaceID, key, []byte(value)) - if err != nil { - return diag.FromErr(errors.Wrap(err, "error creating workers kv")) - } - - d.SetId(fmt.Sprintf("%s/%s", namespaceID, key)) - - tflog.Info(ctx, fmt.Sprintf("Cloudflare Workers KV Namespace ID: %s", d.Id())) - - return resourceCloudflareWorkersKVRead(ctx, d, meta) -} - -func resourceCloudflareWorkersKVDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(*cloudflare.API) - namespaceID, key, err := parseId(d.Id()) - if err != nil { - return diag.FromErr(err) - } - - tflog.Info(ctx, fmt.Sprintf("Deleting Cloudflare Workers KV with id: %+v", d.Id())) - - _, err = client.DeleteWorkersKV(ctx, namespaceID, key) - if err != nil { - return diag.FromErr(errors.Wrap(err, "error deleting workers kv")) - } - - return nil -} - -func resourceCloudflareWorkersKVImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - namespaceID, key, err := parseId(d.Id()) - if err != nil { - return nil, err - } - - d.Set("namespace_id", namespaceID) - d.Set("key", key) - - resourceCloudflareWorkersKVRead(ctx, d, meta) - - return []*schema.ResourceData{d}, nil -} - -func parseId(id string) (string, string, error) { - parts := strings.SplitN(id, "/", 2) - if len(parts) != 2 { - return "", "", fmt.Errorf("error parsing workers kv id: %s", id) - } - return parts[0], parts[1], nil -} diff --git a/internal/provider/resource_cloudflare_worker_kv_test.go b/internal/provider/resource_cloudflare_worker_kv_test.go deleted file mode 100644 index 0e049714b0..0000000000 --- a/internal/provider/resource_cloudflare_worker_kv_test.go +++ /dev/null @@ -1,91 +0,0 @@ -package provider - -import ( - "context" - "fmt" - "testing" - - "github.com/cloudflare/cloudflare-go" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" -) - -func TestAccCloudflareWorkersKV_Basic(t *testing.T) { - t.Parallel() - var kvPair cloudflare.WorkersKVPair - name := generateRandomResourceName() - key := generateRandomResourceName() - value := generateRandomResourceName() - resourceName := "cloudflare_workers_kv." + name - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckAccount(t) }, - ProviderFactories: providerFactories, - CheckDestroy: testAccCloudflareWorkersKVDestroy, - Steps: []resource.TestStep{ - { - Config: testAccCheckCloudflareWorkersKV(name, key, value), - Check: resource.ComposeTestCheckFunc( - testAccCheckCloudflareWorkersKVExists(key, &kvPair), - resource.TestCheckResourceAttr( - resourceName, "value", value, - ), - ), - }, - }, - }) -} - -func testAccCloudflareWorkersKVDestroy(s *terraform.State) error { - client := testAccProvider.Meta().(*cloudflare.API) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "cloudflare_workers_kv" { - continue - } - - namespaceID := rs.Primary.Attributes["namespace_id"] - key := rs.Primary.Attributes["key"] - - _, err := client.ReadWorkersKV(context.Background(), namespaceID, key) - - if err == nil { - return fmt.Errorf("workers kv pair still exists") - } - } - - return nil -} - -func testAccCheckCloudflareWorkersKV(rName string, key string, value string) string { - return testAccCheckCloudflareWorkersKVNamespace(rName) + fmt.Sprintf(` -resource "cloudflare_workers_kv" "%[1]s" { - namespace_id = cloudflare_workers_kv_namespace.%[1]s.id - key = "%[2]s" - value = "%[3]s" -}`, rName, key, value) -} - -func testAccCheckCloudflareWorkersKVExists(key string, kv *cloudflare.WorkersKVPair) resource.TestCheckFunc { - return func(s *terraform.State) error { - client := testAccProvider.Meta().(*cloudflare.API) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "cloudflare_workers_kv" { - continue - } - - namespaceID := rs.Primary.Attributes["namespace_id"] - value, err := client.ReadWorkersKV(context.Background(), namespaceID, key) - if err != nil { - return err - } - - if value == nil { - return fmt.Errorf("workers kv key %s not found in namespace %s", key, namespaceID) - } - } - - return nil - } -} diff --git a/internal/provider/schema_cloudflare_worker_kv.go b/internal/provider/schema_cloudflare_workers_kv.go similarity index 95% rename from internal/provider/schema_cloudflare_worker_kv.go rename to internal/provider/schema_cloudflare_workers_kv.go index 61b93beb00..e27c50ea98 100644 --- a/internal/provider/schema_cloudflare_worker_kv.go +++ b/internal/provider/schema_cloudflare_workers_kv.go @@ -6,6 +6,7 @@ func resourceCloudflareWorkerKVSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ "key": { Type: schema.TypeString, + ForceNew: true, Required: true, }, "namespace_id": { diff --git a/templates/resources/workers_kv.md b/templates/resources/workers_kv.md deleted file mode 100644 index 5ae8481ab1..0000000000 --- a/templates/resources/workers_kv.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -layout: "cloudflare" -page_title: "Cloudflare: cloudflare_workers_kv" -description: Provides the ability to manage Cloudflare Workers KV features. ---- - -# cloudflare_workers_kv - -Provides a Workers KV Pair. _NOTE:_ This resource uses the Cloudflare account APIs. This requires setting the `CLOUDFLARE_ACCOUNT_ID` environment variable or `account_id` provider argument. - -## Example Usage - -```hcl -resource "cloudflare_workers_kv_namespace" "example_ns" { - title = "test-namespace" -} - -resource "cloudflare_workers_kv" "example" { - namespace_id = cloudflare_workers_kv_namespace.example_ns.id - key = "test-key" - value = "test value" -} -``` - -## Argument Reference - -The following arguments are supported: - -- `namespace_id` - (Required) The ID of the Workers KV namespace in which you want to create the KV pair -- `key` - (Required) The key name -- `value` - (Required) The string value to be stored in the key - -## Import - -Workers KV Namespace settings can be imported using it's ID. **Note:** Because the same key can exist in multiple namespaces, the ID is a composite key of the format /. - -``` -$ terraform import cloudflare_workers_kv.example beaeb6716c9443eaa4deef11763ccca6/test-key -``` - -where: - -- `beaeb6716c9443eaa4deef11763ccca6` is the ID of the namespace and `test-key` is the key diff --git a/templates/resources/workers_kv.md.tmpl b/templates/resources/workers_kv.md.tmpl new file mode 100644 index 0000000000..152b87f432 --- /dev/null +++ b/templates/resources/workers_kv.md.tmpl @@ -0,0 +1,25 @@ +--- +page_title: "{{.Name}} {{.Type}} - {{.RenderedProviderName}}" +subcategory: "" +description: |- +{{ .Description | plainmarkdown | trimspace | prefixlines " " }} +--- + +# {{.Name}} ({{.Type}}) + +{{ .Description | trimspace }} + +~> This resource uses the Cloudflare account APIs. This requires setting the +`CLOUDFLARE_ACCOUNT_ID` environment variable or `account_id` provider argument. + +## Example Usage + +{{ tffile (printf "%s%s%s" "examples/resources/" .Name "/resource.tf") }} + +{{ .SchemaMarkdown | trimspace }} + +## Import + +Import is supported using the following syntax: + +{{ codefile "shell" (printf "%s%s%s" "examples/resources/" .Name "/import.sh") }} From 23cb47776463e137ab8a722573a41be44aec37c1 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 22 Nov 2022 07:46:19 +1100 Subject: [PATCH 2/4] file renames to ensure worker*s* is consistent --- ...source_cloudflare_workers_cron_trigger.go} | 0 ...e_cloudflare_workers_cron_trigger_test.go} | 0 .../resource_cloudflare_workers_kv.go | 105 ++++++++++++++++++ .../resource_cloudflare_workers_kv_test.go | 91 +++++++++++++++ ...o => resource_cloudflare_workers_route.go} | 0 ...resource_cloudflare_workers_route_test.go} | 0 ... => resource_cloudflare_workers_script.go} | 0 ...esource_cloudflare_workers_script_test.go} | 0 ...schema_cloudflare_workers_cron_trigger.go} | 0 ....go => schema_cloudflare_workers_route.go} | 0 ...go => schema_cloudflare_workers_script.go} | 0 11 files changed, 196 insertions(+) rename internal/provider/{resource_cloudflare_worker_cron_trigger.go => resource_cloudflare_workers_cron_trigger.go} (100%) rename internal/provider/{resource_cloudflare_worker_cron_trigger_test.go => resource_cloudflare_workers_cron_trigger_test.go} (100%) create mode 100644 internal/provider/resource_cloudflare_workers_kv.go create mode 100644 internal/provider/resource_cloudflare_workers_kv_test.go rename internal/provider/{resource_cloudflare_worker_route.go => resource_cloudflare_workers_route.go} (100%) rename internal/provider/{resource_cloudflare_worker_route_test.go => resource_cloudflare_workers_route_test.go} (100%) rename internal/provider/{resource_cloudflare_worker_script.go => resource_cloudflare_workers_script.go} (100%) rename internal/provider/{resource_cloudflare_worker_script_test.go => resource_cloudflare_workers_script_test.go} (100%) rename internal/provider/{schema_cloudflare_worker_cron_trigger.go => schema_cloudflare_workers_cron_trigger.go} (100%) rename internal/provider/{schema_cloudflare_worker_route.go => schema_cloudflare_workers_route.go} (100%) rename internal/provider/{schema_cloudflare_worker_script.go => schema_cloudflare_workers_script.go} (100%) diff --git a/internal/provider/resource_cloudflare_worker_cron_trigger.go b/internal/provider/resource_cloudflare_workers_cron_trigger.go similarity index 100% rename from internal/provider/resource_cloudflare_worker_cron_trigger.go rename to internal/provider/resource_cloudflare_workers_cron_trigger.go diff --git a/internal/provider/resource_cloudflare_worker_cron_trigger_test.go b/internal/provider/resource_cloudflare_workers_cron_trigger_test.go similarity index 100% rename from internal/provider/resource_cloudflare_worker_cron_trigger_test.go rename to internal/provider/resource_cloudflare_workers_cron_trigger_test.go diff --git a/internal/provider/resource_cloudflare_workers_kv.go b/internal/provider/resource_cloudflare_workers_kv.go new file mode 100644 index 0000000000..57c8cfed5d --- /dev/null +++ b/internal/provider/resource_cloudflare_workers_kv.go @@ -0,0 +1,105 @@ +package provider + +import ( + "context" + "fmt" + "strings" + + cloudflare "github.com/cloudflare/cloudflare-go" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/pkg/errors" +) + +func resourceCloudflareWorkerKV() *schema.Resource { + return &schema.Resource{ + Schema: resourceCloudflareWorkerKVSchema(), + CreateContext: resourceCloudflareWorkersKVUpdate, + ReadContext: resourceCloudflareWorkersKVRead, + UpdateContext: resourceCloudflareWorkersKVUpdate, + DeleteContext: resourceCloudflareWorkersKVDelete, + Importer: &schema.ResourceImporter{ + StateContext: resourceCloudflareWorkersKVImport, + }, + Description: "Provides a Workers KV Pair.", + } +} + +func resourceCloudflareWorkersKVRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + client := meta.(*cloudflare.API) + namespaceID, key, err := parseId(d.Id()) + if err != nil { + return diag.FromErr(err) + } + + value, err := client.ReadWorkersKV(ctx, namespaceID, key) + if err != nil { + return diag.FromErr(errors.Wrap(err, "error reading workers kv")) + } + + if value == nil { + d.SetId("") + return nil + } + + d.Set("value", string(value)) + return nil +} + +func resourceCloudflareWorkersKVUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + client := meta.(*cloudflare.API) + namespaceID := d.Get("namespace_id").(string) + key := d.Get("key").(string) + value := d.Get("value").(string) + + _, err := client.WriteWorkersKV(ctx, namespaceID, key, []byte(value)) + if err != nil { + return diag.FromErr(errors.Wrap(err, "error creating workers kv")) + } + + d.SetId(fmt.Sprintf("%s/%s", namespaceID, key)) + + tflog.Info(ctx, fmt.Sprintf("Cloudflare Workers KV Namespace ID: %s", d.Id())) + + return resourceCloudflareWorkersKVRead(ctx, d, meta) +} + +func resourceCloudflareWorkersKVDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + client := meta.(*cloudflare.API) + namespaceID, key, err := parseId(d.Id()) + if err != nil { + return diag.FromErr(err) + } + + tflog.Info(ctx, fmt.Sprintf("Deleting Cloudflare Workers KV with id: %+v", d.Id())) + + _, err = client.DeleteWorkersKV(ctx, namespaceID, key) + if err != nil { + return diag.FromErr(errors.Wrap(err, "error deleting workers kv")) + } + + return nil +} + +func resourceCloudflareWorkersKVImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + namespaceID, key, err := parseId(d.Id()) + if err != nil { + return nil, err + } + + d.Set("namespace_id", namespaceID) + d.Set("key", key) + + resourceCloudflareWorkersKVRead(ctx, d, meta) + + return []*schema.ResourceData{d}, nil +} + +func parseId(id string) (string, string, error) { + parts := strings.SplitN(id, "/", 2) + if len(parts) != 2 { + return "", "", fmt.Errorf("error parsing workers kv id: %s", id) + } + return parts[0], parts[1], nil +} diff --git a/internal/provider/resource_cloudflare_workers_kv_test.go b/internal/provider/resource_cloudflare_workers_kv_test.go new file mode 100644 index 0000000000..0e049714b0 --- /dev/null +++ b/internal/provider/resource_cloudflare_workers_kv_test.go @@ -0,0 +1,91 @@ +package provider + +import ( + "context" + "fmt" + "testing" + + "github.com/cloudflare/cloudflare-go" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccCloudflareWorkersKV_Basic(t *testing.T) { + t.Parallel() + var kvPair cloudflare.WorkersKVPair + name := generateRandomResourceName() + key := generateRandomResourceName() + value := generateRandomResourceName() + resourceName := "cloudflare_workers_kv." + name + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheckAccount(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCloudflareWorkersKVDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckCloudflareWorkersKV(name, key, value), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudflareWorkersKVExists(key, &kvPair), + resource.TestCheckResourceAttr( + resourceName, "value", value, + ), + ), + }, + }, + }) +} + +func testAccCloudflareWorkersKVDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*cloudflare.API) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "cloudflare_workers_kv" { + continue + } + + namespaceID := rs.Primary.Attributes["namespace_id"] + key := rs.Primary.Attributes["key"] + + _, err := client.ReadWorkersKV(context.Background(), namespaceID, key) + + if err == nil { + return fmt.Errorf("workers kv pair still exists") + } + } + + return nil +} + +func testAccCheckCloudflareWorkersKV(rName string, key string, value string) string { + return testAccCheckCloudflareWorkersKVNamespace(rName) + fmt.Sprintf(` +resource "cloudflare_workers_kv" "%[1]s" { + namespace_id = cloudflare_workers_kv_namespace.%[1]s.id + key = "%[2]s" + value = "%[3]s" +}`, rName, key, value) +} + +func testAccCheckCloudflareWorkersKVExists(key string, kv *cloudflare.WorkersKVPair) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := testAccProvider.Meta().(*cloudflare.API) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "cloudflare_workers_kv" { + continue + } + + namespaceID := rs.Primary.Attributes["namespace_id"] + value, err := client.ReadWorkersKV(context.Background(), namespaceID, key) + if err != nil { + return err + } + + if value == nil { + return fmt.Errorf("workers kv key %s not found in namespace %s", key, namespaceID) + } + } + + return nil + } +} diff --git a/internal/provider/resource_cloudflare_worker_route.go b/internal/provider/resource_cloudflare_workers_route.go similarity index 100% rename from internal/provider/resource_cloudflare_worker_route.go rename to internal/provider/resource_cloudflare_workers_route.go diff --git a/internal/provider/resource_cloudflare_worker_route_test.go b/internal/provider/resource_cloudflare_workers_route_test.go similarity index 100% rename from internal/provider/resource_cloudflare_worker_route_test.go rename to internal/provider/resource_cloudflare_workers_route_test.go diff --git a/internal/provider/resource_cloudflare_worker_script.go b/internal/provider/resource_cloudflare_workers_script.go similarity index 100% rename from internal/provider/resource_cloudflare_worker_script.go rename to internal/provider/resource_cloudflare_workers_script.go diff --git a/internal/provider/resource_cloudflare_worker_script_test.go b/internal/provider/resource_cloudflare_workers_script_test.go similarity index 100% rename from internal/provider/resource_cloudflare_worker_script_test.go rename to internal/provider/resource_cloudflare_workers_script_test.go diff --git a/internal/provider/schema_cloudflare_worker_cron_trigger.go b/internal/provider/schema_cloudflare_workers_cron_trigger.go similarity index 100% rename from internal/provider/schema_cloudflare_worker_cron_trigger.go rename to internal/provider/schema_cloudflare_workers_cron_trigger.go diff --git a/internal/provider/schema_cloudflare_worker_route.go b/internal/provider/schema_cloudflare_workers_route.go similarity index 100% rename from internal/provider/schema_cloudflare_worker_route.go rename to internal/provider/schema_cloudflare_workers_route.go diff --git a/internal/provider/schema_cloudflare_worker_script.go b/internal/provider/schema_cloudflare_workers_script.go similarity index 100% rename from internal/provider/schema_cloudflare_worker_script.go rename to internal/provider/schema_cloudflare_workers_script.go From 59198206c71d235b3a86e5d9c14bc76232da8e74 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 22 Nov 2022 07:48:15 +1100 Subject: [PATCH 3/4] resource/cloudflare_workers_kv: fix descriptions --- docs/resources/workers_kv.md | 6 +++--- .../provider/schema_cloudflare_workers_kv.go | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/docs/resources/workers_kv.md b/docs/resources/workers_kv.md index 8fa0cbf20a..e0bbe73328 100644 --- a/docs/resources/workers_kv.md +++ b/docs/resources/workers_kv.md @@ -31,9 +31,9 @@ resource "cloudflare_workers_kv" "example" { ### Required -- `key` (String) **Modifying this attribute will force creation of a new resource.** -- `namespace_id` (String) **Modifying this attribute will force creation of a new resource.** -- `value` (String) +- `key` (String) Name of the KV pair. **Modifying this attribute will force creation of a new resource.** +- `namespace_id` (String) The ID of the Workers KV namespace in which you want to create the KV pair. **Modifying this attribute will force creation of a new resource.** +- `value` (String) Value of the KV pair. ### Read-Only diff --git a/internal/provider/schema_cloudflare_workers_kv.go b/internal/provider/schema_cloudflare_workers_kv.go index e27c50ea98..c22948976b 100644 --- a/internal/provider/schema_cloudflare_workers_kv.go +++ b/internal/provider/schema_cloudflare_workers_kv.go @@ -5,18 +5,21 @@ import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" func resourceCloudflareWorkerKVSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ "key": { - Type: schema.TypeString, - ForceNew: true, - Required: true, + Type: schema.TypeString, + ForceNew: true, + Required: true, + Description: "Name of the KV pair.", }, "namespace_id": { - Type: schema.TypeString, - ForceNew: true, - Required: true, + Type: schema.TypeString, + ForceNew: true, + Required: true, + Description: "The ID of the Workers KV namespace in which you want to create the KV pair.", }, "value": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + Description: "Value of the KV pair.", }, } } From 2c74ce0ac5da7eb74bec1631f59569ee70290f26 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 22 Nov 2022 08:02:52 +1100 Subject: [PATCH 4/4] resource/cloudflare_workers_kv: add test coverage for forced recreation --- .../resource_cloudflare_workers_kv_test.go | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/internal/provider/resource_cloudflare_workers_kv_test.go b/internal/provider/resource_cloudflare_workers_kv_test.go index 0e049714b0..bca98db2cd 100644 --- a/internal/provider/resource_cloudflare_workers_kv_test.go +++ b/internal/provider/resource_cloudflare_workers_kv_test.go @@ -19,7 +19,39 @@ func TestAccCloudflareWorkersKV_Basic(t *testing.T) { resourceName := "cloudflare_workers_kv." + name resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckAccount(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckAccount(t) + }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCloudflareWorkersKVDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckCloudflareWorkersKV(name, key, value), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudflareWorkersKVExists(key, &kvPair), + resource.TestCheckResourceAttr( + resourceName, "value", value, + ), + ), + }, + }, + }) +} + +func TestAccCloudflareWorkersKV_NameForcesRecreation(t *testing.T) { + t.Parallel() + var kvPair cloudflare.WorkersKVPair + name := generateRandomResourceName() + key := generateRandomResourceName() + value := generateRandomResourceName() + resourceName := "cloudflare_workers_kv." + name + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckAccount(t) + }, ProviderFactories: providerFactories, CheckDestroy: testAccCloudflareWorkersKVDestroy, Steps: []resource.TestStep{ @@ -32,6 +64,16 @@ func TestAccCloudflareWorkersKV_Basic(t *testing.T) { ), ), }, + { + Config: testAccCheckCloudflareWorkersKV(name, key+"-updated", value), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudflareWorkersKVExists(key, &kvPair), + resource.TestCheckResourceAttr( + resourceName, "value", value, + ), + ), + ExpectNonEmptyPlan: false, + }, }, }) }