<s> """<STR_LIT>""" <EOL> import logging <EOL> from compass . db . api import adapter as adapter_api <EOL> from compass . db . api import database <EOL> from compass . db . api import permission <EOL> from compass . db . api import user as user_api <EOL> from compass . db . api import utils <EOL> from compass . db import exception <EOL> SUPPORTED_FIELDS = [ <EOL> '<STR_LIT:name>' , <EOL> ] <EOL> RESP_FIELDS = [ <EOL> '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> RESP_OS_FIELDS = [ <EOL> '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' <EOL> ] <EOL> RESP_ROLES_FIELDS = [ <EOL> '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' <EOL> ] <EOL> RESP_FLAVORS_FIELDS = [ <EOL> '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> ADAPTER_MAPPING = None <EOL> FLAVOR_MAPPING = None <EOL> def load_adapters ( force_reload = False ) : <EOL> global ADAPTER_MAPPING <EOL> if force_reload or ADAPTER_MAPPING is None : <EOL> logging . info ( '<STR_LIT>' ) <EOL> ADAPTER_MAPPING = adapter_api . get_adapters_internal ( <EOL> force_reload = force_reload <EOL> ) <EOL> def load_flavors ( force_reload = False ) : <EOL> global FLAVOR_MAPPING <EOL> if force_reload or FLAVOR_MAPPING is None : <EOL> logging . info ( '<STR_LIT>' ) <EOL> FLAVOR_MAPPING = { } <EOL> adapters_flavors = adapter_api . get_flavors_internal ( <EOL> force_reload = force_reload <EOL> ) <EOL> for adapter_name , adapter_flavors in adapters_flavors . items ( ) : <EOL> for flavor_name , flavor in adapter_flavors . items ( ) : <EOL> FLAVOR_MAPPING [ '<STR_LIT>' % ( adapter_name , flavor_name ) ] = flavor <EOL> def _filter_adapters ( adapter_config , filter_name , filter_value ) : <EOL> if filter_name not in adapter_config : <EOL> return False <EOL> if isinstance ( filter_value , list ) : <EOL> return bool ( <EOL> adapter_config [ filter_name ] in filter_value <EOL> ) <EOL> elif isinstance ( filter_value , dict ) : <EOL> return all ( [ <EOL> _filter_adapters ( <EOL> adapter_config [ filter_name ] , <EOL> sub_filter_key , sub_filter_value <EOL> ) <EOL> for sub_filter_key , sub_filter_value in filter_value . items ( ) <EOL> ] ) <EOL> else : <EOL> return adapter_config [ filter_name ] == filter_value <EOL> @ utils . supported_filters ( optional_support_keys = SUPPORTED_FIELDS ) <EOL> @ database . run_in_session ( ) <EOL> @ user_api . check_user_permission ( <EOL> permission . PERMISSION_LIST_ADAPTERS <EOL> ) <EOL> @ utils . output_filters ( name = utils . general_filter_callback ) <EOL> @ utils . wrap_to_dict ( <EOL> RESP_FIELDS , <EOL> supported_oses = RESP_OS_FIELDS , <EOL> roles = RESP_ROLES_FIELDS , <EOL> flavors = RESP_FLAVORS_FIELDS <EOL> ) <EOL> def list_adapters ( user = None , session = None , ** filters ) : <EOL> """<STR_LIT>""" <EOL> load_adapters ( ) <EOL> return ADAPTER_MAPPING . values ( ) <EOL> @ utils . supported_filters ( [ ] ) <EOL> @ database . run_in_session ( ) <EOL> @ user_api . check_user_permission ( <EOL> permission . PERMISSION_LIST_ADAPTERS <EOL> ) <EOL> @ utils . wrap_to_dict ( <EOL> RESP_FIELDS , <EOL> supported_oses = RESP_OS_FIELDS , <EOL> roles = RESP_ROLES_FIELDS , <EOL> flavors = RESP_FLAVORS_FIELDS <EOL> ) <EOL> def get_adapter ( adapter_id , user = None , session = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> load_adapters ( ) <EOL> if adapter_id not in ADAPTER_MAPPING : <EOL> raise exception . RecordNotExists ( <EOL> '<STR_LIT>' % adapter_id <EOL> ) <EOL> return ADAPTER_MAPPING [ adapter_id ] <EOL> @ utils . supported_filters ( [ ] ) <EOL> @ database . run_in_session ( ) <EOL> @ user_api . check_user_permission ( <EOL> permission . PERMISSION_LIST_METADATAS <EOL> ) <EOL> @ utils . wrap_to_dict ( RESP_FLAVORS_FIELDS ) <EOL> def list_flavors ( user = None , session = None , ** filters ) : <EOL> """<STR_LIT>""" <EOL> load_flavors ( ) <EOL> return FLAVOR_MAPPING . values ( ) <EOL> @ utils . supported_filters ( [ ] ) <EOL> @ database . run_in_session ( ) <EOL> @ user_api . check_user_permission ( <EOL> permission . PERMISSION_LIST_METADATAS <EOL> ) <EOL> @ utils . wrap_to_dict ( RESP_FLAVORS_FIELDS ) <EOL> def get_flavor ( flavor_id , user = None , session = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> load_flavors ( ) <EOL> if flavor_id not in FLAVOR_MAPPING : <EOL> raise exception . RecordNotExists ( <EOL> '<STR_LIT>' % flavor_id <EOL> ) <EOL> return FLAVOR_MAPPING [ flavor_id ] </s>
