diff --git a/.changelog/2049.txt b/.changelog/2049.txt new file mode 100644 index 0000000000..747299ae33 --- /dev/null +++ b/.changelog/2049.txt @@ -0,0 +1,15 @@ +```release-note:dependency +provider: bumps github.com/cloudflare/cloudflare-go from 0.54.0 to 0.55.0 +``` + +```release-note:enhancement +resource/cloudflare_workers_kv: add support for explicitly setting `account_id` on the resource +``` + +```release-note:enhancement +resource/cloudflare_workers_kv_namespace: add support for explicitly setting `account_id` on the resource +``` + +```release-note:enhancement +resource/cloudflare_workers_kv_namespace: swap internals to use new method signatures from cloudflare-go release +``` diff --git a/docs/resources/workers_kv.md b/docs/resources/workers_kv.md index e0bbe73328..ae3b4aa841 100644 --- a/docs/resources/workers_kv.md +++ b/docs/resources/workers_kv.md @@ -2,24 +2,27 @@ page_title: "cloudflare_workers_kv Resource - Cloudflare" subcategory: "" description: |- - Provides a Workers KV Pair. + Provides a resource to manage a Cloudflare Workers KV Pair. --- # cloudflare_workers_kv (Resource) -Provides a Workers KV Pair. +Provides a resource to manage a Cloudflare Workers KV Pair. ~> This resource uses the Cloudflare account APIs. This requires setting the -`CLOUDFLARE_ACCOUNT_ID` environment variable or `account_id` provider argument. +`CLOUDFLARE_ACCOUNT_ID` environment variable or `account_id` provider argument +if you do not explicitly set the resource level `account_id` value. ## Example Usage ```terraform resource "cloudflare_workers_kv_namespace" "example_ns" { - title = "test-namespace" + account_id = "f037e56e89293a057740de681ac9abbe" + title = "test-namespace" } resource "cloudflare_workers_kv" "example" { + account_id = "f037e56e89293a057740de681ac9abbe" namespace_id = cloudflare_workers_kv_namespace.example_ns.id key = "test-key" value = "test value" @@ -35,6 +38,10 @@ resource "cloudflare_workers_kv" "example" { - `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. +### Optional + +- `account_id` (String) The account identifier to target for the resource. + ### Read-Only - `id` (String) The ID of this resource. diff --git a/docs/resources/workers_kv_namespace.md b/docs/resources/workers_kv_namespace.md index f3b41580e2..aaa6d42b74 100644 --- a/docs/resources/workers_kv_namespace.md +++ b/docs/resources/workers_kv_namespace.md @@ -1,35 +1,45 @@ --- -layout: "cloudflare" -page_title: "Cloudflare: cloudflare_workers_kv_namespace" -description: Provides the ability to manage Cloudflare Workers KV Namespace features. +page_title: "cloudflare_workers_kv_namespace Resource - Cloudflare" +subcategory: "" +description: |- + Provides the ability to manage Cloudflare Workers KV Namespace features. --- -# cloudflare_workers_kv_namespace +# cloudflare_workers_kv_namespace (Resource) -Provides a Workers KV Namespace +Provides the ability to manage Cloudflare Workers KV Namespace features. + +~> 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" { - title = "test-namespace" + account_id = "f037e56e89293a057740de681ac9abbe" + title = "test-namespace" } ``` -## Argument Reference + +## Schema -The following arguments are supported: +### Required -- `title` - (Required) The name of the namespace you wish to create. +- `title` (String) -## Import +### Optional -Workers KV Namespace settings can be imported using it's ID +- `account_id` (String) The account identifier to target for the resource. -``` -$ terraform import cloudflare_workers_kv_namespace.example beaeb6716c9443eaa4deef11763ccca6 -``` +### Read-Only -where: +- `id` (String) The ID of this resource. -- `beaeb6716c9443eaa4deef11763ccca6` is the ID of the namespace +## Import + +Import is supported using the following syntax: + +```shell +$ terraform import cloudflare_workers_kv_namespace.example / +``` diff --git a/examples/resources/cloudflare_workers_kv/resource.tf b/examples/resources/cloudflare_workers_kv/resource.tf index fe578681f5..5e69baf6aa 100644 --- a/examples/resources/cloudflare_workers_kv/resource.tf +++ b/examples/resources/cloudflare_workers_kv/resource.tf @@ -1,8 +1,10 @@ resource "cloudflare_workers_kv_namespace" "example_ns" { - title = "test-namespace" + account_id = "f037e56e89293a057740de681ac9abbe" + title = "test-namespace" } resource "cloudflare_workers_kv" "example" { + account_id = "f037e56e89293a057740de681ac9abbe" namespace_id = cloudflare_workers_kv_namespace.example_ns.id key = "test-key" value = "test value" diff --git a/examples/resources/cloudflare_workers_kv_namespace/import.sh b/examples/resources/cloudflare_workers_kv_namespace/import.sh new file mode 100644 index 0000000000..7f9b1f17de --- /dev/null +++ b/examples/resources/cloudflare_workers_kv_namespace/import.sh @@ -0,0 +1 @@ +$ terraform import cloudflare_workers_kv_namespace.example / diff --git a/examples/resources/cloudflare_workers_kv_namespace/resource.tf b/examples/resources/cloudflare_workers_kv_namespace/resource.tf new file mode 100644 index 0000000000..3f8a59065c --- /dev/null +++ b/examples/resources/cloudflare_workers_kv_namespace/resource.tf @@ -0,0 +1,4 @@ +resource "cloudflare_workers_kv_namespace" "example" { + account_id = "f037e56e89293a057740de681ac9abbe" + title = "test-namespace" +} diff --git a/go.mod b/go.mod index 5aab5dd681..ccc2ead766 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/agext/levenshtein v1.2.3 // indirect github.com/apparentlymart/go-cidr v1.1.0 // indirect - github.com/cloudflare/cloudflare-go v0.54.0 + github.com/cloudflare/cloudflare-go v0.55.0 github.com/fatih/color v1.13.0 // indirect github.com/google/uuid v1.3.0 github.com/hashicorp/errwrap v1.1.0 // indirect diff --git a/go.sum b/go.sum index 57bb7458ba..252a8a8b12 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A= github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= @@ -31,8 +31,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cloudflare/cloudflare-go v0.54.0 h1:sC219lgpWzBHs49w5Pd4yAiDt/rr54C5G05hvo9eDbw= -github.com/cloudflare/cloudflare-go v0.54.0/go.mod h1:cfBrZN1yb+PQfCFqz4PKz5Wnnqa64SlBtlpESPAhk14= +github.com/cloudflare/cloudflare-go v0.55.0 h1:r/+AC9WX7+/G3K7DH5l58Mmnc8dIF5kyQsKW7NmNlX8= +github.com/cloudflare/cloudflare-go v0.55.0/go.mod h1:2N8L4vv3eobUgkB41tSiIJWRK4u/jJsK3IQz3EgFS+8= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= @@ -249,7 +249,7 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/urfave/cli/v2 v2.23.4/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI= +github.com/urfave/cli/v2 v2.23.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= diff --git a/internal/provider/resource_cloudflare_workers_kv.go b/internal/provider/resource_cloudflare_workers_kv.go index 57c8cfed5d..3f8441cef7 100644 --- a/internal/provider/resource_cloudflare_workers_kv.go +++ b/internal/provider/resource_cloudflare_workers_kv.go @@ -22,7 +22,7 @@ func resourceCloudflareWorkerKV() *schema.Resource { Importer: &schema.ResourceImporter{ StateContext: resourceCloudflareWorkersKVImport, }, - Description: "Provides a Workers KV Pair.", + Description: "Provides a resource to manage a Cloudflare Workers KV Pair.", } } @@ -33,7 +33,15 @@ func resourceCloudflareWorkersKVRead(ctx context.Context, d *schema.ResourceData return diag.FromErr(err) } - value, err := client.ReadWorkersKV(ctx, namespaceID, key) + accountID := d.Get("account_id").(string) + if accountID == "" { + accountID = client.AccountID + } + + value, err := client.GetWorkersKV(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.GetWorkersKVParams{ + NamespaceID: namespaceID, + Key: key, + }) if err != nil { return diag.FromErr(errors.Wrap(err, "error reading workers kv")) } @@ -43,6 +51,7 @@ func resourceCloudflareWorkersKVRead(ctx context.Context, d *schema.ResourceData return nil } + d.Set("account_id", accountID) d.Set("value", string(value)) return nil } @@ -53,7 +62,16 @@ func resourceCloudflareWorkersKVUpdate(ctx context.Context, d *schema.ResourceDa key := d.Get("key").(string) value := d.Get("value").(string) - _, err := client.WriteWorkersKV(ctx, namespaceID, key, []byte(value)) + accountID := d.Get("account_id").(string) + if accountID == "" { + accountID = client.AccountID + } + + _, err := client.WriteWorkersKVEntry(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.WriteWorkersKVEntryParams{ + NamespaceID: namespaceID, + Key: key, + Value: []byte(value), + }) if err != nil { return diag.FromErr(errors.Wrap(err, "error creating workers kv")) } @@ -72,9 +90,17 @@ func resourceCloudflareWorkersKVDelete(ctx context.Context, d *schema.ResourceDa return diag.FromErr(err) } + accountID := d.Get("account_id").(string) + if accountID == "" { + accountID = client.AccountID + } + tflog.Info(ctx, fmt.Sprintf("Deleting Cloudflare Workers KV with id: %+v", d.Id())) - _, err = client.DeleteWorkersKV(ctx, namespaceID, key) + _, err = client.DeleteWorkersKVEntry(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.DeleteWorkersKVEntryParams{ + NamespaceID: namespaceID, + Key: key, + }) if err != nil { return diag.FromErr(errors.Wrap(err, "error deleting workers kv")) } diff --git a/internal/provider/resource_cloudflare_workers_kv_namespace.go b/internal/provider/resource_cloudflare_workers_kv_namespace.go index ce4ca937f0..8e7201d2ef 100644 --- a/internal/provider/resource_cloudflare_workers_kv_namespace.go +++ b/internal/provider/resource_cloudflare_workers_kv_namespace.go @@ -3,6 +3,7 @@ package provider import ( "context" "fmt" + "strings" "github.com/cloudflare/cloudflare-go" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -21,19 +22,25 @@ func resourceCloudflareWorkersKVNamespace() *schema.Resource { Importer: &schema.ResourceImporter{ StateContext: resourceCloudflareWorkersKVNamespaceImport, }, + Description: "Provides the ability to manage Cloudflare Workers KV Namespace features.", } } func resourceCloudflareWorkersKVNamespaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { client := meta.(*cloudflare.API) - req := &cloudflare.WorkersKVNamespaceRequest{ + accountID := d.Get("account_id").(string) + if accountID == "" { + accountID = client.AccountID + } + + req := cloudflare.CreateWorkersKVNamespaceParams{ Title: d.Get("title").(string), } tflog.Debug(ctx, fmt.Sprintf("[Info] Creating Cloudflare Workers KV Namespace from struct: %+v", req)) - r, err := client.CreateWorkersKVNamespace(ctx, req) + r, err := client.CreateWorkersKVNamespace(ctx, cloudflare.AccountIdentifier(accountID), req) if err != nil { return diag.FromErr(errors.Wrap(err, "error creating workers kv namespace")) } @@ -46,14 +53,19 @@ func resourceCloudflareWorkersKVNamespaceCreate(ctx context.Context, d *schema.R tflog.Info(ctx, fmt.Sprintf("Cloudflare Workers KV Namespace ID: %s", d.Id())) - return nil + return resourceCloudflareWorkersKVNamespaceRead(ctx, d, meta) } func resourceCloudflareWorkersKVNamespaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { client := meta.(*cloudflare.API) namespaceID := d.Id() - resp, err := client.ListWorkersKVNamespaces(ctx) + accountID := d.Get("account_id").(string) + if accountID == "" { + accountID = client.AccountID + } + + resp, _, err := client.ListWorkersKVNamespaces(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.ListWorkersKVNamespacesParams{}) if err != nil { return diag.FromErr(errors.Wrap(err, "error reading workers kv namespaces")) } @@ -71,57 +83,77 @@ func resourceCloudflareWorkersKVNamespaceRead(ctx context.Context, d *schema.Res return nil } + d.Set("account_id", accountID) + return nil } func resourceCloudflareWorkersKVNamespaceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { client := meta.(*cloudflare.API) - namespace := &cloudflare.WorkersKVNamespaceRequest{ - Title: d.Get("title").(string), + accountID := d.Get("account_id").(string) + if accountID == "" { + accountID = client.AccountID } - tflog.Info(ctx, fmt.Sprintf("Updating Cloudflare Workers KV Namespace from struct %+v", namespace)) - - _, err := client.UpdateWorkersKVNamespace(ctx, d.Id(), namespace) + _, err := client.UpdateWorkersKVNamespace(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.UpdateWorkersKVNamespaceParams{ + NamespaceID: d.Id(), + Title: d.Get("title").(string), + }) if err != nil { return diag.FromErr(errors.Wrap(err, "error updating workers kv namespace")) } - return nil + return resourceCloudflareWorkersKVNamespaceRead(ctx, d, meta) } func resourceCloudflareWorkersKVNamespaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { client := meta.(*cloudflare.API) + accountID := d.Get("account_id").(string) + if accountID == "" { + accountID = client.AccountID + } tflog.Info(ctx, fmt.Sprintf("Deleting Cloudflare Workers KV Namespace with id: %+v", d.Id())) - _, err := client.DeleteWorkersKVNamespace(ctx, d.Id()) + _, err := client.DeleteWorkersKVNamespace(ctx, cloudflare.AccountIdentifier(accountID), d.Id()) if err != nil { return diag.FromErr(errors.Wrap(err, "error deleting workers kv namespace")) } + d.SetId("") return nil } func resourceCloudflareWorkersKVNamespaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + attributes := strings.SplitN(d.Id(), "/", 2) + + if len(attributes) != 2 { + return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"accountID/namespaceID\"", d.Id()) + } + + accountID, namespaceID := attributes[0], attributes[1] + d.Set("account_id", accountID) + client := meta.(*cloudflare.API) - namespaces, err := client.ListWorkersKVNamespaces(ctx) + namespaces, _, err := client.ListWorkersKVNamespaces(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.ListWorkersKVNamespacesParams{}) var title string for _, n := range namespaces { - if n.ID == d.Id() { + if n.ID == namespaceID { title = n.Title } } if err != nil { - return nil, fmt.Errorf("error finding workers kv namespace %q: %w", d.Id(), err) + return nil, fmt.Errorf("error finding workers kv namespace %q: %w", namespaceID, err) } d.Set("title", title) - d.SetId(d.Id()) + d.SetId(namespaceID) + + resourceCloudflareWorkersKVNamespaceRead(ctx, d, meta) return []*schema.ResourceData{d}, nil } diff --git a/internal/provider/resource_cloudflare_workers_kv_namespace_test.go b/internal/provider/resource_cloudflare_workers_kv_namespace_test.go index 167d6da1bd..f2b81b8f99 100644 --- a/internal/provider/resource_cloudflare_workers_kv_namespace_test.go +++ b/internal/provider/resource_cloudflare_workers_kv_namespace_test.go @@ -24,9 +24,7 @@ func TestAccCloudflareWorkersKVNamespace_Basic(t *testing.T) { Config: testAccCheckCloudflareWorkersKVNamespace(rnd), Check: resource.ComposeTestCheckFunc( testAccCheckCloudflareWorkersKVNamespaceExists(rnd, &namespace), - resource.TestCheckResourceAttr( - resourceName, "title", rnd, - ), + resource.TestCheckResourceAttr(resourceName, "title", rnd), ), }, }, @@ -41,15 +39,19 @@ func testAccCloudflareWorkersKVNamespaceDestroy(s *terraform.State) error { continue } - resp, err := client.ListWorkersKVNamespaces(context.Background()) + accountID := rs.Primary.Attributes["account_id"] + if accountID == "" { + accountID = client.AccountID + } + resp, _, err := client.ListWorkersKVNamespaces(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.ListWorkersKVNamespacesParams{}) if err == nil { return err } for _, n := range resp { if n.ID == rs.Primary.ID { - return fmt.Errorf("Namespace still exists") + return fmt.Errorf("namespace still exists but should not") } } } @@ -67,7 +69,16 @@ resource "cloudflare_workers_kv_namespace" "%[1]s" { func testAccCheckCloudflareWorkersKVNamespaceExists(title string, namespace *cloudflare.WorkersKVNamespace) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*cloudflare.API) - resp, err := client.ListWorkersKVNamespaces(context.Background()) + + rs, ok := s.RootModule().Resources["cloudflare_workers_kv_namespace."+title] + if !ok { + return fmt.Errorf("not found: %s", title) + } + accountID := rs.Primary.Attributes["account_id"] + if accountID == "" { + accountID = client.AccountID + } + resp, _, err := client.ListWorkersKVNamespaces(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.ListWorkersKVNamespacesParams{}) if err != nil { return err } diff --git a/internal/provider/resource_cloudflare_workers_kv_test.go b/internal/provider/resource_cloudflare_workers_kv_test.go index bca98db2cd..83ea976327 100644 --- a/internal/provider/resource_cloudflare_workers_kv_test.go +++ b/internal/provider/resource_cloudflare_workers_kv_test.go @@ -3,6 +3,7 @@ package provider import ( "context" "fmt" + "os" "testing" "github.com/cloudflare/cloudflare-go" @@ -78,6 +79,35 @@ func TestAccCloudflareWorkersKV_NameForcesRecreation(t *testing.T) { }) } +func TestAccCloudflareWorkersKV_WithAccountID(t *testing.T) { + t.Parallel() + var kvPair cloudflare.WorkersKVPair + name := generateRandomResourceName() + key := generateRandomResourceName() + value := generateRandomResourceName() + accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") + resourceName := "cloudflare_workers_kv." + name + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckAccount(t) + }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCloudflareWorkersKVDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckCloudflareWorkersKVWithAccount(name, key, value, accountID), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudflareWorkersKVExists(key, &kvPair), + resource.TestCheckResourceAttr(resourceName, "value", value), + resource.TestCheckResourceAttr(resourceName, "account_id", accountID), + ), + }, + }, + }) +} + func testAccCloudflareWorkersKVDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*cloudflare.API) @@ -89,7 +119,12 @@ func testAccCloudflareWorkersKVDestroy(s *terraform.State) error { namespaceID := rs.Primary.Attributes["namespace_id"] key := rs.Primary.Attributes["key"] - _, err := client.ReadWorkersKV(context.Background(), namespaceID, key) + accountID := rs.Primary.Attributes["account_id"] + if accountID == "" { + accountID = client.AccountID + } + + _, err := client.GetWorkersKV(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.GetWorkersKVParams{NamespaceID: namespaceID, Key: key}) if err == nil { return fmt.Errorf("workers kv pair still exists") @@ -108,6 +143,16 @@ resource "cloudflare_workers_kv" "%[1]s" { }`, rName, key, value) } +func testAccCheckCloudflareWorkersKVWithAccount(rName string, key string, value string, accountID string) string { + return testAccCheckCloudflareWorkersKVNamespace(rName) + fmt.Sprintf(` +resource "cloudflare_workers_kv" "%[1]s" { + account_id = "%[4]s" + namespace_id = cloudflare_workers_kv_namespace.%[1]s.id + key = "%[2]s" + value = "%[3]s" +}`, rName, key, value, accountID) +} + func testAccCheckCloudflareWorkersKVExists(key string, kv *cloudflare.WorkersKVPair) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*cloudflare.API) @@ -117,8 +162,13 @@ func testAccCheckCloudflareWorkersKVExists(key string, kv *cloudflare.WorkersKVP continue } + accountID := rs.Primary.Attributes["account_id"] + if accountID == "" { + accountID = client.AccountID + } + namespaceID := rs.Primary.Attributes["namespace_id"] - value, err := client.ReadWorkersKV(context.Background(), namespaceID, key) + value, err := client.GetWorkersKV(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.GetWorkersKVParams{NamespaceID: namespaceID, Key: key}) if err != nil { return err } diff --git a/internal/provider/schema_cloudflare_workers_kv.go b/internal/provider/schema_cloudflare_workers_kv.go index c22948976b..ebe14c2e00 100644 --- a/internal/provider/schema_cloudflare_workers_kv.go +++ b/internal/provider/schema_cloudflare_workers_kv.go @@ -4,6 +4,12 @@ import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" func resourceCloudflareWorkerKVSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ + "account_id": { + Description: "The account identifier to target for the resource.", + Type: schema.TypeString, + Optional: true, + Computed: true, + }, "key": { Type: schema.TypeString, ForceNew: true, diff --git a/internal/provider/schema_cloudflare_workers_kv_namespace.go b/internal/provider/schema_cloudflare_workers_kv_namespace.go index eac3e07e27..3a0d3e1444 100644 --- a/internal/provider/schema_cloudflare_workers_kv_namespace.go +++ b/internal/provider/schema_cloudflare_workers_kv_namespace.go @@ -4,6 +4,12 @@ import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" func resourceCloudflareWorkersKVNamespaceSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ + "account_id": { + Description: "The account identifier to target for the resource.", + Type: schema.TypeString, + Optional: true, + Computed: true, + }, "title": { Type: schema.TypeString, Required: true, diff --git a/templates/resources/workers_kv.md.tmpl b/templates/resources/workers_kv.md.tmpl index 152b87f432..5c85b75e79 100644 --- a/templates/resources/workers_kv.md.tmpl +++ b/templates/resources/workers_kv.md.tmpl @@ -10,7 +10,8 @@ description: |- {{ .Description | trimspace }} ~> This resource uses the Cloudflare account APIs. This requires setting the -`CLOUDFLARE_ACCOUNT_ID` environment variable or `account_id` provider argument. +`CLOUDFLARE_ACCOUNT_ID` environment variable or `account_id` provider argument +if you do not explicitly set the resource level `account_id` value. ## Example Usage diff --git a/templates/resources/workers_kv_namespace.md b/templates/resources/workers_kv_namespace.md deleted file mode 100644 index f3b41580e2..0000000000 --- a/templates/resources/workers_kv_namespace.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -layout: "cloudflare" -page_title: "Cloudflare: cloudflare_workers_kv_namespace" -description: Provides the ability to manage Cloudflare Workers KV Namespace features. ---- - -# cloudflare_workers_kv_namespace - -Provides a Workers KV Namespace - -## Example Usage - -```hcl -resource "cloudflare_workers_kv_namespace" "example" { - title = "test-namespace" -} -``` - -## Argument Reference - -The following arguments are supported: - -- `title` - (Required) The name of the namespace you wish to create. - -## Import - -Workers KV Namespace settings can be imported using it's ID - -``` -$ terraform import cloudflare_workers_kv_namespace.example beaeb6716c9443eaa4deef11763ccca6 -``` - -where: - -- `beaeb6716c9443eaa4deef11763ccca6` is the ID of the namespace diff --git a/templates/resources/workers_kv_namespace.md.tmpl b/templates/resources/workers_kv_namespace.md.tmpl new file mode 100644 index 0000000000..152b87f432 --- /dev/null +++ b/templates/resources/workers_kv_namespace.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") }}