Skip to content

Commit

Permalink
fix support for environment variables on Starter plan
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonsnir committed Sep 17, 2024
1 parent c691a9c commit 1c5f4a5
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 22 deletions.
2 changes: 2 additions & 0 deletions UPDATING_OPENAPI_JSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This project uses a modified `openapi.json`. Please maintain these instructions
1. Remove the `domain` property from the `required` array of the `DnsZone` object.
1. Remove the `values`, `scopes` and `is_secret` parameters from the `updateEnvVar` operation.
1. Add a request body schema to the `updateEnvVar` operation, by copying it from an earlier version of the `openapi.json`.
1. Remove `scopes` from the `required` array of the `updateEnvVar` operation request body.
1. Remove `scopes` from the `required` array of the `EnvVar` object.
1. Add a `package_path` property of type `string` to the `Repo` object.
1. Add a `branch` property of type `string` to the `Repo` object.
1. Add a `functions_region` property of type `string` to the `Site` object.
Expand Down
2 changes: 0 additions & 2 deletions internal/netlifyapi/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8398,7 +8398,6 @@ components:
type: boolean
required:
- key
- scopes
- updated_at
- updated_by
- values
Expand Down Expand Up @@ -11965,7 +11964,6 @@ components:
type: boolean
requiredProperties:
- values
- scopes
- is_secret
uploadDeployFunction_200_response:
example:
Expand Down
30 changes: 19 additions & 11 deletions internal/netlifyapi/model_env_var.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 32 additions & 7 deletions internal/provider/environment_variable_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ type environmentVariableValueModel struct {
ContextParameter types.String `tfsdk:"context_parameter"`
}

var allScopes = []string{"builds", "functions", "runtime", "post-processing"}
var allScopesValues = []attr.Value{
types.StringValue("builds"),
types.StringValue("functions"),
types.StringValue("runtime"),
types.StringValue("post-processing"),
}

func (r *environmentVariableResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_environment_variable"
}
Expand Down Expand Up @@ -110,15 +118,10 @@ func (r *environmentVariableResource) Schema(_ context.Context, _ resource.Schem
Description: "One or more of builds, functions, runtime, and post-processing",
Validators: []validator.Set{
setvalidator.ValueStringsAre(
stringvalidator.OneOf("builds", "functions", "runtime", "post-processing"),
stringvalidator.OneOf(allScopes...),
),
},
Default: setdefault.StaticValue(types.SetValueMust(types.StringType, []attr.Value{
types.StringValue("builds"),
types.StringValue("functions"),
types.StringValue("runtime"),
types.StringValue("post-processing"),
})),
Default: setdefault.StaticValue(types.SetValueMust(types.StringType, allScopesValues)),
},
"values": schema.SetNestedAttribute{
Optional: true,
Expand Down Expand Up @@ -204,6 +207,9 @@ func (r *environmentVariableResource) Create(ctx context.Context, req resource.C
for i, scope := range plan.Scopes {
scopes[i] = scope.ValueString()
}
if hasAllScopes(scopes) {
scopes = nil
}
var values []netlifyapi.EnvVarValue
var isSecret bool
if len(plan.SecretValues) > 0 {
Expand Down Expand Up @@ -306,6 +312,9 @@ func (r *environmentVariableResource) Update(ctx context.Context, req resource.U
for i, scope := range plan.Scopes {
scopes[i] = scope.ValueString()
}
if hasAllScopes(scopes) {
scopes = nil
}
var values []netlifyapi.EnvVarValue
var isSecret bool
if len(plan.SecretValues) > 0 {
Expand Down Expand Up @@ -431,3 +440,19 @@ func parseValues(values []netlifyapi.EnvVarValue) []environmentVariableValueMode
}
return envVarValues
}

func hasAllScopes(scopes []string) bool {
for _, scope := range allScopes {
found := false
for _, s := range scopes {
if s == scope {
found = true
break
}
}
if !found {
return false
}
}
return true
}
1 change: 1 addition & 0 deletions internal/provider/environment_variable_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ resource "netlify_environment_variable" "b" {
team_id = "66ae34e11a567e9092e3850f"
site_id = "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"
key = "C_B"
scopes = ["builds", "functions", "runtime", "post-processing"]
values = [
{
value = "staging"
Expand Down
32 changes: 32 additions & 0 deletions internal/provider/starter_plan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccStarterEnvVar(t *testing.T) {
accTest(t, []resource.TestStep{
{
Config: `resource "netlify_environment_variable" "site_level" {
team_id = "66e98216e3fe031846dc998a"
site_id = "fbba82b0-f1e9-4e92-9203-eefc62857545"
key = "TEST_SITE_LEVEL"
values = [
{
value = "/path/here",
context = "all",
}
]
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("netlify_environment_variable.site_level", "team_id", "66e98216e3fe031846dc998a"),
resource.TestCheckResourceAttr("netlify_environment_variable.site_level", "site_id", "fbba82b0-f1e9-4e92-9203-eefc62857545"),
resource.TestCheckResourceAttr("netlify_environment_variable.site_level", "key", "TEST_SITE_LEVEL"),
),
},
}, func(s *terraform.State) error { return nil })
}
2 changes: 0 additions & 2 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3440,7 +3440,6 @@
},
"requiredProperties": [
"values",
"scopes",
"is_secret"
]
}
Expand Down Expand Up @@ -10948,7 +10947,6 @@
},
"required": [
"key",
"scopes",
"values",
"updated_at",
"updated_by"
Expand Down

0 comments on commit 1c5f4a5

Please sign in to comment.