Pysphere在设置vm_mor后插入注释时出现问题

时间:2014-08-11 22:09:46

标签: python vmware vmware-tools

我的程序的目标是解析从oracle数据库返回的一些信息。然后,当它解析此信息时,构建一个pickle字典,使用pysphere模块将数据插入到vcenter vm.object中。但是,目前我在尝试将注释保存到用户设置vm_object时遇到了问题,该用户设置是使用vm_mor标识符设置的。在我循环浏览VM属性之前它正常工作,但现在我正在尝试通过VM-mor搜索我有问题我会发布它的工作方式和我想要的方式,但它不起作用。

这是它不起作用的方式和我得到的错误代码:

import cx_Oracle
import datetime
from decimal import *
from pysphere import VIServer, MORTypes, VIProperty, VIMor
from pysphere.resources import VimService_services as VI
from pysphere.vi_virtual_machine import VIVirtualMachine
import pickle
import sys
import time

vc = VIServer()
vc.connect(hostname,username,password, trace_file="debug.txt")

properties = ['config.name', 'config.annotation']
licenses_list = []
license_built_list = []

def change_annotation(vm_mor, new_annotation):
    request = VI.ReconfigVM_TaskRequestMsg()
    _this = request.new__this(vm_mor)
    _this.set_attribute_type(vm_mor.get_attribute_type())
    request.set_element__this(_this)

    spec = request.new_spec()
    spec.set_element_annotation(new_annotation)
    request.set_element_spec(spec)
    vc._proxy.ReconfigVM_Task(request)

oracle = cx_Oracle.connect()
cursor = oracle.cursor()
cursor_guestos = oracle.cursor()

select_customer = ( ''' ''')

cursor.execute(select_customer)
for value in cursor:
   licenses_list.append(value)


for index, value in enumerate(licenses_list):
   customer_id = value[0]
   vm_mor = value[1]
   vm_id = value[2]
   license_id = value[3]
   product = value[4]
   guest_os_id = 0

   sql_guest_os = (''' ''')%int(vm_id)
   cursor_guestos.execute(sql_guest_os)

   for guest_os_id in cursor_guestos:
      guest_os_id = guest_os_id[0]

   if licenses_list[index][2] == licenses_list[index+1][2]:     
      license_built_list.append((licenses_list[index][4], licenses_list[index][3]))
      continue     
   else:
      license_built_list.append((licenses_list[index][4], licenses_list[index][3]))       


   pickle_dict = {"license":license_built_list, "customer_id":customer_id, "guest_os":guest_os_id}
   pickle.dump( pickle_dict, open( vm_mor, "wb" ) )
   annotation = vm_mor

   vm_mor_query =  VIMor(str(vm_mor), MORTypes.VirtualMachine)
   try:
      vm = VIVirtualMachine(vc, vm_mor_query)
   except:
      continue
   change_annotation(vm, annotation) #this is where it breaks.

   pickle_dict = {}
   annotation = {}
   vm_mor = {}
   license_built_list=[]




Traceback (most recent call last):
  File "/home/grant/.eclipse/org.eclipse.platform_3.8_155965261/plugins/org.python.pydev_3.4.1.201403181715/pysrc/pydevd.py", line 1844, in <module>
    debugger.run(setup['file'], None, None)
  File "/home/grant/.eclipse/org.eclipse.platform_3.8_155965261/plugins/org.python.pydev_3.4.1.201403181715/pysrc/pydevd.py", line 1372, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/grant/workspace/Metering/metering-home/push-annotation-pickle.py", line 96, in <module>
    change_annotation(vm, annotation)
  File "/home/grant/workspace/Metering/metering-home/push-annotation-pickle.py", line 25, in change_annotation
    _this.set_attribute_type(vm_mor.get_attribute_type())
AttributeError: 'VIVirtualMachine' object has no attribute 'get_attribute_type'

它的工作方式:

def change_annotation(vm_mor, new_annotation):
    request = VI.ReconfigVM_TaskRequestMsg()
    _this = request.new__this(vm_mor)
    _this.set_attribute_type(vm_mor.get_attribute_type())
    request.set_element__this(_this)

    spec = request.new_spec()
    spec.set_element_annotation(new_annotation)
    request.set_element_spec(spec)
    vc._proxy.ReconfigVM_Task(request)

vc = VIServer()
vc.connect()

properties = ['config.name', 'config.annotation']
vm_mors = []

for vm_mor_sql in cursor:
   vm_mors.append(vm_mor_sql[0])


for vc_vm in vc._retrieve_properties_traversal(property_names=properties, obj_type='VirtualMachine'):
   vm_mor = vc_vm.Obj
   if vm_mor in vm_mors:
      vm = dict()
      print vm_mor,
      try:
         for vm_prop in vc_vm.PropSet:
            vm[vm_prop.Name] = vm_prop.Val
      except AttributeError:
         print 'missing properties'
         continue
      try:
         change_annotation(vm_mor, 'test insert')
      except:
         continue

任何帮助都会得到满足。

0 个答案:

没有答案