2017-08-23 15:40:57 -04:00
|
|
|
package driver
|
2017-07-01 23:46:38 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/vmware/govmomi"
|
|
|
|
"github.com/vmware/govmomi/find"
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
"fmt"
|
|
|
|
"github.com/vmware/govmomi/object"
|
2017-07-02 08:27:52 -04:00
|
|
|
"time"
|
2017-08-15 19:49:44 -04:00
|
|
|
"github.com/vmware/govmomi/session"
|
|
|
|
"github.com/vmware/govmomi/vim25/soap"
|
|
|
|
"github.com/vmware/govmomi/vim25"
|
2017-07-01 23:46:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Driver struct {
|
|
|
|
ctx context.Context
|
|
|
|
client *govmomi.Client
|
|
|
|
finder *find.Finder
|
2017-07-02 16:29:50 -04:00
|
|
|
datacenter *object.Datacenter
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
|
|
|
|
2017-08-23 15:40:57 -04:00
|
|
|
type ConnectConfig struct {
|
|
|
|
VCenterServer string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
InsecureConnection bool
|
|
|
|
Datacenter string
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:22:38 -04:00
|
|
|
func NewDriver(ctx context.Context, config *ConnectConfig) (*Driver, error) {
|
2017-07-01 23:46:38 -04:00
|
|
|
|
|
|
|
vcenter_url, err := url.Parse(fmt.Sprintf("https://%v/sdk", config.VCenterServer))
|
|
|
|
if err != nil {
|
2017-07-02 16:29:50 -04:00
|
|
|
return nil, err
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
2017-08-15 19:49:44 -04:00
|
|
|
credentials := url.UserPassword(config.Username, config.Password)
|
|
|
|
vcenter_url.User = credentials
|
|
|
|
|
|
|
|
soapClient := soap.NewClient(vcenter_url, config.InsecureConnection)
|
|
|
|
vimClient, err := vim25.NewClient(ctx, soapClient)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vimClient.RoundTripper = session.KeepAlive(vimClient.RoundTripper, 10*time.Minute)
|
|
|
|
client := &govmomi.Client{
|
|
|
|
Client: vimClient,
|
|
|
|
SessionManager: session.NewManager(vimClient),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.SessionManager.Login(ctx, credentials)
|
2017-07-01 23:46:38 -04:00
|
|
|
if err != nil {
|
2017-07-02 16:29:50 -04:00
|
|
|
return nil, err
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
finder := find.NewFinder(client.Client, false)
|
|
|
|
datacenter, err := finder.DatacenterOrDefault(ctx, config.Datacenter)
|
|
|
|
if err != nil {
|
2017-07-02 16:29:50 -04:00
|
|
|
return nil, err
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
|
|
|
finder.SetDatacenter(datacenter)
|
|
|
|
|
|
|
|
d := Driver{
|
|
|
|
ctx: ctx,
|
|
|
|
client: client,
|
|
|
|
datacenter: datacenter,
|
|
|
|
finder: finder,
|
|
|
|
}
|
2017-07-02 16:29:50 -04:00
|
|
|
return &d, nil
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|