From 9879aa038277ec3635c0e02e476d7bae34e87301 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 3 Jan 2018 21:13:56 +0530 Subject: [PATCH] VMware: Add support for resource allocation info (#33990) This fix adds support for Virtual Machine resource allocation information. This information is related to limit and reserve CPU and Memory of virtual machine. Signed-off-by: Abhijeet Kasurde --- .../modules/cloud/vmware/vmware_guest.py | 71 +++++++++++++++++++ .../vmware_guest/tasks/create_d1_c1_f0.yml | 6 +- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index bfc0547ca7d..e9a5b66d937 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -95,6 +95,15 @@ options: - " - C(memory_reservation_lock) (boolean): If set true, memory resource reservation for VM will always be equal to the VM's memory size. version_added: 2.5" - ' - C(max_connections) (integer): Maximum number of active remote display connections for the virtual machines. version_added: 2.5.' + - ' - C(mem_limit) (integer): The memory utilization of a virtual machine will not exceed this limit. Unit is MB. + version_added: 2.5' + - ' - C(mem_reservation) (integer): The amount of memory resource that is guaranteed available to the virtual + machine. Unit is MB. version_added: 2.5' + - ' - C(cpu_limit) (integer): The CPU utilization of a virtual machine will not exceed this limit. Unit is MHz. + version_added: 2.5' + - ' - C(cpu_reservation) (integer): The amount of CPU resource that is guaranteed available to the virtual machine. + Unit is MHz. version_added: 2.5' + guest_id: description: - Set the guest ID (Debian, RHEL, Windows...). @@ -232,6 +241,10 @@ EXAMPLES = r''' scsi: paravirtual memory_reservation: 512 memory_reservation_lock: True + mem_limit: 8096 + mem_reservation: 4096 + cpu_limit: 8096 + cpu_reservation: 4096 max_connections: 5 hotadd_cpu: True hotremove_cpu: True @@ -626,6 +639,59 @@ class PyVmomiHelper(PyVmomi): self.change_detected = True self.configspec.guestId = self.params['guest_id'] + def configure_resource_alloc_info(self, vm_obj): + """ + Function to configure resource allocation information about virtual machine + :param vm_obj: VM object in case of reconfigure, None in case of deploy + :return: None + """ + self.configspec.memoryAllocation = vim.ResourceAllocationInfo() + self.configspec.cpuAllocation = vim.ResourceAllocationInfo() + + if 'hardware' in self.params: + if 'mem_limit' in self.params['hardware']: + mem_limit = None + try: + mem_limit = int(self.params['hardware'].get('mem_limit')) + except ValueError as e: + self.module.fail_json(msg="hardware.mem_limit attribute should be an integer value.") + self.configspec.memoryAllocation.limit = mem_limit + if vm_obj is None or self.configspec.memoryAllocation.limit != vm_obj.config.memoryAllocation.limit: + self.change_detected = True + + if 'mem_reservation' in self.params['hardware']: + mem_reservation = None + try: + mem_reservation = int(self.params['hardware'].get('mem_reservation')) + except ValueError as e: + self.module.fail_json(msg="hardware.mem_reservation should be an integer value.") + + self.configspec.memoryAllocation.reservation = mem_reservation + if vm_obj is None or \ + self.configspec.memoryAllocation.reservation != vm_obj.config.memoryAllocation.reservation: + self.change_detected = True + + if 'cpu_limit' in self.params['hardware']: + cpu_limit = None + try: + cpu_limit = int(self.params['hardware'].get('cpu_limit')) + except ValueError as e: + self.module.fail_json(msg="hardware.cpu_limit attribute should be an integer value.") + self.configspec.cpuAllocation.limit = cpu_limit + if vm_obj is None or self.configspec.cpuAllocation.limit != vm_obj.config.cpuAllocation.limit: + self.change_detected = True + + if 'cpu_reservation' in self.params['hardware']: + cpu_reservation = None + try: + cpu_reservation = int(self.params['hardware'].get('cpu_reservation')) + except ValueError as e: + self.module.fail_json(msg="hardware.cpu_reservation should be an integer value.") + self.configspec.cpuAllocation.reservation = cpu_reservation + if vm_obj is None or \ + self.configspec.cpuAllocation.reservation != vm_obj.config.cpuAllocation.reservation: + self.change_detected = True + def configure_cpu_and_memory(self, vm_obj, vm_creation=False): # set cpu/memory/etc if 'hardware' in self.params: @@ -1433,6 +1499,7 @@ class PyVmomiHelper(PyVmomi): self.configure_guestid(vm_obj=vm_obj, vm_creation=True) self.configure_cpu_and_memory(vm_obj=vm_obj, vm_creation=True) self.configure_hardware_params(vm_obj=vm_obj) + self.configure_resource_alloc_info(vm_obj=vm_obj) self.configure_disks(vm_obj=vm_obj) self.configure_network(vm_obj=vm_obj) self.configure_cdrom(vm_obj=vm_obj) @@ -1501,6 +1568,9 @@ class PyVmomiHelper(PyVmomi): resource_pool = self.get_resource_pool() try: task = destfolder.CreateVM_Task(config=self.configspec, pool=resource_pool) + except vmodl.fault.InvalidRequest as e: + self.module.fail_json(msg="Failed to create virtual machine due to invalid configuration " + "parameter %s" % to_native(e.msg)) except vim.fault.RestrictedVersion as e: self.module.fail_json(msg="Failed to create virtual machine due to " "product versioning restrictions: %s" % to_native(e.msg)) @@ -1570,6 +1640,7 @@ class PyVmomiHelper(PyVmomi): self.configure_network(vm_obj=self.current_vm_obj) self.configure_cdrom(vm_obj=self.current_vm_obj) self.customize_customvalues(vm_obj=self.current_vm_obj, config_spec=self.configspec) + self.configure_resource_alloc_info(vm_obj=self.current_vm_obj) if self.params['annotation'] and self.current_vm_obj.config.annotation != self.params['annotation']: self.configspec.annotation = str(self.params['annotation']) diff --git a/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml index deb31f95902..259a24db670 100644 --- a/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml +++ b/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml @@ -46,12 +46,16 @@ memory_mb: 512 hotadd_memory: true hotadd_cpu: false - # vcsim does not support this changes so commenting + # vcsim does not support these settings, so commenting # till the time. # memory_reservation: 512 # memory_reservation_lock: False # nested_virt: True # hotremove_cpu: True + # mem_limit: 8096 + # mem_reservation: 4096 + # cpu_limit: 8096 + # cpu_reservation: 4096 max_connections: 10 disk: - size: 0gb