Skip to content

Commit

Permalink
[feat sr-iov] Adding memory_reservation_locked_to_max property to (#2093
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vasilsatanasov authored Jan 8, 2024
1 parent d87bea8 commit 9aac934
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions vsphere/data_source_vsphere_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestAccDataSourceVSphereVirtualMachine_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.vsphere_virtual_machine.template", "guest_id"),
resource.TestCheckResourceAttrSet("data.vsphere_virtual_machine.template", "scsi_type"),
resource.TestCheckResourceAttrSet("data.vsphere_virtual_machine.template", "memory"),
resource.TestCheckResourceAttrSet("data.vsphere_virtual_machine.template", "memory_reservation_locked_to_max"),
resource.TestCheckResourceAttrSet("data.vsphere_virtual_machine.template", "num_cpus"),
resource.TestCheckResourceAttrSet("data.vsphere_virtual_machine.template", "num_cores_per_socket"),
resource.TestCheckResourceAttrSet("data.vsphere_virtual_machine.template", "firmware"),
Expand Down
54 changes: 54 additions & 0 deletions vsphere/resource_vsphere_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,27 @@ func TestAccResourceVSphereVirtualMachine_SRIOV(t *testing.T) {
})
}

func TestAccResourceVSphereVirtualMachine_createMemoryReservationLockedToMax(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
RunSweepers()
testAccPreCheck(t)
testAccResourceVSphereVirtualMachinePreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereVirtualMachineCheckExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereVirtualMachineCreateMemoryLockToMax(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereVirtualMachineCheckExists(true),
resource.TestCheckResourceAttr("vsphere_virtual_machine.vm", "memory_reservation_locked_to_max", "true"),
),
},
},
})
}

func testAccResourceVSphereVirtualMachinePreCheck(t *testing.T) {
// Note that TF_VAR_VSPHERE_USE_LINKED_CLONE is also a variable and its presence
// speeds up tests greatly, but it's not a necessary variable, so we don't
Expand Down Expand Up @@ -7383,6 +7404,39 @@ resource "vsphere_virtual_machine" "vm" {
)
}

func testAccResourceVSphereVirtualMachineCreateMemoryLockToMax() string {
return fmt.Sprintf(`
%s // Mix and match config
resource "vsphere_virtual_machine" "vm" {
name = "testacc-test"
resource_pool_id = data.vsphere_host.roothost1.resource_pool_id
datastore_id = data.vsphere_datastore.rootds1.id
host_system_id = data.vsphere_host.roothost1.id
num_cpus = 2
memory = 2048
memory_reservation = 2048
memory_reservation_locked_to_max = true
guest_id = "other3xLinuxGuest"
wait_for_guest_net_timeout = 0
network_interface {
network_id = "${data.vsphere_network.network1.id}"
}
disk {
label = "disk0"
size = 20
}
}
`,

testAccResourceVSphereVirtualMachineConfigBase(),
)
}

// Tests to skip until new features are developed.

// Needs storage policy resource
Expand Down
24 changes: 23 additions & 1 deletion vsphere/virtual_machine_config_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ func schemaVirtualMachineConfigSpec() map[string]*schema.Schema {
Default: 1024,
Description: "The size of the virtual machine's memory, in MB.",
},
"memory_reservation_locked_to_max": {
Type: schema.TypeBool,
Optional: true,
Description: "If set true, memory resource reservation for this virtual machine will always be equal to the virtual machine's memory size;" +
"increases in memory size will be rejected when a corresponding reservation increase is not possible." +
" This feature may only be enabled if it is currently possible to reserve all of the virtual machine's memory.",
},
"memory_hot_add_enabled": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -957,6 +964,12 @@ func flattenVirtualMachineConfigInfo(d *schema.ResourceData, obj *types.VirtualM
_ = d.Set("num_cores_per_socket", obj.Hardware.NumCoresPerSocket)
_ = d.Set("memory", obj.Hardware.MemoryMB)
_ = d.Set("memory_hot_add_enabled", obj.MemoryHotAddEnabled)

memoryReservationLockedToMax := false
if obj.MemoryReservationLockedToMax != nil {
memoryReservationLockedToMax = *obj.MemoryReservationLockedToMax
}
_ = d.Set("memory_reservation_locked_to_max", memoryReservationLockedToMax)
_ = d.Set("cpu_hot_add_enabled", obj.CpuHotAddEnabled)
_ = d.Set("cpu_hot_remove_enabled", obj.CpuHotRemoveEnabled)
_ = d.Set("swap_placement_policy", obj.SwapPlacement)
Expand Down Expand Up @@ -1044,8 +1057,17 @@ func expandVirtualMachineConfigSpecChanged(d *schema.ResourceData, client *govmo
// cloning from a template that has it enabled. The solution is to set it to
// false when needed, but leave it alone when the change is not necessary.
func getMemoryReservationLockedToMax(d *schema.ResourceData) *bool {
if d.Get("memory_reservation").(int) != d.Get("memory").(int) {
memory := d.Get("memory").(int)
memoryReservation := d.Get("memory_reservation").(int)
memoryLockMax := d.Get("memory_reservation_locked_to_max").(bool)

if memory != memoryReservation {
return structure.BoolPtr(false)
}

if memory == memoryReservation && memoryLockMax == true {
return structure.BoolPtr(true)
}

return nil
}

0 comments on commit 9aac934

Please sign in to comment.