From 485455b2f2809afbbeda02dc44def0f64927e742 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Nov 2016 10:23:58 -0800 Subject: [PATCH] providers/template: disallow file paths in `template` Fixes #8660 This disallows file paths in `template`. This already had a deprecation warning so we're just removing that. --- .../template/datasource_template_file.go | 26 +++++------------- .../template/datasource_template_file_test.go | 27 +------------------ 2 files changed, 7 insertions(+), 46 deletions(-) diff --git a/builtin/providers/template/datasource_template_file.go b/builtin/providers/template/datasource_template_file.go index 6a04ccfca..28e5f6033 100644 --- a/builtin/providers/template/datasource_template_file.go +++ b/builtin/providers/template/datasource_template_file.go @@ -25,7 +25,6 @@ func dataSourceFile() *schema.Resource { Optional: true, Description: "Contents of the template", ConflictsWith: []string{"filename"}, - ValidateFunc: validateTemplateAttribute, }, "filename": &schema.Schema{ Type: schema.TypeString, @@ -82,13 +81,14 @@ func renderFile(d *schema.ResourceData) (string, error) { filename := d.Get("filename").(string) vars := d.Get("vars").(map[string]interface{}) + contents := template if template == "" && filename != "" { - template = filename - } + data, _, err := pathorcontents.Read(filename) + if err != nil { + return "", err + } - contents, _, err := pathorcontents.Read(template) - if err != nil { - return "", err + contents = data } rendered, err := execute(contents, vars) @@ -145,20 +145,6 @@ func hash(s string) string { return hex.EncodeToString(sha[:]) } -func validateTemplateAttribute(v interface{}, key string) (ws []string, es []error) { - _, wasPath, err := pathorcontents.Read(v.(string)) - if err != nil { - es = append(es, err) - return - } - - if wasPath { - 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)) - } - - return -} - func validateVarsAttribute(v interface{}, key string) (ws []string, es []error) { // vars can only be primitives right now var badVars []string diff --git a/builtin/providers/template/datasource_template_file_test.go b/builtin/providers/template/datasource_template_file_test.go index 43dda582c..da9c1d98a 100644 --- a/builtin/providers/template/datasource_template_file_test.go +++ b/builtin/providers/template/datasource_template_file_test.go @@ -2,8 +2,6 @@ package template import ( "fmt" - "io/ioutil" - "os" "strings" "sync" "testing" @@ -26,6 +24,7 @@ func TestTemplateRendering(t *testing.T) { {`{a="foo"}`, `$${a}`, `foo`}, {`{a="hello"}`, `$${replace(a, "ello", "i")}`, `hi`}, {`{}`, `${1+2+3}`, `6`}, + {`{}`, `/`, `/`}, } for _, tt := range cases { @@ -47,30 +46,6 @@ func TestTemplateRendering(t *testing.T) { } } -func TestValidateTemplateAttribute(t *testing.T) { - file, err := ioutil.TempFile("", "testtemplate") - if err != nil { - t.Fatal(err) - } - file.WriteString("Hello world.") - file.Close() - defer os.Remove(file.Name()) - - ws, es := validateTemplateAttribute(file.Name(), "test") - - if len(es) != 0 { - t.Fatalf("Unexpected errors: %#v", es) - } - - if len(ws) != 1 { - t.Fatalf("Expected 1 warning, got %d", len(ws)) - } - - if !strings.Contains(ws[0], "Specifying a path directly is deprecated") { - t.Fatalf("Expected warning about path, got: %s", ws[0]) - } -} - func TestValidateVarsAttribute(t *testing.T) { cases := map[string]struct { Vars map[string]interface{}