<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import six . moves . urllib . parse as urlparse <EOL> import six . moves . urllib . request as urllib <EOL> import suds . sax . element as element <EOL> from oslo_vmware . _i18n import _LW <EOL> from oslo_vmware import service <EOL> from oslo_vmware import vim_util <EOL> SERVICE_TYPE = '<STR_LIT>' <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class Pbm ( service . Service ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , protocol = '<STR_LIT>' , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , <EOL> wsdl_url = None , cacert = None , insecure = True , pool_maxsize = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> base_url = service . Service . build_base_url ( protocol , host , port ) <EOL> soap_url = base_url + '<STR_LIT>' <EOL> super ( Pbm , self ) . __init__ ( wsdl_url , soap_url , cacert , insecure , <EOL> pool_maxsize ) <EOL> def set_soap_cookie ( self , cookie ) : <EOL> """<STR_LIT>""" <EOL> elem = element . Element ( '<STR_LIT>' ) . setText ( cookie ) <EOL> self . client . set_options ( soapheaders = elem ) <EOL> def retrieve_service_content ( self ) : <EOL> ref = vim_util . get_moref ( service . SERVICE_INSTANCE , SERVICE_TYPE ) <EOL> return self . PbmRetrieveServiceContent ( ref ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_all_profiles ( session ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> pbm = session . pbm <EOL> profile_manager = pbm . service_content . profileManager <EOL> res_type = pbm . client . factory . create ( '<STR_LIT>' ) <EOL> res_type . resourceType = '<STR_LIT>' <EOL> profiles = [ ] <EOL> profile_ids = session . invoke_api ( pbm , <EOL> '<STR_LIT>' , <EOL> profile_manager , <EOL> resourceType = res_type ) <EOL> LOG . debug ( "<STR_LIT>" , profile_ids ) <EOL> if profile_ids : <EOL> profiles = session . invoke_api ( pbm , <EOL> '<STR_LIT>' , <EOL> profile_manager , <EOL> profileIds = profile_ids ) <EOL> return profiles <EOL> def get_profile_id_by_name ( session , profile_name ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , profile_name ) <EOL> for profile in get_all_profiles ( session ) : <EOL> if profile . name == profile_name : <EOL> profile_id = profile . profileId <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT:id>' : profile_id , <EOL> '<STR_LIT:name>' : profile_name } ) <EOL> return profile_id <EOL> return None <EOL> def filter_hubs_by_profile ( session , hubs , profile_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : hubs , <EOL> '<STR_LIT>' : profile_id } ) <EOL> pbm = session . pbm <EOL> placement_solver = pbm . service_content . placementSolver <EOL> filtered_hubs = session . invoke_api ( pbm , <EOL> '<STR_LIT>' , <EOL> placement_solver , <EOL> hubsToSearch = hubs , <EOL> profile = profile_id ) <EOL> LOG . debug ( "<STR_LIT>" , filtered_hubs ) <EOL> return filtered_hubs <EOL> def convert_datastores_to_hubs ( pbm_client_factory , datastores ) : <EOL> """<STR_LIT>""" <EOL> hubs = [ ] <EOL> for ds in datastores : <EOL> hub = pbm_client_factory . create ( '<STR_LIT>' ) <EOL> hub . hubId = ds . value <EOL> hub . hubType = '<STR_LIT>' <EOL> hubs . append ( hub ) <EOL> return hubs <EOL> def filter_datastores_by_hubs ( hubs , datastores ) : <EOL> """<STR_LIT>""" <EOL> filtered_dss = [ ] <EOL> hub_ids = [ hub . hubId for hub in hubs ] <EOL> for ds in datastores : <EOL> if ds . value in hub_ids : <EOL> filtered_dss . append ( ds ) <EOL> return filtered_dss <EOL> def get_pbm_wsdl_location ( vc_version ) : <EOL> """<STR_LIT>""" <EOL> if not vc_version : <EOL> return <EOL> ver = vc_version . split ( '<STR_LIT:.>' ) <EOL> major_minor = ver [ <NUM_LIT:0> ] <EOL> if len ( ver ) >= <NUM_LIT:2> : <EOL> major_minor = '<STR_LIT>' % ( major_minor , ver [ <NUM_LIT:1> ] ) <EOL> curr_dir = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> pbm_service_wsdl = os . path . join ( curr_dir , '<STR_LIT>' , major_minor , <EOL> '<STR_LIT>' ) <EOL> if not os . path . exists ( pbm_service_wsdl ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , pbm_service_wsdl ) <EOL> return <EOL> pbm_wsdl = urlparse . urljoin ( '<STR_LIT>' , urllib . pathname2url ( pbm_service_wsdl ) ) <EOL> LOG . debug ( "<STR_LIT>" , pbm_wsdl ) <EOL> return pbm_wsdl <EOL> def get_profiles ( session , vm ) : <EOL> """<STR_LIT>""" <EOL> pbm = session . pbm <EOL> profile_manager = pbm . service_content . profileManager <EOL> object_ref = pbm . client . factory . create ( '<STR_LIT>' ) <EOL> object_ref . key = vm . value <EOL> object_ref . objectType = '<STR_LIT>' <EOL> return session . invoke_api ( pbm , '<STR_LIT>' , <EOL> profile_manager , entity = object_ref ) <EOL> def get_profiles_by_ids ( session , profile_ids ) : <EOL> """<STR_LIT>""" <EOL> profiles = [ ] <EOL> if profile_ids : <EOL> pbm = session . pbm <EOL> profile_manager = pbm . service_content . profileManager <EOL> profiles = session . invoke_api ( pbm , <EOL> '<STR_LIT>' , <EOL> profile_manager , <EOL> profileIds = profile_ids ) <EOL> return profiles </s>
