rest

package
v0.54.0-alpha.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: Apache-2.0 Imports: 16 Imported by: 158

Documentation

Index

Examples

Constants

View Source
const (
	Path = "/rest"
)

Variables

This section is empty.

Functions

func IsStatusError added in v0.34.0

func IsStatusError(err error, code int) bool

Types

type Client

type Client struct {
	*soap.Client
	// contains filtered or unexported fields
}

Client extends soap.Client to support JSON encoding, while inheriting security features, debug tracing and session persistence.

func NewClient

func NewClient(c *vim25.Client) *Client

NewClient creates a new Client instance.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request, resBody any) error

Do sends the http.Request, decoding resBody if provided.

func (*Client) Download added in v0.23.0

func (c *Client) Download(ctx context.Context, u *url.URL, param *soap.Download) (io.ReadCloser, int64, error)

Download wraps soap.Client.Download, adding the REST authentication header

func (*Client) DownloadAttachment added in v0.44.0

func (c *Client) DownloadAttachment(ctx context.Context, req *http.Request, filename string) error

DownloadAttachment writes the response to given filename, defaulting to Content-Disposition filename in the response. A filename of "-" writes the response to stdout.

func (*Client) DownloadFile added in v0.23.0

func (c *Client) DownloadFile(ctx context.Context, file string, u *url.URL, param *soap.Download) error

DownloadFile wraps soap.Client.DownloadFile, adding the REST authentication header

func (*Client) Login

func (c *Client) Login(ctx context.Context, user *url.Userinfo) error

Login creates a new session via Basic Authentication with the given url.Userinfo.

func (*Client) LoginByToken added in v0.20.0

func (c *Client) LoginByToken(ctx context.Context) error
Example
package main

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

	_ "github.com/vmware/govmomi/lookup/simulator"
	"github.com/vmware/govmomi/simulator"
	"github.com/vmware/govmomi/sts"
	"github.com/vmware/govmomi/vapi/rest"
	"github.com/vmware/govmomi/vim25"

	_ "github.com/vmware/govmomi/sts/simulator"
)

func main() {
	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
		c, err := sts.NewClient(ctx, vc)
		if err != nil {
			return err
		}

		// Issue a bearer token
		req := sts.TokenRequest{
			Userinfo: url.UserPassword("Administrator@VSPHERE.LOCAL", "password"),
		}

		signer, err := c.Issue(ctx, req)
		if err != nil {
			return err
		}

		rc := rest.NewClient(vc)

		err = rc.LoginByToken(rc.WithSigner(ctx, signer))
		if err != nil {
			return err
		}

		session, err := rc.Session(ctx)
		if err != nil {
			return err
		}

		// Note: vcsim does not currently parse the token NameID for rest as it does for soap
		fmt.Println(session.User)

		return nil
	})
}
Output:

TODO

func (*Client) Logout

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

Logout deletes the current session.

func (*Client) MarshalJSON added in v0.23.0

func (c *Client) MarshalJSON() ([]byte, error)

func (*Client) Path added in v0.23.0

func (c *Client) Path() string

Path returns rest.Path (see cache.Client)

func (*Client) Resource added in v0.22.0

func (c *Client) Resource(path string) *Resource

Resource helper for the given path.

func (*Client) Session added in v0.22.0

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

Session returns the user's current session. Nil is returned if the session is not authenticated.

func (*Client) SessionID added in v0.23.0

func (c *Client) SessionID(id ...string) string

SessionID is set by calling Login() or optionally with the given id param

func (*Client) UnmarshalJSON added in v0.23.0

func (c *Client) UnmarshalJSON(b []byte) error

func (*Client) Upload added in v0.23.0

func (c *Client) Upload(ctx context.Context, f io.Reader, u *url.URL, param *soap.Upload) error

Upload wraps soap.Client.Upload, adding the REST authentication header

func (*Client) Valid added in v0.23.0

func (c *Client) Valid() bool

Valid returns whether or not the client is valid and ready for use. This should be called after unmarshalling the client.

func (*Client) WithHeader added in v0.27.0

func (c *Client) WithHeader(
	ctx context.Context,
	headers http.Header) context.Context

WithHeader returns a new Context populated with the provided headers map. Calls to a VAPI REST client with this context will populate the HTTP headers map using the provided headers.

func (*Client) WithSigner added in v0.20.0

func (c *Client) WithSigner(ctx context.Context, s Signer) context.Context

type Error added in v0.53.0

type Error struct {
	Messages []LocalizableMessage `json:"messages"`

	Data json.RawMessage `json:"data"`

	ErrorType Type `json:"error_type"`
}

type LocalizableMessage added in v0.21.0

