vsphereclient

package
v0.0.0-...-298751d Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: AGPL-3.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const UbuntuOVF = `` /* 10166-byte string literal not displayed */

Variables

View Source
var (
	// ValidDiskProvisioningTypes is a list of valid disk provisioning types.
	ValidDiskProvisioningTypes = []DiskProvisioningType{
		DiskTypeThin,
		DiskTypeThick,
		DiskTypeThickLazyZero,
	}
	// DefaultDiskProvisioningType is the default disk provisioning type
	// if none is selected in the model config.
	DefaultDiskProvisioningType = DiskTypeThick
)

Functions

func GenerateMAC

func GenerateMAC() (string, error)

GenerateMAC generates a random hardware address that meets VMWare requirements for MAC address: 00:50:56:XX:YY:ZZ where XX is between 00 and 3f. https://pubs.vmware.com/vsphere-4-esx-vcenter/index.jsp?topic=/com.vmware.vsphere.server_configclassic.doc_41/esx_server_config/advanced_networking/c_setting_up_mac_addresses.html

func IsExtendDiskError

func IsExtendDiskError(err error) bool

IsExtendDiskError returns whether the cause of this error was a failure to extend a VM disk.

func VerifyMAC

func VerifyMAC(mac string) bool

VerifyMAC verifies that the MAC is valid for VMWare.

Types

type Client

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

Client encapsulates a vSphere client, exposing the subset of functionality that we require in the Juju provider.

func Dial

func Dial(
	ctx context.Context,
	u *url.URL,
	datacenter string,
	logger loggo.Logger,
) (*Client, error)

Dial dials a new vSphere client connection using the given URL, scoped to the specified datacenter. The resulting Client's Close method must be called in order to release resources allocated by Dial.

func (*Client) Close

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

Close logs out and closes the client connection.

func (*Client) ComputeResources

func (c *Client) ComputeResources(ctx context.Context) ([]ComputeResource, error)

ComputeResources returns a slice of all compute resources in the datacenter, along with a slice of each compute resource's full path.

func (*Client) CreateTemplateVM

func (c *Client) CreateTemplateVM(
	ctx context.Context,
	ovaArgs ImportOVAParameters,
) (vm *object.VirtualMachine, err error)

CreateTemplateVM returns a vSphere template VM that instances can be created from.

func (*Client) CreateVirtualMachine

func (c *Client) CreateVirtualMachine(
	ctx context.Context,
	args CreateVirtualMachineParams,
) (_ *mo.VirtualMachine, err error)

CreateVirtualMachine creates and powers on a new VM.

The creation process makes use of a vSphere feature called template VMs. If it doesn't already exist, a template VM will be created within args.VMDKDirectory based off of the OVA data provided by args.ReadOVA.

Once the template VM is available, a new instance will be cloned from it. Configuration settings from args.Constraints are then applied through a reconfigure step. Once the constraints are applied, the newly cloned VM will be powered on.

func (*Client) Datastores

func (c *Client) Datastores(ctx context.Context) ([]mo.Datastore, error)

Datastores returns list of all datastores in the system.

func (*Client) DeleteDatastoreFile

func (c *Client) DeleteDatastoreFile(ctx context.Context, datastorePath string) error

DeleteDatastoreFile deletes a file or directory in the datastore.

func (*Client) DestroyVMFolder

func (c *Client) DestroyVMFolder(ctx context.Context, folderPath string) error

DestroyVMFolder destroys a folder(folderPath could be either relative path of vmfolder of datacenter or full path).

func (*Client) EnsureVMFolder

func (c *Client) EnsureVMFolder(ctx context.Context, parentFolderName string, relativeFolderPath string) (*object.Folder, error)

EnsureVMFolder creates the a VM folder with the given path if it doesn't already exist. Two string arguments needed: relativeFolderPath will be split on "/" whereas parentFolderName is the subfolder in DC's root-folder. The parentFolderName will fallback to DC's root-folder if it's an empty string.

func (*Client) FindFolder

func (c *Client) FindFolder(ctx context.Context, folderPath string) (vmFolder *object.Folder, err error)

