1

The goal of my program is to parse some information returned from an oracle database. Then as it parses this information build a pickle dictionary to insert data into a vcenter vm.object using the pysphere module. However, currently I'm running into an issue when trying to save the annotation to a user set vm_object which is set by using the vm_mor identifier.Before when I was loopping through VM properties it was working correctly, but now that im trying to search via VM-mor im having issues I'll post the way it worked and the way I want to do it but it isn't working.

This is the way it doesn't work and the error code I get:

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'

The way it works:

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

Any help would be appriciated.

Grant Zukel
  • 1,153
  • 2
  • 24
  • 48

0 Answers0