package client import ( "context" "regexp" "esxlib/pkg/sshutil" "github.com/vmware/govmomi/session/cache" "github.com/vmware/govmomi/vim25" "github.com/vmware/govmomi/vim25/soap" ) var diskRegexp = regexp.MustCompile("\\[(.*?)\\] (.*)") type Option func(c *Client) type Client struct { sshAuth *sshutil.Auth vim *vim25.Client VirtualMachines VirtualMachines } type ClientRef interface { VIM() *vim25.Client } func (c *Client) VIM() *vim25.Client { return c.vim } func WithSSH(host, user, password string) Option { return func(c *Client) { c.sshAuth = &sshutil.Auth{ Host: host, User: user, Password: password, } } } func NewClient(ctx context.Context, url string, insecure bool, opts ...Option) (*Client, error) { // Parse URL from string u, err := soap.ParseURL(url) if err != nil { return nil, err } // Share govc's session cache s := &cache.Session{ URL: u, Insecure: insecure, } c := new(vim25.Client) err = s.Login(ctx, c, nil) if err != nil { return nil, err } client := &Client{ vim: c, } for _, opt := range opts { opt(client) } client.VirtualMachines.ClientRef = client client.VirtualMachines.sshAuth = client.sshAuth return client, nil }