Skip to content

Commit

Permalink
provider/vsphere: Add support for memory reservation
Browse files Browse the repository at this point in the history
  • Loading branch information
furukawataka02 committed Apr 25, 2016
1 parent d9ae2fd commit cba1567
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
21 changes: 20 additions & 1 deletion builtin/providers/vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ type cdrom struct {
path string
}

type memoryAllocation struct {
reservation int64
}

type virtualMachine struct {
name string
folder string
Expand All @@ -67,6 +71,7 @@ type virtualMachine struct {
datastore string
vcpu int
memoryMb int64
memoryAllocation memoryAllocation
template string
networkInterfaces []networkInterface
hardDisks []hardDisk
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -632,6 +634,7 @@ resource "vsphere_virtual_machine" "foo" {
%s
vcpu = 2
memory = 4096
memory_reservation = 4096
gateway = "%s"
network_interface {
label = "%s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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
Expand Down

0 comments on commit cba1567

Please sign in to comment.