-3

I'm having issues with my class in python the first time through it will return the correct values, however on the second pass when it gets to: vm_return.vmPerf()

all of the stuff that was defined in the init class is completely wiped from variables and all that remains is:

passed_vm_mor

there for it can't find the object because it doesn't exist anymore when i call it the second time it doesn't make any sense. Its prob just a misunderstanding on my part though..

import atexit
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import sys
import time

#globals

passed_vm_mor = ''


class getVM(object):

   def __init__(self, passed_vm_mor):
      self.passed_vm_mor = passed_vm_mor
      vcenter_connection = SmartConnect(host = hostname,user = username,pwd = password)
      atexit.register(Disconnect, vcenter_connection)             
      content = vcenter_connection.RetrieveContent()       
      perf_dict = {} 
      perfList = content.perfManager.perfCounter

      for counter in perfList: #build the vcenter counters for the objects
         counter_full = "{}.{}.{}".format(counter.groupInfo.key,counter.nameInfo.key,counter.rollupType)
         perf_dict[counter_full] = counter.key

      viewType = [vim.VirtualMachine]
      props = ['name','runtime.powerState', 'datastore']
      specType = vim.VirtualMachine
      objView = content.viewManager.CreateContainerView(content.rootFolder,viewType,True)  
      tSpec = vim.PropertyCollector.TraversalSpec(name = 'tSpecName', path = 'view', skip = False, type = vim.view.ContainerView)    
      pSpec = vim.PropertyCollector.PropertySpec(all = False, pathSet = props,type = specType)   
      oSpec = vim.PropertyCollector.ObjectSpec(obj = objView,selectSet = [tSpec],skip = False)   
      pfSpec = vim.PropertyCollector.FilterSpec(objectSet = [oSpec], propSet = [pSpec], reportMissingObjectsInResults = False)   
      vm_properties = content.propertyCollector.RetrieveProperties(specSet = [pfSpec])  
      objView.Destroy()     

      for vm_property in vm_properties: #loop through the list built from vcenter and build dictonaries.
         property_dic = {}
         for prop in vm_property.propSet:
            property_dic[prop.name] = prop.val 

         vm = vm_property.obj
         vm_mor = vm._moId
         if self.passed_vm_mor == vm_mor:
            self.vm = vm_property.obj
         else:
            continue   

   def vmPerf(self):
      self.vm_mor = self.vm._moId
      self.bootOptionsSupported = self.vm.capability.bootOptionsSupported   
      self.bootRetryOptionsSupported = self.vm.capability.bootRetryOptionsSupported   
      self.changeTrackingSupported = self.vm.capability.changeTrackingSupported   
      self.consolePreferencesSupported = self.vm.capability.consolePreferencesSupported 


cursor = db.cursor()
customer_id=24
sql = ('''select a.vm_mor from vms a, vm_groups b, customers c where c.customer_id = %d and c.customer_id = b.customer_id and b.vm_group_id = a.vm_group_id  ''') % customer_id
cursor.execute(sql)

for vm_mor in cursor:

         vm_return = getVM(vm_mor[0])
         vm_return.vmPerf()
PythonDevOps
  • 45
  • 2
  • 7
  • this isn't all of the data im pulling i just shortened it ... – PythonDevOps Jul 24 '14 at 23:17
  • basically the second time vmPerf is called there are no more variables and so the object is gone so it can't get the data. How do i fix that so i can look through it and always return the data.. – PythonDevOps Jul 24 '14 at 23:18
  • 1
    1) Please edit your question, do not write comments on it. 2) Do you intend for anything other than `self.passed_vm_mor` to persist after the ``__init__`` method returns? Currently, all of your assignments beside that are to method-local variables, not to class attributes. – aruisdante Jul 24 '14 at 23:19
  • `sql = ('''select a.vm_mor from vms a, vm_groups b, customers c where c.customer_id = %d and c.customer_id = b.customer_id and b.vm_group_id = a.vm_group_id ''') % customer_id # I LOVE ME SOME SQL INJECTION` – Jakob Bowyer Jul 24 '14 at 23:21
  • Have you considered making your class iterable? [Build a Basic Python Iterator](http://stackoverflow.com/questions/19151/build-a-basic-python-iterator) – Dan Oberlam Jul 24 '14 at 23:24
  • It was a simple edit bc I changed some stuff to post online just names and one didn't line up. No reason to comment that. Second, vm = vm_property.obj is supposed to persist if the passed vm_mor = the vm_mor inside of vcenter. – PythonDevOps Jul 25 '14 at 15:31

1 Answers1

1

As long as you do things like:

self.vm_mor = self.vm._moId

you'll always fail on the next call simply because now self.vm_mor contains only an id (_moId) now.

I'm not sure what do you want to achieve by doing it, but it would make more sense to do:

self._moId = self.vm._moId

if you want to have a "direct access" to the inner variables of self.vm_mor.

Further, pay attention that in the following loop:

for vm_mor in cursor:

     vm_return = getVM(vm_mor[0])
     vm_return.vmPerf()

you keep overriding vm_return again and again with different v_mors.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • how else are you supposed to loop the vm mors through the class and get the out put? For each mor in the database query it passes it to the class and the class checks to see if that mor is in vcenter, if its not it goes to the next mor then repeats until found then it sets the classes object to the vm.object – PythonDevOps Jul 25 '14 at 15:34
  • @PythonDevOps "...repeats until found..." - nope, it just keeps on looping until there's nothing left. – Nir Alfasi Jul 25 '14 at 15:38