Fix chef provisioner validateFn

Correctly validate Chef provisioner's `use_policyfile`
field even if its value is a string type.

Signed-off-by: Jeremiah Snapp <jeremiah@chef.io>
This commit is contained in:
Jeremiah Snapp 2018-01-19 10:49:04 -05:00
parent 48517fbab4
commit 7595e27772
1 changed files with 18 additions and 6 deletions

View File

@ -11,6 +11,7 @@ import (
"os"
"path"
"regexp"
"strconv"
"strings"
"sync"
"text/template"
@ -370,18 +371,29 @@ func applyFn(ctx context.Context) error {
}
func validateFn(c *terraform.ResourceConfig) (ws []string, es []error) {
usePolicyFile, ok := c.Get("use_policyfile")
if !ok {
usePolicyFile = false
usePolicyFile := false
if usePolicyFileRaw, ok := c.Get("use_policyfile"); ok {
switch usePolicyFileRaw := usePolicyFileRaw.(type) {
case bool:
usePolicyFile = usePolicyFileRaw
case string:
usePolicyFileBool, err := strconv.ParseBool(usePolicyFileRaw)
if err != nil {
return ws, append(es, errors.New("\"use_policyfile\" must be a boolean"))
}
usePolicyFile = usePolicyFileBool
default:
return ws, append(es, errors.New("\"use_policyfile\" must be a boolean"))
}
}
if !usePolicyFile.(bool) && !c.IsSet("run_list") {
if !usePolicyFile && !c.IsSet("run_list") {
es = append(es, errors.New("\"run_list\": required field is not set"))
}
if usePolicyFile.(bool) && !c.IsSet("policy_name") {
if usePolicyFile && !c.IsSet("policy_name") {
es = append(es, errors.New("using policyfile, but \"policy_name\" not set"))
}
if usePolicyFile.(bool) && !c.IsSet("policy_group") {
if usePolicyFile && !c.IsSet("policy_group") {
es = append(es, errors.New("using policyfile, but \"policy_group\" not set"))
}