Skip to content

fix: add memory_reservation_locked_to_max property to r/vsphere_virtual_macine schema for sr-iov #2093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}