-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2945c4d
Add support for shared environment variables
dglsparsons 6a26ffd
Skip DNS Record test until a test domain exists
dglsparsons 04dd28f
Update deps + Go version
dglsparsons 720e76f
Fix test
dglsparsons 1739230
PR feedback
dglsparsons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
4 changes: 4 additions & 0 deletions
4
examples/resources/vercel_shared_environment_variable/import.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
19 changes: 19 additions & 0 deletions
19
examples/resources/vercel_shared_environment_variable/resource.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woops