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

Implement Workers KV Namespaces #443

Merged
merged 11 commits into from
Nov 28, 2019
1 change: 1 addition & 0 deletions cloudflare/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func Provider() terraform.ResourceProvider {
"cloudflare_waf_rule": resourceCloudflareWAFRule(),
"cloudflare_worker_route": resourceCloudflareWorkerRoute(),
"cloudflare_worker_script": resourceCloudflareWorkerScript(),
"cloudflare_workers_kv_namespace": resourceCloudflareWorkersKVNamespace(),
"cloudflare_zone_lockdown": resourceCloudflareZoneLockdown(),
"cloudflare_zone_settings_override": resourceCloudflareZoneSettingsOverride(),
"cloudflare_zone": resourceCloudflareZone(),
Expand Down
132 changes: 132 additions & 0 deletions cloudflare/resource_cloudflare_workers_kv_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package cloudflare

import (
"context"
"fmt"
"log"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"
)

func resourceCloudflareWorkersKVNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareWorkersKVNamespaceCreate,
Read: resourceCloudflareWorkersKVNamespaceRead,
Update: resourceCloudflareWorkersKVNamespaceUpdate,
Delete: resourceCloudflareWorkersKVNamespaceDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudflareWorkersKVNamespaceImport,
},

Schema: map[string]*schema.Schema{
"title": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func resourceCloudflareWorkersKVNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

req := &cloudflare.WorkersKVNamespaceRequest{
Title: d.Get("title").(string),
}

log.Printf("[Info] Creating Cloudflare Workers KV Namespace from struct: %+v", req)

r, err := client.CreateWorkersKVNamespace(context.Background(), req)
if err != nil {
return errors.Wrap(err, "error creating workers kv namespace")
}

if r.Result.ID == "" {
return fmt.Errorf("failed to find id in Create response; resource was empty")
}

d.SetId(r.Result.ID)

log.Printf("[INFO] Cloudflare Workers KV Namespace ID: %s", d.Id())

return nil
}

func resourceCloudflareWorkersKVNamespaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
namespaceId := d.Id()

resp, err := client.ListWorkersKVNamespaces(context.Background())
if err != nil {
return errors.Wrap(err, "error reading workers kv namespaces")
}

var namespace cloudflare.WorkersKVNamespace
for _, r := range resp.Result {
if r.ID == namespaceId {
namespace = r
break
}
}

if namespace.ID == "" {
d.SetId("")
return nil
}

return nil
}

func resourceCloudflareWorkersKVNamespaceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

namespace := &cloudflare.WorkersKVNamespaceRequest{
Title: d.Get("title").(string),
}

log.Printf("[INFO] Updating Cloudflare Workers KV Namespace from struct %+v", namespace)

_, err := client.UpdateWorkersKVNamespace(context.Background(), d.Id(), namespace)
if err != nil {
return errors.Wrap(err, "error updating workers kv namespace")
}

return nil
}

func resourceCloudflareWorkersKVNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

log.Printf("[INFO] Deleting Cloudflare Workers KV Namespace with id: %+v", d.Id())

_, err := client.DeleteWorkersKVNamespace(context.Background(), d.Id())
if err != nil {
return errors.Wrap(err, "error deleting workers kv namespace")
}

return nil
}

func resourceCloudflareWorkersKVNamespaceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*cloudflare.API)

namespaces, err := client.ListWorkersKVNamespaces(context.Background())
var title string

for _, n := range namespaces.Result {
if n.ID == d.Id() {
title = n.Title
}
}

if err != nil {
return nil, fmt.Errorf("error finding workers kv namespace %q: %s", d.Id(), err)
}

d.Set("title", title)
d.SetId(d.Id())

return []*schema.ResourceData{d}, nil
}
84 changes: 84 additions & 0 deletions cloudflare/resource_cloudflare_workers_kv_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cloudflare

import (
"context"
"fmt"
"testing"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccCloudflareWorkersKVNamespace_Basic(t *testing.T) {
t.Parallel()
var namespace cloudflare.WorkersKVNamespace
rnd := generateRandomResourceName()
resourceName := "cloudflare_workers_kv_namespace." + rnd
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCloudflareWorkersKVNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareWorkersKVNamespace(rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareWorkersKVNamespaceExists(rnd, &namespace),
resource.TestCheckResourceAttr(
resourceName, "title", rnd,
),
),
},
},
})
}

func testAccCloudflareWorkersKVNamespaceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudflare.API)

for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudflare_workers_kv_namespace" {
continue
}

resp, err := client.ListWorkersKVNamespaces(context.Background())

if err == nil {
return err
}

for _, n := range resp.Result {
if n.ID == rs.Primary.ID {
return fmt.Errorf("Namespace still exists")
}
}
}

return nil
}

func testAccCheckCloudflareWorkersKVNamespace(rName string) string {
return fmt.Sprintf(`
resource "cloudflare_workers_kv_namespace" "%[1]s" {
title = "%[1]s"
}`, rName)
}

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())
if err != nil {
return err
}

for _, n := range resp.Result {
if n.Title == title {
*namespace = n
return nil
}
}

return fmt.Errorf("namespace not found")
}
}
3 changes: 3 additions & 0 deletions website/cloudflare.erb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
<li<%= sidebar_current("docs-cloudflare-resource-worker-script") %>>
<a href="/docs/providers/cloudflare/r/worker_script.html">cloudflare_worker_script</a>
</li>
<li<%= sidebar_current("docs-cloudflare-resource-workers-kv-namespace") %>>
<a href="/docs/providers/cloudflare/r/workers_kv_namespace.html">cloudflare_workers_kv_namespace</a>
</li>
<li<%= sidebar_current("docs-cloudflare-resource-zone") %>>
<a href="/docs/providers/cloudflare/r/zone.html">cloudflare_zone</a>
</li>
Expand Down
37 changes: 37 additions & 0 deletions website/docs/r/workers_kv_namespace.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
layout: "cloudflare"
page_title: "Cloudflare: cloudflare_workers_kv_namespace"
sidebar_current: "docs-cloudflare-resource-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