<s> import requests <EOL> import ssl <EOL> import datetime <EOL> import utils <EOL> import ConfigParser <EOL> import os <EOL> from django . conf import settings <EOL> from pyVmomi import vim <EOL> from ConfigParser import SafeConfigParser <EOL> from pyVim . connect import SmartConnect <EOL> class PluginManager : <EOL> property_file_path = settings . HERE + '<STR_LIT>' <EOL> resurce_folder_path = settings . HERE + '<STR_LIT>' <EOL> def create_event_keyvalue_pairs ( self ) : <EOL> try : <EOL> eri_list = [ ] <EOL> for file in os . listdir ( self . resurce_folder_path ) : <EOL> file = open ( self . resurce_folder_path + '<STR_LIT:/>' + file ) <EOL> eri = vim . Extension . ResourceInfo ( ) <EOL> eri . module = '<STR_LIT>' <EOL> for line in file : <EOL> if len ( line ) > <NUM_LIT:2> and '<STR_LIT:=>' in line : <EOL> if '<STR_LIT>' in line : <EOL> eri . locale = line . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . lstrip ( ) . rstrip ( ) <EOL> else : <EOL> prop = line . split ( '<STR_LIT:=>' ) <EOL> key_val = vim . KeyValue ( ) <EOL> key_val . key = prop [ <NUM_LIT:0> ] . lstrip ( ) . rstrip ( ) <EOL> key_val . value = prop [ <NUM_LIT:1> ] . lstrip ( ) . rstrip ( ) <EOL> eri . data . append ( key_val ) <EOL> file . close ( ) <EOL> eri_list . append ( eri ) <EOL> return eri_list <EOL> except Exception as ex : <EOL> return '<STR_LIT>' + str ( ex ) <EOL> def get_extensionKey ( self ) : <EOL> cp = SafeConfigParser ( ) <EOL> cp . read ( self . property_file_path ) <EOL> key = cp . get ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> return key <EOL> def get_extension ( self , vcp_url , thumb_print ) : <EOL> try : <EOL> cp = SafeConfigParser ( ) <EOL> cp . read ( self . property_file_path ) <EOL> company = cp . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> descr = cp . get ( '<STR_LIT>' , '<STR_LIT:description>' ) <EOL> key = cp . get ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> events = cp . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( "<STR_LIT:U+002C>" ) <EOL> tasks = cp . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( "<STR_LIT:U+002C>" ) <EOL> version = utils . get_plugin_version ( ) <EOL> if '<STR_LIT>' in version : <EOL> return version <EOL> label = cp . get ( '<STR_LIT>' , '<STR_LIT:label>' ) <EOL> sys_guiprotocol = vcp_url . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> description = vim . Description ( ) <EOL> description . label = label <EOL> description . summary = descr <EOL> ext = vim . Extension ( ) <EOL> ext . company = company <EOL> ext . version = version <EOL> ext . key = key <EOL> ext . description = description <EOL> ext . lastHeartbeatTime = datetime . datetime . now ( ) <EOL> server_info = vim . Extension . ServerInfo ( ) <EOL> server_info . serverThumbprint = thumb_print <EOL> server_info . type = sys_guiprotocol . upper ( ) <EOL> server_info . url = vcp_url <EOL> server_info . description = description <EOL> server_info . company = company <EOL> server_info . adminEmail = [ '<STR_LIT>' ] <EOL> ext . server = [ server_info ] <EOL> client = vim . Extension . ClientInfo ( ) <EOL> client . url = vcp_url <EOL> client . company = company <EOL> client . version = version <EOL> client . description = description <EOL> client . type = "<STR_LIT>" <EOL> ext . client = [ client ] <EOL> event_info = [ ] <EOL> for e in events : <EOL> ext_event_type_info = vim . Extension . EventTypeInfo ( ) <EOL> ext_event_type_info . eventID = e <EOL> event_info . append ( ext_event_type_info ) <EOL> task_info = [ ] <EOL> for t in tasks : <EOL> ext_type_info = vim . Extension . TaskTypeInfo ( ) <EOL> ext_type_info . taskID = t <EOL> task_info . append ( ext_type_info ) <EOL> ext . taskList = task_info <EOL> ext . eventList = event_info <EOL> resource_list = self . create_event_keyvalue_pairs ( ) <EOL> if isinstance ( resource_list , str ) : <EOL> return resource_list <EOL> ext . resourceList = resource_list <EOL> return ext <EOL> except ConfigParser . NoOptionError as ex : <EOL> return '<STR_LIT>' + str ( ex ) <EOL> except Exception as ex : <EOL> return str ( ex ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> def install_vCenter_plugin ( <EOL> self , <EOL> vc_ip , <EOL> usernName , <EOL> password , <EOL> port , <EOL> vcp_url , <EOL> thumb_print ) : <EOL> try : <EOL> try : <EOL> ssl . _create_default_https_context = ssl . _create_unverified_context <EOL> except AttributeError : <EOL> print '<STR_LIT>' <EOL> si = SmartConnect ( "<STR_LIT>" , vc_ip , int ( port ) , usernName , password ) <EOL> ext = self . get_extension ( vcp_url , thumb_print ) <EOL> if isinstance ( ext , vim . Extension ) : <EOL> si . RetrieveServiceContent ( ) . extensionManager . RegisterExtension ( ext ) <EOL> return True <EOL> else : <EOL> return ext <EOL> except vim . fault . NoPermission as ex : <EOL> return '<STR_LIT>' <EOL> except Exception as ex : <EOL> return str ( ex ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> def uninstall_vCenter_plugin ( self , vc_ip , usernName , password , port ) : <EOL> try : <EOL> try : <EOL> ssl . _create_default_https_context = ssl . _create_unverified_context <EOL> except AttributeError : <EOL> print '<STR_LIT>' <EOL> si = SmartConnect ( "<STR_LIT>" , vc_ip , int ( port ) , usernName , password ) <EOL> extkey = self . get_extensionKey ( ) <EOL> si . RetrieveServiceContent ( ) . extensionManager . UnregisterExtension ( extkey ) <EOL> return True <EOL> except vim . fault . NoPermission as ex : <EOL> return '<STR_LIT>' <EOL> except Exception as ex : <EOL> return str ( ex ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> def upgrade_vCenter_plugin ( <EOL> self , <EOL> vc_ip , <EOL> usernName , <EOL> password , <EOL> port , <EOL> vcp_url , <EOL> thumb_print ) : <EOL> try : <EOL> try : <EOL> ssl . _create_default_https_context = ssl . _create_unverified_context <EOL> except AttributeError : <EOL> print '<STR_LIT>' <EOL> si = SmartConnect ( "<STR_LIT>" , vc_ip , int ( port ) , usernName , password ) <EOL> ext = self . get_extension ( vcp_url , thumb_print ) <EOL> if isinstance ( ext , vim . Extension ) : <EOL> si . RetrieveServiceContent ( ) . extensionManager . UpdateExtension ( ext ) <EOL> return True <EOL> else : <EOL> return ext <EOL> except vim . fault . NoPermission as ex : <EOL> return '<STR_LIT>' <EOL> except Exception as ex : <EOL> return str ( ex ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> def find_plugin ( self , vc_ip , usernName , password , port ) : <EOL> try : <EOL> try : <EOL> ssl . _create_default_https_context = ssl . _create_unverified_context <EOL> except AttributeError : <EOL> print '<STR_LIT>' <EOL> si = SmartConnect ( "<STR_LIT>" , vc_ip , int ( port ) , usernName , password ) <EOL> extkey = self . get_extensionKey ( ) <EOL> ext = si . RetrieveServiceContent ( ) . extensionManager . FindExtension ( extkey ) <EOL> if ext is None : <EOL> return False <EOL> else : <EOL> try : <EOL> return '<STR_LIT>' + ext . client [ <NUM_LIT:0> ] . url . split ( '<STR_LIT:/>' ) [ <NUM_LIT:2> ] <EOL> except : <EOL> return '<STR_LIT>' <EOL> except vim . fault . NoPermission as ex : <EOL> return '<STR_LIT>' <EOL> except Exception as ex : <EOL> return str ( ex ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> def check_credential ( self , vc_ip , usernName , password , port ) : <EOL> try : <EOL> try : <EOL> ssl . _create_default_https_context = ssl . _create_unverified_context <EOL> except AttributeError : <EOL> print '<STR_LIT>' <EOL> si = SmartConnect ( "<STR_LIT>" , vc_ip , int ( port ) , usernName , password ) <EOL> if si is None : <EOL> return False <EOL> else : <EOL> return True <EOL> except requests . exceptions . ConnectionError : <EOL> return '<STR_LIT>' <EOL> except vim . fault . InvalidLogin : <EOL> return '<STR_LIT>' <EOL> except vim . fault . NoPermission as ex : <EOL> return '<STR_LIT>' <EOL> except Exception : <EOL> return '<STR_LIT>' </s>
