<s> """<STR_LIT>""" <EOL> import time <EOL> from cafe . engine . behaviors import BaseBehavior <EOL> from cloudcafe . common . exceptions import BuildErrorException , TimeoutException <EOL> from cloudcafe . networking . lbaas . common . types import LBaaSStatusTypes <EOL> class BaseLoadBalancersBehaviors ( BaseBehavior ) : <EOL> def __init__ ( self , lbaas_client_type , config ) : <EOL> super ( BaseLoadBalancersBehaviors , self ) . __init__ ( ) <EOL> self . config = config <EOL> self . lbaas_client_type = lbaas_client_type <EOL> def create_active_lbaas_object ( self , lbaas_model_type , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> create_func = getattr ( self . lbaas_client_type , <EOL> "<STR_LIT>" . format ( lbaas_model_type ) ) <EOL> except AttributeError as ex : <EOL> error_message = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( type_ = lbaas_model_type , <EOL> message = ex . message ) ) <EOL> self . _log . error ( error_message ) <EOL> raise Exception ( error_message ) <EOL> else : <EOL> resp = create_func ( ** kwargs ) <EOL> assert resp . status_code == <NUM_LIT> <EOL> assert resp . entity is not None <EOL> lbaas_model_obj = resp . entity <EOL> resp = self . wait_for_lbaas_object_status ( <EOL> lbaas_object_id = lbaas_model_obj . entity . id , <EOL> lbaas_model_type = lbaas_model_type , <EOL> desired_status = LBaaSStatusTypes . ACTIVE ) <EOL> return resp <EOL> def update_lbaas_object_and_wait_for_active ( <EOL> self , lbaas_model_type , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> update_func = getattr ( self . lbaas_client_type , <EOL> "<STR_LIT>" . format ( lbaas_model_type ) ) <EOL> except AttributeError as ex : <EOL> error_message = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( type_ = lbaas_model_type , <EOL> message = ex . message ) ) <EOL> self . _log . error ( error_message ) <EOL> raise Exception ( error_message ) <EOL> else : <EOL> resp = update_func ( ** kwargs ) <EOL> assert resp . status_code == <NUM_LIT> <EOL> assert resp . entity is not None <EOL> lbaas_model_obj = resp . entity <EOL> resp = self . wait_for_lbaas_object_status ( <EOL> lbaas_object_id = lbaas_model_obj . entity . id , <EOL> lbaas_model_type = lbaas_model_type , <EOL> desired_status = LBaaSStatusTypes . ACTIVE ) <EOL> return resp <EOL> def wait_for_lbaas_object_status ( self , lbaas_object_id , lbaas_model_type , <EOL> desired_status , interval_time = None , <EOL> timeout = None ) : <EOL> """<STR_LIT>""" <EOL> interval_time = interval_time or self . config . lbaas_status_interval <EOL> timeout = timeout or self . config . lbaas_timeout <EOL> end_time = time . time ( ) + timeout <EOL> while time . time ( ) < end_time : <EOL> try : <EOL> get_func = getattr ( self . lbaas_client_type , <EOL> "<STR_LIT>" . format ( lbaas_model_type ) ) <EOL> except AttributeError as ex : <EOL> error_message = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( type_ = lbaas_model_type , <EOL> message = ex . message ) ) <EOL> self . _log . error ( error_message ) <EOL> raise Exception ( error_message ) <EOL> else : <EOL> resp = get_func ( lbaas_object_id ) <EOL> if not resp . ok : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( type_ = lbaas_model_type , <EOL> code = resp . status_code , <EOL> reason = resp . reason ) ) <EOL> if resp . entity is None : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( resp . content ) ) <EOL> lbaas_object = resp . entity <EOL> if lbaas_object . status . lower ( ) == LBaaSStatusTypes . ERROR . lower ( ) : <EOL> raise BuildErrorException ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( type_ = lbaas_model_type , <EOL> id_ = lbaas_object . id ) ) <EOL> if lbaas_object . status == desired_status : <EOL> break <EOL> time . sleep ( interval_time ) <EOL> else : <EOL> raise TimeoutException ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> type_ = lbaas_model_type , timeout = timeout , <EOL> id_ = lbaas_object_id , status = desired_status ) ) <EOL> return resp </s>
