@@ -776,6 +776,8 @@ func (f *Folder) PlaceVmsXCluster(ctx *Context, req *types.PlaceVmsXCluster) soa
776
776
}
777
777
778
778
body .Res = new (types.PlaceVmsXClusterResponse )
779
+ hostRequired := req .PlacementSpec .HostRecommRequired != nil && * req .PlacementSpec .HostRecommRequired
780
+ datastoreRequired := req .PlacementSpec .DatastoreRecommRequired != nil && * req .PlacementSpec .DatastoreRecommRequired
779
781
780
782
for _ , spec := range specs {
781
783
pool := ctx .Map .Get (pools [rand .Intn (len (pools ))]).(* ResourcePool )
@@ -793,6 +795,8 @@ func (f *Folder) PlaceVmsXCluster(ctx *Context, req *types.PlaceVmsXCluster) soa
793
795
}
794
796
body .Res .Returnval .Faults = append (body .Res .Returnval .Faults , faults )
795
797
} else {
798
+ var configSpec * types.VirtualMachineConfigSpec
799
+
796
800
res := types.ClusterRecommendation {
797
801
Key : "1" ,
798
802
Type : "V1" ,
@@ -804,13 +808,59 @@ func (f *Folder) PlaceVmsXCluster(ctx *Context, req *types.PlaceVmsXCluster) soa
804
808
}
805
809
806
810
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
+ }
809
859
}
810
860
811
861
res .Action = append (res .Action , & types.ClusterClusterInitialPlacementAction {
812
862
ClusterInitialPlacementAction : placementAction ,
813
- ConfigSpec : & spec . ConfigSpec ,
863
+ ConfigSpec : configSpec ,
814
864
})
815
865
816
866
body .Res .Returnval .PlacementInfos = append (body .Res .Returnval .PlacementInfos ,
0 commit comments