<s> """<STR_LIT>""" <EOL> from datetime import datetime , timedelta <EOL> from httplib import BadStatusLine <EOL> from requests . exceptions import ConnectionError <EOL> from time import sleep <EOL> from cloudcafe . common . tools import time <EOL> from cloudcafe . cloudkeep . common . responses import CloudkeepResponse <EOL> class OrdersBehavior ( object ) : <EOL> def __init__ ( self , orders_client , secrets_client , config ) : <EOL> super ( OrdersBehavior , self ) . __init__ ( ) <EOL> self . orders_client = orders_client <EOL> self . secrets_client = secrets_client <EOL> self . config = config <EOL> self . created_orders = [ ] <EOL> def _wait_for_order_to_activate ( self , create_resp ) : <EOL> """<STR_LIT>""" <EOL> if not create_resp . entity or not create_resp . entity . reference : <EOL> return <EOL> stop_time = datetime . now ( ) + timedelta ( seconds = <NUM_LIT:30> ) <EOL> order_ref = create_resp . entity . reference <EOL> order_id = CloudkeepResponse . get_id_from_ref ( order_ref ) <EOL> while datetime . now ( ) < stop_time : <EOL> order = self . orders_client . get_order ( order_id ) . entity <EOL> if not order : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if order . status == '<STR_LIT>' : <EOL> return <EOL> sleep ( <NUM_LIT:0.1> ) <EOL> def create_and_check_order ( self , name = None , payload_content_type = None , <EOL> algorithm = None , bit_length = None , mode = None ) : <EOL> """<STR_LIT>""" <EOL> resp = self . create_order_overriding_cfg ( <EOL> name = name , algorithm = algorithm , bit_length = bit_length , <EOL> mode = mode , payload_content_type = payload_content_type ) <EOL> get_order_resp = self . orders_client . get_order ( order_id = resp . id ) <EOL> behavior_response = CloudkeepResponse ( resp = resp . create_resp , <EOL> get_resp = get_order_resp ) <EOL> return behavior_response <EOL> def create_order_from_config ( self , use_expiration = False ) : <EOL> """<STR_LIT>""" <EOL> expiration = None <EOL> if use_expiration : <EOL> expiration = time . get_tomorrow_timestamp ( ) <EOL> resp = self . create_order ( <EOL> name = self . config . name , <EOL> payload_content_type = self . config . payload_content_type , <EOL> algorithm = self . config . algorithm , <EOL> bit_length = self . config . bit_length , <EOL> mode = self . config . mode , <EOL> expiration = expiration , <EOL> order_type = '<STR_LIT:key>' ) <EOL> return resp <EOL> def create_order_overriding_cfg ( <EOL> self , name = None , expiration = None , algorithm = None , bit_length = None , <EOL> payload_content_type = None , payload_content_encoding = None , <EOL> mode = None , headers = None , order_type = '<STR_LIT:key>' ) : <EOL> """<STR_LIT>""" <EOL> content_type = payload_content_type or self . config . payload_content_type <EOL> resp = self . create_order ( <EOL> name = name or self . config . name , <EOL> payload_content_type = content_type , <EOL> payload_content_encoding = payload_content_encoding , <EOL> algorithm = algorithm or self . config . algorithm , <EOL> bit_length = bit_length or self . config . bit_length , <EOL> mode = mode or self . config . mode , <EOL> expiration = expiration , <EOL> order_type = order_type , <EOL> headers = headers ) <EOL> return resp <EOL> def create_order ( <EOL> self , name = None , algorithm = None , bit_length = None , mode = None , <EOL> payload_content_type = None , payload_content_encoding = None , <EOL> expiration = None , headers = None , order_type = '<STR_LIT:key>' ) : <EOL> try : <EOL> resp = self . orders_client . create_order ( <EOL> name = name , <EOL> payload_content_type = payload_content_type , <EOL> payload_content_encoding = payload_content_encoding , <EOL> algorithm = algorithm , <EOL> bit_length = bit_length , <EOL> mode = mode , <EOL> expiration = expiration , <EOL> order_type = order_type , <EOL> headers = headers ) <EOL> except ConnectionError as e : <EOL> if type ( e . message . reason ) is BadStatusLine : <EOL> return { '<STR_LIT>' : <NUM_LIT:0> } <EOL> raise e <EOL> self . _wait_for_order_to_activate ( resp ) <EOL> behavior_response = CloudkeepResponse ( resp = resp ) <EOL> order_id = behavior_response . id <EOL> if order_id is not None : <EOL> self . created_orders . append ( behavior_response . id ) <EOL> return behavior_response <EOL> def create_order_w_payload ( <EOL> self , name = None , algorithm = None , bit_length = None , mode = None , <EOL> payload_content_type = None , payload_content_encoding = None , <EOL> expiration = None , payload = None , order_type = '<STR_LIT:key>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> resp = self . orders_client . create_order_w_payload ( <EOL> name = name or self . config . name , <EOL> algorithm = algorithm or self . config . algorithm , <EOL> bit_length = bit_length or self . config . bit_length , <EOL> mode = mode or self . config . mode , <EOL> expiration = expiration , <EOL> payload_content_type = payload_content_type , <EOL> payload_content_encoding = payload_content_encoding , <EOL> payload = payload , <EOL> order_type = order_type ) <EOL> except ConnectionError as e : <EOL> if type ( e . message . reason ) is BadStatusLine : <EOL> return { '<STR_LIT>' : <NUM_LIT:0> } <EOL> raise e <EOL> self . _wait_for_order_to_activate ( resp ) <EOL> behavior_response = CloudkeepResponse ( resp = resp ) <EOL> order_id = behavior_response . id <EOL> if order_id is not None : <EOL> self . created_orders . append ( behavior_response . id ) <EOL> return behavior_response <EOL> def delete_order ( self , order_id , delete_secret = True ) : <EOL> if delete_secret : <EOL> resp = self . orders_client . get_order ( order_id ) <EOL> order = resp . entity <EOL> assert resp . status_code == <NUM_LIT:200> , ( <EOL> '<STR_LIT>' . format ( id = order_id ) ) <EOL> secret_id = order . get_secret_id ( ) <EOL> self . secrets_client . delete_secret ( secret_id ) <EOL> resp = self . orders_client . delete_order ( order_id ) <EOL> if order_id in self . created_orders : <EOL> self . created_orders . remove ( order_id ) <EOL> return resp <EOL> def delete_all_orders_in_db ( self ) : <EOL> order_group = self . orders_client . get_orders ( ) . entity <EOL> found_ids = [ ] <EOL> found_ids . extend ( order_group . get_ids ( ) ) <EOL> while order_group . next is not None : <EOL> query = order_group . get_next_query_data ( ) <EOL> order_group = self . orders_client . get_orders ( <EOL> limit = query [ '<STR_LIT>' ] , <EOL> offset = query [ '<STR_LIT>' ] ) . entity <EOL> found_ids . extend ( order_group . get_ids ( ) ) <EOL> for order_id in found_ids : <EOL> self . delete_order ( order_id ) <EOL> def delete_all_created_orders_and_secrets ( self ) : <EOL> for order_id in list ( self . created_orders ) : <EOL> self . delete_order ( order_id , delete_secret = True ) <EOL> self . created_orders = [ ] <EOL> def remove_from_created_orders ( self , order_id ) : <EOL> if order_id in self . created_orders : <EOL> self . created_orders . remove ( order_id ) <EOL> def find_order ( self , order_id ) : <EOL> order_group = self . orders_client . get_orders ( ) . entity <EOL> ids = order_group . get_ids ( ) <EOL> while order_id not in ids and order_group . next is not None : <EOL> query = order_group . get_next_query_data ( ) <EOL> order_group = self . orders_client . get_orders ( <EOL> limit = query [ '<STR_LIT>' ] , <EOL> offset = query [ '<STR_LIT>' ] ) . entity <EOL> ids = order_group . get_ids ( ) <EOL> for order in order_group . orders : <EOL> if order . get_id ( ) == order_id : <EOL> return order <EOL> else : <EOL> return None </s>
