<s> """<STR_LIT>""" <EOL> import requests <EOL> import time <EOL> from cafe . engine . behaviors import BaseBehavior <EOL> from cloudcafe . common . tools . datagen import rand_name <EOL> from cloudcafe . networking . networks . common . config import NetworkingBaseConfig <EOL> from cloudcafe . networking . networks . common . constants import NeutronResponseCodes , NeutronResource <EOL> from cloudcafe . networking . networks . common . exceptions import ResourceBuildException , ResourceDeleteException , ResourceGetException , ResourceListException , ResourceUpdateException , UnhandledMethodCaseException <EOL> class NetworkingBaseBehaviors ( BaseBehavior ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( NetworkingBaseBehaviors , self ) . __init__ ( ) <EOL> self . config = NetworkingBaseConfig ( ) <EOL> def check_response ( self , resp , status_code , label , message , <EOL> network_id = None ) : <EOL> """<STR_LIT>""" <EOL> response_msg = None <EOL> if network_id : <EOL> label = '<STR_LIT>' . format ( <EOL> label = label , network = network_id ) <EOL> resp_type = type ( resp ) <EOL> if not resp_type == requests . models . Response : <EOL> err_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( label = label , message = message , <EOL> resp_type = resp_type ) <EOL> self . _log . error ( err_msg ) <EOL> response_msg = err_msg <EOL> elif resp . ok and resp . entity and resp . status_code == status_code : <EOL> response_msg = None <EOL> elif not resp . ok or resp . status_code != status_code : <EOL> err_msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> label = label , message = message , status = resp . status_code , <EOL> reason = resp . reason , content = resp . content , <EOL> expected_status = status_code ) <EOL> self . _log . error ( err_msg ) <EOL> response_msg = err_msg <EOL> elif not resp . entity : <EOL> err_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( label = label , message = message ) <EOL> self . _log . error ( err_msg ) <EOL> response_msg = err_msg <EOL> else : <EOL> err_msg = '<STR_LIT>' <EOL> raise UnhandledMethodCaseException ( err_msg ) <EOL> return response_msg <EOL> def filter_entity_list_by_name ( self , entity_list , name ) : <EOL> """<STR_LIT>""" <EOL> new_entity_list = [ ] <EOL> starts_with_name = False <EOL> name = name . strip ( ) <EOL> if name [ - <NUM_LIT:1> ] == '<STR_LIT:*>' : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> starts_with_name = True <EOL> for entity in entity_list : <EOL> if entity . name == name : <EOL> new_entity_list . append ( entity ) <EOL> elif starts_with_name and entity . name . startswith ( name ) : <EOL> new_entity_list . append ( entity ) <EOL> return new_entity_list <EOL> def filter_entity_list_by_attr ( self , entity_list , attr , value ) : <EOL> """<STR_LIT>""" <EOL> new_entity_list = [ ] <EOL> for entity in entity_list : <EOL> if hasattr ( entity , attr ) : <EOL> attr_value = getattr ( entity , attr ) <EOL> if attr_value == value : <EOL> new_entity_list . append ( entity ) <EOL> return new_entity_list <EOL> def get_id_list_from_entity_list ( self , entity_list , name = None ) : <EOL> """<STR_LIT>""" <EOL> if name : <EOL> entity_list = self . filter_entity_list_by_name ( entity_list , name ) <EOL> id_list = [ entity . id for entity in entity_list ] <EOL> return id_list <EOL> def __over_limit_retry ( self , resource_type , timeout , poll_interval , <EOL> status_code , resp , fn_name , fn_kwargs ) : <EOL> """<STR_LIT>""" <EOL> endtime = time . time ( ) + int ( timeout ) <EOL> retry_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> timeout = timeout , resource_type = resource_type , <EOL> fn_name = fn_name ) <EOL> self . _log . info ( retry_msg ) <EOL> while ( resp . status_code == status_code and <EOL> time . time ( ) < endtime ) : <EOL> resp = getattr ( self . client , fn_name ) ( ** fn_kwargs ) <EOL> time . sleep ( poll_interval ) <EOL> return resp <EOL> def _create_resource ( self , resource , resource_build_attempts = None , <EOL> raise_exception = True , poll_interval = None , <EOL> has_name = True , use_exact_name = False , <EOL> attrs_kwargs = None , timeout = None , <EOL> use_over_limit_retry = False ) : <EOL> """<STR_LIT>""" <EOL> resource_type = resource . singular <EOL> name = attrs_kwargs . get ( '<STR_LIT:name>' ) <EOL> if has_name : <EOL> if name is None : <EOL> name = rand_name ( self . config . starts_with_name ) <EOL> elif not use_exact_name : <EOL> name = rand_name ( name ) <EOL> attrs_kwargs [ '<STR_LIT:name>' ] = name <EOL> else : <EOL> if name is None : <EOL> name = '<STR_LIT>' <EOL> poll_interval = poll_interval or self . config . api_poll_interval <EOL> resource_build_attempts = ( resource_build_attempts or <EOL> self . config . api_retries ) <EOL> use_over_limit_retry = ( use_over_limit_retry or <EOL> self . config . use_over_limit_retry ) <EOL> timeout = timeout or self . config . resource_create_timeout <EOL> result = NetworkingResponse ( ) <EOL> err_msg = '<STR_LIT>' . format ( resource_type ) <EOL> for attempt in range ( resource_build_attempts ) : <EOL> self . _log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> attempt_n = attempt + <NUM_LIT:1> , <EOL> attempts = resource_build_attempts , <EOL> resource_type = resource_type ) ) <EOL> create_fn_name = '<STR_LIT>' . format ( resource_type ) <EOL> resp = getattr ( self . client , create_fn_name ) ( ** attrs_kwargs ) <EOL> if use_over_limit_retry : <EOL> entity_too_large_status_code = ( getattr ( self . response_codes , <EOL> '<STR_LIT>' ) ) <EOL> if resp . status_code == entity_too_large_status_code : <EOL> fn_kwargs = attrs_kwargs <EOL> resp = self . __over_limit_retry ( <EOL> resource_type = resource_type , timeout = timeout , <EOL> poll_interval = poll_interval , <EOL> status_code = entity_too_large_status_code , <EOL> resp = resp , fn_name = create_fn_name , <EOL> fn_kwargs = fn_kwargs ) <EOL> response_code = create_fn_name . upper ( ) <EOL> status_code = getattr ( self . response_codes , response_code ) <EOL> resp_check = self . check_response ( <EOL> resp = resp , status_code = status_code , label = name , <EOL> message = err_msg ) <EOL> result . response = resp <EOL> if not resp_check : <EOL> return result <EOL> result . failures . append ( resp_check ) <EOL> time . sleep ( poll_interval ) <EOL> else : <EOL> err_msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> name = name , resource_type = resource_type , <EOL> attempts = resource_build_attempts , <EOL> failures = result . failures ) <EOL> self . _log . error ( err_msg ) <EOL> if raise_exception : <EOL> raise ResourceBuildException ( err_msg ) <EOL> return result <EOL> def _update_resource ( self , resource , resource_id , <EOL> resource_update_attempts = None , raise_exception = False , <EOL> poll_interval = None , attrs_kwargs = None , <EOL> timeout = None , use_over_limit_retry = False ) : <EOL> """<STR_LIT>""" <EOL> resource_type = resource . singular <EOL> poll_interval = poll_interval or self . config . api_poll_interval <EOL> resource_update_attempts = ( resource_update_attempts or <EOL> self . config . api_retries ) <EOL> use_over_limit_retry = ( use_over_limit_retry or <EOL> self . config . use_over_limit_retry ) <EOL> timeout = timeout or self . config . resource_update_timeout <EOL> result = NetworkingResponse ( ) <EOL> err_msg = '<STR_LIT>' . format ( resource_type ) <EOL> for attempt in range ( resource_update_attempts ) : <EOL> self . _log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( attempt_n = attempt + <NUM_LIT:1> , <EOL> attempts = resource_update_attempts , <EOL> resource_type = resource_type , <EOL> resource_id = resource_id ) ) <EOL> update_fn_name = '<STR_LIT>' . format ( resource_type ) <EOL> resp = getattr ( self . client , update_fn_name ) ( resource_id , <EOL> ** attrs_kwargs ) <EOL> if use_over_limit_retry : <EOL> entity_too_large_status_code = ( getattr ( self . response_codes , <EOL> '<STR_LIT>' ) ) <EOL> if resp . status_code == entity_too_large_status_code : <EOL> fn_kwargs = attrs_kwargs <EOL> resource_id_name = '<STR_LIT>' . format ( resource_type ) <EOL> fn_kwargs [ resource_id_name ] = resource_id <EOL> resp = self . __over_limit_retry ( <EOL> resource_type = resource_type , timeout = timeout , <EOL> poll_interval = poll_interval , <EOL> status_code = entity_too_large_status_code , <EOL> resp = resp , fn_name = update_fn_name , <EOL> fn_kwargs = fn_kwargs ) <EOL> response_code = update_fn_name . upper ( ) <EOL> status_code = getattr ( self . response_codes , response_code ) <EOL> resp_check = self . check_response ( <EOL> resp = resp , status_code = status_code , label = resource_id , <EOL> message = err_msg ) <EOL> result . response = resp <EOL> if not resp_check : <EOL> return result <EOL> result . failures . append ( resp_check ) <EOL> time . sleep ( poll_interval ) <EOL> else : <EOL> err_msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> resource_id = resource_id , resource_type = resource_type , <EOL> attempts = resource_update_attempts , <EOL> failures = result . failures ) <EOL> self . _log . error ( err_msg ) <EOL> if raise_exception : <EOL> raise ResourceUpdateException ( err_msg ) <EOL> return result <EOL> def _get_resource ( self , resource , resource_id , resource_get_attempts = None , <EOL> raise_exception = False , poll_interval = None , timeout = None , <EOL> use_over_limit_retry = False , fn_kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> resource_type = resource . singular <EOL> poll_interval = poll_interval or self . config . api_poll_interval <EOL> resource_get_attempts = ( resource_get_attempts or <EOL> self . config . api_retries ) <EOL> use_over_limit_retry = ( use_over_limit_retry or <EOL> self . config . use_over_limit_retry ) <EOL> timeout = timeout or self . config . resource_get_timeout <EOL> result = NetworkingResponse ( ) <EOL> err_msg = '<STR_LIT>' . format ( resource_type ) <EOL> for attempt in range ( resource_get_attempts ) : <EOL> self . _log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( attempt_n = attempt + <NUM_LIT:1> , <EOL> attempts = resource_get_attempts , <EOL> resource_type = resource_type , <EOL> resource_id = resource_id ) ) <EOL> fn_kwargs = fn_kwargs or { } <EOL> resource_id_name = '<STR_LIT>' . format ( resource_type ) <EOL> fn_kwargs [ resource_id_name ] = resource_id <EOL> get_fn_name = '<STR_LIT>' . format ( resource_type ) <EOL> resp = getattr ( self . client , get_fn_name ) ( ** fn_kwargs ) <EOL> if use_over_limit_retry : <EOL> entity_too_large_status_code = ( getattr ( self . response_codes , <EOL> '<STR_LIT>' ) ) <EOL> if resp . status_code == entity_too_large_status_code : <EOL> resp = self . __over_limit_retry ( <EOL> resource_type = resource_type , timeout = timeout , <EOL> poll_interval = poll_interval , <EOL> status_code = entity_too_large_status_code , <EOL> resp = resp , fn_name = get_fn_name , <EOL> fn_kwargs = fn_kwargs ) <EOL> response_code = get_fn_name . upper ( ) <EOL> status_code = getattr ( self . response_codes , response_code ) <EOL> resp_check = self . check_response ( <EOL> resp = resp , <EOL> status_code = status_code , <EOL> label = resource_id , <EOL> message = err_msg ) <EOL> result . response = resp <EOL> if not resp_check : <EOL> return result <EOL> result . failures . append ( resp_check ) <EOL> time . sleep ( poll_interval ) <EOL> else : <EOL> err_msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( resource_id = resource_id , <EOL> resource_type = resource_type , <EOL> attempts = resource_get_attempts , <EOL> failures = result . failures ) <EOL> self . _log . error ( err_msg ) <EOL> if raise_exception : <EOL> raise ResourceGetException ( err_msg ) <EOL> return result <EOL> def _list_resources ( self , resource , resource_list_attempts = None , <EOL> raise_exception = False , poll_interval = None , <EOL> params_kwargs = None , timeout = None , <EOL> use_over_limit_retry = False ) : <EOL> """<STR_LIT>""" <EOL> resource_type = resource . plural <EOL> poll_interval = poll_interval or self . config . api_poll_interval <EOL> resource_list_attempts = ( resource_list_attempts or <EOL> self . config . api_retries ) <EOL> use_over_limit_retry = ( use_over_limit_retry or <EOL> self . config . use_over_limit_retry ) <EOL> timeout = timeout or self . config . resource_get_timeout <EOL> result = NetworkingResponse ( ) <EOL> err_msg = '<STR_LIT>' . format ( resource_type ) <EOL> for attempt in range ( resource_list_attempts ) : <EOL> self . _log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT:list>' . format ( attempt_n = attempt + <NUM_LIT:1> , <EOL> attempts = resource_list_attempts , <EOL> resource_type = resource_type ) ) <EOL> list_fn_name = '<STR_LIT>' . format ( resource_type ) <EOL> resp = getattr ( self . client , list_fn_name ) ( ** params_kwargs ) <EOL> if use_over_limit_retry : <EOL> entity_too_large_status_code = ( getattr ( self . response_codes , <EOL> '<STR_LIT>' ) ) <EOL> if resp . status_code == entity_too_large_status_code : <EOL> fn_kwargs = params_kwargs <EOL> resp = self . __over_limit_retry ( <EOL> resource_type = resource_type , timeout = timeout , <EOL> poll_interval = poll_interval , <EOL> status_code = entity_too_large_status_code , <EOL> resp = resp , fn_name = list_fn_name , <EOL> fn_kwargs = fn_kwargs ) <EOL> response_code = list_fn_name . upper ( ) <EOL> status_code = getattr ( self . response_codes , response_code ) <EOL> resp_check = self . check_response ( <EOL> resp = resp , status_code = status_code , label = '<STR_LIT>' , message = err_msg ) <EOL> result . response = resp <EOL> if not resp_check : <EOL> return result <EOL> result . failures . append ( resp_check ) <EOL> time . sleep ( poll_interval ) <EOL> else : <EOL> err_msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( resource_type = resource_type , <EOL> attempts = resource_list_attempts , <EOL> failures = result . failures ) <EOL> self . _log . error ( err_msg ) <EOL> if raise_exception : <EOL> raise ResourceListException ( err_msg ) <EOL> return result <EOL> def _delete_resource ( self , resource , resource_id , <EOL> resource_delete_attempts = None , raise_exception = False , <EOL> poll_interval = None , timeout = None , <EOL> use_over_limit_retry = False ) : <EOL> """<STR_LIT>""" <EOL> resource_type = resource . singular <EOL> poll_interval = poll_interval or self . config . api_poll_interval <EOL> resource_delete_attempts = ( resource_delete_attempts or <EOL> self . config . api_retries ) <EOL> use_over_limit_retry = ( use_over_limit_retry or <EOL> self . config . use_over_limit_retry ) <EOL> timeout = timeout or self . config . resource_get_timeout <EOL> result = NetworkingResponse ( ) <EOL> for attempt in range ( resource_delete_attempts ) : <EOL> self . _log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( attempt_n = attempt + <NUM_LIT:1> , <EOL> attempts = resource_delete_attempts , <EOL> resource_type = resource_type , <EOL> resource_id = resource_id ) ) <EOL> delete_fn_name = '<STR_LIT>' . format ( resource_type ) <EOL> resp = getattr ( self . client , delete_fn_name ) ( resource_id ) <EOL> if use_over_limit_retry : <EOL> entity_too_large_status_code = ( getattr ( self . response_codes , <EOL> '<STR_LIT>' ) ) <EOL> if resp . status_code == entity_too_large_status_code : <EOL> fn_kwargs = { } <EOL> resource_id_name = '<STR_LIT>' . format ( resource_type ) <EOL> fn_kwargs [ resource_id_name ] = resource_id <EOL> resp = self . __over_limit_retry ( <EOL> resource_type = resource_type , timeout = timeout , <EOL> poll_interval = poll_interval , <EOL> status_code = entity_too_large_status_code , <EOL> resp = resp , fn_name = delete_fn_name , <EOL> fn_kwargs = fn_kwargs ) <EOL> result . response = resp <EOL> response_code = delete_fn_name . upper ( ) <EOL> status_code = getattr ( self . response_codes , response_code ) <EOL> if resp . ok and resp . status_code == status_code : <EOL> return result <EOL> err_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> resource_id = resource_id , <EOL> resource_type = resource_type , <EOL> expected_status = status_code , <EOL> status = resp . status_code , <EOL> reason = resp . reason , <EOL> content = resp . content ) <EOL> self . _log . error ( err_msg ) <EOL> result . failures . append ( err_msg ) <EOL> time . sleep ( poll_interval ) <EOL> else : <EOL> err_msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> resource_id = resource_id , resource_type = resource_type , <EOL> attempts = resource_delete_attempts , <EOL> failures = result . failures ) <EOL> self . _log . error ( err_msg ) <EOL> if raise_exception : <EOL> raise ResourceDeleteException ( err_msg ) <EOL> return result <EOL> def _delete_resources ( self , resource , resource_list = None , name = None , <EOL> tenant_id = None , skip_delete = None ) : <EOL> """<STR_LIT>""" <EOL> resource_type_singular = resource . singular <EOL> resource_type_plural = resource . plural <EOL> if resource_list is None : <EOL> list_fn_name = '<STR_LIT>' . format ( resource_type_plural ) <EOL> resp = getattr ( self , list_fn_name ) ( tenant_id = tenant_id ) <EOL> response_code = list_fn_name . upper ( ) <EOL> status_code = getattr ( self . response_codes , response_code ) <EOL> if resp . response . status_code != status_code : <EOL> get_msg = '<STR_LIT>' . format ( <EOL> resource_type_plural ) <EOL> self . _log . info ( get_msg ) <EOL> return None <EOL> resources = resp . response . entity <EOL> if tenant_id : <EOL> resources = self . filter_entity_list_by_attr ( <EOL> entity_list = resources , attr = '<STR_LIT>' , value = tenant_id ) <EOL> resource_list = self . get_id_list_from_entity_list ( <EOL> entity_list = resources , name = name ) <EOL> do_not_delete = [ ] <EOL> false_values = [ None , '<STR_LIT>' ] <EOL> if skip_delete is not None : <EOL> do_not_delete . extend ( skip_delete ) <EOL> if resource_type_plural == NeutronResource . NETWORKS : <EOL> property_list = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for prop in property_list : <EOL> if ( hasattr ( self . config , prop ) and <EOL> getattr ( self . config , prop ) not in false_values ) : <EOL> do_not_delete . append ( getattr ( self . config , prop ) ) <EOL> for resource_to_skip in do_not_delete : <EOL> if resource_to_skip in resource_list : <EOL> resource_list . remove ( resource_to_skip ) <EOL> log_msg = '<STR_LIT>' . format ( resource_type_plural , <EOL> resource_list ) <EOL> self . _log . info ( log_msg ) <EOL> failed_deletes = [ ] <EOL> delete_fn_name = '<STR_LIT>' . format ( resource_type_singular ) <EOL> for resource_id in resource_list : <EOL> result = getattr ( self , delete_fn_name ) ( resource_id ) <EOL> if result . failures : <EOL> failed_deletes . append ( result . failures ) <EOL> return failed_deletes <EOL> def _clean_resource ( self , resource , resource_id , timeout = None , <EOL> poll_interval = None ) : <EOL> """<STR_LIT>""" <EOL> resource_type = resource . singular <EOL> timeout = timeout or self . config . resource_delete_timeout <EOL> poll_interval = poll_interval or self . config . api_poll_interval <EOL> endtime = time . time ( ) + int ( timeout ) <EOL> log_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( resource_id = resource_id , <EOL> resource_type = resource_type , <EOL> timeout = timeout ) <EOL> self . _log . info ( log_msg ) <EOL> delete_fn_name = '<STR_LIT>' . format ( resource_type ) <EOL> get_fn_name = '<STR_LIT>' . format ( resource_type ) <EOL> resp = None <EOL> while time . time ( ) < endtime : <EOL> try : <EOL> getattr ( self . client , delete_fn_name ) ( resource_id ) <EOL> resp = getattr ( self . client , get_fn_name ) ( resource_id ) <EOL> except Exception as err : <EOL> err_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> resource_type = resource_type , error = err ) <EOL> self . _log . error ( err_msg ) <EOL> if ( resp is not None and <EOL> resp . status_code == NeutronResponseCodes . NOT_FOUND ) : <EOL> return None <EOL> time . sleep ( poll_interval ) <EOL> err_msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( resource_id = resource_id , <EOL> resource_type = resource_type , <EOL> timeout = timeout ) <EOL> self . _log . error ( err_msg ) <EOL> return resource_id <EOL> def _clean_resources ( self , resource , resource_list , timeout = None , <EOL> poll_interval = None ) : <EOL> """<STR_LIT>""" <EOL> resource_type_plural = resource . plural <EOL> log_msg = '<STR_LIT>' . format ( <EOL> resource_type = resource_type_plural , resource_list = resource_list ) <EOL> self . _log . info ( log_msg ) <EOL> undeleted_resources = [ ] <EOL> for resource_id in resource_list : <EOL> result = self . _clean_resource ( resource = resource , <EOL> resource_id = resource_id , <EOL> timeout = timeout , <EOL> poll_interval = poll_interval ) <EOL> if result : <EOL> undeleted_resources . append ( result ) <EOL> if undeleted_resources : <EOL> err_msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> resource_type = resource_type_plural , <EOL> undeleted_resources = undeleted_resources ) <EOL> self . _log . error ( err_msg ) <EOL> return undeleted_resources <EOL> class NetworkingResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . response = None <EOL> self . failures = list ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . response , <EOL> self . failures ) </s>
