Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/cloudflare_workers_kv: key to force creation of a new resource #2044

Merged
merged 4 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2044.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_workers_kv: `key` changes force creation of a new resource
```
45 changes: 25 additions & 20 deletions docs/resources/workers_kv.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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) 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.

## 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 <namespace_id>/<key>.
- `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 <namespace_id>/<key_name>
```
1 change: 1 addition & 0 deletions examples/resources/cloudflare_workers_kv/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ terraform import cloudflare_workers_kv.example <namespace_id>/<key_name>
9 changes: 9 additions & 0 deletions examples/resources/cloudflare_workers_kv/resource.tf
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func resourceCloudflareWorkerKV() *schema.Resource {
Importer: &schema.ResourceImporter{
StateContext: resourceCloudflareWorkersKVImport,
},
Description: "Provides a Workers KV Pair.",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
},
},
})
}
Expand Down
21 changes: 0 additions & 21 deletions internal/provider/schema_cloudflare_worker_kv.go

This file was deleted.

25 changes: 25 additions & 0 deletions internal/provider/schema_cloudflare_workers_kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package provider

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,
Description: "Name of the KV pair.",
},
"namespace_id": {
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,
Description: "Value of the KV pair.",
},
}
}
43 changes: 0 additions & 43 deletions templates/resources/workers_kv.md

This file was deleted.

25 changes: 25 additions & 0 deletions templates/resources/workers_kv.md.tmpl
Original file line number Diff line number Diff line change
@@ -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") }}