<s> from keystoneclient import exceptions as keystone_ex <EOL> from keystoneclient import service_catalog as keystone_service_catalog <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_serialization import jsonutils as json <EOL> from six . moves . urllib import parse as urlparse <EOL> from sahara import context <EOL> from sahara import exceptions as ex <EOL> from sahara . i18n import _LW <EOL> LOG = logging . getLogger ( __name__ ) <EOL> ERRORS_TO_RETRY = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> opts = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:5> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:10> , <EOL> help = '<STR_LIT>' ) <EOL> ] <EOL> retries = cfg . OptGroup ( name = '<STR_LIT>' , <EOL> title = '<STR_LIT>' ) <EOL> CONF = cfg . CONF <EOL> CONF . register_group ( retries ) <EOL> CONF . register_opts ( opts , group = retries ) <EOL> def url_for ( service_catalog = None , service_type = '<STR_LIT>' , <EOL> endpoint_type = "<STR_LIT>" ) : <EOL> if not service_catalog : <EOL> service_catalog = context . current ( ) . service_catalog <EOL> try : <EOL> return keystone_service_catalog . ServiceCatalogV2 ( <EOL> { '<STR_LIT>' : json . loads ( service_catalog ) } ) . url_for ( <EOL> service_type = service_type , endpoint_type = endpoint_type , <EOL> region_name = CONF . os_region_name ) <EOL> except keystone_ex . EndpointNotFound : <EOL> return keystone_service_catalog . ServiceCatalogV3 ( <EOL> context . get_auth_token ( ) , <EOL> { '<STR_LIT>' : json . loads ( service_catalog ) } ) . url_for ( <EOL> service_type = service_type , endpoint_type = endpoint_type , <EOL> region_name = CONF . os_region_name ) <EOL> def retrieve_auth_url ( endpoint_type = "<STR_LIT>" ) : <EOL> version = '<STR_LIT>' if CONF . use_identity_api_v3 else '<STR_LIT>' <EOL> ctx = context . current ( ) <EOL> if ctx . service_catalog : <EOL> info = urlparse . urlparse ( url_for ( ctx . service_catalog , '<STR_LIT>' , <EOL> endpoint_type ) ) <EOL> else : <EOL> info = urlparse . urlparse ( CONF . keystone_authtoken . auth_uri ) <EOL> return "<STR_LIT>" % ( info [ : <NUM_LIT:2> ] + ( version , ) ) <EOL> def execute_with_retries ( method , * args , ** kwargs ) : <EOL> attempts = CONF . retries . retries_number + <NUM_LIT:1> <EOL> while attempts > <NUM_LIT:0> : <EOL> try : <EOL> return method ( * args , ** kwargs ) <EOL> except Exception as e : <EOL> error_code = getattr ( e , '<STR_LIT>' , None ) or getattr ( <EOL> e , '<STR_LIT>' , None ) or getattr ( e , '<STR_LIT:code>' , None ) <EOL> if error_code in ERRORS_TO_RETRY : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> method = method . __name__ , <EOL> error_msg = e , <EOL> error_code = error_code ) ) <EOL> attempts -= <NUM_LIT:1> <EOL> retry_after = getattr ( e , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> context . sleep ( max ( retry_after , CONF . retries . retry_after ) ) <EOL> else : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> method = method . __name__ , error_msg = e ) ) <EOL> raise e <EOL> else : <EOL> raise ex . MaxRetriesExceeded ( attempts , method . __name__ ) </s>
