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

Add Support for Edge Config Tokens #162

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 1 commit into from
Apr 15, 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
4 changes: 2 additions & 2 deletions client/edge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type CreateEdgeConfigRequest struct {
TeamID string `json:"-"`
}

func (c *Client) CreateEdgeConfig(ctx context.Context, request CreateEdgeConfigRequest) (e *EdgeConfig, err error) {
func (c *Client) CreateEdgeConfig(ctx context.Context, request CreateEdgeConfigRequest) (e EdgeConfig, err error) {
url := fmt.Sprintf("%s/v1/edge-config", c.baseURL)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
Expand All @@ -37,7 +37,7 @@ func (c *Client) CreateEdgeConfig(ctx context.Context, request CreateEdgeConfigR
return e, err
}

func (c *Client) GetEdgeConfig(ctx context.Context, id, teamID string) (e *EdgeConfig, err error) {
func (c *Client) GetEdgeConfig(ctx context.Context, id, teamID string) (e EdgeConfig, err error) {
url := fmt.Sprintf("%s/v1/edge-config/%s", c.baseURL, id)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
Expand Down
102 changes: 102 additions & 0 deletions client/edge_config_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package client

import (
"context"
"fmt"

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

type EdgeConfigToken struct {
TeamID string
Token string `json:"token"`
Label string `json:"label"`
ID string `json:"id"`
EdgeConfigID string `json:"edgeConfigId"`
}

func (e EdgeConfigToken) ConnectionString() string {
return fmt.Sprintf(
"https://edge-config.vercel.com/%s?token=%s",
e.EdgeConfigID,
e.Token,
)
}

type CreateEdgeConfigTokenRequest struct {
Label string `json:"label"`
TeamID string `json:"-"`
EdgeConfigID string `json:"-"`
}

func (c *Client) CreateEdgeConfigToken(ctx context.Context, request CreateEdgeConfigTokenRequest) (e EdgeConfigToken, err error) {
url := fmt.Sprintf("%s/v1/edge-config/%s/token", c.baseURL, request.EdgeConfigID)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(request))
tflog.Trace(ctx, "creating edge config token", map[string]interface{}{
"url": url,
"payload": payload,
})
err = c.doRequest(clientRequest{
ctx: ctx,
method: "POST",
url: url,
body: payload,
}, &e)
e.Label = request.Label
e.TeamID = request.TeamID
e.EdgeConfigID = request.EdgeConfigID
return e, err
}

type EdgeConfigTokenRequest struct {
TeamID string
EdgeConfigID string
Token string
}

func (c *Client) DeleteEdgeConfigToken(ctx context.Context, request EdgeConfigTokenRequest) error {
url := fmt.Sprintf("%s/v1/edge-config/%s/tokens", c.baseURL, request.EdgeConfigID)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(
struct {
Tokens []string `json:"tokens"`
}{
Tokens: []string{request.Token},
},
))

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

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

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

err = c.doRequest(clientRequest{
ctx: ctx,
method: "GET",
url: url,
}, &e)
e.TeamID = request.TeamID
return e, err
}
10 changes: 8 additions & 2 deletions docs/data-sources/edge_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
page_title: "vercel_edge_config Data Source - terraform-provider-vercel"
subcategory: ""
description: |-
Provides information about an existing Edge Config resource.
Provides information about an existing Edge Config.
An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.
---

# vercel_edge_config (Data Source)

Provides information about an existing Edge Config resource.
Provides information about an existing Edge Config.

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

## Example Usage

```terraform
data "vercel_edge_config" "example" {
id = "ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
44 changes: 44 additions & 0 deletions docs/data-sources/edge_config_token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_edge_config_token Data Source - terraform-provider-vercel"
subcategory: ""
description: |-
Provides information about an existing Edge Config Token.
An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.
An Edge Config token is used to authenticate against an Edge Config's endpoint.
---

# vercel_edge_config_token (Data Source)

Provides information about an existing Edge Config Token.

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

An Edge Config token is used to authenticate against an Edge Config's endpoint.

## Example Usage

```terraform
data "vercel_edge_config_token" "test" {
edge_config_id = "ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
token = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```

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

### Required

- `edge_config_id` (String) The label of the Edge Config Token.
- `token` (String) A read access token used for authenticating against the Edge Config's endpoint for high volume, low-latency requests.

### 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

- `connection_string` (String) A connection string is a URL that connects a project to an Edge Config. The variable can be called anything, but our Edge Config client SDK will search for process.env.EDGE_CONFIG by default.
- `id` (String) The ID of this resource.
- `label` (String) The label of the Edge Config Token.
18 changes: 17 additions & 1 deletion docs/resources/edge_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ An Edge Config is a global data store that enables experimentation with feature

```terraform
resource "vercel_edge_config" "example" {
name = "my-edge-config"
name = "example"
}

resource "vercel_project" "example" {
name = "edge-config-example"
}

resource "vercel_edge_config_token" "example" {
edge_config_id = vercel_edge_config.example.id
label = "example token"
}

resource "vercel_project_environment_variable" "example" {
project_id = vercel_project.example.id
target = ["production", "preview", "development"]
key = "EDGE_CONFIG"
value = vercel_edge_config_token.example.connection_string
}
```

Expand Down
77 changes: 77 additions & 0 deletions docs/resources/edge_config_token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_edge_config_token Resource - terraform-provider-vercel"
subcategory: ""
description: |-
Provides an Edge Config Token resource.
An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.
An Edge Config token is used to authenticate against an Edge Config's endpoint.
---

# vercel_edge_config_token (Resource)

Provides an Edge Config Token resource.

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

An Edge Config token is used to authenticate against an Edge Config's endpoint.

## Example Usage

```terraform
resource "vercel_edge_config" "example" {
name = "example"
}

resource "vercel_project" "example" {
name = "edge-config-example"
}

resource "vercel_edge_config_token" "example" {
edge_config_id = vercel_edge_config.example.id
label = "example token"
}

resource "vercel_project_environment_variable" "example" {
project_id = vercel_project.example.id
target = ["production", "preview", "development"]
key = "EDGE_CONFIG"
value = vercel_edge_config_token.example.connection_string
}
```

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

### Required

- `edge_config_id` (String) The label of the Edge Config Token.
- `label` (String) The label of the Edge Config Token.

### 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

- `connection_string` (String) A connection string is a URL that connects a project to an Edge Config. The variable can be called anything, but our Edge Config client SDK will search for process.env.EDGE_CONFIG by default.
- `id` (String) The ID of this resource.
- `token` (String) A read access token used for authenticating against the Edge Config's endpoint for high volume, low-latency requests.

## 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 token value.
# - edge_config_id is hard to find, but can be found by navigating to the Edge Config in the Vercel UI and looking at the URL. It should begin with `ecfg_`.
# - token can be found in the Vercel UI under Storage, Edge Config, the specific Edge Config, Tokens.
terraform import vercel_edge_config.example ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

# Alternatively, you can import via the team_id and edge_config_id.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - edge_config_id is hard to find, but can be found by navigating to the Edge Config in the Vercel UI and looking at the URL. It should begin with `ecfg_`.
# - token can be found in the Vercel UI under Storage, Edge Config, the specific Edge Config, Tokens.
terraform import vercel_edge_config.example team_xxxxxxxxxxxxxxxxxxxxxxxx/ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
```
3 changes: 3 additions & 0 deletions examples/data-sources/vercel_edge_config/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "vercel_edge_config" "example" {
id = "ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "vercel_edge_config_token" "test" {
edge_config_id = "ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
token = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
18 changes: 17 additions & 1 deletion examples/resources/vercel_edge_config/resource.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
resource "vercel_edge_config" "example" {
name = "my-edge-config"
name = "example"
}

resource "vercel_project" "example" {
name = "edge-config-example"
}

resource "vercel_edge_config_token" "example" {
edge_config_id = vercel_edge_config.example.id
label = "example token"
}

resource "vercel_project_environment_variable" "example" {
project_id = vercel_project.example.id
target = ["production", "preview", "development"]
key = "EDGE_CONFIG"
value = vercel_edge_config_token.example.connection_string
}
11 changes: 11 additions & 0 deletions examples/resources/vercel_edge_config_token/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 token value.
# - edge_config_id is hard to find, but can be found by navigating to the Edge Config in the Vercel UI and looking at the URL. It should begin with `ecfg_`.
# - token can be found in the Vercel UI under Storage, Edge Config, the specific Edge Config, Tokens.
terraform import vercel_edge_config.example ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

# Alternatively, you can import via the team_id and edge_config_id.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - edge_config_id is hard to find, but can be found by navigating to the Edge Config in the Vercel UI and looking at the URL. It should begin with `ecfg_`.
# - token can be found in the Vercel UI under Storage, Edge Config, the specific Edge Config, Tokens.
terraform import vercel_edge_config.example team_xxxxxxxxxxxxxxxxxxxxxxxx/ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
19 changes: 19 additions & 0 deletions examples/resources/vercel_edge_config_token/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
resource "vercel_edge_config" "example" {
name = "example"
}

resource "vercel_project" "example" {
name = "edge-config-example"
}

resource "vercel_edge_config_token" "example" {
edge_config_id = vercel_edge_config.example.id
label = "example token"
}

resource "vercel_project_environment_variable" "example" {
project_id = vercel_project.example.id
target = ["production", "preview", "development"]
key = "EDGE_CONFIG"
value = vercel_edge_config_token.example.connection_string
}
2 changes: 1 addition & 1 deletion vercel/data_source_edge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (d *edgeConfigDataSource) Configure(ctx context.Context, req datasource.Con
func (r *edgeConfigDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: `
Provides information about an existing Edge Config resource.
Provides information about an existing Edge Config.

An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.`,
Attributes: map[string]schema.Attribute{
Expand Down
Loading