diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 1b50b4842..289a461b9 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -53,7 +53,6 @@ type RunConfig struct { // Communicator settings Comm communicator.Config `mapstructure:",squash"` SSHKeyPairName string `mapstructure:"ssh_keypair_name"` - SSHPrivateIp bool `mapstructure:"ssh_private_ip"` SSHInterface string `mapstructure:"ssh_interface"` } @@ -78,14 +77,6 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { // Validation errs := c.Comm.Prepare(ctx) - if c.SSHPrivateIp && c.SSHInterface != "" { - errs = append(errs, errors.New("ssh_interface and ssh_private_ip should not both be specified")) - } - - // Legacy configurable - if c.SSHPrivateIp { - c.SSHInterface = "private_ip" - } // Valadating ssh_interface if c.SSHInterface != "public_ip" && diff --git a/fix/fixer.go b/fix/fixer.go index 5ca0b3a18..9f6247ee9 100644 --- a/fix/fixer.go +++ b/fix/fixer.go @@ -33,6 +33,7 @@ func init() { "manifest-filename": new(FixerManifestFilename), "amazon-shutdown_behavior": new(FixerAmazonShutdownBehavior), "amazon-enhanced-networking": new(FixerAmazonEnhancedNetworking), + "amazon-private-ip": new(FixerAmazonPrivateIP), "docker-email": new(FixerDockerEmail), } @@ -50,6 +51,7 @@ func init() { "manifest-filename", "amazon-shutdown_behavior", "amazon-enhanced-networking", + "amazon-private-ip", "docker-email", } } diff --git a/fix/fixer_amazon_enhanced_networking.go b/fix/fixer_amazon_enhanced_networking.go index 4c9330ebe..7188da7f7 100644 --- a/fix/fixer_amazon_enhanced_networking.go +++ b/fix/fixer_amazon_enhanced_networking.go @@ -1,6 +1,8 @@ package fix import ( + "strings" + "github.com/mitchellh/mapstructure" ) @@ -22,6 +24,19 @@ func (FixerAmazonEnhancedNetworking) Fix(input map[string]interface{}) (map[stri // 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 + } enhancedNetworkingRaw, ok := builder["enhanced_networking"] if !ok { continue diff --git a/fix/fixer_amazon_enhanced_networking_test.go b/fix/fixer_amazon_enhanced_networking_test.go index f8b5be178..78b58d582 100644 --- a/fix/fixer_amazon_enhanced_networking_test.go +++ b/fix/fixer_amazon_enhanced_networking_test.go @@ -17,12 +17,12 @@ func TestFixerAmazonEnhancedNetworking(t *testing.T) { // Attach field == false { Input: map[string]interface{}{ - "type": "ebs", + "type": "amazon-ebs", "enhanced_networking": false, }, Expected: map[string]interface{}{ - "type": "ebs", + "type": "amazon-ebs", "ena_support": false, }, }, @@ -30,12 +30,12 @@ func TestFixerAmazonEnhancedNetworking(t *testing.T) { // Attach field == true { Input: map[string]interface{}{ - "type": "ebs", + "type": "amazon-ebs", "enhanced_networking": true, }, Expected: map[string]interface{}{ - "type": "ebs", + "type": "amazon-ebs", "ena_support": true, }, }, diff --git a/fix/fixer_amazon_private_ip.go b/fix/fixer_amazon_private_ip.go new file mode 100644 index 000000000..7bfce1291 --- /dev/null +++ b/fix/fixer_amazon_private_ip.go @@ -0,0 +1,70 @@ +package fix + +import ( + "log" + "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 { + 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\"`" +} diff --git a/fix/fixer_amazon_private_ip_test.go b/fix/fixer_amazon_private_ip_test.go new file mode 100644 index 000000000..554584ded --- /dev/null +++ b/fix/fixer_amazon_private_ip_test.go @@ -0,0 +1,64 @@ +package fix + +import ( + "reflect" + "testing" +) + +func TestFixerAmazonPrivateIP_Impl(t *testing.T) { + var _ Fixer = new(FixerAmazonPrivateIP) +} + +func TestFixerAmazonPrivateIP(t *testing.T) { + cases := []struct { + Input map[string]interface{} + Expected map[string]interface{} + }{ + // Attach field == false + { + Input: map[string]interface{}{ + "type": "amazon-ebs", + "ssh_private_ip": false, + }, + + Expected: map[string]interface{}{ + "type": "amazon-ebs", + "ssh_interface": "public_ip", + }, + }, + + // Attach field == true + { + Input: map[string]interface{}{ + "type": "amazon-ebs", + "ssh_private_ip": true, + }, + + Expected: map[string]interface{}{ + "type": "amazon-ebs", + "ssh_interface": "private_ip", + }, + }, + } + + for _, tc := range cases { + var f FixerAmazonPrivateIP + + input := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Input}, + } + + expected := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Expected}, + } + + output, err := f.Fix(input) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !reflect.DeepEqual(output, expected) { + t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected) + } + } +} diff --git a/fix/fixer_amazon_shutdown_behavior.go b/fix/fixer_amazon_shutdown_behavior.go index 2e47c5592..686ef5119 100644 --- a/fix/fixer_amazon_shutdown_behavior.go +++ b/fix/fixer_amazon_shutdown_behavior.go @@ -1,6 +1,8 @@ package fix import ( + "strings" + "github.com/mitchellh/mapstructure" ) @@ -31,7 +33,7 @@ func (FixerAmazonShutdownBehavior) Fix(input map[string]interface{}) (map[string continue } - if builderType != "amazon-ebs" && builderType != "amazon-ebsvolume" && builderType != "amazon-instance" && builderType != "amazon-chroot" { + if !strings.HasPrefix(builderType, "amazon-") { continue }