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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fix
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/mitchellh/mapstructure"
    12  )
    13  
    14  // FixerAmazonPrivateIP is a Fixer that replaces instances of `"private_ip":
    15  // true` with `"ssh_interface": "private_ip"`
    16  type FixerAmazonPrivateIP struct{}
    17  
    18  func (FixerAmazonPrivateIP) DeprecatedOptions() map[string][]string {
    19  	return map[string][]string{
    20  		"*amazon*": []string{"ssh_private_ip"},
    21  	}
    22  }
    23  
    24  func (FixerAmazonPrivateIP) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    25  	type template struct {
    26  		Builders []map[string]interface{}
    27  	}
    28  
    29  	// Decode the input into our structure, if we can
    30  	var tpl template
    31  	if err := mapstructure.Decode(input, &tpl); err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	// Go through each builder and replace the enhanced_networking if we can
    36  	for _, builder := range tpl.Builders {
    37  		builderTypeRaw, ok := builder["type"]
    38  		if !ok {
    39  			continue
    40  		}
    41  
    42  		builderType, ok := builderTypeRaw.(string)
    43  		if !ok {
    44  			continue
    45  		}
    46  
    47  		if !strings.HasPrefix(builderType, "amazon-") {
    48  			continue
    49  		}
    50  
    51  		// if ssh_interface already set, do nothing
    52  		if _, ok := builder["ssh_interface"]; ok {
    53  			continue
    54  		}
    55  
    56  		privateIPi, ok := builder["ssh_private_ip"]
    57  		if !ok {
    58  			continue
    59  		}
    60  		privateIP, ok := privateIPi.(bool)
    61  		if !ok {
    62  			var err error
    63  			privateIP, err = strconv.ParseBool(privateIPi.(string))
    64  			if err != nil {
    65  				return nil, fmt.Errorf("ssh_private_ip is not a boolean, %s", err)
    66  			}
    67  		}
    68  
    69  		delete(builder, "ssh_private_ip")
    70  		if privateIP {
    71  			builder["ssh_interface"] = "private_ip"
    72  		} else {
    73  			builder["ssh_interface"] = "public_ip"
    74  		}
    75  	}
    76  
    77  	input["builders"] = tpl.Builders
    78  	return input, nil
    79  }
    80  
    81  func (FixerAmazonPrivateIP) Synopsis() string {
    82  	return "Replaces `\"ssh_private_ip\": true` in amazon builders with `\"ssh_interface\": \"private_ip\"`"
    83  }