From 4cf0f34a867444d470df6470196e7046151b7f09 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 20 Oct 2022 04:40:38 -0700 Subject: [PATCH 1/4] adds vmware_vm.get_mks_ticket #291 --- src/saltext/vmware/modules/vm.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/saltext/vmware/modules/vm.py b/src/saltext/vmware/modules/vm.py index 9d2426e6..02f82e34 100644 --- a/src/saltext/vmware/modules/vm.py +++ b/src/saltext/vmware/modules/vm.py @@ -648,3 +648,34 @@ def relocate( if ret == "success": return {"virtual_machine": "moved"} return {"virtual_machine": "failed to move"} + +def get_mks_ticket(vm_name, ticket_type, service_instance=None, profile=None): + """ + Get ticket of virtual machine of passed object type. + + vm_name + The name of the virtual machine to relocate. + + ticket_type + Type of ticket. + + service_instance + (optional) The Service Instance from which to obtain managed object references. + + profile + Profile to use (optional) + + CLI Example: + + .. code-block:: bash + + salt '*' vmware_vm.get_mks_ticket vm_name=vm01 ticket_type=webmks + """ + if service_instance is None: + service_instance = connect.get_service_instance(config=__opts__, profile=profile) + + log.info(f"Acquiring ticket {ticket_type} for {vm_name}") + vm_ref = utils_common.get_mor_by_property(service_instance, vim.VirtualMachine, vm_name) + ticket = vm_ref.AcquireTicket(ticket_type) + + return {"host": ticket.host, "ticket": ticket.ticket} From 3044490b2328adaa645b103bf98dad5a179ec210 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 20 Oct 2022 04:45:31 -0700 Subject: [PATCH 2/4] fix json return --- src/saltext/vmware/modules/vm.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/saltext/vmware/modules/vm.py b/src/saltext/vmware/modules/vm.py index 02f82e34..7b88398b 100644 --- a/src/saltext/vmware/modules/vm.py +++ b/src/saltext/vmware/modules/vm.py @@ -1,4 +1,5 @@ # SPDX-License-Identifier: Apache-2.0 +import json import logging import salt.exceptions @@ -11,7 +12,7 @@ log = logging.getLogger(__name__) try: - from pyVmomi import vim + from pyVmomi import vim, VmomiSupport HAS_PYVMOMI = True except ImportError: @@ -678,4 +679,4 @@ def get_mks_ticket(vm_name, ticket_type, service_instance=None, profile=None): vm_ref = utils_common.get_mor_by_property(service_instance, vim.VirtualMachine, vm_name) ticket = vm_ref.AcquireTicket(ticket_type) - return {"host": ticket.host, "ticket": ticket.ticket} + return json.loads(json.dumps(ticket, cls=VmomiSupport.VmomiJSONEncoder)) From 81d181fee775c975cc7464b8daa83220194c6991 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 20 Oct 2022 06:13:59 -0700 Subject: [PATCH 3/4] adds allocated resources to info #288 --- src/saltext/vmware/modules/vm.py | 10 +++++++++- src/saltext/vmware/utils/vm.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/saltext/vmware/modules/vm.py b/src/saltext/vmware/modules/vm.py index 7b88398b..a9bf4a79 100644 --- a/src/saltext/vmware/modules/vm.py +++ b/src/saltext/vmware/modules/vm.py @@ -295,9 +295,13 @@ def info(vm_name=None, service_instance=None, profile=None): datacenter_ref = utils_common.get_parent_type(vm, vim.Datacenter) mac_address = utils_vm.get_mac_address(vm) network = utils_vm.get_network(vm) + disk_bytes = utils_vm.get_disk_size(vm) tags = [] for tag in vm.tag: - tags.append(tag.name) + try: + tags.append(tag.name) + except AttributeError: + pass folder_path = utils_common.get_path(vm, service_instance) info[vm.summary.config.name] = { "guest_name": vm.summary.config.name, @@ -313,6 +317,9 @@ def info(vm_name=None, service_instance=None, profile=None): "tags": tags, "folder": folder_path, "moid": vm._moId, + "num_cpu": vm.config.hardware.numCPU, + "memory_mb": vm.config.hardware.memoryMB, + "disk_bytes": disk_bytes, } return info @@ -650,6 +657,7 @@ def relocate( return {"virtual_machine": "moved"} return {"virtual_machine": "failed to move"} + def get_mks_ticket(vm_name, ticket_type, service_instance=None, profile=None): """ Get ticket of virtual machine of passed object type. diff --git a/src/saltext/vmware/utils/vm.py b/src/saltext/vmware/utils/vm.py index a81aa6d4..5a1f69a9 100644 --- a/src/saltext/vmware/utils/vm.py +++ b/src/saltext/vmware/utils/vm.py @@ -602,6 +602,20 @@ def read_ovf_from_ova(ova_path): exit(f"Could not read file: {ova_path}") +def get_disk_size(vm): + """ + Returns total disk size in bytes from the virtual machine object. + vm + Virtual Machine Object from which to obtain disk size. + """ + size = 0.0 + for device in vm.config.hardware.device: + if isinstance(device, vim.vm.device.VirtualDisk): + size += device.capacityInBytes + + return size + + def get_network(vm): """ Returns network from a virtual machine object. From 52f28fe790ded4fda53183a1fa09885b278c5f74 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 20 Oct 2022 06:15:40 -0700 Subject: [PATCH 4/4] removed wrong commit --- src/saltext/vmware/modules/vm.py | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/src/saltext/vmware/modules/vm.py b/src/saltext/vmware/modules/vm.py index a9bf4a79..5fe2f93a 100644 --- a/src/saltext/vmware/modules/vm.py +++ b/src/saltext/vmware/modules/vm.py @@ -656,35 +656,3 @@ def relocate( if ret == "success": return {"virtual_machine": "moved"} return {"virtual_machine": "failed to move"} - - -def get_mks_ticket(vm_name, ticket_type, service_instance=None, profile=None): - """ - Get ticket of virtual machine of passed object type. - - vm_name - The name of the virtual machine to relocate. - - ticket_type - Type of ticket. - - service_instance - (optional) The Service Instance from which to obtain managed object references. - - profile - Profile to use (optional) - - CLI Example: - - .. code-block:: bash - - salt '*' vmware_vm.get_mks_ticket vm_name=vm01 ticket_type=webmks - """ - if service_instance is None: - service_instance = connect.get_service_instance(config=__opts__, profile=profile) - - log.info(f"Acquiring ticket {ticket_type} for {vm_name}") - vm_ref = utils_common.get_mor_by_property(service_instance, vim.VirtualMachine, vm_name) - ticket = vm_ref.AcquireTicket(ticket_type) - - return json.loads(json.dumps(ticket, cls=VmomiSupport.VmomiJSONEncoder))