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

Support setting apply_to_all_custom_environments on vercel_shared_environment_variable resources #329

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
May 19, 2025
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
43 changes: 23 additions & 20 deletions client/shared_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
)

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"`
Comment string `json:"comment"`
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"`
Comment string `json:"comment"`
ApplyToAllCustomEnvironments bool `json:"applyToAllCustomEnvironments"`
}

type SharedEnvVarRequest struct {
Expand All @@ -25,10 +26,11 @@ type SharedEnvVarRequest struct {
}

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

type CreateSharedEnvironmentVariableRequest struct {
Expand Down Expand Up @@ -169,14 +171,15 @@ type UpdateSharedEnvironmentVariableRequestProjectIDUpdates struct {
}

type UpdateSharedEnvironmentVariableRequest struct {
Value string `json:"value,omitempty"`
Type string `json:"type,omitempty"`
ProjectIDs []string `json:"projectId,omitempty"`
ProjectIDUpdates UpdateSharedEnvironmentVariableRequestProjectIDUpdates `json:"projectIdUpdates,omitempty"`
Target []string `json:"target,omitempty"`
Comment string `json:"comment,omitempty"`
TeamID string `json:"-"`
EnvID string `json:"-"`
Value string `json:"value,omitempty"`
Type string `json:"type,omitempty"`
ProjectIDs []string `json:"projectId,omitempty"`
ProjectIDUpdates UpdateSharedEnvironmentVariableRequestProjectIDUpdates `json:"projectIdUpdates,omitempty"`
ApplyToAllCustomEnvironments bool `json:"applyToAllCustomEnvironments,omitempty"`
Target []string `json:"target,omitempty"`
Comment string `json:"comment,omitempty"`
TeamID string `json:"-"`
EnvID string `json:"-"`
}

func (c *Client) UpdateSharedEnvironmentVariable(ctx context.Context, request UpdateSharedEnvironmentVariableRequest) (e SharedEnvironmentVariableResponse, err error) {
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/shared_environment_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ data "vercel_shared_environment_variable" "example_by_key_and_target" {

### Read-Only

- `apply_to_all_custom_environments` (Boolean) Whether the Environment Variable should be applied to all custom environments.
- `comment` (String) A comment explaining what the environment variable is for.
- `project_ids` (Set of String) The ID of the Vercel project.
- `sensitive` (Boolean) Whether the Environment Variable is sensitive or not.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/shared_environment_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ resource "vercel_shared_environment_variable" "example" {

### Optional

- `apply_to_all_custom_environments` (Boolean) Whether the shared environment variable should be applied to all custom environments in the linked projects.
- `comment` (String) A comment explaining what the environment variable is for.
- `sensitive` (Boolean) Whether the Environment Variable is sensitive or not. (May be affected by a [team-wide environment variable policy](https://vercel.com/docs/projects/environment-variables/sensitive-environment-variables#environment-variables-policy))
- `team_id` (String) The ID of the Vercel team. Shared environment variables require a team.
Expand Down
4 changes: 4 additions & 0 deletions vercel/data_source_shared_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ For more detailed information, please see the [Vercel documentation](https://ver
Description: "A comment explaining what the environment variable is for.",
Computed: true,
},
"apply_to_all_custom_environments": schema.BoolAttribute{
Description: "Whether the Environment Variable should be applied to all custom environments.",
Computed: true,
},
},
}
}
Expand Down
2 changes: 2 additions & 0 deletions vercel/data_source_shared_environment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestAcc_SharedEnvironmentVariableDataSource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.vercel_shared_environment_variable.test", "key", "test_acc_"+name),
resource.TestCheckResourceAttr("data.vercel_shared_environment_variable.test", "value", "foobar"),
resource.TestCheckResourceAttr("data.vercel_shared_environment_variable.test", "apply_to_all_custom_environments", "true"),
resource.TestCheckTypeSetElemAttr("data.vercel_shared_environment_variable.test", "target.*", "production"),
resource.TestCheckTypeSetElemAttr("data.vercel_shared_environment_variable.test", "target.*", "preview"),
resource.TestCheckResourceAttr("data.vercel_shared_environment_variable.test", "sensitive", "false"),
Expand Down Expand Up @@ -49,6 +50,7 @@ resource "vercel_shared_environment_variable" "test" {
value = "foobar"
target = [ "production", "preview" ]
project_ids = [ vercel_project.test.id ]
apply_to_all_custom_environments = true
}

data "vercel_shared_environment_variable" "test" {
Expand Down
61 changes: 35 additions & 26 deletions vercel/resource_shared_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,26 @@ For more detailed information, please see the [Vercel documentation](https://ver
stringvalidator.LengthBetween(0, 1000),
},
},
"apply_to_all_custom_environments": schema.BoolAttribute{
Description: "Whether the shared environment variable should be applied to all custom environments in the linked projects.",
Optional: true,
Computed: true,
},
},
}
}

// SharedEnvironmentVariable reflects the state terraform stores internally for a project environment variable.
type SharedEnvironmentVariable struct {
Target types.Set `tfsdk:"target"`
Key types.String `tfsdk:"key"`
Value types.String `tfsdk:"value"`
TeamID types.String `tfsdk:"team_id"`
ProjectIDs types.Set `tfsdk:"project_ids"`
ID types.String `tfsdk:"id"`
Sensitive types.Bool `tfsdk:"sensitive"`
Comment types.String `tfsdk:"comment"`
Target types.Set `tfsdk:"target"`
Key types.String `tfsdk:"key"`
Value types.String `tfsdk:"value"`
TeamID types.String `tfsdk:"team_id"`
ProjectIDs types.Set `tfsdk:"project_ids"`
ID types.String `tfsdk:"id"`
Sensitive types.Bool `tfsdk:"sensitive"`
Comment types.String `tfsdk:"comment"`
ApplyToAllCustomEnvironments types.Bool `tfsdk:"apply_to_all_custom_environments"`
}

func (e *SharedEnvironmentVariable) toCreateSharedEnvironmentVariableRequest(ctx context.Context, diags diag.Diagnostics) (req client.CreateSharedEnvironmentVariableRequest, ok bool) {
Expand Down Expand Up @@ -201,9 +207,10 @@ func (e *SharedEnvironmentVariable) toCreateSharedEnvironmentVariableRequest(ctx

return client.CreateSharedEnvironmentVariableRequest{
EnvironmentVariable: client.SharedEnvironmentVariableRequest{
Target: target,
Type: envVariableType,
ProjectIDs: projectIDs,
ApplyToAllCustomEnvironments: e.ApplyToAllCustomEnvironments.ValueBool(),
Target: target,
Type: envVariableType,
ProjectIDs: projectIDs,
EnvironmentVariables: []client.SharedEnvVarRequest{
{
Key: e.Key.ValueString(),
Expand Down Expand Up @@ -238,13 +245,14 @@ func (e *SharedEnvironmentVariable) toUpdateSharedEnvironmentVariableRequest(ctx
envVariableType = "encrypted"
}
return client.UpdateSharedEnvironmentVariableRequest{
Value: e.Value.ValueString(),
Target: target,
Type: envVariableType,
TeamID: e.TeamID.ValueString(),
EnvID: e.ID.ValueString(),
ProjectIDs: projectIDs,
Comment: e.Comment.ValueString(),
ApplyToAllCustomEnvironments: e.ApplyToAllCustomEnvironments.ValueBool(),
Value: e.Value.ValueString(),
Target: target,
Type: envVariableType,
TeamID: e.TeamID.ValueString(),
EnvID: e.ID.ValueString(),
ProjectIDs: projectIDs,
Comment: e.Comment.ValueString(),
}, true
}

Expand All @@ -268,14 +276,15 @@ func convertResponseToSharedEnvironmentVariable(response client.SharedEnvironmen
}

return SharedEnvironmentVariable{
Target: types.SetValueMust(types.StringType, target),
Key: types.StringValue(response.Key),
Value: value,
ProjectIDs: types.SetValueMust(types.StringType, projectIDs),
TeamID: toTeamID(response.TeamID),
ID: types.StringValue(response.ID),
Sensitive: types.BoolValue(response.Type == "sensitive"),
Comment: types.StringValue(response.Comment),
ApplyToAllCustomEnvironments: types.BoolValue(response.ApplyToAllCustomEnvironments),
Target: types.SetValueMust(types.StringType, target),
Key: types.StringValue(response.Key),
Value: value,
ProjectIDs: types.SetValueMust(types.StringType, projectIDs),
TeamID: toTeamID(response.TeamID),
ID: types.StringValue(response.ID),
Sensitive: types.BoolValue(response.Type == "sensitive"),
Comment: types.StringValue(response.Comment),
}
}

Expand Down
4 changes: 4 additions & 0 deletions vercel/resource_shared_environment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestAcc_SharedEnvironmentVariables(t *testing.T) {
resource.TestCheckResourceAttr("vercel_shared_environment_variable.example", "key", fmt.Sprintf("test_acc_foo_%s", nameSuffix)),
resource.TestCheckResourceAttr("vercel_shared_environment_variable.example", "value", "bar"),
resource.TestCheckResourceAttr("vercel_shared_environment_variable.example", "comment", "Test comment for example"),
resource.TestCheckResourceAttr("vercel_shared_environment_variable.example", "apply_to_all_custom_environments", "true"),
resource.TestCheckTypeSetElemAttr("vercel_shared_environment_variable.example", "target.*", "production"),

testAccSharedEnvironmentVariableExists(testClient(t), "vercel_shared_environment_variable.sensitive_example", testTeam(t)),
Expand All @@ -63,6 +64,7 @@ func TestAcc_SharedEnvironmentVariables(t *testing.T) {
testAccSharedEnvironmentVariableExists(testClient(t), "vercel_shared_environment_variable.example", testTeam(t)),
resource.TestCheckResourceAttr("vercel_shared_environment_variable.example", "key", fmt.Sprintf("test_acc_foo_%s", nameSuffix)),
resource.TestCheckResourceAttr("vercel_shared_environment_variable.example", "value", "updated-bar"),
resource.TestCheckResourceAttr("vercel_shared_environment_variable.example", "apply_to_all_custom_environments", "false"),
resource.TestCheckTypeSetElemAttr("vercel_shared_environment_variable.example", "target.*", "development"),
resource.TestCheckTypeSetElemAttr("vercel_shared_environment_variable.example", "target.*", "preview"),

Expand Down Expand Up @@ -113,6 +115,7 @@ resource "vercel_shared_environment_variable" "example" {
vercel_project.example.id
]
comment = "Test comment for example"
apply_to_all_custom_environments = true
}

resource "vercel_shared_environment_variable" "sensitive_example" {
Expand Down Expand Up @@ -155,6 +158,7 @@ resource "vercel_shared_environment_variable" "example" {
vercel_project.example.id,
vercel_project.example2.id
]
apply_to_all_custom_environments = false
}

resource "vercel_shared_environment_variable" "sensitive_example" {
Expand Down