Skip to content

Commit

Permalink
vcsim: add content library VmConfigSpec support
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed May 13, 2022
1 parent ca7ee51 commit 3325da0
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion vapi/simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package simulator

import (
"archive/tar"
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -53,6 +55,7 @@ import (
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
vim "github.com/vmware/govmomi/vim25/types"
"github.com/vmware/govmomi/vim25/xml"
)

type item struct {
Expand Down Expand Up @@ -1675,7 +1678,34 @@ func (i *item) ovf() string {
return ""
}

func vmConfigSpec(ctx context.Context, c *vim25.Client, deploy vcenter.Deploy) (*types.VirtualMachineConfigSpec, error) {
if deploy.VmConfigSpec == nil {
return nil, nil
}

b, err := base64.StdEncoding.DecodeString(deploy.VmConfigSpec.XML)
if err != nil {
return nil, err
}

var spec *types.VirtualMachineConfigSpec

dec := xml.NewDecoder(bytes.NewReader(b))
dec.TypeFunc = c.Types
err = dec.Decode(&spec)
if err != nil {
return nil, err
}

return spec, nil
}

func (s *handler) libraryDeploy(ctx context.Context, c *vim25.Client, lib *library.Library, item *item, deploy vcenter.Deploy) (*nfc.LeaseInfo, error) {
config, err := vmConfigSpec(ctx, c, deploy)
if err != nil {
return nil, err
}

name := item.ovf()
desc, err := ioutil.ReadFile(filepath.Join(libraryPath(lib, item.ID), name))
if err != nil {
Expand Down Expand Up @@ -1769,7 +1799,17 @@ func (s *handler) libraryDeploy(ctx context.Context, c *vim25.Client, lib *libra
return nil, err
}

return info, lease.Complete(ctx)
if err = lease.Complete(ctx); err != nil {
return nil, err
}

if config != nil {
if err = s.reconfigVM(info.Entity, *config); err != nil {
return nil, err
}
}

return info, nil
}

func (s *handler) libraryItemOVF(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -1898,6 +1938,17 @@ func (s *handler) deleteVM(ref *types.ManagedObjectReference) {
})
}

func (s *handler) reconfigVM(ref types.ManagedObjectReference, config types.VirtualMachineConfigSpec) error {
return s.withClient(func(ctx context.Context, c *vim25.Client) error {
vm := object.NewVirtualMachine(c, ref)
task, err := vm.Reconfigure(ctx, config)
if err != nil {
return err
}
return task.Wait(ctx)
})
}

func (s *handler) cloneVM(source string, name string, p *library.Placement, storage *vcenter.DiskStorage) (*types.ManagedObjectReference, error) {
var folder, pool, host, ds *types.ManagedObjectReference
if p.Folder != "" {
Expand Down

0 comments on commit 3325da0

Please sign in to comment.