Skip to content

Commit 348f785

Browse files
committed
add tests and comments
1 parent 9d2b35a commit 348f785

File tree

3 files changed

+126
-10
lines changed

3 files changed

+126
-10
lines changed

provisioner/terraform/parameters.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,44 @@ func rawRichParameterNames(workdir string) ([]string, error) {
2727

2828
var coderParameterNames []string
2929
for _, entry := range entries {
30-
if !strings.HasSuffix(entry.Name(), ".tf") || !strings.HasSuffix(entry.Name(), ".tf.json") {
30+
if !strings.HasSuffix(entry.Name(), ".tf") && !strings.HasSuffix(entry.Name(), ".tf.json") {
3131
continue
3232
}
3333

34-
var parsedHCL *hcl.File
35-
var diags hcl.Diagnostics
36-
hclFilepath := path.Join(workdir, entry.Name())
37-
parser := hclparse.NewParser()
34+
var (
35+
parsedTF *hcl.File
36+
diags hcl.Diagnostics
37+
tfFilepath = path.Join(workdir, entry.Name())
38+
parser = hclparse.NewParser()
39+
)
40+
41+
// Support .tf.json files.
42+
// Warning: since JSON parsing in Go automatically sorts maps
43+
// alphabetically, we can't preserve the original order of parameters
44+
// like in HCL.
3845
if strings.HasSuffix(entry.Name(), ".tf.json") {
39-
parsedHCL, diags = parser.ParseJSONFile(hclFilepath)
46+
parsedTF, diags = parser.ParseJSONFile(tfFilepath)
4047
} else {
41-
parsedHCL, diags = parser.ParseHCLFile(hclFilepath)
48+
parsedTF, diags = parser.ParseHCLFile(tfFilepath)
4249
}
50+
4351
if diags.HasErrors() {
4452
return nil, hcl.Diagnostics{
4553
{
4654
Severity: hcl.DiagError,
4755
Summary: "Failed to parse HCL file",
48-
Detail: fmt.Sprintf("parser.ParseHCLFile can't parse %q file", hclFilepath),
56+
Detail: fmt.Sprintf("parser.ParseHCLFile can't parse %q file", tfFilepath),
4957
},
5058
}
5159
}
5260

53-
content, _, _ := parsedHCL.Body.PartialContent(terraformWithCoderParametersSchema)
61+
content, _, _ := parsedTF.Body.PartialContent(terraformWithCoderParametersSchema)
5462
for _, block := range content.Blocks {
5563
if block.Type == "data" && block.Labels[0] == "coder_parameter" && len(block.Labels) == 2 {
5664
coderParameterNames = append(coderParameterNames, block.Labels[1])
5765
}
5866
}
5967
}
68+
6069
return coderParameterNames, nil
6170
}

provisioner/terraform/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func loadEnabledFeatures(moduleDir string) (map[string]bool, hcl.Diagnostics) {
103103

104104
var found bool
105105
for _, entry := range entries {
106-
if !strings.HasSuffix(entry.Name(), ".tf") || !strings.HasSuffix(entry.Name(), ".tf.json") {
106+
if !strings.HasSuffix(entry.Name(), ".tf") && !strings.HasSuffix(entry.Name(), ".tf.json") {
107107
continue
108108
}
109109

provisioner/terraform/provision_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,31 @@ func TestProvision(t *testing.T) {
254254
},
255255
Apply: true,
256256
},
257+
{
258+
Name: "single-resource-json",
259+
Files: map[string]string{
260+
"main.tf.json": `{
261+
"resource": {
262+
"null_resource": {
263+
"A": [
264+
{}
265+
]
266+
}
267+
}
268+
}`,
269+
},
270+
Response: &proto.Provision_Response{
271+
Type: &proto.Provision_Response_Complete{
272+
Complete: &proto.Provision_Complete{
273+
Resources: []*proto.Resource{{
274+
Name: "A",
275+
Type: "null_resource",
276+
}},
277+
},
278+
},
279+
},
280+
Apply: true,
281+
},
257282
{
258283
Name: "bad-syntax-1",
259284
Files: map[string]string{
@@ -349,6 +374,88 @@ func TestProvision(t *testing.T) {
349374
},
350375
},
351376
},
377+
{
378+
Name: "rich-parameter-with-value-json",
379+
Files: map[string]string{
380+
"main.tf.json": `{
381+
"data": {
382+
"coder_parameter": {
383+
"example": [
384+
{
385+
"default": "foobar",
386+
"name": "Example",
387+
"type": "string"
388+
}
389+
],
390+
"sample": [
391+
{
392+
"default": "foobaz",
393+
"name": "Sample",
394+
"type": "string"
395+
}
396+
]
397+
}
398+
},
399+
"resource": {
400+
"null_resource": {
401+
"example": [
402+
{
403+
"triggers": {
404+
"misc": "${data.coder_parameter.example.value}"
405+
}
406+
}
407+
]
408+
}
409+
},
410+
"terraform": [
411+
{
412+
"required_providers": [
413+
{
414+
"coder": {
415+
"source": "coder/coder",
416+
"version": "0.6.20"
417+
}
418+
}
419+
]
420+
}
421+
]
422+
}`,
423+
},
424+
Request: &proto.Provision_Plan{
425+
RichParameterValues: []*proto.RichParameterValue{
426+
{
427+
Name: "Example",
428+
Value: "foobaz",
429+
},
430+
{
431+
Name: "Sample",
432+
Value: "foofoo",
433+
},
434+
},
435+
},
436+
Response: &proto.Provision_Response{
437+
Type: &proto.Provision_Response_Complete{
438+
Complete: &proto.Provision_Complete{
439+
Parameters: []*proto.RichParameter{
440+
{
441+
Name: "Example",
442+
Type: "string",
443+
DefaultValue: "foobar",
444+
},
445+
{
446+
Name: "Sample",
447+
Type: "string",
448+
DefaultValue: "foobaz",
449+
},
450+
},
451+
Resources: []*proto.Resource{{
452+
Name: "example",
453+
Type: "null_resource",
454+
}},
455+
},
456+
},
457+
},
458+
},
352459
{
353460
Name: "git-auth",
354461
Files: map[string]string{

0 commit comments

Comments
 (0)