provider/template: warn when template specified as path

Turns out the BC code allowed users to move from `filename` to
`template` to squash the warning without having to switch from template
paths to template contents.

Here we warn when `template` is specified as a path so we can remove the
functionality in the future and remove this source of confusion.

refs #3732
This commit is contained in:
Paul Hinze 2016-03-10 12:34:56 -06:00
parent 2dc1c022d3
commit 0036db8f82
2 changed files with 42 additions and 0 deletions

View File

@ -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,
@ -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
}

View File

@ -2,6 +2,9 @@ package template
import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"testing"
@ -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.