Skip to content

Commit 1c5f4a5

Browse files
committed
fix support for environment variables on Starter plan
1 parent c691a9c commit 1c5f4a5

File tree

7 files changed

+86
-22
lines changed

7 files changed

+86
-22
lines changed

UPDATING_OPENAPI_JSON.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This project uses a modified `openapi.json`. Please maintain these instructions
1111
1. Remove the `domain` property from the `required` array of the `DnsZone` object.
1212
1. Remove the `values`, `scopes` and `is_secret` parameters from the `updateEnvVar` operation.
1313
1. Add a request body schema to the `updateEnvVar` operation, by copying it from an earlier version of the `openapi.json`.
14+
1. Remove `scopes` from the `required` array of the `updateEnvVar` operation request body.
15+
1. Remove `scopes` from the `required` array of the `EnvVar` object.
1416
1. Add a `package_path` property of type `string` to the `Repo` object.
1517
1. Add a `branch` property of type `string` to the `Repo` object.
1618
1. Add a `functions_region` property of type `string` to the `Site` object.

internal/netlifyapi/api/openapi.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8398,7 +8398,6 @@ components:
83988398
type: boolean
83998399
required:
84008400
- key
8401-
- scopes
84028401
- updated_at
84038402
- updated_by
84048403
- values
@@ -11965,7 +11964,6 @@ components:
1196511964
type: boolean
1196611965
requiredProperties:
1196711966
- values
11968-
- scopes
1196911967
- is_secret
1197011968
uploadDeployFunction_200_response:
1197111969
example:

internal/netlifyapi/model_env_var.go

Lines changed: 19 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/provider/environment_variable_resource.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ type environmentVariableValueModel struct {
5252
ContextParameter types.String `tfsdk:"context_parameter"`
5353
}
5454

55+
var allScopes = []string{"builds", "functions", "runtime", "post-processing"}
56+
var allScopesValues = []attr.Value{
57+
types.StringValue("builds"),
58+
types.StringValue("functions"),
59+
types.StringValue("runtime"),
60+
types.StringValue("post-processing"),
61+
}
62+
5563
func (r *environmentVariableResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
5664
resp.TypeName = req.ProviderTypeName + "_environment_variable"
5765
}
@@ -110,15 +118,10 @@ func (r *environmentVariableResource) Schema(_ context.Context, _ resource.Schem
110118
Description: "One or more of builds, functions, runtime, and post-processing",
111119
Validators: []validator.Set{
112120
setvalidator.ValueStringsAre(
113-
stringvalidator.OneOf("builds", "functions", "runtime", "post-processing"),
121+
stringvalidator.OneOf(allScopes...),
114122
),
115123
},
116-
Default: setdefault.StaticValue(types.SetValueMust(types.StringType, []attr.Value{
117-
types.StringValue("builds"),
118-
types.StringValue("functions"),
119-
types.StringValue("runtime"),
120-
types.StringValue("post-processing"),
121-
})),
124+
Default: setdefault.StaticValue(types.SetValueMust(types.StringType, allScopesValues)),
122125
},
123126
"values": schema.SetNestedAttribute{
124127
Optional: true,
@@ -204,6 +207,9 @@ func (r *environmentVariableResource) Create(ctx context.Context, req resource.C
204207
for i, scope := range plan.Scopes {
205208
scopes[i] = scope.ValueString()
206209
}
210+
if hasAllScopes(scopes) {
211+
scopes = nil
212+
}
207213
var values []netlifyapi.EnvVarValue
208214
var isSecret bool
209215
if len(plan.SecretValues) > 0 {
@@ -306,6 +312,9 @@ func (r *environmentVariableResource) Update(ctx context.Context, req resource.U
306312
for i, scope := range plan.Scopes {
307313
scopes[i] = scope.ValueString()
308314
}
315+
if hasAllScopes(scopes) {
316+
scopes = nil
317+
}
309318
var values []netlifyapi.EnvVarValue
310319
var isSecret bool
311320
if len(plan.SecretValues) > 0 {
@@ -431,3 +440,19 @@ func parseValues(values []netlifyapi.EnvVarValue) []environmentVariableValueMode
431440
}
432441
return envVarValues
433442
}
443+
444+
func hasAllScopes(scopes []string) bool {
445+
for _, scope := range allScopes {
446+
found := false
447+
for _, s := range scopes {
448+
if s == scope {
449+
found = true
450+
break
451+
}
452+
}
453+
if !found {
454+
return false
455+
}
456+
}
457+
return true
458+
}

internal/provider/environment_variable_resource_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ resource "netlify_environment_variable" "b" {
100100
team_id = "66ae34e11a567e9092e3850f"
101101
site_id = "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"
102102
key = "C_B"
103+
scopes = ["builds", "functions", "runtime", "post-processing"]
103104
values = [
104105
{
105106
value = "staging"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-testing/terraform"
8+
)
9+
10+
func TestAccStarterEnvVar(t *testing.T) {
11+
accTest(t, []resource.TestStep{
12+
{
13+
Config: `resource "netlify_environment_variable" "site_level" {
14+
team_id = "66e98216e3fe031846dc998a"
15+
site_id = "fbba82b0-f1e9-4e92-9203-eefc62857545"
16+
key = "TEST_SITE_LEVEL"
17+
values = [
18+
{
19+
value = "/path/here",
20+
context = "all",
21+
}
22+
]
23+
}
24+
`,
25+
Check: resource.ComposeAggregateTestCheckFunc(
26+
resource.TestCheckResourceAttr("netlify_environment_variable.site_level", "team_id", "66e98216e3fe031846dc998a"),
27+
resource.TestCheckResourceAttr("netlify_environment_variable.site_level", "site_id", "fbba82b0-f1e9-4e92-9203-eefc62857545"),
28+
resource.TestCheckResourceAttr("netlify_environment_variable.site_level", "key", "TEST_SITE_LEVEL"),
29+
),
30+
},
31+
}, func(s *terraform.State) error { return nil })
32+
}

openapi.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,7 +3440,6 @@
34403440
},
34413441
"requiredProperties": [
34423442
"values",
3443-
"scopes",
34443443
"is_secret"
34453444
]
34463445
}
@@ -10948,7 +10947,6 @@
1094810947
},
1094910948
"required": [
1095010949
"key",
10951-
"scopes",
1095210950
"values",
1095310951
"updated_at",
1095410952
"updated_by"

0 commit comments

Comments
 (0)