Modularise vCenter rest client (#9793)
This commit is contained in:
parent
99800619b7
commit
ada2a431ef
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue