这是indexloc提供的服务,不要输入任何密码
Skip to content

Add support for edge_config_item #210

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

Merged
merged 5 commits into from
Oct 11, 2024
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
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this patch instead of post?

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this patch instead of delete?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The endpoint is to update the contents of an edge-config, rather than to manage a single edge config item. So you submit a list of 'operations', saying what you want to happen to each item.

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
```
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
Loading