github.com/hashicorp/packer@v1.14.3/builder/null/config.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  //go:generate packer-sdc mapstructure-to-hcl2 -type Config
     5  
     6  package null
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/hashicorp/packer-plugin-sdk/common"
    12  	"github.com/hashicorp/packer-plugin-sdk/communicator"
    13  	packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
    14  	"github.com/hashicorp/packer-plugin-sdk/template/config"
    15  	"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
    16  )
    17  
    18  type Config struct {
    19  	common.PackerConfig `mapstructure:",squash"`
    20  
    21  	CommConfig communicator.Config `mapstructure:",squash"`
    22  }
    23  
    24  func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
    25  
    26  	err := config.Decode(c, &config.DecodeOpts{
    27  		PluginType:        BuilderId,
    28  		Interpolate:       true,
    29  		InterpolateFilter: &interpolate.RenderFilter{},
    30  	}, raws...)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	var errs *packersdk.MultiError
    36  	if es := c.CommConfig.Prepare(nil); len(es) > 0 {
    37  		errs = packersdk.MultiErrorAppend(errs, es...)
    38  	}
    39  
    40  	if c.CommConfig.Type != "none" {
    41  		if c.CommConfig.Host() == "" {
    42  			errs = packersdk.MultiErrorAppend(errs,
    43  				fmt.Errorf("a Host must be specified, please reference your communicator documentation"))
    44  		}
    45  
    46  		if c.CommConfig.User() == "" {
    47  			errs = packersdk.MultiErrorAppend(errs,
    48  				fmt.Errorf("a Username must be specified, please reference your communicator documentation"))
    49  		}
    50  
    51  		if !c.CommConfig.SSHAgentAuth && c.CommConfig.Password() == "" && c.CommConfig.SSHPrivateKeyFile == "" {
    52  			errs = packersdk.MultiErrorAppend(errs,
    53  				fmt.Errorf("one authentication method must be specified, please reference your communicator documentation"))
    54  		}
    55  
    56  		if (c.CommConfig.SSHAgentAuth &&
    57  			(c.CommConfig.SSHPassword != "" || c.CommConfig.SSHPrivateKeyFile != "")) ||
    58  			(c.CommConfig.SSHPassword != "" && c.CommConfig.SSHPrivateKeyFile != "") {
    59  			errs = packersdk.MultiErrorAppend(errs,
    60  				fmt.Errorf("only one of ssh_agent_auth, ssh_password, and ssh_private_key_file must be specified"))
    61  
    62  		}
    63  	}
    64  
    65  	if errs != nil && len(errs.Errors) > 0 {
    66  		return nil, errs
    67  	}
    68  
    69  	return nil, nil
    70  }