FindFolder should be able to search for both entire filepaths or relative (.) filepaths. If the user passes "test" or "/<DC>/vm/test" as folder, it should return the pointer for the same folder, and should also deal with the case where folderPath is nil or empty.

func (*Client) Folders

func (c *Client) Folders(ctx context.Context) (*object.DatacenterFolders, error)

Folders returns the datacenter's folders object.

func (*Client) GetTargetDatastore

func (c *Client) GetTargetDatastore(
	ctx context.Context,
	computeResource *mo.ComputeResource,
	rootDiskSource string,
) (*object.Datastore, error)

GetTargetDatastore returns the proper datastore for a compute resource. given a root disk constraint.

func (*Client) ListVMTemplates

func (c *Client) ListVMTemplates(ctx context.Context, path string) ([]*object.VirtualMachine, error)

ListVMTemplates returns a list of virtual machine objects in the given path, that have been marked as templates.

func (*Client) MoveVMFolderInto

func (c *Client) MoveVMFolderInto(ctx context.Context, parentPath, childPath string) error

MoveVMFolderInto moves one VM folder into another.

func (*Client) MoveVMsInto

func (c *Client) MoveVMsInto(
	ctx context.Context,
	folderPath string,
	vms ...types.ManagedObjectReference,
) error

MoveVMsInto moves a set of VMs into a folder.

func (*Client) RemoveVirtualMachines

func (c *Client) RemoveVirtualMachines(ctx context.Context, path string) error

RemoveVirtualMachines removes VMs matching the given path from the system. The path may include wildcards, to match multiple VMs.

func (*Client) ResourcePools

func (c *Client) ResourcePools(ctx context.Context, path string) ([]*object.ResourcePool, error)

ResourcePools returns a list of all of the resource pools (possibly nested) under the given path.

func (*Client) UpdateVirtualMachineExtraConfig

func (c *Client) UpdateVirtualMachineExtraConfig(
	ctx context.Context,
	vmInfo *mo.VirtualMachine,
	metadata map[string]string,
) error

UpdateVirtualMachineExtraConfig updates the "ExtraConfig" attributes of the specified virtual machine. Keys with empty values will be removed from the config; existing keys that are unspecified in the map will be untouched.

func (*Client) UserHasRootLevelPrivilege

func (c *Client) UserHasRootLevelPrivilege(ctx context.Context, privilege string) (bool, error)

UserHasRootLevelPrivilege returns whether the connected user has the specified privilege on the root-level object.

func (*Client) VirtualMachineObjectToManagedObject

func (c *Client) VirtualMachineObjectToManagedObject(ctx context.Context, vmObject *object.VirtualMachine) (mo.VirtualMachine, error)

VirtualMachineObjectToManagedObject returns a virtual machine managed object, given an *object.VirtualMachine

func (*Client) VirtualMachines

func (c *Client) VirtualMachines(ctx context.Context, path string) ([]*mo.VirtualMachine, error)

VirtualMachines return list of all VMs in the system matching the given path.

type ComputeResource

type ComputeResource struct {
	Resource *mo.ComputeResource
	Path     string
}

ComputeResource stores an mo.ComputeResource along with its full path

type CreateVirtualMachineParams

type CreateVirtualMachineParams struct {
	StatusUpdateParams StatusUpdateParams

	// Name is the name to give the virtual machine. The VM name is used
	// for its hostname also.
	Name string

	// Folder is the path of the VM folder, relative to the root VM folder,
	// in which to create the VM.
	Folder string

	// Series is the name of the OS series that the image will run.
	Series string

	// UserData is the cloud-init user-data.
	UserData string

	// ComputeResource is the compute resource (host or cluster) to be used
	// to create the VM.
	ComputeResource *mo.ComputeResource

	// ForceVMHardwareVersion if set, will attempt to upgrade the VM to the
	// specified hardware version. If not supported by the deployment of VSphere,
	// this option will be ignored.
	ForceVMHardwareVersion int64

	// ResourcePool is a reference to the pool the VM should be
	// created in.
	ResourcePool types.ManagedObjectReference

	// Metadata are metadata key/value pairs to apply to the VM as
	// "extra config".
	Metadata map[string]string

	// Constraints contains the resource constraints for the virtual machine.
	Constraints constraints.Value

	// Networks contain a list of network devices the VM should have.
	NetworkDevices []NetworkDevice

	// EnableDiskUUID controls whether the VMware disk should expose a
	// consistent UUID to the guest OS.
	EnableDiskUUID bool

	// DiskProvisioningType specifies how disks should be provisioned when
	// cloning a template.
	DiskProvisioningType DiskProvisioningType

	Datastore *object.Datastore

	VMTemplate *object.VirtualMachine
}

