packer/builder/null/config.go
hashicorp-copywrite[bot] 19055df3ec
[COMPLIANCE] License changes (#12568)
* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at https://hashi.co/license-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUSL-1.1

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-10 15:53:29 -07:00

70 lines
2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
//go:generate packer-sdc mapstructure-to-hcl2 -type Config
package null
import (
"fmt"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
CommConfig communicator.Config `mapstructure:",squash"`
}
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
err := config.Decode(c, &config.DecodeOpts{
PluginType: BuilderId,
Interpolate: true,
InterpolateFilter: &interpolate.RenderFilter{},
}, raws...)
if err != nil {
return nil, err
}
var errs *packersdk.MultiError
if es := c.CommConfig.Prepare(nil); len(es) > 0 {
errs = packersdk.MultiErrorAppend(errs, es...)
}
if c.CommConfig.Type != "none" {
if c.CommConfig.Host() == "" {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("a Host must be specified, please reference your communicator documentation"))
}
if c.CommConfig.User() == "" {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("a Username must be specified, please reference your communicator documentation"))
}
if !c.CommConfig.SSHAgentAuth && c.CommConfig.Password() == "" && c.CommConfig.SSHPrivateKeyFile == "" {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("one authentication method must be specified, please reference your communicator documentation"))
}
if (c.CommConfig.SSHAgentAuth &&
(c.CommConfig.SSHPassword != "" || c.CommConfig.SSHPrivateKeyFile != "")) ||
(c.CommConfig.SSHPassword != "" && c.CommConfig.SSHPrivateKeyFile != "") {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("only one of ssh_agent_auth, ssh_password, and ssh_private_key_file must be specified"))
}
}
if errs != nil && len(errs.Errors) > 0 {
return nil, errs
}
return nil, nil
}