Skip to content

Commit 485455b

Browse files
committed
providers/template: disallow file paths in template
Fixes hashicorp#8660 This disallows file paths in `template`. This already had a deprecation warning so we're just removing that.
1 parent 594014d commit 485455b

2 files changed

Lines changed: 7 additions & 46 deletions

File tree

builtin/providers/template/datasource_template_file.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func dataSourceFile() *schema.Resource {
2525
Optional: true,
2626
Description: "Contents of the template",
2727
ConflictsWith: []string{"filename"},
28-
ValidateFunc: validateTemplateAttribute,
2928
},
3029
"filename": &schema.Schema{
3130
Type: schema.TypeString,
@@ -82,13 +81,14 @@ func renderFile(d *schema.ResourceData) (string, error) {
8281
filename := d.Get("filename").(string)
8382
vars := d.Get("vars").(map[string]interface{})
8483

84+
contents := template
8585
if template == "" && filename != "" {
86-
template = filename
87-
}
86+
data, _, err := pathorcontents.Read(filename)
87+
if err != nil {
88+
return "", err
89+
}
8890

89-
contents, _, err := pathorcontents.Read(template)
90-
if err != nil {
91-
return "", err
91+
contents = data
9292
}
9393

9494
rendered, err := execute(contents, vars)
@@ -145,20 +145,6 @@ func hash(s string) string {
145145
return hex.EncodeToString(sha[:])
146146
}
147147

148-
func validateTemplateAttribute(v interface{}, key string) (ws []string, es []error) {
149-
_, wasPath, err := pathorcontents.Read(v.(string))
150-
if err != nil {
151-
es = append(es, err)
152-
return
153-
}
154-
155-
if wasPath {
156-
ws = append(ws, fmt.Sprintf("%s: looks like you specified a path instead of file contents. Use `file()` to load this path. Specifying a path directly is deprecated and will be removed in a future version.", key))
157-
}
158-
159-
return
160-
}
161-
162148
func validateVarsAttribute(v interface{}, key string) (ws []string, es []error) {
163149
// vars can only be primitives right now
164150
var badVars []string

builtin/providers/template/datasource_template_file_test.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package template
22

33
import (
44
"fmt"
5-
"io/ioutil"
6-
"os"
75
"strings"
86
"sync"
97
"testing"
@@ -26,6 +24,7 @@ func TestTemplateRendering(t *testing.T) {
2624
{`{a="foo"}`, `$${a}`, `foo`},
2725
{`{a="hello"}`, `$${replace(a, "ello", "i")}`, `hi`},
2826
{`{}`, `${1+2+3}`, `6`},
27+
{`{}`, `/`, `/`},
2928
}
3029

3130
for _, tt := range cases {
@@ -47,30 +46,6 @@ func TestTemplateRendering(t *testing.T) {
4746
}
4847
}
4948

50-
func TestValidateTemplateAttribute(t *testing.T) {
51-
file, err := ioutil.TempFile("", "testtemplate")
52-
if err != nil {
53-
t.Fatal(err)
54-
}
55-
file.WriteString("Hello world.")
56-
file.Close()
57-
defer os.Remove(file.Name())
58-
59-
ws, es := validateTemplateAttribute(file.Name(), "test")
60-
61-
if len(es) != 0 {
62-
t.Fatalf("Unexpected errors: %#v", es)
63-
}
64-
65-
if len(ws) != 1 {
66-
t.Fatalf("Expected 1 warning, got %d", len(ws))
67-
}
68-
69-
if !strings.Contains(ws[0], "Specifying a path directly is deprecated") {
70-
t.Fatalf("Expected warning about path, got: %s", ws[0])
71-
}
72-
}
73-
7449
func TestValidateVarsAttribute(t *testing.T) {
7550
cases := map[string]struct {
7651
Vars map[string]interface{}

0 commit comments

Comments
 (0)