CreateVirtualMachineParams contains the parameters required for creating a new virtual machine.

type DiskProvisioningType

type DiskProvisioningType string
const (
	// DiskTypeThin sets the provisioning type for disks, when cloning
	// from a template, to "thin". This is also known as "sparse"
	// provisioning. Only written blocks inside the virtual disk are
	// deducted from the underlying datastore.
	DiskTypeThin DiskProvisioningType = "thin"
	// DiskTypeThickLazyZero sets the provisioning type for disks to
	// "Thick Provision Lazy Zeroed". The entire size of the virtual disk,
	// written and unwritten space, is deducted from the underlying datastore.
	// Unwritten blocks inside the virtual disk, are not zeroed out. This
	// speeds up disk cloning but introduces two pitfals:
	//   * If there is previously written data on the datastore where the
	//     space is allocated, it can be recovered in the newly instantiated
	//     machine that now ocupies that space. This can be a potential
	//     security risk.
	//   * Before new data will be written to a disk area, the hypervisor
	//     will first zero out the disk area before writing to it. This adds
	//     latency to disk writes in that area.
	DiskTypeThickLazyZero DiskProvisioningType = "thick-lazy-zero"
	// DiskTypeThick sets the provisioning type for disks to
	// "Thick Provision Eagerly Zeroed". The entire size of the virtual
	// disk will be deducted from the underlying datastore. Any unwritten
	// disk areas will be zeroed out during cloning. This is the default
	// disk provisioning type.
	DiskTypeThick DiskProvisioningType = "thick"

	// ArchTag is the CPU architecture tag that gets added to VM templates
	// when imported from the image-download simplestream entries.
	ArchTag = "arch"
)

type ImportOVAParameters

type ImportOVAParameters struct {
	StatusUpdateParams StatusUpdateParams

	// ReadOVA returns the location of, and an io.ReadCloser for,
	// the OVA from which to extract the VMDK. The location may be
	// used for reporting progress. The ReadCloser must be closed
	// by the caller when it is finished with it.
	ReadOVA func() (location string, _ io.ReadCloser, _ error)

	// OVASHA256 is the expected SHA-256 hash of the OVA.
	OVASHA256 string

	// ResourcePool is a reference to the pool the VM should be
	// created in.
	ResourcePool types.ManagedObjectReference

	// TemplateName is the name of the template that gets created
	// from the OVA
	TemplateName string

	Datastore         *object.Datastore
	DestinationFolder *object.Folder
	Arch              string
	Series            string
}

ImportOVAParameters contains the parameters needed to import a VM template from simplestreams.

type NetworkDevice

type NetworkDevice struct {
	// Network is the name of the network the device should be connected to.
	// If empty it will be connected to the default "VM Network" network.
	Network string
	// MAC is the hardware address of the network device.
	MAC string
}

NetworkDevice defines a single network device attached to a newly created VM.

type StatusUpdateParams

type StatusUpdateParams struct {
	// UpdateProgress is a function that should be called before/during
	// long-running operations to provide a progress reporting.
	UpdateProgress func(string)

	// UpdateProgressInterval is the amount of time to wait between calls
	// to UpdateProgress. This should be lower when the operation is
	// interactive (bootstrap), and higher when non-interactive.
	UpdateProgressInterval time.Duration

	// Clock is used for controlling the timing of progress updates.
	Clock clock.Clock
}

StatusUpdateParams contains parameters commonly used to send status updates.

Jump to

Keyboard shortcuts

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