github.com/hashicorp/packer@v1.14.3/fix/fixer_amazon_temporary_security_group_cidrs.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fix
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/mitchellh/mapstructure"
    10  )
    11  
    12  type FixerAmazonTemporarySecurityCIDRs struct{}
    13  
    14  func (FixerAmazonTemporarySecurityCIDRs) DeprecatedOptions() map[string][]string {
    15  	return map[string][]string{
    16  		"*amazon*": []string{"temporary_security_group_source_cidr"},
    17  	}
    18  }
    19  
    20  func (FixerAmazonTemporarySecurityCIDRs) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    21  	// Our template type we'll use for this fixer only
    22  	type template struct {
    23  		Builders []map[string]interface{}
    24  	}
    25  
    26  	// Decode the input into our structure, if we can
    27  	var tpl template
    28  	if err := mapstructure.Decode(input, &tpl); err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	// Go through each builder and replace the temporary_security_group_source_cidr if we can
    33  	for _, builder := range tpl.Builders {
    34  		builderTypeRaw, ok := builder["type"]
    35  		if !ok {
    36  			continue
    37  		}
    38  
    39  		builderType, ok := builderTypeRaw.(string)
    40  		if !ok {
    41  			continue
    42  		}
    43  
    44  		if !strings.HasPrefix(builderType, "amazon-") {
    45  			continue
    46  		}
    47  
    48  		temporarySecurityGroupCIDR, ok := builder["temporary_security_group_source_cidr"].(string)
    49  		if !ok {
    50  			continue
    51  		}
    52  
    53  		delete(builder, "temporary_security_group_source_cidr")
    54  		builder["temporary_security_group_source_cidrs"] = []string{temporarySecurityGroupCIDR}
    55  	}
    56  
    57  	input["builders"] = tpl.Builders
    58  	return input, nil
    59  }
    60  
    61  func (FixerAmazonTemporarySecurityCIDRs) Synopsis() string {
    62  	return `Replaces "temporary_security_group_source_cidr" (string) with "temporary_security_group_source_cidrs" (list of strings)`
    63  }