packer-cn/fix/fixer_amazon_temporary_secu...

55 lines
1.3 KiB
Go
Raw Normal View History

package fix
import (
"strings"
"github.com/mitchellh/mapstructure"
)
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
}
2019-04-02 02:42:31 -04:00
// 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
}
2019-04-02 02:42:31 -04:00
temporarySecurityGroupCIDR, ok := builder["temporary_security_group_source_cidr"].(string)
if !ok {
continue
}
2019-04-02 02:42:31 -04:00
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 {
2019-04-02 02:42:31 -04:00
return `Replaces "temporary_security_group_source_cidr" (string) with "temporary_security_group_source_cidrs" (list of strings)`
}