packer-cn/common/step_connect.go

56 lines
1.4 KiB
Go
Raw Normal View History

package common
import (
2018-04-25 07:22:38 -04:00
"github.com/hashicorp/packer/helper/multistep"
"fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
2018-04-25 07:22:38 -04:00
"context"
)
type ConnectConfig struct {
VCenterServer string `mapstructure:"vcenter_server"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
InsecureConnection bool `mapstructure:"insecure_connection"`
2017-07-02 16:29:50 -04:00
Datacenter string `mapstructure:"datacenter"`
}
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"))
}
if c.Username == "" {
2018-05-06 17:26:04 -04:00
errs = append(errs, fmt.Errorf("'username' is required"))
}
if c.Password == "" {
2018-05-06 17:26:04 -04:00
errs = append(errs, fmt.Errorf("'password' is required"))
}
return errs
}
type StepConnect struct {
Config *ConnectConfig
}
2018-05-16 09:05:08 -04:00
func (s *StepConnect) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
d, err := driver.NewDriver(&driver.ConnectConfig{
VCenterServer: s.Config.VCenterServer,
Username: s.Config.Username,
Password: s.Config.Password,
InsecureConnection: s.Config.InsecureConnection,
Datacenter: s.Config.Datacenter,
})
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("driver", d)
return multistep.ActionContinue
}
func (s *StepConnect) Cleanup(multistep.StateBag) {}