Skip to content

Commit

Permalink
Add support for edge_config_item (#210)
Browse files Browse the repository at this point in the history
* Implement vercel_edge_config_item

* Remove Token requirement on resource and make attributes require replace to avoid writing an update API.

* Create Edge Config Item datasource

* Fix implementation

* Flesh out docs + couple of additional fixes

---------

Co-authored-by: Luke Phillips-Sheard <[email protected]>
  • Loading branch information
dglsparsons and LukeSheard authored Oct 11, 2024
1 parent ab51749 commit 8fa3a85
Show file tree
Hide file tree
Showing 11 changed files with 854 additions and 0 deletions.
135 changes: 135 additions & 0 deletions client/edge_config_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package client

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
)

type EdgeConfigOperation struct {
Operation string `json:"operation"`
Key string `json:"key"`
Value string `json:"value"`
}

type EdgeConfigItem struct {
TeamID string
Key string `json:"key"`
Value string `json:"value"`
EdgeConfigID string `json:"edgeConfigId"`
}

type CreateEdgeConfigItemRequest struct {
EdgeConfigID string
TeamID string
Key string
Value string
}

func (c *Client) CreateEdgeConfigItem(ctx context.Context, request CreateEdgeConfigItemRequest) (e EdgeConfigItem, err error) {
url := fmt.Sprintf("%s/v1/edge-config/%s/items", c.baseURL, request.EdgeConfigID)
teamID := c.teamID(request.TeamID)
if teamID != "" {
url = fmt.Sprintf("%s?teamId=%s", url, teamID)
}

payload := string(mustMarshal(
struct {
Items []EdgeConfigOperation `json:"items"`
}{
Items: []EdgeConfigOperation{
{
Operation: "upsert",
Key: request.Key,
Value: request.Value,
},
},
},
))
tflog.Info(ctx, "creating edge config token", map[string]interface{}{
"url": url,
"payload": payload,
})
err = c.doRequest(clientRequest{
ctx: ctx,
method: "PATCH",
url: url,
body: payload,
}, nil)

return EdgeConfigItem{
Key: request.Key,
Value: request.Value,
EdgeConfigID: request.EdgeConfigID,
TeamID: teamID,
}, err
}

type EdgeConfigItemRequest struct {
EdgeConfigID string
TeamID string
Key string
Value string
}

func (c *Client) DeleteEdgeConfigItem(ctx context.Context, request EdgeConfigItemRequest) error {
url := fmt.Sprintf("%s/v1/edge-config/%s/items", c.baseURL, request.EdgeConfigID)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}

payload := string(mustMarshal(
struct {
Items []EdgeConfigOperation `json:"items"`
}{
Items: []EdgeConfigOperation{
{
Operation: "delete",
Key: request.Key,
Value: request.Value,
},
},
},
))

tflog.Info(ctx, "deleting edge config token", map[string]interface{}{
"url": url,
"payload": payload,
})
return c.doRequest(clientRequest{
ctx: ctx,
method: "PATCH",
url: url,
body: payload,
}, nil)
}

func (c *Client) GetEdgeConfigItem(ctx context.Context, request EdgeConfigItemRequest) (e EdgeConfigItem, err error) {
url := fmt.Sprintf("%s/v1/edge-config/%s/item/%s", c.baseURL, request.EdgeConfigID, request.Key)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}

tflog.Info(ctx, "getting edge config token", map[string]interface{}{
"url": url,
})

err = c.doRequest(clientRequest{
ctx: ctx,
method: "GET",
url: url,
errorOnNoContent: true,
}, &e)

if noContent(err) {
return e, APIError{
StatusCode: 404,
Message: "Edge Config Item not found",
Code: "not_found",
}
}

