From ada2a431efebc95dfa45dd89177fe3bd0dcda72e Mon Sep 17 00:00:00 2001 From: Sylvia Moss Date: Thu, 20 Aug 2020 17:55:25 +0200 Subject: [PATCH] Modularise vCenter rest client (#9793) --- builder/vsphere/driver/driver.go | 34 ++++++++++++++++++++++--------- builder/vsphere/driver/library.go | 4 ++-- builder/vsphere/driver/vm.go | 26 +++++++++++++++++++---- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/builder/vsphere/driver/driver.go b/builder/vsphere/driver/driver.go index 3ea367266..57a7b87f5 100644 --- a/builder/vsphere/driver/driver.go +++ b/builder/vsphere/driver/driver.go @@ -19,7 +19,7 @@ type Driver struct { // context that controls the authenticated sessions used to run the VM commands ctx context.Context client *govmomi.Client - restClient *rest.Client + restClient *RestClient finder *find.Finder datacenter *object.Datacenter } @@ -59,12 +59,6 @@ func NewDriver(config *ConnectConfig) (*Driver, error) { return nil, err } - restClient := rest.NewClient(vimClient) - err = restClient.Login(ctx, credentials) - if err != nil { - return nil, err - } - finder := find.NewFinder(client.Client, false) datacenter, err := finder.DatacenterOrDefault(ctx, config.Datacenter) if err != nil { @@ -73,11 +67,31 @@ func NewDriver(config *ConnectConfig) (*Driver, error) { finder.SetDatacenter(datacenter) d := Driver{ - ctx: ctx, - client: client, - restClient: restClient, + ctx: ctx, + client: client, + restClient: &RestClient{ + client: rest.NewClient(vimClient), + credentials: credentials, + }, datacenter: datacenter, finder: finder, } return &d, nil } + +// The rest.Client requires vCenter. +// RestClient is to modularize the rest.Client session and use it only when is necessary. +// This will allow users without vCenter to use the other features that doesn't use the rest.Client. +// To use the client login/logout must be done to create an authenticated session. +type RestClient struct { + client *rest.Client + credentials *url.Userinfo +} + +func (r *RestClient) Login(ctx context.Context) error { + return r.client.Login(ctx, r.credentials) +} + +func (r *RestClient) Logout(ctx context.Context) error { + return r.client.Logout(ctx) +} diff --git a/builder/vsphere/driver/library.go b/builder/vsphere/driver/library.go index 8bb965073..c25646947 100644 --- a/builder/vsphere/driver/library.go +++ b/builder/vsphere/driver/library.go @@ -10,7 +10,7 @@ type Library struct { } func (d *Driver) FindContentLibrary(name string) (*Library, error) { - lm := library.NewManager(d.restClient) + lm := library.NewManager(d.restClient.client) l, err := lm.GetLibraryByName(d.ctx, name) if err != nil { return nil, err @@ -22,7 +22,7 @@ func (d *Driver) FindContentLibrary(name string) (*Library, error) { } func (d *Driver) FindContentLibraryItem(libraryId string, name string) (*library.Item, error) { - lm := library.NewManager(d.restClient) + lm := library.NewManager(d.restClient.client) items, err := lm.GetLibraryItems(d.ctx, libraryId) if err != nil { return nil, err diff --git a/builder/vsphere/driver/vm.go b/builder/vsphere/driver/vm.go index 1b0b819b3..250ac5573 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -684,6 +684,11 @@ func (vm *VirtualMachine) ConvertToTemplate() error { } func (vm *VirtualMachine) ImportOvfToContentLibrary(ovf vcenter.OVF) error { + err := vm.driver.restClient.Login(vm.driver.ctx) + if err != nil { + return err + } + l, err := vm.driver.FindContentLibrary(ovf.Target.LibraryID) if err != nil { return err @@ -706,12 +711,21 @@ func (vm *VirtualMachine) ImportOvfToContentLibrary(ovf vcenter.OVF) error { ovf.Source.Value = vm.vm.Reference().Value ovf.Source.Type = "VirtualMachine" - vcm := vcenter.NewManager(vm.driver.restClient) + vcm := vcenter.NewManager(vm.driver.restClient.client) _, err = vcm.CreateOVF(vm.driver.ctx, ovf) - return err + if err != nil { + return err + } + + return vm.driver.restClient.Logout(vm.driver.ctx) } func (vm *VirtualMachine) ImportToContentLibrary(template vcenter.Template) error { + err := vm.driver.restClient.Login(vm.driver.ctx) + if err != nil { + return err + } + l, err := vm.driver.FindContentLibrary(template.Library) if err != nil { return err @@ -761,9 +775,13 @@ func (vm *VirtualMachine) ImportToContentLibrary(template vcenter.Template) erro template.VMHomeStorage.Datastore = d.ds.Reference().Value } - vcm := vcenter.NewManager(vm.driver.restClient) + vcm := vcenter.NewManager(vm.driver.restClient.client) _, err = vcm.CreateTemplate(vm.driver.ctx, template) - return err + if err != nil { + return err + } + + return vm.driver.restClient.Logout(vm.driver.ctx) } func (vm *VirtualMachine) GetDir() (string, error) {