generated func now validates based on data provided in context, which needs to include the future values if it is to be interpolated properly.

This commit is contained in:
Megan Marsh 2019-12-11 15:43:11 -08:00
parent 528b853178
commit 5d266b20d0
2 changed files with 28 additions and 8 deletions

View File

@ -53,10 +53,6 @@ func PlaceholderData() map[string]string {
return placeholderData
}
type StepProvision struct {
Comm packer.Communicator
}
func PopulateProvisionHookData(state multistep.StateBag) map[string]interface{} {
hookData := map[string]interface{}{}
// Read communicator data into hook data
@ -76,13 +72,24 @@ func PopulateProvisionHookData(state multistep.StateBag) map[string]interface{}
}
// Loop over all field values and retrieve them from the ssh config
for fieldName, _ := range pd {
if fieldName == "ID" {
continue
}
fVal := v.FieldByName(fieldName)
hookData[fieldName] = fVal.Interface()
if fVal.IsZero() {
log.Printf("Megan NONINTERFACABLE fVal is %#v", fVal)
} else {
hookData[fieldName] = fVal.Interface()
}
}
return hookData
}
type StepProvision struct {
Comm packer.Communicator
}
func (s *StepProvision) runWithHook(ctx context.Context, state multistep.StateBag, hooktype string) multistep.StepAction {
// hooktype will be either packer.HookProvision or packer.HookCleanupProvision
comm := s.Comm

View File

@ -3,6 +3,7 @@ package interpolate
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
@ -164,9 +165,21 @@ func funcGenTemplateDir(ctx *Context) interface{} {
}
func funcGenGenerated(ctx *Context) interface{} {
return func(k string) (string, error) {
// Return the key inside braces _without_ the generated func attached
return fmt.Sprintf("{{.%s}}", k), nil
return func(s string) (string, error) {
log.Printf("Megan context data is %#v", ctx.Data)
if data, ok := ctx.Data.(map[string]string); ok {
// PlaceholderData has been passed into generator, and we can check
// the value against the placeholder data to make sure that it is
// valid
if heldPlace, ok := data[s]; ok {
return heldPlace, nil
} else {
return "", fmt.Errorf("loaded data, but couldnt find winrm in it", s)
}
}
return "", fmt.Errorf("Error validating computed variable: the given "+
"variable %s will not be passed into your plugin.", s)
}
}