github.com/hashicorp/packer@v1.14.3/fix/fixer_amazon_shutdown_behavior.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  // FixerAmazonShutdownBehavior fix the spelling of "shutdown_behavior"
    13  // template in a Amazon builder
    14  type FixerAmazonShutdownBehavior struct{}
    15  
    16  func (FixerAmazonShutdownBehavior) DeprecatedOptions() map[string][]string {
    17  	return map[string][]string{
    18  		"*amazon*": []string{"shutdown_behaviour"},
    19  	}
    20  }
    21  
    22  func (FixerAmazonShutdownBehavior) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    23  	// The type we'll decode into; we only care about builders
    24  	type template struct {
    25  		Builders []map[string]interface{}
    26  	}
    27  
    28  	// Decode the input into our structure, if we can
    29  	var tpl template
    30  	if err := mapstructure.Decode(input, &tpl); err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	for _, builder := range tpl.Builders {
    35  		builderTypeRaw, ok := builder["type"]
    36  		if !ok {
    37  			continue
    38  		}
    39  
    40  		builderType, ok := builderTypeRaw.(string)
    41  		if !ok {
    42  			continue
    43  		}
    44  
    45  		if !strings.HasPrefix(builderType, "amazon-") {
    46  			continue
    47  		}
    48  
    49  		shutdownBehavior, ok := builder["shutdown_behaviour"]
    50  
    51  		if ok {
    52  			builder["shutdown_behavior"] = shutdownBehavior
    53  			delete(builder, "shutdown_behaviour")
    54  		}
    55  	}
    56  
    57  	input["builders"] = tpl.Builders
    58  	return input, nil
    59  }
    60  
    61  func (FixerAmazonShutdownBehavior) Synopsis() string {
    62  	return `Changes "shutdown_behaviour" to "shutdown_behavior" in Amazon builders.`
    63  }