type LocalizableMessage struct {
	Args           []string `json:"args,omitempty"`
	DefaultMessage string   `json:"default_message,omitempty"`
	ID             string   `json:"id,omitempty"`
}

LocalizableMessage represents a localizable error

func (*LocalizableMessage) Error added in v0.21.0

func (m *LocalizableMessage) Error() string

type Notification added in v0.53.0

type Notification struct {
	// Id is the notification id.
	Id string `json:"id"`

	// Time the notification was raised/found.
	Time time.Time `json:"time"`

	Message LocalizableMessage `json:"message"`

	Resolution LocalizableMessage `json:"resolution"`
}

Notification contains fields to describe any info/warning/error messages that Tasks can raise.

type Notifications added in v0.53.0

type Notifications struct {
	// Info notification messages reported.
	Info []Notification `json:"info"`

	// Warning notification messages reported.
	Warnings []Notification `json:"warnings"`

	// Errors notification messages reported.
	Errors []Notification `json:"errors"`
}

Notifications contains info/warning/error messages that can be reported.

type RawResponse added in v0.27.0

type RawResponse struct {
	bytes.Buffer
}

RawResponse may be used with the Do method as the resBody argument in order to capture the raw response data.

type Resource added in v0.22.0

type Resource struct {
	// contains filtered or unexported fields
}

Resource wraps url.URL with helpers

func (*Resource) Request added in v0.22.0

func (r *Resource) Request(method string, body ...any) *http.Request

Request returns a new http.Request for the given method. An optional body can be provided for POST and PATCH methods.

func (*Resource) String added in v0.22.0

func (r *Resource) String() string

func (*Resource) WithAction added in v0.22.0

func (r *Resource) WithAction(action string) *Resource

WithAction sets adds action to the URL.RawQuery

func (*Resource) WithID added in v0.22.0

func (r *Resource) WithID(id string) *Resource

WithID appends id to the URL.Path

func (*Resource) WithParam added in v0.22.0

func (r *Resource) WithParam(name string, value string) *Resource

WithParam adds one parameter on the URL.RawQuery

func (*Resource) WithPathEncodedParam added in v0.28.0

func (r *Resource) WithPathEncodedParam(name string, value string) *Resource

WithPathEncodedParam appends a parameter on the URL.RawQuery, For special cases where URL Path-style encoding is needed

func (*Resource) WithSubpath added in v0.36.0

func (r *Resource) WithSubpath(subpath string) *Resource

WithSubpath appends the provided subpath to the URL.Path

type Session added in v0.22.0

type Session struct {
	User         string    `json:"user"`
	Created      time.Time `json:"created_time"`
	LastAccessed time.Time `json:"last_accessed_time"`
}

Session information

type Signer added in v0.20.0

type Signer interface {
	SignRequest(*http.Request) error
}

type Type added in v0.53.0

type Type string
const (
	ErrError Type = "ERROR"

	ErrorAlreadyExists Type = "ALREADY_EXISTS"

	ErrAlreadyInDesiredState Type = "ALREADY_IN_DESIRED_STATE"

	ErrCanceled Type = "CANCELED"

	ErrConcurrentChange Type = "CONCURRENT_CHANGE"

	ErrFeatureInUse Type = "FEATURE_IN_USE"

	ErrInternalServer Type = "INTERNAL_SERVER_ERROR"

	ErrInvalidArgument Type = "INVALID_ARGUMENT"

	ErrInvalidElementConfiguration Type = "INVALID_ELEMENT_CONFIGURATION"

	ErrInvalidElementType Type = "INVALID_ELEMENT_TYPE"

	ErrInvalidRequest Type = "INVALID_REQUEST"

	ErrNotAllowedInCurrentState Type = "NOT_ALLOWED_IN_CURRENT_STATE"

	ErrNotFound Type = "NOT_FOUND"

	ErrOperationNotFound Type = "OPERATION_NOT_FOUND"

	ErrResourceBusy Type = "RESOURCE_BUSY"

	ErrResourceInUse Type = "RESOURCE_IN_USE"

	ErrResourceInaccessible Type = "RESOURCE_INACCESSIBLE"

	ErrServiceUnavailable Type = "SERVICE_UNAVAILABLE"

	ErrTimedOut Type = "TIMED_OUT"

	ErrUnableToAllocateResource Type = "UNABLE_TO_ALLOCATE_RESOURCE"

	ErrUnauthenticated Type = "UNAUTHENTICATED"

	ErrUnauthorized Type = "UNAUTHORIZED"

	ErrUnexpectedInput Type = "UNEXPECTED_INPUT"

	ErrUnsupported Type = "UNSUPPORTED"

	ErrUnverifiedPeer Type = "UNVERIFIED_PEER"
)

Jump to

Keyboard shortcuts

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