Skip to content

Commit

Permalink
vcsim: add support for cloning HostSystems
Browse files Browse the repository at this point in the history
The simulator.ClusterComputeResource.AddHost method uses the compiled in esx.HostSystem template by default,
as does simulator.AddStandaloneHost.
With this change, if the HostConnectSpec.UserName is that of a HostSystem.Name in the given Cluster or
of a standalone host, use that HostSystem as the template.
  • Loading branch information
dougm committed Dec 10, 2021
1 parent afc67f5 commit 3214d97
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
10 changes: 10 additions & 0 deletions govc/test/vcsim.bats
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ EOF
objs=$(govc find / | wc -l)
assert_equal 23 "$objs"

run govc cluster.add -cluster DC0_C0 -hostname DC0_C0_H0-clone -username DC0_C0_H0 -password pass -noverify
assert_success
objs=$(govc find / | wc -l)
assert_equal 24 "$objs"

run govc host.add -hostname DC0_H0-clone -username DC0_H0 -password pass -noverify
assert_success
objs=$(govc find / | wc -l)
assert_equal 27 "$objs" # ComputeResource + ResourcePool + HostSystem

run govc host.portgroup.add -host DC0_H0 -vswitch vSwitch0 bridge
assert_success # issue #2016
}
Expand Down
15 changes: 12 additions & 3 deletions simulator/cluster_compute_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,25 @@ func (add *addHost) Run(task *Task) (types.AnyType, types.BaseMethodFault) {
return nil, &types.NoHost{}
}

host := NewHostSystem(esx.HostSystem)
cr := add.ClusterComputeResource
template := esx.HostSystem

if h := task.ctx.Map.FindByName(spec.UserName, cr.Host); h != nil {
// "clone" an existing host from the inventory
template = h.(*HostSystem).HostSystem
template.Vm = nil
} else {
template.Network = cr.Network[:1] // VM Network
}

host := NewHostSystem(template)
host.configure(spec, add.req.AsConnected)

cr := add.ClusterComputeResource
task.ctx.Map.PutEntity(cr, task.ctx.Map.NewEntity(host))
host.Summary.Host = &host.Self

cr.Host = append(cr.Host, host.Reference())
addComputeResource(cr.Summary.GetComputeResourceSummary(), host)
host.Network = cr.Network[:1] // VM Network

return host.Reference(), nil
}
Expand Down
24 changes: 20 additions & 4 deletions simulator/host_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ func NewHostSystem(host mo.HostSystem) *HostSystem {
hs.Summary.Runtime = &hs.Runtime
hs.Summary.Runtime.BootTime = &now

// shallow copy Summary.Hardware, as each host will be assigned its own .Uuid
hardware := *host.Summary.Hardware
hs.Summary.Hardware = &hardware

info := *esx.HostHardwareInfo
hs.Hardware = &info
if hs.Hardware == nil {
// shallow copy Hardware, as each host will be assigned its own .Uuid
info := *esx.HostHardwareInfo
hs.Hardware = &info
}

cfg := new(types.HostConfigInfo)
deepCopy(hs.Config, cfg)
Expand Down Expand Up @@ -189,8 +193,20 @@ func CreateStandaloneHost(ctx *Context, f *Folder, spec types.HostConnectSpec) (
return nil, &types.NoHost{}
}

template := esx.HostSystem
network := ctx.Map.getEntityDatacenter(f).defaultNetwork()

if p := ctx.Map.FindByName(spec.UserName, f.ChildEntity); p != nil {
cr := p.(*mo.ComputeResource)
h := ctx.Map.Get(cr.Host[0])
// "clone" an existing host from the inventory
template = h.(*HostSystem).HostSystem
template.Vm = nil
network = cr.Network
}

pool := NewResourcePool()
host := NewHostSystem(esx.HostSystem)
host := NewHostSystem(template)
host.configure(spec, false)

summary := new(types.ComputeResourceSummary)
Expand All @@ -210,7 +226,7 @@ func CreateStandaloneHost(ctx *Context, f *Folder, spec types.HostConnectSpec) (
ctx.Map.PutEntity(cr, ctx.Map.NewEntity(pool))

cr.Name = host.Name
cr.Network = ctx.Map.getEntityDatacenter(f).defaultNetwork()
cr.Network = network
cr.Host = append(cr.Host, host.Reference())
cr.ResourcePool = &pool.Self

Expand Down

0 comments on commit 3214d97

Please sign in to comment.