// NewClient returns a logged in client
NewClient returns a logged in client
// NewClient returns a logged in client
func NewClient(u *url.URL, user, pw string, cache bool) (c Client, err error) {
	if cache {
		c, err := clientFromDisk(u.Host, pw, u)
		if err == nil {
			c.Cached = true
			return c, nil
		}
	}
	ctx := context.Background()
	if u.Host == "" {
		return Client{}, fmt.Errorf("you need to provide a url, I.E https://vcenter/sdk")
	}
	u.User = url.UserPassword(user, pw)
	soapClient := soap.NewClient(u, true)
	c.c, err = vim25.NewClient(ctx, soapClient)
	if err != nil {
		return c, fmt.Errorf("unable to connect to %v ", u)
	}
	c.r = rest.NewClient(c.c)
	err = sessionLogin(c.c, u)
	if err != nil {
		return Client{}, err
	}
	ui := url.UserPassword(user, pw)
	if c.r != nil {
		err = c.r.Login(ctx, ui)
		if err != nil {
			return c, fmt.Errorf("rest login %v", err)
		}
		ses, err := c.r.Session(ctx)
		if err != nil {
			log.Fatal(err)
		}
		if ses == nil {
			log.Fatal("expected non-nil session")
		}
	}
	err = sessionCheck(c.c)
	if err != nil {
		return Client{}, err
	}
	c.m = view.NewManager(c.c)
	c.ctx = ctx
	if cache {
		err := c.save2Disk(u.Host, pw)
		if err != nil {
			return c, fmt.Errorf("failed to save cached creds to disk")
		}
		c.Cached = true
	}
	return c, err
}
func NewClient ( u * url . URL , user , pw string , cache bool ) ( c Client , err error ) { if cache { c , err := clientFromDisk ( u . Host , pw , u ) if err == nil { c . Cached = true return c , nil } } ctx := context . Background ( ) if u . Host == " " { return Client { } , fmt . Errorf ( " " ) } u . User = url . UserPassword ( user , pw ) soapClient := soap . NewClient ( u , true ) c . c , err = vim25 . NewClient ( ctx , soapClient ) if err != nil { return c , fmt . Errorf ( " " , u ) } c . r = rest . NewClient ( c . c ) err = sessionLogin ( c . c , u ) if err != nil { return Client { } , err } ui := url . UserPassword ( user , pw ) if c . r != nil { err = c . r . Login ( ctx , ui ) if err != nil { return c , fmt . Errorf ( " " , err ) } ses , err := c . r . Session ( ctx ) if err != nil { log . Fatal ( err ) } if ses == nil { log . Fatal ( " " ) } } err = sessionCheck ( c . c ) if err != nil { return Client { } , err } c . m = view . NewManager ( c . c ) c . ctx = ctx if cache { err := c . save2Disk ( u . Host , pw ) if err != nil { return c , fmt . Errorf ( " " ) } c . Cached = true } return c , err }
NewClient returns a logged in client
NewClient returns a logged in client
// load from cache if enabled, will fall through to login code if there are any issues