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

Add support for shared environment variables #110

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
May 15, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.20"
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v5
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.20"
id: go
- name: Install Task
uses: arduino/setup-task@v1
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.20"
id: go
- uses: hashicorp/setup-terraform@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tasks:
cmds:
- go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@v0.7.0
status:
- which staticcheck
Copy link
Member

Choose a reason for hiding this comment

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

woops

- which tfplugindocs

docs:
desc: "Update the docs generated from description fields"
Expand All @@ -46,7 +46,7 @@ tasks:
install-staticcheck:
desc: "Install the staticheck tool"
cmds:
- go install honnef.co/go/tools/cmd/staticcheck@2022.1.3
- go install honnef.co/go/tools/cmd/staticcheck@2023.1
status:
- which staticcheck

Expand Down
72 changes: 72 additions & 0 deletions client/shared_environment_variable_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package client

import (
"context"
"fmt"
"net/http"
"strings"

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

type SharedEnvironmentVariableResponse struct {
Key string `json:"key"`
TeamID string `json:"ownerId"`
ID string `json:"id,omitempty"`
Value string `json:"value"`
Type string `json:"type"`
Target []string `json:"target"`
ProjectIDs []string `json:"projectId"`
}

type SharedEnvVarRequest struct {
Key string `json:"key"`
Value string `json:"value"`
}

type SharedEnvironmentVariableRequest struct {
Type string `json:"type"`
ProjectIDs []string `json:"projectId"`
Target []string `json:"target"`
EnvironmentVariables []SharedEnvVarRequest `json:"evs"`
}

type CreateSharedEnvironmentVariableRequest struct {
EnvironmentVariable SharedEnvironmentVariableRequest
TeamID string
}

// CreateSharedEnvironmentVariable will create a brand new shared environment variable if one does not exist.
func (c *Client) CreateSharedEnvironmentVariable(ctx context.Context, request CreateSharedEnvironmentVariableRequest) (e SharedEnvironmentVariableResponse, err error) {
url := fmt.Sprintf("%s/v1/env", c.baseURL)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(request.EnvironmentVariable))
req, err := http.NewRequestWithContext(
ctx,
"POST",
url,
strings.NewReader(payload),
)
if err != nil {
return e, err
}
tflog.Trace(ctx, "creating shared environment variable", map[string]interface{}{
"url": url,
"payload": payload,
})
var response struct {
Created []SharedEnvironmentVariableResponse `json:"created"`
}
err = c.doRequest(req, &response)
if err != nil {
return e, err
}
if len(response.Created) != 1 {
return e, fmt.Errorf("expected 1 environment variable to be created, got %d", len(response.Created))
}
// Override the value, as it returns the encrypted value.
response.Created[0].Value = request.EnvironmentVariable.EnvironmentVariables[0].Value
return response.Created[0], err
}
39 changes: 39 additions & 0 deletions client/shared_environment_variable_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package client

