packer-cn/builder/vsphere/common/step_connect.go
Wilken Rivera 9ec8b67392
Add golangci-lint to project (#8686)
* Add golangci-lint as linting tool

* Disable failing staticchecks to start; GitHub issue to handle coming soon

* Run `goimports -w` to repair all source files that have improperly
formatted imports

* makefile: Add ci-lint target to run on travis

This change adds a new make target for running golangci-lint on newly
added Go files only. This target is expected to run during Packer ci builds.

* .github/contributing: Add code linting instructions

* travis: Update job configuration to run parallel builds
2020-02-14 11:42:29 -05:00

65 lines
1.7 KiB
Go

//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type ConnectConfig
package common
import (
"context"
"fmt"
"github.com/hashicorp/packer/builder/vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
)
type ConnectConfig struct {
// 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"`
}
func (c *ConnectConfig) Prepare() []error {
var errs []error
if c.VCenterServer == "" {
errs = append(errs, fmt.Errorf("'vcenter_server' is required"))
}
if c.Username == "" {
errs = append(errs, fmt.Errorf("'username' is required"))
}
if c.Password == "" {
errs = append(errs, fmt.Errorf("'password' is required"))
}
return errs
}
type StepConnect struct {
Config *ConnectConfig
}
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) {}