Skip to content
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

Add new VM cpu and memory functions #432

Merged
merged 29 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d05517b
add main implementation
vbauzys Aug 25, 2020
3faee0a
git push origin master
vbauzys Aug 25, 2020
a497ca1
git push origin master
vbauzys Sep 3, 2020
71d524d
git push origin master
vbauzys Sep 22, 2020
879296d
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Oct 21, 2020
02a4d81
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Oct 21, 2020
b365d6c
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Oct 27, 2020
808752b
removed not needed after merge
vbauzys Oct 29, 2020
29c17ae
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Nov 5, 2020
9073403
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Nov 12, 2020
9efad14
Merge branch 'vmware:master' into master
vbauzys Oct 11, 2021
dabfd82
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Nov 8, 2021
be532a2
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Nov 10, 2021
6cf856a
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Nov 23, 2021
144ef18
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Nov 25, 2021
8c0217f
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Dec 8, 2021
05ac73e
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Dec 17, 2021
9056c05
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Dec 20, 2021
cb919b2
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Jan 5, 2022
253df2d
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Jan 13, 2022
a1bccce
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Jan 17, 2022
2fa708c
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Jan 17, 2022
5161390
Merge branch 'main' of github.com:vmware/go-vcloud-director into main
vbauzys Jan 19, 2022
7c0cf81
Merge branch 'main' of github.com:vmware/go-vcloud-director into main
vbauzys Jan 19, 2022
f550a29
add implementation for new ChangeMemory and ChangeCPU
vbauzys Jan 31, 2022
eb01707
update number of change
vbauzys Jan 31, 2022
be26c39
add improvement
vbauzys Feb 1, 2022
39fea67
add improvement for test
vbauzys Feb 1, 2022
85c759b
update changelog
vbauzys Feb 2, 2022
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
2 changes: 2 additions & 0 deletions .changes/v2.15.0/432-deprecations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Deprecated `vm.ChangeMemorySize` in favor of `vm.ChangeMemory` [GH-432]
* Deprecated `vm.ChangeCPUCount` and `vm.ChangeCPUCountWithCore` in favor of `vm.ChangeCPU` [GH-432]
1 change: 1 addition & 0 deletions .changes/v2.15.0/432-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add methods `vm.ChangeCPU` and `vm.ChangeMemory` [GH-432]
8 changes: 2 additions & 6 deletions govcd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,10 @@ func spawnVM(name string, memorySize int, vdc Vdc, vapp VApp, net types.NetworkC
fmt.Printf(". Done\n")

fmt.Printf("# Applying 2 vCPU and "+strconv.Itoa(memorySize)+"MB configuration for VM '%s'", name)
task, err = vm.ChangeCPUCount(2)
check.Assert(err, IsNil)
err = task.WaitTaskCompletion()
err = vm.ChangeCPU(2, 1)
check.Assert(err, IsNil)

task, err = vm.ChangeMemorySize(memorySize)
check.Assert(err, IsNil)
err = task.WaitTaskCompletion()
err = vm.ChangeMemory(int64(memorySize))
check.Assert(err, IsNil)
fmt.Printf(". Done\n")

Expand Down
40 changes: 40 additions & 0 deletions govcd/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (vm *VM) PowerOff() (Task, error) {
// (i.e. CPUs x cores per socket)
// Cpu cores count is inherited from template.
// https://communities.vmware.com/thread/576209
// Deprecated: use vm.ChangeCPU instead
func (vm *VM) ChangeCPUCount(virtualCpuCount int) (Task, error) {
return vm.ChangeCPUCountWithCore(virtualCpuCount, nil)
}
Expand All @@ -224,6 +225,7 @@ func (vm *VM) ChangeCPUCount(virtualCpuCount int) (Task, error) {
// (i.e. CPUs x cores per socket) and cores per socket.
// Socket count is a result of: virtual logical processors/cores per socket
// https://communities.vmware.com/thread/576209
// Deprecated: use vm.ChangeCPU instead
func (vm *VM) ChangeCPUCountWithCore(virtualCpuCount int, coresPerSocket *int) (Task, error) {

err := vm.Refresh()
Expand Down Expand Up @@ -357,6 +359,7 @@ func (vm *VM) ChangeNetworkConfig(networks []map[string]interface{}) (Task, erro
types.MimeNetworkConnectionSection, "error changing network config: %s", networkSection)
}

// Deprecated: use vm.ChangeMemory instead
func (vm *VM) ChangeMemorySize(size int) (Task, error) {

err := vm.Refresh()
Expand Down Expand Up @@ -1813,3 +1816,40 @@ func (vm *VM) getTenantContext() (*TenantContext, error) {
}
return parentVdc.getTenantContext()
}

// ChangeMemory sets memory value. Size is MB
func (vm *VM) ChangeMemory(sizeInMb int64) error {
vmSpecSection := vm.VM.VmSpecSection
description := vm.VM.Description
// update treats same values as changes and fails, with no values provided - no changes are made for that section
vmSpecSection.DiskSection = nil

vmSpecSection.MemoryResourceMb.Configured = sizeInMb

_, err := vm.UpdateVmSpecSection(vmSpecSection, description)
if err != nil {
return fmt.Errorf("error changing memory size: %s", err)
}
return nil
}

// ChangeCPUCount sets number of available virtual logical processors
// (i.e. CPUs x cores per socket)
// Cpu cores count is inherited from template.
// https://communities.vmware.com/thread/576209
func (vm *VM) ChangeCPU(cpus, cpuCores int) error {
vmSpecSection := vm.VM.VmSpecSection
description := vm.VM.Description
// update treats same values as changes and fails, with no values provided - no changes are made for that section
vmSpecSection.DiskSection = nil

vmSpecSection.NumCpus = takeIntAddress(cpus)
// has to come together
vmSpecSection.NumCoresPerSocket = takeIntAddress(cpuCores)

_, err := vm.UpdateVmSpecSection(vmSpecSection, description)
if err != nil {
return fmt.Errorf("error changing cpu size: %s", err)
}
return nil
}
63 changes: 63 additions & 0 deletions govcd/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1831,3 +1831,66 @@ func (vcd *TestVCD) Test_CreateStandaloneVMFromTemplate(check *C) {
vappList = vdc.GetVappList()
check.Assert(len(vappList), Equals, vappNum)
}

func (vcd *TestVCD) Test_VMChangeCPU(check *C) {
if vcd.skipVappTests {
check.Skip("Skipping test because vapp was not successfully created at setup")
}

vapp := vcd.findFirstVapp()
existingVm, vmName := vcd.findFirstVm(vapp)
if vmName == "" {
check.Skip("skipping test because no VM is found")
}

currentCpus := existingVm.VmSpecSection.NumCpus
currentCores := existingVm.VmSpecSection.NumCoresPerSocket

check.Assert(0, Not(Equals), currentCpus)
check.Assert(0, Not(Equals), currentCores)

vm, err := vcd.client.Client.GetVMByHref(existingVm.HREF)
check.Assert(err, IsNil)

cores := 2
cpuCount := 4

err = vm.ChangeCPU(cpuCount, cores)
check.Assert(err, IsNil)

check.Assert(*vm.VM.VmSpecSection.NumCpus, Equals, cpuCount)
check.Assert(*vm.VM.VmSpecSection.NumCoresPerSocket, Equals, cores)

// return to previous value
err = vm.ChangeCPU(*currentCpus, *currentCores)
check.Assert(err, IsNil)
}

func (vcd *TestVCD) Test_VMChangeMemory(check *C) {
if vcd.skipVappTests {
check.Skip("Skipping test because vapp was not successfully created at setup")
}

vapp := vcd.findFirstVapp()
existingVm, vmName := vcd.findFirstVm(vapp)
if vmName == "" {
check.Skip("skipping test because no VM is found")
}
check.Assert(existingVm.VmSpecSection.MemoryResourceMb, Not(IsNil))

currentMemory := existingVm.VmSpecSection.MemoryResourceMb.Configured
check.Assert(0, Not(Equals), currentMemory)

vm, err := vcd.client.Client.GetVMByHref(existingVm.HREF)
check.Assert(err, IsNil)

err = vm.ChangeMemory(2304)
check.Assert(err, IsNil)

check.Assert(existingVm.VmSpecSection.MemoryResourceMb, Not(IsNil))
check.Assert(vm.VM.VmSpecSection.MemoryResourceMb.Configured, Equals, int64(2304))

// return to previous value
err = vm.ChangeMemory(currentMemory)
check.Assert(err, IsNil)
}