cache

package
v0.30.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: Apache-2.0 Imports: 14 Imported by: 47

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	json.Marshaler
	json.Unmarshaler

	Valid() bool
	Path() string
}

Client interface to support client session caching

type Session

type Session struct {
	URL         *url.URL // URL of a vCenter or ESXi instance
	DirSOAP     string   // DirSOAP cache directory. Defaults to "$HOME/.govmomi/sessions"
	DirREST     string   // DirREST cache directory. Defaults to "$HOME/.govmomi/rest_sessions"
	Insecure    bool     // Insecure param for soap.NewClient (tls.Config.InsecureSkipVerify)
	Passthrough bool     // Passthrough disables caching when set to true
	Reauth      bool     // Reauth skips loading of cached sessions when set to true

	LoginSOAP func(context.Context, *vim25.Client) error // LoginSOAP defaults to session.Manager.Login()
	LoginREST func(context.Context, *rest.Client) error  // LoginREST defaults to rest.Client.Login()
}

Session provides methods to cache authenticated vim25.Client and rest.Client sessions. Use of session cache avoids the expense of creating and deleting vSphere sessions. It also helps avoid the problem of "leaking sessions", as Session.Login will only create a new authenticated session if the cached session does not exist or is invalid. By default, username/password authentication is used to create new sessions. The Session.Login{SOAP,REST} fields can be set to use other methods, such as SAML token authentication (see govc session.login for example).

When Reauth is set to true, Login skips loading file cache and performs username/password authentication, which is helpful in the case that the password in URL is different than previously cached session. Comparing to `Passthrough`, the file cache will be updated after authentication is done.

func (*Session) Endpoint

func (s *Session) Endpoint() *url.URL

Endpoint returns a copy of the Session.URL with Password, Query and Fragment removed.

func (*Session) Load

func (s *Session) Load(ctx context.Context, c Client, config func(*soap.Client) error) (bool, error)

Load a Client from the file cache. Returns false if no cache exists or is invalid. An error is returned if the file cannot be opened or is not json encoded. After loading the Client from the file: Returns true if the session is still valid, false otherwise indicating the client requires authentication. An error is returned if the session ID cannot be validated. Returns false if Session.Passthrough is true.

func (*Session) Login

func (s *Session) Login(ctx context.Context, c Client, config func(*soap.Client) error) error

Login returns a cached session via Load() if valid. Otherwise, creates a new authenticated session and saves to the cache. The config func can be used to apply soap.Client configuration, such as TLS settings. When Session.Passthrough is true, Login will always create a new session.

Example
package main

import (
	"context"
	"fmt"
	"net/url"

	"github.com/vmware/govmomi/session/cache"
	"github.com/vmware/govmomi/simulator"
	"github.com/vmware/govmomi/vim25"

	"github.com/vmware/govmomi/vapi/rest"
	_ "github.com/vmware/govmomi/vapi/simulator"
)

func main() {
	simulator.Run(func(ctx context.Context, sim *vim25.Client) error {
		u := sim.URL()
		u.User = simulator.DefaultLogin

		// Login() each client twice
		// 1) creates a new authenticated session
		// 2) uses a cached session (proved by removing the password)
		for i := 0; i < 2; i++ {
			s := &cache.Session{
				URL:      u,
				Insecure: true,
			}

			vc := new(vim25.Client)
			err := s.Login(ctx, vc, nil)
			if err != nil {
				return err
			}

			rc := new(rest.Client)
			err = s.Login(ctx, rc, nil)
			if err != nil {
				return err
			}

			fmt.Printf("%s authenticated\n", u.User)
			u.User = url.User(u.User.Username()) // Remove password
		}
		return nil
	})
}
Output:

user:pass authenticated
user authenticated

func (*Session) Logout

func (s *Session) Logout(ctx context.Context, c Client) error

Login calls the Logout method for the given Client if Session.Passthrough is true. Otherwise returns nil.

func (*Session) Save

func (s *Session) Save(c Client) error

Save a Client in the file cache. Session will not be saved if Session.Passthrough is true.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL