Skip to content

Commit

Permalink
[plugins/vsphere][fix] Update VSphere Plugin for Resoto 3.0 (#1443)
Browse files Browse the repository at this point in the history
  • Loading branch information
lloesche authored Feb 14, 2023
1 parent c2560ba commit 2fda9de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
28 changes: 10 additions & 18 deletions plugins/vsphere/resoto_plugin_vsphere/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
"""
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions plugins/vsphere/resoto_plugin_vsphere/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2fda9de

Please sign in to comment.