From 2fda9de80b0a4930be2cd612011f623de2fb16af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20L=C3=B6sche?= Date: Tue, 14 Feb 2023 20:08:21 +0100 Subject: [PATCH] [plugins/vsphere][fix] Update VSphere Plugin for Resoto 3.0 (#1443) --- .../vsphere/resoto_plugin_vsphere/__init__.py | 28 +++++++------------ .../resoto_plugin_vsphere/resources.py | 6 ++-- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/plugins/vsphere/resoto_plugin_vsphere/__init__.py b/plugins/vsphere/resoto_plugin_vsphere/__init__.py index 70f81b7a2c..c2dac4b04d 100644 --- a/plugins/vsphere/resoto_plugin_vsphere/__init__.py +++ b/plugins/vsphere/resoto_plugin_vsphere/__init__.py @@ -1,6 +1,6 @@ from datetime import datetime -import resotolib.logger +from resotolib.logger import log from resotolib.config import Config from resotolib.baseplugin import BaseCollectorPlugin from resotolib.baseresources import BaseResource, InstanceStatus @@ -12,23 +12,12 @@ from pyVmomi import vim -log = resotolib.logger.getLogger("resoto." + __name__) - class VSphereCollectorPlugin(BaseCollectorPlugin): cloud = "vsphere" - def __init__(self, *args, **kwargs) -> None: - super().__init__(*args, **kwargs) - if Config.vsphere.host: - self.vsphere_client = get_vsphere_client() - def get_cluster(self) -> VSphereCluster: - """ - use --vsphere-host as the clustername - """ - if Config.vsphere.host: - return VSphereCluster(Config.vsphere.host, {}) + return VSphereCluster(id=Config.vsphere.host) def get_keymap_from_vmlist(self, list_vm) -> VSphereCluster: """ @@ -55,7 +44,7 @@ def add_instances(self, parent: BaseResource) -> None: """ loop over VMs and add them as VSphereInstance to parent """ - content = self.vsphere_client.client.RetrieveContent() + content = get_vsphere_client().client.RetrieveContent() container = content.rootFolder # starting point to look into view_type = [vim.VirtualMachine] # object types to look for @@ -78,11 +67,14 @@ def add_instances(self, parent: BaseResource) -> None: for list_vm in vms: try: tags = self.get_custom_attributes(list_vm, keys) - # get TS and create clean datetime - ctime = datetime.fromtimestamp(list_vm.config.createDate.timestamp()) + + try: + ctime = datetime.fromtimestamp(list_vm.config.createDate.timestamp()) + except AttributeError: + ctime = None vm = VSphereInstance( - list_vm._moId, + id=list_vm._moId, name=str(list_vm.name), instance_cores=int(list_vm.config.hardware.numCPU), instance_memory=int(list_vm.config.hardware.memoryMB / 1024), @@ -103,7 +95,7 @@ def collect(self) -> None: return cluster = self.get_cluster() - dc1 = VSphereDataCenter("dc1", tags={}) + dc1 = VSphereDataCenter(id="dc1") self.graph.add_resource(self.graph.root, cluster) self.graph.add_resource(cluster, dc1) diff --git a/plugins/vsphere/resoto_plugin_vsphere/resources.py b/plugins/vsphere/resoto_plugin_vsphere/resources.py index 26cb24867b..33d5674e39 100644 --- a/plugins/vsphere/resoto_plugin_vsphere/resources.py +++ b/plugins/vsphere/resoto_plugin_vsphere/resources.py @@ -46,19 +46,19 @@ def _vm(self): def delete(self, graph: Graph) -> bool: if self._vm() is None: - log.error(f"could not find vm name {self.name} with id {self.id}") + log.error(f"Could not find vm name {self.name} with id {self.id}") log.debug(f"Deleting resource {self.id} in account {self.account(graph).id} region {self.region(graph).id}") if self._vm().runtime.powerState == "poweredOn": task = self._vm().PowerOffVM_Task() self._vsphere_client().wait_for_tasks([task]) - log.debug(f"task finished - state: {task.info.state}") + log.debug(f"Task finished - state: {task.info.state}") log.info(f"Destroying VM {self.id} with name {self.name}") task = self._vm().Destroy_Task() self._vsphere_client().wait_for_tasks([task]) - log.debug(f"task finished - state: {task.info.state}") + log.debug(f"Task finished - state: {task.info.state}") return True def update_tag(self, key, value) -> bool: