2016-10-26 19:08:23 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
* *******************************************************
|
|
|
|
* Copyright (c) VMware, Inc. 2016. All Rights Reserved.
|
|
|
|
* *******************************************************
|
|
|
|
*
|
|
|
|
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
|
|
|
|
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
|
|
|
|
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
|
|
|
|
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
"""
|
|
|
|
|
2017-02-09 20:14:36 -05:00
|
|
|
__author__ = 'VMware, Inc.'
|
|
|
|
__copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.'
|
2017-02-10 16:55:12 -05:00
|
|
|
__vcenter_version__ = '6.5+'
|
2017-02-09 20:14:36 -05:00
|
|
|
|
2017-02-17 18:03:22 -05:00
|
|
|
import atexit
|
|
|
|
|
2016-10-26 19:08:23 -04:00
|
|
|
from com.vmware.vcenter.vm.hardware_client import Memory
|
2017-01-23 16:28:22 -05:00
|
|
|
from samples.vsphere.common import vapiconnect
|
|
|
|
from samples.vsphere.common.sample_util import parse_cli_args_vm
|
|
|
|
from samples.vsphere.common.sample_util import pp
|
|
|
|
from samples.vsphere.vcenter.setup import testbed
|
2016-10-26 19:08:23 -04:00
|
|
|
|
2017-01-23 16:28:22 -05:00
|
|
|
from samples.vsphere.vcenter.helper.vm_helper import get_vm
|
2016-10-26 19:08:23 -04:00
|
|
|
|
|
|
|
"""
|
|
|
|
Demonstrates how to configure the memory related settings of a virtual machine.
|
|
|
|
|
|
|
|
Sample Prerequisites:
|
|
|
|
The sample needs an existing VM.
|
|
|
|
"""
|
|
|
|
|
|
|
|
vm = None
|
|
|
|
vm_name = None
|
|
|
|
stub_config = None
|
|
|
|
memory_svc = None
|
|
|
|
cleardata = False
|
|
|
|
orig_memory_info = None
|
|
|
|
|
|
|
|
|
|
|
|
def setup(context=None):
|
|
|
|
global vm, vm_name, stub_config, cleardata
|
|
|
|
if context:
|
|
|
|
# Run sample suite via setup script
|
|
|
|
stub_config = context.stub_config
|
|
|
|
vm_name = testbed.config['VM_NAME_DEFAULT']
|
|
|
|
else:
|
|
|
|
# Run sample in standalone mode
|
|
|
|
server, username, password, cleardata, skip_verification, vm_name = \
|
|
|
|
parse_cli_args_vm(testbed.config['VM_NAME_DEFAULT'])
|
|
|
|
stub_config = vapiconnect.connect(server,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
skip_verification)
|
2017-02-17 18:03:22 -05:00
|
|
|
atexit.register(vapiconnect.logout, stub_config)
|
|
|
|
|
2016-10-26 19:08:23 -04:00
|
|
|
|
|
|
|
def run():
|
|
|
|
global vm
|
|
|
|
vm = get_vm(stub_config, vm_name)
|
|
|
|
if not vm:
|
2017-01-25 02:27:34 -05:00
|
|
|
raise Exception('Sample requires an existing vm with name ({}). '
|
|
|
|
'Please create the vm first.'.format(vm_name))
|
2016-10-26 19:08:23 -04:00
|
|
|
print("Using VM '{}' ({}) for Memory Sample".format(vm_name, vm))
|
|
|
|
|
|
|
|
# Create Memory stub used for making requests
|
|
|
|
global memory_svc
|
|
|
|
memory_svc = Memory(stub_config)
|
|
|
|
|
|
|
|
print('\n# Example: Get current Memory configuration')
|
|
|
|
memory_info = memory_svc.get(vm)
|
|
|
|
print('vm.hardware.Memory.get({}) -> {}'.format(vm, pp(memory_info)))
|
|
|
|
|
|
|
|
# Save current Memory info to verify that we have cleaned up properly
|
|
|
|
global orig_memory_info
|
|
|
|
orig_memory_info = memory_info
|
|
|
|
|
|
|
|
print('\n# Example: Update memory size_mib field of Memory configuration')
|
|
|
|
update_spec = Memory.UpdateSpec(size_mib=8 * 1024)
|
|
|
|
print('vm.hardware.Memory.update({}, {})'.format(vm, update_spec))
|
|
|
|
memory_svc.update(vm, update_spec)
|
|
|
|
|
|
|
|
# Get new Memory configuration
|
|
|
|
memory_info = memory_svc.get(vm)
|
|
|
|
print('vm.hardware.Memory.get({}) -> {}'.format(vm, pp(memory_info)))
|
|
|
|
|
|
|
|
print('\n# Example: Update hot_add_enabled field of Memory configuration')
|
|
|
|
update_spec = Memory.UpdateSpec(hot_add_enabled=True)
|
|
|
|
print('vm.hardware.Memory.update({}, {})'.format(vm, update_spec))
|
|
|
|
memory_svc.update(vm, update_spec)
|
|
|
|
|
|
|
|
# Get new Memory configuration
|
|
|
|
memory_info = memory_svc.get(vm)
|
|
|
|
print('vm.hardware.Memory.get({}) -> {}'.format(vm, pp(memory_info)))
|
|
|
|
|
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
print('\n# Cleanup: Revert Memory configuration')
|
|
|
|
update_spec = Memory.UpdateSpec(size_mib=orig_memory_info.size_mib,
|
|
|
|
hot_add_enabled=orig_memory_info.
|
|
|
|
hot_add_enabled)
|
|
|
|
print('vm.hardware.Memory.update({}, {})'.format(vm, update_spec))
|
|
|
|
memory_svc.update(vm, update_spec)
|
|
|
|
|
|
|
|
# Get final Memory configuration
|
|
|
|
memory_info = memory_svc.get(vm)
|
|
|
|
print('vm.hardware.Memory.get({}) -> {}'.format(vm, pp(memory_info)))
|
|
|
|
|
|
|
|
if memory_info != orig_memory_info:
|
|
|
|
print('vm.hardware.Memory WARNING: '
|
|
|
|
'Final Memory info does not match original')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-02-17 18:03:22 -05:00
|
|
|
setup()
|
|
|
|
run()
|
|
|
|
if cleardata:
|
|
|
|
cleanup()
|
2016-10-26 19:08:23 -04:00
|
|
|
|
2017-02-24 03:41:48 -05:00
|
|
|
|
2016-10-26 19:08:23 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|