vsphere

package module
v0.0.0-...-5f66813 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

README

Go Reference Tests Latest Release Go Report Card codecov go.mod Go version

vsphere-client

Convenience library to create a vSphere client using govmomi

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRESTClient

func NewRESTClient(ctx context.Context, vc *vim25.Client) (*rest.Client, error)

NewRESTClient returns a vCenter REST (VAPI) API client with active keep-alive configured via environment variables.

Use Logout() to release resources and perform a clean logout from vCenter.

func NewSOAPClient

func NewSOAPClient(ctx context.Context) (*govmomi.Client, error)

NewSOAPClient returns a vCenter SOAP API client with active keep-alive configured via environment variables.

Use Logout() to release resources and perform a clean logout from vCenter.

Types

type Client

type Client struct {
	Soap *govmomi.Client
	Rest *rest.Client
}

Client is a combined vCenter SOAP and REST (VAPI) client

func NewClient

func NewClient(ctx context.Context) (*Client, error)

NewClient returns a combined vCenter SOAP and REST (VAPI) client with active keep-alive configured via environment variables.

Use Logout() to release resources and perform a clean logout from vCenter.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"path/filepath"

	"github.com/vmware/govmomi/simulator"
	"github.com/vmware/govmomi/vim25"
	"go.uber.org/zap"

	"github.com/embano1/vsphere-client"
	"github.com/embano1/vsphere-client/logger"
)

func main() {
	// use vcenter simulator
	simulator.Run(func(ctx context.Context, simClient *vim25.Client) error {
		l, err := setup(simClient.URL().String())
		if err != nil {
			return fmt.Errorf("setup environment: %w", err)
		}

		ctx = logger.Set(ctx, l)
		c, err := vsphere.NewClient(ctx)
		if err != nil {
			l.Fatal("create vsphere client", zap.Error(err))
		}

		defer func() {
			if err = c.Logout(); err != nil {
				l.Warn("logout", zap.Error(err))
			}
		}()

		l.Info("connected to vcenter", zap.String("version", c.Soap.Version))
		return nil
	})

}

// this is only needed for the example. In a real deployment, e.g. Kubernetes
// the secret and environment variables would be injected.
func setup(url string) (*zap.Logger, error) {
	dir, err := os.MkdirTemp("", "")
	if err != nil {
		return nil, fmt.Errorf("create temp directory: %w", err)
	}

	f, err := os.Create(filepath.Join(dir, "username"))
	if err != nil {
		return nil, fmt.Errorf("create user file: %w", err)
	}

	_, err = f.Write([]byte("usr"))
	if err != nil {
		return nil, fmt.Errorf("write to user file: %w", err)
	}
	if err = f.Close(); err != nil {
		return nil, fmt.Errorf("close user file: %w", err)
	}

	f, err = os.Create(filepath.Join(dir, "password"))
	if err != nil {
		return nil, fmt.Errorf("create password file: %w", err)
	}

	_, err = f.Write([]byte("pass"))
	if err != nil {
		return nil, fmt.Errorf("write to password file: %w", err)
	}

	if err = f.Close(); err != nil {
		return nil, fmt.Errorf("close password file: %w", err)
	}

	env := map[string]string{
		"VCENTER_URL":         url,
		"VCENTER_INSECURE":    "true",
		"VCENTER_SECRET_PATH": dir,
	}

	for e, v := range env {
		if err = os.Setenv(e, v); err != nil {
			return nil, fmt.Errorf("set %q env var: %w", e, err)
		}
	}

	// create logger
	cfg := zap.NewDevelopmentConfig()
	cfg.OutputPaths = []string{"stdout"}
	cfg.EncoderConfig.TimeKey = ""   // no timestamps
	cfg.EncoderConfig.CallerKey = "" // no func lines
	cfg.EncoderConfig.ConsoleSeparator = " "

	l, err := cfg.Build()
	if err != nil {
		return nil, fmt.Errorf("create logger: %w", err)
	}

	return l, nil
}
Output:

INFO connected to vcenter {"version": "7.0"}

func (*Client) Logout

func (c *Client) Logout() error

Logout attempts a clean logout from the various vCenter APIs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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