<s> from libcloud . common . cloudstack import CloudStackDriverMixIn <EOL> from libcloud . loadbalancer . base import LoadBalancer , Member , Driver , Algorithm <EOL> from libcloud . loadbalancer . base import DEFAULT_ALGORITHM <EOL> from libcloud . loadbalancer . types import Provider <EOL> from libcloud . loadbalancer . types import State <EOL> from libcloud . utils . misc import reverse_dict <EOL> class CloudStackLBDriver ( CloudStackDriverMixIn , Driver ) : <EOL> """<STR_LIT>""" <EOL> api_name = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> type = Provider . CLOUDSTACK <EOL> _VALUE_TO_ALGORITHM_MAP = { <EOL> '<STR_LIT>' : Algorithm . ROUND_ROBIN , <EOL> '<STR_LIT>' : Algorithm . LEAST_CONNECTIONS <EOL> } <EOL> _ALGORITHM_TO_VALUE_MAP = reverse_dict ( _VALUE_TO_ALGORITHM_MAP ) <EOL> LB_STATE_MAP = { <EOL> '<STR_LIT>' : State . RUNNING , <EOL> } <EOL> def __init__ ( self , key , secret = None , secure = True , host = None , <EOL> path = None , port = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> host = host if host else self . host <EOL> path = path if path else self . path <EOL> if path is not None : <EOL> self . path = path <EOL> if host is not None : <EOL> self . host = host <EOL> if ( self . type == Provider . CLOUDSTACK ) and ( not host or not path ) : <EOL> raise Exception ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> super ( CloudStackLBDriver , self ) . __init__ ( key = key , secret = secret , <EOL> secure = secure , <EOL> host = host , port = port ) <EOL> def list_protocols ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' ] <EOL> def list_balancers ( self ) : <EOL> balancers = self . _sync_request ( command = '<STR_LIT>' , <EOL> method = '<STR_LIT:GET>' ) <EOL> balancers = balancers . get ( '<STR_LIT>' , [ ] ) <EOL> return [ self . _to_balancer ( balancer ) for balancer in balancers ] <EOL> def get_balancer ( self , balancer_id ) : <EOL> balancer = self . _sync_request ( command = '<STR_LIT>' , <EOL> params = { '<STR_LIT:id>' : balancer_id } , <EOL> method = '<STR_LIT:GET>' ) <EOL> balancer = balancer . get ( '<STR_LIT>' , [ ] ) <EOL> if not balancer : <EOL> raise Exception ( "<STR_LIT>" + str ( balancer_id ) ) <EOL> return self . _to_balancer ( balancer [ <NUM_LIT:0> ] ) <EOL> def create_balancer ( self , name , members , protocol = '<STR_LIT:http>' , port = <NUM_LIT> , <EOL> algorithm = DEFAULT_ALGORITHM , location = None , <EOL> private_port = None , network_id = None , vpc_id = None ) : <EOL> """<STR_LIT>""" <EOL> args = { } <EOL> ip_args = { } <EOL> if location is None : <EOL> locations = self . _sync_request ( command = '<STR_LIT>' , method = '<STR_LIT:GET>' ) <EOL> location = locations [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> else : <EOL> location = location . id <EOL> if private_port is None : <EOL> private_port = port <EOL> if network_id is not None : <EOL> args [ '<STR_LIT>' ] = network_id <EOL> ip_args [ '<STR_LIT>' ] = network_id <EOL> if vpc_id is not None : <EOL> ip_args [ '<STR_LIT>' ] = vpc_id <EOL> ip_args . update ( { '<STR_LIT>' : location , <EOL> '<STR_LIT>' : network_id , <EOL> '<STR_LIT>' : vpc_id } ) <EOL> result = self . _async_request ( command = '<STR_LIT>' , <EOL> params = ip_args , <EOL> method = '<STR_LIT:GET>' ) <EOL> public_ip = result [ '<STR_LIT>' ] <EOL> args . update ( { '<STR_LIT>' : self . _ALGORITHM_TO_VALUE_MAP [ algorithm ] , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : private_port , <EOL> '<STR_LIT>' : port , <EOL> '<STR_LIT>' : public_ip [ '<STR_LIT:id>' ] } ) <EOL> result = self . _sync_request ( <EOL> command = '<STR_LIT>' , <EOL> params = args , <EOL> method = '<STR_LIT:GET>' ) <EOL> listbalancers = self . _sync_request ( <EOL> command = '<STR_LIT>' , <EOL> params = args , <EOL> method = '<STR_LIT:GET>' ) <EOL> listbalancers = [ rule for rule in listbalancers [ '<STR_LIT>' ] if <EOL> rule [ '<STR_LIT:id>' ] == result [ '<STR_LIT:id>' ] ] <EOL> if len ( listbalancers ) != <NUM_LIT:1> : <EOL> return None <EOL> balancer = self . _to_balancer ( listbalancers [ <NUM_LIT:0> ] ) <EOL> for member in members : <EOL> balancer . attach_member ( member ) <EOL> return balancer <EOL> def destroy_balancer ( self , balancer ) : <EOL> self . _async_request ( command = '<STR_LIT>' , <EOL> params = { '<STR_LIT:id>' : balancer . id } , <EOL> method = '<STR_LIT:GET>' ) <EOL> self . _async_request ( command = '<STR_LIT>' , <EOL> params = { '<STR_LIT:id>' : balancer . ex_public_ip_id } , <EOL> method = '<STR_LIT:GET>' ) <EOL> def balancer_attach_member ( self , balancer , member ) : <EOL> member . port = balancer . ex_private_port <EOL> self . _async_request ( command = '<STR_LIT>' , <EOL> params = { '<STR_LIT:id>' : balancer . id , <EOL> '<STR_LIT>' : member . id } , <EOL> method = '<STR_LIT:GET>' ) <EOL> return True <EOL> def balancer_detach_member ( self , balancer , member ) : <EOL> self . _async_request ( command = '<STR_LIT>' , <EOL> params = { '<STR_LIT:id>' : balancer . id , <EOL> '<STR_LIT>' : member . id } , <EOL> method = '<STR_LIT:GET>' ) <EOL> return True <EOL> def balancer_list_members ( self , balancer ) : <EOL> members = self . _sync_request ( command = '<STR_LIT>' , <EOL> params = { '<STR_LIT:id>' : balancer . id } , <EOL> method = '<STR_LIT:GET>' ) <EOL> members = members [ '<STR_LIT>' ] <EOL> return [ self . _to_member ( m , balancer . ex_private_port , balancer ) <EOL> for m in members ] <EOL> def _to_balancer ( self , obj ) : <EOL> balancer = LoadBalancer ( <EOL> id = obj [ '<STR_LIT:id>' ] , <EOL> name = obj [ '<STR_LIT:name>' ] , <EOL> state = self . LB_STATE_MAP . get ( obj [ '<STR_LIT:state>' ] , State . UNKNOWN ) , <EOL> ip = obj [ '<STR_LIT>' ] , <EOL> port = obj [ '<STR_LIT>' ] , <EOL> driver = self . connection . driver <EOL> ) <EOL> balancer . ex_private_port = obj [ '<STR_LIT>' ] <EOL> balancer . ex_public_ip_id = obj [ '<STR_LIT>' ] <EOL> return balancer <EOL> def _to_member ( self , obj , port , balancer ) : <EOL> return Member ( <EOL> id = obj [ '<STR_LIT:id>' ] , <EOL> ip = obj [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> port = port , <EOL> balancer = balancer <EOL> ) </s>
