2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type ConnectConfig
|
|
|
|
|
2018-01-24 06:04:39 -05:00
|
|
|
package common
|
2017-07-01 20:52:10 -04:00
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
2017-07-01 20:52:10 -04:00
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-10-31 17:42:24 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-07-01 20:52:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConnectConfig struct {
|
2020-01-07 19:59:31 -05:00
|
|
|
// vCenter server hostname.
|
|
|
|
VCenterServer string `mapstructure:"vcenter_server"`
|
|
|
|
// vSphere username.
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
// vSphere password.
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
// Do not validate vCenter server's TLS certificate. Defaults to `false`.
|
|
|
|
InsecureConnection bool `mapstructure:"insecure_connection"`
|
|
|
|
// VMware datacenter name. Required if there is more than one datacenter in vCenter.
|
|
|
|
Datacenter string `mapstructure:"datacenter"`
|
2017-07-01 20:52:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnectConfig) Prepare() []error {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
if c.VCenterServer == "" {
|
2018-05-06 17:26:04 -04:00
|
|
|
errs = append(errs, fmt.Errorf("'vcenter_server' is required"))
|
2017-07-01 20:52:10 -04:00
|
|
|
}
|
|
|
|
if c.Username == "" {
|
2018-05-06 17:26:04 -04:00
|
|
|
errs = append(errs, fmt.Errorf("'username' is required"))
|
2017-07-01 20:52:10 -04:00
|
|
|
}
|
|
|
|
if c.Password == "" {
|
2018-05-06 17:26:04 -04:00
|
|
|
errs = append(errs, fmt.Errorf("'password' is required"))
|
2017-07-01 20:52:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
type StepConnect struct {
|
2018-01-24 06:04:39 -05:00
|
|
|
Config *ConnectConfig
|
2017-07-01 20:52:10 -04:00
|
|
|
}
|
|
|
|
|
2020-07-10 05:01:10 -04:00
|
|
|
func (s *StepConnect) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
d, err := driver.NewDriver(ctx, &driver.ConnectConfig{
|
2018-01-24 06:04:39 -05:00
|
|
|
VCenterServer: s.Config.VCenterServer,
|
|
|
|
Username: s.Config.Username,
|
|
|
|
Password: s.Config.Password,
|
|
|
|
InsecureConnection: s.Config.InsecureConnection,
|
|
|
|
Datacenter: s.Config.Datacenter,
|
2017-08-23 15:40:57 -04:00
|
|
|
})
|
2017-07-01 20:52:10 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-08-23 15:40:57 -04:00
|
|
|
state.Put("driver", d)
|
2017-07-01 20:52:10 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepConnect) Cleanup(multistep.StateBag) {}
|