mirror of https://github.com/hashicorp/packer
Merge pull request #7450 from AkshatM/rename_security_group_cidr_to_security_group_cidrs
Rename and change `temporary_security_group_source_cidr` to accept a list of strings (for Amazon builders).pull/7461/head
commit
17c14770a7
@ -0,0 +1,53 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FixerAmazonTemporarySecurityCIDRs struct{}
|
||||
|
||||
func (FixerAmazonTemporarySecurityCIDRs) Fix(input map[string]interface{}) (map[string]interface{}, error) {
|
||||
// Our template type we'll use for this fixer only
|
||||
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 temporary_security_group_source_cidr 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
|
||||
}
|
||||
|
||||
temporarySecurityGroupCIDR, ok := builder["temporary_security_group_source_cidr"].(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
delete(builder, "temporary_security_group_source_cidr")
|
||||
builder["temporary_security_group_source_cidrs"] = []string{temporarySecurityGroupCIDR}
|
||||
}
|
||||
|
||||
input["builders"] = tpl.Builders
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func (FixerAmazonTemporarySecurityCIDRs) Synopsis() string {
|
||||
return `Replaces "temporary_security_group_source_cidr" (string) with "temporary_security_group_source_cidrs" (list of strings)`
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFixerAmazonTemporarySecurityCIDRs_Impl(t *testing.T) {
|
||||
var _ Fixer = new(FixerAmazonTemporarySecurityCIDRs)
|
||||
}
|
||||
|
||||
func TestFixerAmazonTemporarySecurityCIDRs(t *testing.T) {
|
||||
cases := []struct {
|
||||
Input map[string]interface{}
|
||||
Expected map[string]interface{}
|
||||
}{
|
||||
{
|
||||
Input: map[string]interface{}{
|
||||
"type": "amazon-ebs",
|
||||
"temporary_security_group_source_cidr": "0.0.0.0/0",
|
||||
},
|
||||
|
||||
Expected: map[string]interface{}{
|
||||
"type": "amazon-ebs",
|
||||
"temporary_security_group_source_cidrs": []string{"0.0.0.0/0"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
var f FixerAmazonTemporarySecurityCIDRs
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue