Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b29ab11

Browse files
committed
useManagedVariables
1 parent 84f6fe5 commit b29ab11

File tree

2 files changed

+183
-146
lines changed

2 files changed

+183
-146
lines changed

provisioner/terraform/parse.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (*server) Parse(request *proto.Parse_Request, stream proto.DRPCProvisioner_
6767
var parameters []*proto.ParameterSchema
6868
var templateVariables []*proto.TemplateVariable
6969

70-
useManagedVariables := flags[featureUseManagedVariables]
70+
useManagedVariables := flags != nil && flags[featureUseManagedVariables]
7171
if useManagedVariables {
7272
for _, v := range variables {
7373
mv, err := convertTerraformVariableToManagedVariable(v)
@@ -97,15 +97,24 @@ func (*server) Parse(request *proto.Parse_Request, stream proto.DRPCProvisioner_
9797
}
9898

9999
func loadEnabledFeatures(moduleDir string) (map[string]bool, hcl.Diagnostics, error) {
100-
parser := hclparse.NewParser()
101-
mainFile, err := parser.ParseHCLFile(path.Join(moduleDir, "main.tf"))
102-
if err != nil {
103-
return nil, nil, xerrors.Errorf("can't parse main.tf file: %w", err)
104-
}
105-
106100
flags := map[string]bool{}
107101
var diags hcl.Diagnostics
108102

103+
// FIXME: Only flags placed in the "main.tf" are considered.
104+
mainFilepath := path.Join(moduleDir, "main.tf")
105+
_, err := os.Stat(mainFilepath)
106+
if os.IsNotExist(err) {
107+
return flags, diags, nil
108+
} else if err != nil {
109+
return flags, diags, xerrors.Errorf("can't stat file %q: %w", mainFilepath, err)
110+
}
111+
112+
parser := hclparse.NewParser()
113+
mainFile, diags := parser.ParseHCLFile(mainFilepath)
114+
if diags.HasErrors() {
115+
return flags, diags, xerrors.Errorf("can't parse file %q: %w", mainFilepath, diags)
116+
}
117+
109118
content, _ := mainFile.Body.Content(terraformWithFeaturesSchema)
110119
for _, block := range content.Blocks {
111120
if block.Type == "provider" && block.Labels[0] == "coder" {
@@ -192,7 +201,7 @@ func convertTerraformVariableToManagedVariable(variable *tfconfig.Variable) (*pr
192201
Description: variable.Description,
193202
Type: variable.Type,
194203
DefaultValue: defaultData,
195-
Required: variable.Required,
204+
Required: defaultData == "", // variable.Required is always false?
196205
Sensitive: variable.Sensitive,
197206
}, nil
198207
}

provisioner/terraform/parse_test.go

Lines changed: 166 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -24,149 +24,148 @@ func TestParse(t *testing.T) {
2424
// error containing this string before a Complete response is returned.
2525
ErrorContains string
2626
}{
27-
// {
28-
// Name: "single-variable",
29-
// Files: map[string]string{
30-
// "main.tf": `variable "A" {
31-
// description = "Testing!"
32-
// }`,
33-
// },
34-
// Response: &proto.Parse_Response{
35-
// Type: &proto.Parse_Response_Complete{
36-
// Complete: &proto.Parse_Complete{
37-
// ParameterSchemas: []*proto.ParameterSchema{{
38-
// Name: "A",
39-
// RedisplayValue: true,
40-
// AllowOverrideSource: true,
41-
// Description: "Testing!",
42-
// DefaultDestination: &proto.ParameterDestination{
43-
// Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
44-
// },
45-
// }},
46-
// },
47-
// },
48-
// },
49-
// },
50-
// {
51-
// Name: "default-variable-value",
52-
// Files: map[string]string{
53-
// "main.tf": `variable "A" {
54-
// default = "wow"
55-
// }`,
56-
// },
57-
// Response: &proto.Parse_Response{
58-
// Type: &proto.Parse_Response_Complete{
59-
// Complete: &proto.Parse_Complete{
60-
// ParameterSchemas: []*proto.ParameterSchema{{
61-
// Name: "A",
62-
// RedisplayValue: true,
63-
// AllowOverrideSource: true,
64-
// DefaultSource: &proto.ParameterSource{
65-
// Scheme: proto.ParameterSource_DATA,
66-
// Value: "wow",
67-
// },
68-
// DefaultDestination: &proto.ParameterDestination{
69-
// Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
70-
// },
71-
// }},
72-
// },
73-
// },
74-
// },
75-
// },
76-
// {
77-
// Name: "variable-validation",
78-
// Files: map[string]string{
79-
// "main.tf": `variable "A" {
80-
// validation {
81-
// condition = var.A == "value"
82-
// }
83-
// }`,
84-
// },
85-
// Response: &proto.Parse_Response{
86-
// Type: &proto.Parse_Response_Complete{
87-
// Complete: &proto.Parse_Complete{
88-
// ParameterSchemas: []*proto.ParameterSchema{{
89-
// Name: "A",
90-
// RedisplayValue: true,
91-
// ValidationCondition: `var.A == "value"`,
92-
// ValidationTypeSystem: proto.ParameterSchema_HCL,
93-
// AllowOverrideSource: true,
94-
// DefaultDestination: &proto.ParameterDestination{
95-
// Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
96-
// },
97-
// }},
98-
// },
99-
// },
100-
// },
101-
// },
102-
// {
103-
// Name: "bad-syntax",
104-
// Files: map[string]string{
105-
// "main.tf": "a;sd;ajsd;lajsd;lasjdf;a",
106-
// },
107-
// ErrorContains: `The ";" character is not valid.`,
108-
// },
109-
// {
110-
// Name: "multiple-variables",
111-
// Files: map[string]string{
112-
// "main1.tf": `variable "foo" { }
113-
// variable "bar" { }`,
114-
// "main2.tf": `variable "baz" { }
115-
// variable "quux" { }`,
116-
// },
117-
// Response: &proto.Parse_Response{
118-
// Type: &proto.Parse_Response_Complete{
119-
// Complete: &proto.Parse_Complete{
120-
// ParameterSchemas: []*proto.ParameterSchema{
121-
// {
122-
// Name: "foo",
123-
// RedisplayValue: true,
124-
// AllowOverrideSource: true,
125-
// Description: "",
126-
// DefaultDestination: &proto.ParameterDestination{
127-
// Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
128-
// },
129-
// },
130-
// {
131-
// Name: "bar",
132-
// RedisplayValue: true,
133-
// AllowOverrideSource: true,
134-
// Description: "",
135-
// DefaultDestination: &proto.ParameterDestination{
136-
// Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
137-
// },
138-
// },
139-
// {
140-
// Name: "baz",
141-
// RedisplayValue: true,
142-
// AllowOverrideSource: true,
143-
// Description: "",
144-
// DefaultDestination: &proto.ParameterDestination{
145-
// Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
146-
// },
147-
// },
148-
// {
149-
// Name: "quux",
150-
// RedisplayValue: true,
151-
// AllowOverrideSource: true,
152-
// Description: "",
153-
// DefaultDestination: &proto.ParameterDestination{
154-
// Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
155-
// },
156-
// },
157-
// },
158-
// },
159-
// },
160-
// },
161-
// },
16227
{
163-
Name: "enable-managed-variables",
28+
Name: "single-variable",
16429
Files: map[string]string{
16530
"main.tf": `variable "A" {
16631
description = "Testing!"
167-
type = "string"
32+
}`,
33+
},
34+
Response: &proto.Parse_Response{
35+
Type: &proto.Parse_Response_Complete{
36+
Complete: &proto.Parse_Complete{
37+
ParameterSchemas: []*proto.ParameterSchema{{
38+
Name: "A",
39+
RedisplayValue: true,
40+
AllowOverrideSource: true,
41+
Description: "Testing!",
42+
DefaultDestination: &proto.ParameterDestination{
43+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
44+
},
45+
}},
46+
},
47+
},
48+
},
49+
},
50+
{
51+
Name: "default-variable-value",
52+
Files: map[string]string{
53+
"main.tf": `variable "A" {
54+
default = "wow"
55+
}`,
56+
},
57+
Response: &proto.Parse_Response{
58+
Type: &proto.Parse_Response_Complete{
59+
Complete: &proto.Parse_Complete{
60+
ParameterSchemas: []*proto.ParameterSchema{{
61+
Name: "A",
62+
RedisplayValue: true,
63+
AllowOverrideSource: true,
64+
DefaultSource: &proto.ParameterSource{
65+
Scheme: proto.ParameterSource_DATA,
66+
Value: "wow",
67+
},
68+
DefaultDestination: &proto.ParameterDestination{
69+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
70+
},
71+
}},
72+
},
73+
},
74+
},
75+
},
76+
{
77+
Name: "variable-validation",
78+
Files: map[string]string{
79+
"main.tf": `variable "A" {
80+
validation {
81+
condition = var.A == "value"
82+
}
83+
}`,
84+
},
85+
Response: &proto.Parse_Response{
86+
Type: &proto.Parse_Response_Complete{
87+
Complete: &proto.Parse_Complete{
88+
ParameterSchemas: []*proto.ParameterSchema{{
89+
Name: "A",
90+
RedisplayValue: true,
91+
ValidationCondition: `var.A == "value"`,
92+
ValidationTypeSystem: proto.ParameterSchema_HCL,
93+
AllowOverrideSource: true,
94+
DefaultDestination: &proto.ParameterDestination{
95+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
96+
},
97+
}},
98+
},
99+
},
100+
},
101+
},
102+
{
103+
Name: "bad-syntax",
104+
Files: map[string]string{
105+
"main.tf": "a;sd;ajsd;lajsd;lasjdf;a",
106+
},
107+
ErrorContains: `The ";" character is not valid.`,
108+
},
109+
{
110+
Name: "multiple-variables",
111+
Files: map[string]string{
112+
"main1.tf": `variable "foo" { }
113+
variable "bar" { }`,
114+
"main2.tf": `variable "baz" { }
115+
variable "quux" { }`,
116+
},
117+
Response: &proto.Parse_Response{
118+
Type: &proto.Parse_Response_Complete{
119+
Complete: &proto.Parse_Complete{
120+
ParameterSchemas: []*proto.ParameterSchema{
121+
{
122+
Name: "foo",
123+
RedisplayValue: true,
124+
AllowOverrideSource: true,
125+
Description: "",
126+
DefaultDestination: &proto.ParameterDestination{
127+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
128+
},
129+
},
130+
{
131+
Name: "bar",
132+
RedisplayValue: true,
133+
AllowOverrideSource: true,
134+
Description: "",
135+
DefaultDestination: &proto.ParameterDestination{
136+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
137+
},
138+
},
139+
{
140+
Name: "baz",
141+
RedisplayValue: true,
142+
AllowOverrideSource: true,
143+
Description: "",
144+
DefaultDestination: &proto.ParameterDestination{
145+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
146+
},
147+
},
148+
{
149+
Name: "quux",
150+
RedisplayValue: true,
151+
AllowOverrideSource: true,
152+
Description: "",
153+
DefaultDestination: &proto.ParameterDestination{
154+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
155+
},
156+
},
157+
},
158+
},
159+
},
160+
},
161+
},
162+
{
163+
Name: "enable-managed-variables-with-default-value",
164+
Files: map[string]string{
165+
"main.tf": `variable "A" {
166+
description = "Testing!"
167+
type = string
168168
default = "abc"
169-
required = true
170169
sensitive = true
171170
}
172171
@@ -191,6 +190,35 @@ func TestParse(t *testing.T) {
191190
},
192191
},
193192
},
193+
{
194+
Name: "enable-managed-variables-without-default",
195+
Files: map[string]string{
196+
"main.tf": `variable "A" {
197+
description = "Testing!"
198+
type = string
199+
sensitive = true
200+
}
201+
202+
provider "coder" {
203+
feature_use_managed_variables = true
204+
}`,
205+
},
206+
Response: &proto.Parse_Response{
207+
Type: &proto.Parse_Response_Complete{
208+
Complete: &proto.Parse_Complete{
209+
TemplateVariables: []*proto.TemplateVariable{
210+
{
211+
Name: "A",
212+
Description: "Testing!",
213+
Type: "string",
214+
Required: true,
215+
Sensitive: true,
216+
},
217+
},
218+
},
219+
},
220+
},
221+
},
194222
}
195223

196224
for _, testCase := range testCases {

0 commit comments

Comments
 (0)