diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 15d4ccaf1..2752e0071 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -58,6 +58,10 @@ type cdrom struct { path string } +type memoryAllocation struct { + reservation int64 +} + type virtualMachine struct { name string folder string @@ -67,6 +71,7 @@ type virtualMachine struct { datastore string vcpu int memoryMb int64 + memoryAllocation memoryAllocation template string networkInterfaces []networkInterface hardDisks []hardDisk @@ -124,6 +129,13 @@ func resourceVSphereVirtualMachine() *schema.Resource { ForceNew: true, }, + "memory_reservation": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 0, + ForceNew: true, + }, + "datacenter": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -371,6 +383,9 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ name: d.Get("name").(string), vcpu: d.Get("vcpu").(int), memoryMb: int64(d.Get("memory").(int)), + memoryAllocation: memoryAllocation{ + reservation: int64(d.Get("memory_reservation").(int)), + }, } if v, ok := d.GetOk("folder"); ok { @@ -672,6 +687,7 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("datacenter", dc) d.Set("memory", mvm.Summary.Config.MemorySizeMB) + d.Set("memory_reservation", mvm.Summary.Config.MemoryReservation) d.Set("cpu", mvm.Summary.Config.NumCpu) d.Set("datastore", rootDatastore) @@ -1104,7 +1120,10 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { NumCPUs: vm.vcpu, NumCoresPerSocket: 1, MemoryMB: vm.memoryMb, - DeviceChange: networkDevices, + MemoryAllocation: &types.ResourceAllocationInfo{ + Reservation: vm.memoryAllocation.reservation, + }, + DeviceChange: networkDevices, } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 264884b39..d98876fd6 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -61,6 +61,8 @@ func TestAccVSphereVirtualMachine_basic(t *testing.T) { "vsphere_virtual_machine.foo", "vcpu", "2"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.foo", "memory", "4096"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.foo", "memory_reservation", "4096"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.foo", "disk.#", "2"), resource.TestCheckResourceAttr( @@ -632,6 +634,7 @@ resource "vsphere_virtual_machine" "foo" { %s vcpu = 2 memory = 4096 + memory_reservation = 4096 gateway = "%s" network_interface { label = "%s" diff --git a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown index 87965428d..f1f0b3ba8 100644 --- a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown +++ b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown @@ -64,6 +64,7 @@ The following arguments are supported: * `name` - (Required) The virtual machine name * `vcpu` - (Required) The number of virtual CPUs to allocate to the virtual machine * `memory` - (Required) The amount of RAM (in MB) to allocate to the virtual machine +* `memory_reservation` - (Optional) The amount of RAM (in MB) to reserve physical memory resource; defaults to 0 (means not to reserve) * `datacenter` - (Optional) The name of a Datacenter in which to launch the virtual machine * `cluster` - (Optional) Name of a Cluster in which to launch the virtual machine * `resource_pool` (Optional) The name of a Resource Pool in which to launch the virtual machine. Requires full path (see cluster example).