Skip to content

Commit 3b2816a

Browse files
committed
Add optional recommRequired PlaceVmsXCluster req arguments
PlaceVmsXCluster request has two optional bool fields as to whether host and/or datastore selection is required as a part of the placement response. Update simulator PlaceVmsXCluster response to include the ConfigSpec only when the datastore selection is required to match real VC behavior.
1 parent 5a40bd9 commit 3b2816a

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

simulator/folder.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,8 @@ func (f *Folder) PlaceVmsXCluster(ctx *Context, req *types.PlaceVmsXCluster) soa
776776
}
777777

778778
body.Res = new(types.PlaceVmsXClusterResponse)
779+
hostRequired := req.PlacementSpec.HostRecommRequired != nil && *req.PlacementSpec.HostRecommRequired
780+
datastoreRequired := req.PlacementSpec.DatastoreRecommRequired != nil && *req.PlacementSpec.DatastoreRecommRequired
779781

780782
for _, spec := range specs {
781783
pool := ctx.Map.Get(pools[rand.Intn(len(pools))]).(*ResourcePool)
@@ -793,6 +795,8 @@ func (f *Folder) PlaceVmsXCluster(ctx *Context, req *types.PlaceVmsXCluster) soa
793795
}
794796
body.Res.Returnval.Faults = append(body.Res.Returnval.Faults, faults)
795797
} else {
798+
var configSpec *types.VirtualMachineConfigSpec
799+
796800
res := types.ClusterRecommendation{
797801
Key: "1",
798802
Type: "V1",
@@ -804,13 +808,59 @@ func (f *Folder) PlaceVmsXCluster(ctx *Context, req *types.PlaceVmsXCluster) soa
804808
}
805809

806810
placementAction := types.ClusterInitialPlacementAction{
807-
TargetHost: cluster.Host[rand.Intn(len(cluster.Host))],
808-
Pool: &pool.Self,
811+
Pool: &pool.Self,
812+
}
813+
814+
if hostRequired {
815+
placementAction.TargetHost = cluster.Host[rand.Intn(len(cluster.Host))]
816+
}
817+
818+
if datastoreRequired {
819+
configSpec = &spec.ConfigSpec
820+
821+
// TODO: This is just an initial implementation aimed at returning some data but it is not
822+
// necessarily fully consistent, like we should ensure the host, if also required, has the
823+
// datastore mounted.
824+
ds := ctx.Map.Get(cluster.Datastore[rand.Intn(len(cluster.Datastore))]).(*Datastore)
825+
826+
if configSpec.Files == nil {
827+
configSpec.Files = new(types.VirtualMachineFileInfo)
828+
}
829+
configSpec.Files.VmPathName = fmt.Sprintf("[%[1]s] %[2]s/%[2]s.vmx", ds.Name, spec.ConfigSpec.Name)
830+
831+
for _, change := range configSpec.DeviceChange {
832+
dspec := change.GetVirtualDeviceConfigSpec()
833+
834+
if dspec.FileOperation != types.VirtualDeviceConfigSpecFileOperationCreate {
835+
continue
836+
}
837+
838+
switch dspec.Operation {
839+
case types.VirtualDeviceConfigSpecOperationAdd:
840+
device := dspec.Device
841+
d := device.GetVirtualDevice()
842+
843+
switch device.(type) {
844+
case *types.VirtualDisk:
845+
switch b := d.Backing.(type) {
846+
case types.BaseVirtualDeviceFileBackingInfo:
847+
info := b.GetVirtualDeviceFileBackingInfo()
848+
info.Datastore = types.NewReference(ds.Reference())
849+
850+
var dsPath object.DatastorePath
851+
if dsPath.FromString(info.FileName) {
852+
dsPath.Datastore = ds.Name
853+
info.FileName = dsPath.String()
854+
}
855+
}
856+
}
857+
}
858+
}
809859
}
810860

811861
res.Action = append(res.Action, &types.ClusterClusterInitialPlacementAction{
812862
ClusterInitialPlacementAction: placementAction,
813-
ConfigSpec: &spec.ConfigSpec,
863+
ConfigSpec: configSpec,
814864
})
815865

816866
body.Res.Returnval.PlacementInfos = append(body.Res.Returnval.PlacementInfos,

vim25/types/unreleased.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ func init() {
9898
type PlaceVmsXClusterSpec struct {
9999
DynamicData
100100

101-
ResourcePools []ManagedObjectReference `xml:"resourcePools,omitempty"`
102-
VmPlacementSpecs []PlaceVmsXClusterSpecVmPlacementSpec `xml:"vmPlacementSpecs,omitempty"`
101+
ResourcePools []ManagedObjectReference `xml:"resourcePools,omitempty"`
102+
VmPlacementSpecs []PlaceVmsXClusterSpecVmPlacementSpec `xml:"vmPlacementSpecs,omitempty"`
103+
HostRecommRequired *bool `xml:"hostRecommRequired"`
104+
DatastoreRecommRequired *bool `xml:"datastoreRecommRequired"`
103105
}
104106

105107
func init() {

0 commit comments

Comments
 (0)