<s> from keystoneclient import session as keystone <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from sahara import exceptions as ex <EOL> from sahara . i18n import _ <EOL> from sahara . i18n import _LE <EOL> CONF = cfg . CONF <EOL> LOG = logging . getLogger ( __name__ ) <EOL> _SESSION_CACHE = None <EOL> SESSION_TYPE_CINDER = '<STR_LIT>' <EOL> SESSION_TYPE_KEYSTONE = '<STR_LIT>' <EOL> SESSION_TYPE_NEUTRON = '<STR_LIT>' <EOL> SESSION_TYPE_NOVA = '<STR_LIT>' <EOL> SESSION_TYPE_INSECURE = '<STR_LIT>' <EOL> def cache ( ) : <EOL> global _SESSION_CACHE <EOL> if not _SESSION_CACHE : <EOL> _SESSION_CACHE = SessionCache ( ) <EOL> return _SESSION_CACHE <EOL> class SessionCache ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _sessions = { } <EOL> self . _session_funcs = { <EOL> SESSION_TYPE_CINDER : self . get_cinder_session , <EOL> SESSION_TYPE_KEYSTONE : self . get_keystone_session , <EOL> SESSION_TYPE_NEUTRON : self . get_neutron_session , <EOL> SESSION_TYPE_NOVA : self . get_nova_session , <EOL> SESSION_TYPE_INSECURE : self . get_insecure_session , <EOL> } <EOL> def _set_session ( self , session_type , session ) : <EOL> '''<STR_LIT>''' <EOL> self . _sessions [ session_type ] = session <EOL> def get_session ( self , session_type = SESSION_TYPE_INSECURE ) : <EOL> '''<STR_LIT>''' <EOL> session_function = self . _session_funcs . get ( session_type ) <EOL> if session_function : <EOL> return session_function ( ) <EOL> else : <EOL> LOG . error ( <EOL> _LE ( '<STR_LIT>' ) . <EOL> format ( type = session_type ) ) <EOL> raise ex . SaharaException ( <EOL> _ ( '<STR_LIT>' ) . <EOL> format ( type = session_type ) ) <EOL> def get_insecure_session ( self ) : <EOL> session = self . _sessions . get ( SESSION_TYPE_INSECURE ) <EOL> if not session : <EOL> session = keystone . Session ( verify = False ) <EOL> self . _set_session ( SESSION_TYPE_INSECURE , session ) <EOL> return session <EOL> def get_cinder_session ( self ) : <EOL> session = self . _sessions . get ( SESSION_TYPE_CINDER ) <EOL> if not session : <EOL> if not CONF . cinder . api_insecure and CONF . cinder . ca_file : <EOL> session = keystone . Session ( <EOL> cert = CONF . cinder . ca_file , verify = True ) <EOL> else : <EOL> session = self . get_insecure_session ( ) <EOL> self . _set_session ( SESSION_TYPE_CINDER , session ) <EOL> return session <EOL> def get_keystone_session ( self ) : <EOL> session = self . _sessions . get ( SESSION_TYPE_KEYSTONE ) <EOL> if not session : <EOL> if not CONF . keystone . api_insecure and CONF . keystone . ca_file : <EOL> session = keystone . Session ( <EOL> cert = CONF . keystone . ca_file , verify = True ) <EOL> else : <EOL> session = self . get_insecure_session ( ) <EOL> self . _set_session ( SESSION_TYPE_KEYSTONE , session ) <EOL> return session <EOL> def get_neutron_session ( self ) : <EOL> session = self . _sessions . get ( SESSION_TYPE_NEUTRON ) <EOL> if not session : <EOL> if not CONF . neutron . api_insecure and CONF . neutron . ca_file : <EOL> session = keystone . Session ( <EOL> cert = CONF . neutron . ca_file , verify = True ) <EOL> else : <EOL> session = self . get_insecure_session ( ) <EOL> self . _set_session ( SESSION_TYPE_NEUTRON , session ) <EOL> return session <EOL> def get_nova_session ( self ) : <EOL> session = self . _sessions . get ( SESSION_TYPE_NOVA ) <EOL> if not session : <EOL> if not CONF . nova . api_insecure and CONF . nova . ca_file : <EOL> session = keystone . Session ( <EOL> cert = CONF . nova . ca_file , verify = True ) <EOL> else : <EOL> session = self . get_insecure_session ( ) <EOL> self . _set_session ( SESSION_TYPE_NOVA , session ) <EOL> return session <EOL> def token_for_auth ( self , auth ) : <EOL> return self . get_keystone_session ( ) . get_auth_headers ( auth ) . get ( <EOL> '<STR_LIT>' ) </s>
