Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/template: warn when template specified as path #5563

Merged
merged 1 commit into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions builtin/providers/template/resource_template_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func resourceFile() *schema.Resource {
Description: "Contents of the template",
ForceNew: true,
ConflictsWith: []string{"filename"},
ValidateFunc: validateTemplateAttribute,
},
"filename": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -174,3 +175,17 @@ func hash(s string) string {
sha := sha256.Sum256([]byte(s))
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
}
27 changes: 27 additions & 0 deletions builtin/providers/template/resource_template_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package template

import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"testing"

Expand Down Expand Up @@ -77,6 +80,30 @@ func TestTemplateVariableChange(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])
}
}

// This test covers a panic due to config.Func formerly being a
// shared map, causing multiple template_file resources to try and
// accessing it parallel during their lang.Eval() runs.
Expand Down