e.TeamID = c.teamID(request.TeamID)
return e, err
}
46 changes: 46 additions & 0 deletions docs/data-sources/edge_config_item.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_edge_config_item Data Source - terraform-provider-vercel"
subcategory: ""
description: |-
Provides the value of an existing Edge Config Item.
An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.
An Edge Config Item is a value within an Edge Config.
---

# vercel_edge_config_item (Data Source)

Provides the value of an existing Edge Config Item.

An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.

An Edge Config Item is a value within an Edge Config.

## Example Usage

```terraform
data "vercel_edge_config" "example" {
id = "ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
data "vercel_edge_config_item" "test" {
id = data.vercel_edge_config.example.id
key = "foobar"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `id` (String) The ID of the Edge Config that the item should exist under.
- `key` (String) The name of the key you want to retrieve within your Edge Config.

### Optional

- `team_id` (String) The ID of the team the Edge Config should exist under. Required when configuring a team resource if a default team has not been set in the provider.

### Read-Only

- `value` (String) The value assigned to the key.
62 changes: 62 additions & 0 deletions docs/resources/edge_config_item.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_edge_config_item Resource - terraform-provider-vercel"
subcategory: ""
description: |-
Provides an Edge Config Item.
An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.
An Edge Config Item is a value within an Edge Config.
---

# vercel_edge_config_item (Resource)

Provides an Edge Config Item.

An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.

An Edge Config Item is a value within an Edge Config.

## Example Usage

```terraform
resource "vercel_edge_config" "example" {
name = "example"
}
resource "vercel_edge_config_item" "example" {
edge_config_id = vercel_edge_config.example.id
key = "foobar"
value = "baz"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `edge_config_id` (String) The ID of the Edge Config store.
- `key` (String) The name of the key you want to add to or update within your Edge Config.
- `value` (String) The value you want to assign to the key.

### Optional

- `team_id` (String) The ID of the team the Edge Config should exist under. Required when configuring a team resource if a default team has not been set in the provider.

## Import

Import is supported using the following syntax:

```shell
# If importing into a personal account, or with a team configured on
# the provider, simply use the edge config id and the key of the item to import.
# - edge_config_id can be found by navigating to the Edge Config in the Vercel UI. It should begin with `ecfg_`.
# - key is the key of teh item to import.
terraform import vercel_edge_config.example ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/example_key

# Alternatively, you can import via the team_id, edge_config_id and the key of the item to import.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - edge_config_id can be found by navigating to the Edge Config in the Vercel UI. It should begin with `ecfg_`.
# - key is the key of the item to import.
terraform import vercel_edge_config.example team_xxxxxxxxxxxxxxxxxxxxxxxx/ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/example_key
```
8 changes: 8 additions & 0 deletions examples/data-sources/vercel_edge_config_item/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data "vercel_edge_config" "example" {
id = "ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

data "vercel_edge_config_item" "test" {
id = data.vercel_edge_config.example.id
key = "foobar"
}
11 changes: 11 additions & 0 deletions examples/resources/vercel_edge_config_item/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# If importing into a personal account, or with a team configured on
# the provider, simply use the edge config id and the key of the item to import.
# - edge_config_id can be found by navigating to the Edge Config in the Vercel UI. It should begin with `ecfg_`.
# - key is the key of teh item to import.
terraform import vercel_edge_config.example ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/example_key

# Alternatively, you can import via the team_id, edge_config_id and the key of the item to import.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - edge_config_id can be found by navigating to the Edge Config in the Vercel UI. It should begin with `ecfg_`.
# - key is the key of the item to import.
terraform import vercel_edge_config.example team_xxxxxxxxxxxxxxxxxxxxxxxx/ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/example_key
9 changes: 9 additions & 0 deletions examples/resources/vercel_edge_config_item/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "vercel_edge_config" "example" {
name = "example"
}

resource "vercel_edge_config_item" "example" {
edge_config_id = vercel_edge_config.example.id
key = "foobar"
value = "baz"
}
Loading

0 comments on commit 8fa3a85

Please sign in to comment.