2014-07-24 17 views
-3

我有在python我的同班同学的问题在第一时间通过将在第二次返回但是正确的值,当它到达: vm_return.vmPerf()如何通过Python类环

所有的初始化类的定义是完全从变量抹去的东西,剩下的事情就是:

passed_vm_mor

那里不能当找到对象,因为它不存在了我第二次称它没有任何意义。它的概率只是我的一部分,尽管一个误区..

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() 
+0

这不是所有的数据我拉我只是缩短它... – PythonDevOps

+0

基本上第二次调用vmPerf没有更多的变量,所以对象不见了,所以它不能得到的数据。我如何解决这个问题,以便我可以查看它并始终返回数据。 – PythonDevOps

+1

1)请编辑你的问题,不要对它写评论。 2)'__init__''方法返回后,你是否打算除了'self.passed_vm_mor'之外的其他任何东西?目前,除此之外,您的所有分配都是方法局部变量,而不是类属性。 – aruisdante

回答

1

只要你做这样的事情:

self.vm_mor = self.vm._moId 

你永远下次调用失败,只是因为现在self.vm_mor只包含一个id (_moId)。

我不知道你想通过做它来实现的,但它会更有意义的事:

self._moId = self.vm._moId 

,如果你想拥有一个“直接访问”的内部变量self.vm_mor

此外,要注意的是,在下面的循环:

for vm_mor in cursor: 

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

你跟上不同v_mor又在发,再次改写vm_return

+0

你还应该怎样通过课堂来让vm mors循环,并让出来?对于数据库查询中的每个数据库,它将它传递给该类,然后类将检查该数据库是否在vcenter中,如果该数据库未转到下一个数据库,则重复此操作直到找到它,然后将类对象设置为vm.object – PythonDevOps

+0

@PythonDevOps“...重复,直到找到......” - 不,它只是继续循环,直到没有任何东西离开。 – alfasin