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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fix
     5  
     6  import (
     7  	"github.com/mitchellh/mapstructure"
     8  )
     9  
    10  // FixerSSHKeyPath changes the "ssh_key_path" of a template
    11  // to "ssh_private_key_file".
    12  type FixerSSHKeyPath struct{}
    13  
    14  func (FixerSSHKeyPath) DeprecatedOptions() map[string][]string {
    15  	return map[string][]string{
    16  		"*": []string{"ssh_key_path"},
    17  	}
    18  }
    19  
    20  func (FixerSSHKeyPath) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    21  	// The type we'll decode into; we only care about builders
    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  	for _, builder := range tpl.Builders {
    33  		sshKeyPathRaw, ok := builder["ssh_key_path"]
    34  		if !ok {
    35  			continue
    36  		}
    37  
    38  		sshKeyPath, ok := sshKeyPathRaw.(string)
    39  		if !ok {
    40  			continue
    41  		}
    42  
    43  		// only assign to ssh_private_key_file if it doesn't
    44  		// already exist; otherwise we'll just ignore ssh_key_path
    45  		_, sshPrivateIncluded := builder["ssh_private_key_file"]
    46  		if !sshPrivateIncluded {
    47  			builder["ssh_private_key_file"] = sshKeyPath
    48  		}
    49  
    50  		delete(builder, "ssh_key_path")
    51  	}
    52  
    53  	input["builders"] = tpl.Builders
    54  	return input, nil
    55  }
    56  
    57  func (FixerSSHKeyPath) Synopsis() string {
    58  	return `Updates builders using "ssh_key_path" to use "ssh_private_key_file"`
    59  }