packer-cn/fix/fixer_amazon_private_ip.go
Brendan Devenney 73c532e772 Ensure amazon-private-ip fixes string values
The "ssh_private_ip" key works with either boolean values or string
representations of booleans. The fixer errors when the value is defined
as, for example, "true" (with quotation marks). This commit will attempt
to convert the string into a bool when necessary to ensure this case is
handled.

Signed-off-by: Brendan Devenney <brendan.devenney@cloudreach.com>
2018-07-05 16:57:54 +01:00

76 lines
1.6 KiB
Go

package fix
import (
"log"
"strconv"
"strings"
"github.com/mitchellh/mapstructure"
)
// FixerAmazonPrivateIP is a Fixer that replaces instances of `"private_ip":
// true` with `"ssh_interface": "private_ip"`
type FixerAmazonPrivateIP struct{}
func (FixerAmazonPrivateIP) Fix(input map[string]interface{}) (map[string]interface{}, error) {
type template struct {
Builders []map[string]interface{}
}
// Decode the input into our structure, if we can
var tpl template
if err := mapstructure.Decode(input, &tpl); err != nil {
return nil, err
}
// Go through each builder and replace the enhanced_networking if we can
for _, builder := range tpl.Builders {
builderTypeRaw, ok := builder["type"]
if !ok {
continue
}
builderType, ok := builderTypeRaw.(string)
if !ok {
continue
}
if !strings.HasPrefix(builderType, "amazon-") {
continue
}
// if ssh_interface already set, do nothing
if _, ok := builder["ssh_interface"]; ok {
continue
}
privateIPi, ok := builder["ssh_private_ip"]
if !ok {
continue
}
privateIP, ok := privateIPi.(bool)
if !ok {
var err error
privateIP, err = strconv.ParseBool(privateIPi.(string))
if err != nil {
log.Fatalf("Wrong type for ssh_private_ip")
continue
}
}
delete(builder, "ssh_private_ip")
if privateIP {
builder["ssh_interface"] = "private_ip"
} else {
builder["ssh_interface"] = "public_ip"
}
}
input["builders"] = tpl.Builders
return input, nil
}
func (FixerAmazonPrivateIP) Synopsis() string {
return "Replaces `\"ssh_private_ip\": true` in amazon builders with `\"ssh_interface\": \"private_ip\"`"
}