github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/vsphere/client.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package vsphere
     5  
     6  import (
     7  	"context"
     8  	"net/url"
     9  
    10  	"github.com/vmware/govmomi/object"
    11  	"github.com/vmware/govmomi/vim25/mo"
    12  	"github.com/vmware/govmomi/vim25/types"
    13  
    14  	environscloudspec "github.com/juju/juju/environs/cloudspec"
    15  	"github.com/juju/juju/provider/vsphere/internal/vsphereclient"
    16  )
    17  
    18  // DialFunc is a function type for dialing vSphere client connections.
    19  type DialFunc func(_ context.Context, _ *url.URL, datacenter string) (Client, error)
    20  
    21  // Client is an interface for interacting with the vSphere API.
    22  type Client interface {
    23  	Close(context.Context) error
    24  	ComputeResources(context.Context) ([]vsphereclient.ComputeResource, error)
    25  	CreateVirtualMachine(context.Context, vsphereclient.CreateVirtualMachineParams) (*mo.VirtualMachine, error)
    26  	CreateTemplateVM(ctx context.Context, ovaArgs vsphereclient.ImportOVAParameters) (vm *object.VirtualMachine, err error)
    27  	Folders(ctx context.Context) (*object.DatacenterFolders, error)
    28  	Datastores(context.Context) ([]mo.Datastore, error)
    29  	DeleteDatastoreFile(context.Context, string) error
    30  	DestroyVMFolder(context.Context, string) error
    31  	EnsureVMFolder(context.Context, string, string) (*object.Folder, error)
    32  	GetTargetDatastore(ctx context.Context, computeResource *mo.ComputeResource, rootDiskSource string) (*object.Datastore, error)
    33  	ListVMTemplates(ctx context.Context, path string) ([]*object.VirtualMachine, error)
    34  	MoveVMFolderInto(context.Context, string, string) error
    35  	MoveVMsInto(context.Context, string, ...types.ManagedObjectReference) error
    36  	RemoveVirtualMachines(context.Context, string) error
    37  	ResourcePools(context.Context, string) ([]*object.ResourcePool, error)
    38  	UpdateVirtualMachineExtraConfig(context.Context, *mo.VirtualMachine, map[string]string) error
    39  	VirtualMachines(context.Context, string) ([]*mo.VirtualMachine, error)
    40  	VirtualMachineObjectToManagedObject(ctx context.Context, vmObject *object.VirtualMachine) (mo.VirtualMachine, error)
    41  	UserHasRootLevelPrivilege(context.Context, string) (bool, error)
    42  	FindFolder(ctx context.Context, folderPath string) (vmFolder *object.Folder, err error)
    43  }
    44  
    45  //go:generate go run go.uber.org/mock/mockgen -package mocks -destination mocks/client_mock.go github.com/juju/juju/provider/vsphere Client
    46  func dialClient(
    47  	ctx context.Context,
    48  	cloudSpec environscloudspec.CloudSpec,
    49  	dial DialFunc,
    50  ) (Client, error) {
    51  	datacenter := cloudSpec.Region
    52  	credAttrs := cloudSpec.Credential.Attributes()
    53  	username := credAttrs[credAttrUser]
    54  	password := credAttrs[credAttrPassword]
    55  	connURL := &url.URL{
    56  		Scheme: "https",
    57  		User:   url.UserPassword(username, password),
    58  		Host:   cloudSpec.Endpoint,
    59  		Path:   "/sdk",
    60  	}
    61  	return dial(ctx, connURL, datacenter)
    62  }