builder/amazon: remove ssh_private_ip

ssh_private_ip should now be set through ssh_interface.

Adds fixer to automatically fix existing json files
This commit is contained in:
Matthew Hooker 2018-02-08 16:47:17 -08:00
parent acfc51f0c9
commit 19a89a101e
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
7 changed files with 158 additions and 14 deletions

View File

@ -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" &&

View File

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

View File

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

View File

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

View File

@ -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\"`"
}

View File

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

View File

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