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

Bump github.com/cloudflare/cloudflare-go from 0.54.0 to 0.55.0 #2049

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
15 changes: 15 additions & 0 deletions .changelog/2049.txt
Original file line number Diff line number Diff line change
@@ -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
```
15 changes: 11 additions & 4 deletions docs/resources/workers_kv.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
44 changes: 27 additions & 17 deletions docs/resources/workers_kv_namespace.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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 <account_id>/<namespace_id>
```
4 changes: 3 additions & 1 deletion examples/resources/cloudflare_workers_kv/resource.tf
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ terraform import cloudflare_workers_kv_namespace.example <account_id>/<namespace_id>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "cloudflare_workers_kv_namespace" "example" {
account_id = "f037e56e89293a057740de681ac9abbe"
title = "test-namespace"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
34 changes: 30 additions & 4 deletions internal/provider/resource_cloudflare_workers_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
}
}

Expand All @@ -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"))
}
Expand All @@ -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
}
Expand All @@ -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"))
}
Expand All @@ -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"))
}
Expand Down
62 changes: 47 additions & 15 deletions internal/provider/resource_cloudflare_workers_kv_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"strings"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand All @@ -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"))
}
Expand All @@ -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"))
}
Expand All @@ -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
}
Loading