import (
"context"
"fmt"
"net/http"
"strings"

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

// DeleteSharedEnvironmentVariable will remove a shared environment variable from Vercel.
func (c *Client) DeleteSharedEnvironmentVariable(ctx context.Context, teamID, variableID string) error {
url := fmt.Sprintf("%s/v1/env", c.baseURL)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
payload := string(mustMarshal(struct {
IDs []string `json:"ids"`
}{
IDs: []string{
variableID,
},
}))
req, err := http.NewRequestWithContext(
ctx,
"DELETE",
url,
strings.NewReader(payload),
)
if err != nil {
return err
}

tflog.Trace(ctx, "deleting shared environment variable", map[string]interface{}{
"url": url,
})
return c.doRequest(req, nil)
}
31 changes: 31 additions & 0 deletions client/shared_environment_variable_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package client

import (
"context"
"fmt"
"net/http"

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

func (c *Client) GetSharedEnvironmentVariable(ctx context.Context, teamID, envID string) (e SharedEnvironmentVariableResponse, err error) {
url := fmt.Sprintf("%s/v1/env/%s", c.baseURL, envID)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
req, err := http.NewRequestWithContext(
ctx,
"GET",
url,
nil,
)
if err != nil {
return e, err
}
tflog.Trace(ctx, "getting shared environment variable", map[string]interface{}{
"url": url,
})
err = c.doRequest(req, &e)
e.TeamID = c.teamID(teamID)
return e, err
}
61 changes: 61 additions & 0 deletions client/shared_environment_variable_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package client

import (
"context"
"fmt"
"net/http"
"strings"

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

type UpdateSharedEnvironmentVariableRequest struct {
Key string `json:"key"`
Value string `json:"value"`
Type string `json:"type"`
ProjectIDs []string `json:"projectId"`
Target []string `json:"target"`
TeamID string `json:"-"`
EnvID string `json:"-"`
}

func (c *Client) UpdateSharedEnvironmentVariable(ctx context.Context, request UpdateSharedEnvironmentVariableRequest) (e SharedEnvironmentVariableResponse, err error) {
url := fmt.Sprintf("%s/v1/env", c.baseURL)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(struct {
Updates map[string]UpdateSharedEnvironmentVariableRequest `json:"updates"`
}{
Updates: map[string]UpdateSharedEnvironmentVariableRequest{
request.EnvID: request,
},
}))
req, err := http.NewRequestWithContext(
ctx,
"PATCH",
url,
strings.NewReader(payload),
)
if err != nil {
return e, err
}

tflog.Trace(ctx, "updating shared environment variable", map[string]interface{}{
"url": url,
"payload": payload,
})
var response struct {
Updated []SharedEnvironmentVariableResponse `json:"updated"`
}
err = c.doRequest(req, &response)
if err != nil {
return e, err
}
if len(response.Updated) != 1 {
return e, fmt.Errorf("expected 1 environment variable to be created, got %d", len(response.Updated))
}
// Override the value, as it returns the encrypted value.
response.Updated[0].Value = request.Value
return response.Updated[0], err
}
70 changes: 70 additions & 0 deletions docs/resources/shared_environment_variable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_shared_environment_variable Resource - terraform-provider-vercel"
subcategory: ""
description: |-
Provides a Shared Environment Variable resource.
A Shared Environment Variable resource defines an Environment Variable that can be shared between multiple Vercel Projects.
For more detailed information, please see the Vercel documentation https://vercel.com/docs/concepts/projects/environment-variables/shared-environment-variables.
---

# vercel_shared_environment_variable (Resource)

Provides a Shared Environment Variable resource.

A Shared Environment Variable resource defines an Environment Variable that can be shared between multiple Vercel Projects.

For more detailed information, please see the [Vercel documentation](https://vercel.com/docs/concepts/projects/environment-variables/shared-environment-variables).

## Example Usage

```terraform
resource "vercel_project" "example" {
name = "example"

git_repository = {
type = "github"
repo = "vercel/some-repo"
}
}

# An environment variable that will be created
# and associated with the "example" project.
resource "vercel_shared_environment_variable" "example" {
key = "EXAMPLE"
value = "some_value"
target = ["production"]
project_ids = [
vercel_project.example.id
]
}
```

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

### Required

- `key` (String) The name of the Environment Variable.
- `project_ids` (Set of String) The ID of the Vercel project.
- `target` (Set of String) The environments that the Environment Variable should be present on. Valid targets are either `production`, `preview`, or `development`.
- `value` (String, Sensitive) The value of the Environment Variable.

### Optional

- `team_id` (String) The ID of the Vercel team. Shared environment variables require a team.

### Read-Only

- `id` (String) The ID of the Environment Variable.

## Import

Import is supported using the following syntax:

```shell
# You can import via the team_id and environment variable id.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - environment variable id can be taken from the network tab on the shared environment variable page.
terraform import vercel_shared_environment_variable.example team_xxxxxxxxxxxxxxxxxxxxxxxx/env_yyyyyyyyyyyyy
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# You can import via the team_id and environment variable id.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - environment variable id can be taken from the network tab on the shared environment variable page.
terraform import vercel_shared_environment_variable.example team_xxxxxxxxxxxxxxxxxxxxxxxx/env_yyyyyyyyyyyyy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
resource "vercel_project" "example" {
name = "example"

git_repository = {
type = "github"
repo = "vercel/some-repo"
}
}

# A shared environment variable that will be created
# and associated with the "example" project.
resource "vercel_shared_environment_variable" "example" {
key = "EXAMPLE"
value = "some_value"
target = ["production"]
project_ids = [
vercel_project.example.id
]
}
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/hashicorp/terraform-plugin-framework v1.2.0
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-go v0.15.0
github.com/hashicorp/terraform-plugin-log v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
Expand Down Expand Up @@ -32,27 +32,27 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.15.0 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.0 // indirect
github.com/hashicorp/terraform-svchost v0.1.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.12.1 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230320184635-7606e756e683 // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
Loading