<s> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> import sys <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from libcloud . common . aliyun import AliyunXmlResponse , SignedAliyunConnection <EOL> from libcloud . common . types import LibcloudError <EOL> from libcloud . loadbalancer . types import State <EOL> from libcloud . loadbalancer . base import Algorithm , Driver , LoadBalancer , Member <EOL> from libcloud . utils . misc import ReprMixin <EOL> from libcloud . utils . py3 import u <EOL> from libcloud . utils . xml import findattr , findtext , findall <EOL> SLB_API_VERSION = '<STR_LIT>' <EOL> SLB_API_HOST = '<STR_LIT>' <EOL> DEFAULT_SIGNATURE_VERSION = '<STR_LIT:1.0>' <EOL> STATE_MAPPINGS = { <EOL> '<STR_LIT>' : State . UNKNOWN , <EOL> '<STR_LIT>' : State . RUNNING , <EOL> '<STR_LIT>' : State . PENDING <EOL> } <EOL> RESOURCE_EXTRA_ATTRIBUTES_MAP = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : int <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u <EOL> } <EOL> } <EOL> } <EOL> SLB_SCHEDULER_TO_ALGORITHM = { <EOL> '<STR_LIT>' : Algorithm . WEIGHTED_ROUND_ROBIN , <EOL> '<STR_LIT>' : Algorithm . WEIGHTED_LEAST_CONNECTIONS <EOL> } <EOL> ALGORITHM_TO_SLB_SCHEDULER = { <EOL> Algorithm . WEIGHTED_ROUND_ROBIN : '<STR_LIT>' , <EOL> Algorithm . WEIGHTED_LEAST_CONNECTIONS : '<STR_LIT>' <EOL> } <EOL> class SLBConnection ( SignedAliyunConnection ) : <EOL> version = SLB_API_VERSION <EOL> host = SLB_API_HOST <EOL> responseCls = AliyunXmlResponse <EOL> service_name = '<STR_LIT>' <EOL> class SLBLoadBalancerAttribute ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , balancer , listeners , backend_servers , extra = None ) : <EOL> self . balancer = balancer <EOL> self . listeners = listeners or [ ] <EOL> self . backend_servers = backend_servers or [ ] <EOL> self . extra = extra or { } <EOL> def is_listening ( self , port ) : <EOL> for listener in self . listeners : <EOL> if listener . get ( '<STR_LIT>' ) == port : <EOL> return True <EOL> return False <EOL> def is_attached ( self , member ) : <EOL> for server in self . backend_servers : <EOL> if server . get ( '<STR_LIT>' ) == member . id : <EOL> return True <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return ( '<STR_LIT>' % <EOL> ( self . balancer . id , self . listeners , self . backend_servers ) ) <EOL> class SLBLoadBalancerListener ( ReprMixin , object ) : <EOL> """<STR_LIT>""" <EOL> _repr_attributes = [ '<STR_LIT:port>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> action = None <EOL> option_keys = [ ] <EOL> def __init__ ( self , port , backend_port , algorithm , bandwidth , extra = None ) : <EOL> self . port = port <EOL> self . backend_port = backend_port <EOL> self . scheduler = ALGORITHM_TO_SLB_SCHEDULER . get ( algorithm , '<STR_LIT>' ) <EOL> self . bandwidth = bandwidth <EOL> self . extra = extra or { } <EOL> @ classmethod <EOL> def create ( cls , port , backend_port , algorithm , bandwidth , extra = None ) : <EOL> return cls ( port , backend_port , algorithm , bandwidth , extra = extra ) <EOL> def get_create_params ( self ) : <EOL> params = self . get_required_params ( ) <EOL> options = self . get_optional_params ( ) <EOL> options . update ( params ) <EOL> return options <EOL> def get_required_params ( self ) : <EOL> params = { '<STR_LIT>' : self . action , <EOL> '<STR_LIT>' : self . port , <EOL> '<STR_LIT>' : self . backend_port , <EOL> '<STR_LIT>' : self . scheduler , <EOL> '<STR_LIT>' : self . bandwidth } <EOL> return params <EOL> def get_optional_params ( self ) : <EOL> options = { } <EOL> for option in self . option_keys : <EOL> if self . extra and option in self . extra : <EOL> options [ option ] = self . extra [ option ] <EOL> return options <EOL> class SLBLoadBalancerHttpListener ( SLBLoadBalancerListener ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' <EOL> option_keys = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , port , backend_port , algorithm , bandwidth , <EOL> sticky_session , health_check , extra = None ) : <EOL> super ( SLBLoadBalancerHttpListener , self ) . __init__ ( <EOL> port , backend_port , algorithm , bandwidth , extra = extra ) <EOL> self . sticky_session = sticky_session <EOL> self . health_check = health_check <EOL> def get_required_params ( self ) : <EOL> params = super ( SLBLoadBalancerHttpListener , <EOL> self ) . get_required_params ( ) <EOL> params [ '<STR_LIT>' ] = self . sticky_session <EOL> params [ '<STR_LIT>' ] = self . health_check <EOL> return params <EOL> @ classmethod <EOL> def create ( cls , port , backend_port , algorithm , bandwidth , extra = { } ) : <EOL> if '<STR_LIT>' not in extra : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in extra : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> sticky_session = extra [ '<STR_LIT>' ] <EOL> health_check = extra [ '<STR_LIT>' ] <EOL> return cls ( port , backend_port , algorithm , bandwidth , sticky_session , <EOL> health_check , extra = extra ) <EOL> class SLBLoadBalancerHttpsListener ( SLBLoadBalancerListener ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' <EOL> option_keys = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , port , backend_port , algorithm , bandwidth , <EOL> sticky_session , health_check , certificate_id , extra = None ) : <EOL> super ( SLBLoadBalancerHttpsListener , self ) . __init__ ( <EOL> port , backend_port , algorithm , bandwidth , extra = extra ) <EOL> self . sticky_session = sticky_session <EOL> self . health_check = health_check <EOL> self . certificate_id = certificate_id <EOL> def get_required_params ( self ) : <EOL> params = super ( SLBLoadBalancerHttpsListener , <EOL> self ) . get_required_params ( ) <EOL> params [ '<STR_LIT>' ] = self . sticky_session <EOL> params [ '<STR_LIT>' ] = self . health_check <EOL> params [ '<STR_LIT>' ] = self . certificate_id <EOL> return params <EOL> @ classmethod <EOL> def create ( cls , port , backend_port , algorithm , bandwidth , extra = { } ) : <EOL> if '<STR_LIT>' not in extra : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in extra : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in extra : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> sticky_session = extra [ '<STR_LIT>' ] <EOL> health_check = extra [ '<STR_LIT>' ] <EOL> certificate_id = extra [ '<STR_LIT>' ] <EOL> return cls ( port , backend_port , algorithm , bandwidth , sticky_session , <EOL> health_check , certificate_id , extra = extra ) <EOL> class SLBLoadBalancerTcpListener ( SLBLoadBalancerListener ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' <EOL> option_keys = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> class SLBLoadBalancerUdpListener ( SLBLoadBalancerTcpListener ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' <EOL> option_keys = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> class SLBServerCertificate ( ReprMixin , object ) : <EOL> _repr_attributes = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> def __init__ ( self , id , name , fingerprint ) : <EOL> self . id = id <EOL> self . name = name <EOL> self . fingerprint = fingerprint <EOL> PROTOCOL_TO_LISTENER_MAP = { <EOL> '<STR_LIT:http>' : SLBLoadBalancerHttpListener , <EOL> '<STR_LIT>' : SLBLoadBalancerHttpsListener , <EOL> '<STR_LIT>' : SLBLoadBalancerTcpListener , <EOL> '<STR_LIT>' : SLBLoadBalancerUdpListener <EOL> } <EOL> class SLBDriver ( Driver ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> connectionCls = SLBConnection <EOL> path = '<STR_LIT:/>' <EOL> namespace = None <EOL> _VALUE_TO_ALGORITHM_MAP = SLB_SCHEDULER_TO_ALGORITHM <EOL> _ALGORITHM_TO_VALUE_MAP = ALGORITHM_TO_SLB_SCHEDULER <EOL> def __init__ ( self , access_id , secret , region ) : <EOL> super ( SLBDriver , self ) . __init__ ( access_id , secret ) <EOL> self . region = region <EOL> def list_protocols ( self ) : <EOL> return list ( PROTOCOL_TO_LISTENER_MAP . keys ( ) ) <EOL> def list_balancers ( self , ex_balancer_ids = None , ex_filters = None ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . region } <EOL> if ex_balancer_ids and isinstance ( ex_balancer_ids , list ) : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( ex_balancer_ids ) <EOL> if ex_filters and isinstance ( ex_filters , dict ) : <EOL> ex_filters . update ( params ) <EOL> params = ex_filters <EOL> resp_body = self . connection . request ( self . path , params = params ) . object <EOL> return self . _to_balancers ( resp_body ) <EOL> def create_balancer ( self , name , port , protocol , algorithm , members , <EOL> ex_bandwidth = None , ex_internet_charge_type = None , <EOL> ex_address_type = None , ex_vswitch_id = None , <EOL> ex_master_zone_id = None , ex_slave_zone_id = None , <EOL> ex_client_token = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . region } <EOL> if name : <EOL> params [ '<STR_LIT>' ] = name <EOL> if not port : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> if not protocol : <EOL> protocol = '<STR_LIT:http>' <EOL> if protocol not in PROTOCOL_TO_LISTENER_MAP : <EOL> raise AttributeError ( '<STR_LIT>' % protocol ) <EOL> bandwidth = - <NUM_LIT:1> <EOL> if ex_bandwidth : <EOL> try : <EOL> bandwidth = int ( ex_bandwidth ) <EOL> except ValueError : <EOL> raise AttributeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params [ '<STR_LIT>' ] = bandwidth <EOL> if ex_internet_charge_type : <EOL> if ex_internet_charge_type . lower ( ) == '<STR_LIT>' : <EOL> if bandwidth == - <NUM_LIT:1> : <EOL> raise AttributeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params [ '<STR_LIT>' ] = ex_internet_charge_type <EOL> if ex_address_type : <EOL> if ex_address_type . lower ( ) not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise AttributeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params [ '<STR_LIT>' ] = ex_address_type <EOL> if ex_vswitch_id : <EOL> params [ '<STR_LIT>' ] = ex_vswitch_id <EOL> if ex_master_zone_id : <EOL> params [ '<STR_LIT>' ] = ex_master_zone_id <EOL> if ex_slave_zone_id : <EOL> params [ '<STR_LIT>' ] = ex_slave_zone_id <EOL> if ex_client_token : <EOL> params [ '<STR_LIT>' ] = ex_client_token <EOL> if members and isinstance ( members , list ) : <EOL> backend_ports = [ member . port for member in members ] <EOL> if len ( set ( backend_ports ) ) != <NUM_LIT:1> : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> backend_port = backend_ports [ <NUM_LIT:0> ] or port <EOL> else : <EOL> backend_port = port <EOL> balancer = None <EOL> try : <EOL> resp_body = self . connection . request ( self . path , params ) . object <EOL> balancer = self . _to_balancer ( resp_body ) <EOL> balancer . port = port <EOL> if members is None : <EOL> members = [ ] <EOL> for member in members : <EOL> self . balancer_attach_member ( balancer , member ) <EOL> self . ex_create_listener ( balancer , backend_port , protocol , <EOL> algorithm , bandwidth , ** kwargs ) <EOL> self . ex_start_listener ( balancer , port ) <EOL> return balancer <EOL> except Exception : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if balancer is not None : <EOL> try : <EOL> self . destroy_balancer ( balancer ) <EOL> except Exception : <EOL> pass <EOL> raise e <EOL> def destroy_balancer ( self , balancer ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : balancer . id } <EOL> resp = self . connection . request ( self . path , params ) <EOL> return resp . success ( ) <EOL> def get_balancer ( self , balancer_id ) : <EOL> balancers = self . list_balancers ( ex_balancer_ids = [ balancer_id ] ) <EOL> if len ( balancers ) != <NUM_LIT:1> : <EOL> raise LibcloudError ( '<STR_LIT>' % <EOL> balancer_id ) <EOL> return balancers [ <NUM_LIT:0> ] <EOL> def balancer_attach_compute_node ( self , balancer , node ) : <EOL> if len ( node . public_ips ) > <NUM_LIT:0> : <EOL> ip = node . public_ips [ <NUM_LIT:0> ] <EOL> else : <EOL> ip = node . private_ips [ <NUM_LIT:0> ] <EOL> member = Member ( id = node . id , ip = ip , port = balancer . port ) <EOL> return self . balancer_attach_member ( balancer , member ) <EOL> def balancer_attach_member ( self , balancer , member ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : balancer . id } <EOL> if member and isinstance ( member , Member ) : <EOL> params [ '<STR_LIT>' ] = self . _to_servers_json ( [ member ] ) <EOL> self . connection . request ( self . path , params ) <EOL> return member <EOL> def balancer_detach_member ( self , balancer , member ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : balancer . id } <EOL> if member and isinstance ( member , Member ) : <EOL> params [ '<STR_LIT>' ] = self . _list_to_json ( [ member . id ] ) <EOL> self . connection . request ( self . path , params ) <EOL> return member <EOL> def balancer_list_members ( self , balancer ) : <EOL> attribute = self . ex_get_balancer_attribute ( balancer ) <EOL> members = [ Member ( server [ '<STR_LIT>' ] , None , None , balancer = balancer , <EOL> extra = { '<STR_LIT>' : server [ '<STR_LIT>' ] } ) <EOL> for server in attribute . backend_servers ] <EOL> return members <EOL> def ex_get_balancer_attribute ( self , balancer ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : balancer . id } <EOL> resp_body = self . connection . request ( self . path , params ) . object <EOL> attribute = self . _to_balancer_attribute ( resp_body ) <EOL> return attribute <EOL> def ex_list_listeners ( self , balancer ) : <EOL> """<STR_LIT>""" <EOL> attribute = self . ex_get_balancer_attribute ( balancer ) <EOL> listeners = [ SLBLoadBalancerListener ( each [ '<STR_LIT>' ] , None , <EOL> None , None ) <EOL> for each in attribute . listeners ] <EOL> return listeners <EOL> def ex_create_listener ( self , balancer , backend_port , protocol , algorithm , <EOL> bandwidth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cls = PROTOCOL_TO_LISTENER_MAP . get ( protocol , <EOL> SLBLoadBalancerHttpListener ) <EOL> if '<STR_LIT>' in kwargs : <EOL> port = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> port = balancer . port <EOL> listener = cls . create ( port , backend_port , algorithm , <EOL> bandwidth , extra = kwargs ) <EOL> params = listener . get_create_params ( ) <EOL> params [ '<STR_LIT>' ] = balancer . id <EOL> params [ '<STR_LIT>' ] = self . region <EOL> resp = self . connection . request ( self . path , params ) <EOL> return resp . success ( ) <EOL> def ex_start_listener ( self , balancer , port ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : balancer . id , <EOL> '<STR_LIT>' : port } <EOL> resp = self . connection . request ( self . path , params ) <EOL> return resp . success ( ) <EOL> def ex_stop_listener ( self , balancer , port ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : balancer . id , <EOL> '<STR_LIT>' : port } <EOL> resp = self . connection . request ( self . path , params ) <EOL> return resp . success ( ) <EOL> def ex_upload_certificate ( self , name , server_certificate , <EOL> private_key ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . region , <EOL> '<STR_LIT>' : server_certificate , <EOL> '<STR_LIT>' : private_key } <EOL> if name : <EOL> params [ '<STR_LIT>' ] = name <EOL> resp_body = self . connection . request ( self . path , params ) . object <EOL> return self . _to_server_certificate ( resp_body ) <EOL> def ex_list_certificates ( self , certificate_ids = [ ] ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . region } <EOL> if certificate_ids and isinstance ( certificate_ids , list ) : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( certificate_ids ) <EOL> resp_body = self . connection . request ( self . path , params ) . object <EOL> cert_elements = findall ( resp_body , <EOL> '<STR_LIT>' , <EOL> namespace = self . namespace ) <EOL> certificates = [ self . _to_server_certificate ( el ) <EOL> for el in cert_elements ] <EOL> return certificates <EOL> def ex_delete_certificate ( self , certificate_id ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . region , <EOL> '<STR_LIT>' : certificate_id } <EOL> resp = self . connection . request ( self . path , params ) <EOL> return resp . success ( ) <EOL> def ex_set_certificate_name ( self , certificate_id , name ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . region , <EOL> '<STR_LIT>' : certificate_id , <EOL> '<STR_LIT>' : name } <EOL> resp = self . connection . request ( self . path , params ) <EOL> return resp . success ( ) <EOL> def _to_balancers ( self , element ) : <EOL> xpath = '<STR_LIT>' <EOL> return [ self . _to_balancer ( el ) <EOL> for el in findall ( element = element , xpath = xpath , <EOL> namespace = self . namespace ) ] <EOL> def _to_balancer ( self , el ) : <EOL> _id = findtext ( element = el , xpath = '<STR_LIT>' , <EOL> namespace = self . namespace ) <EOL> name = findtext ( element = el , xpath = '<STR_LIT>' , <EOL> namespace = self . namespace ) <EOL> status = findtext ( element = el , xpath = '<STR_LIT>' , <EOL> namespace = self . namespace ) <EOL> state = STATE_MAPPINGS . get ( status , State . UNKNOWN ) <EOL> address = findtext ( element = el , xpath = '<STR_LIT>' , <EOL> namespace = self . namespace ) <EOL> extra = self . _get_extra_dict ( <EOL> el , RESOURCE_EXTRA_ATTRIBUTES_MAP [ '<STR_LIT>' ] ) <EOL> balancer = LoadBalancer ( id = _id , name = name , state = state , ip = address , <EOL> port = None , driver = self , extra = extra ) <EOL> return balancer <EOL> def _create_list_params ( self , params , items , label ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( items , str ) : <EOL> items = [ items ] <EOL> for index , item in enumerate ( items ) : <EOL> params [ label % ( index + <NUM_LIT:1> ) ] = item <EOL> return params <EOL> def _get_extra_dict ( self , element , mapping ) : <EOL> """<STR_LIT>""" <EOL> extra = { } <EOL> for attribute , values in mapping . items ( ) : <EOL> transform_func = values [ '<STR_LIT>' ] <EOL> value = findattr ( element = element , <EOL> xpath = values [ '<STR_LIT>' ] , <EOL> namespace = self . namespace ) <EOL> if value : <EOL> try : <EOL> extra [ attribute ] = transform_func ( value ) <EOL> except Exception : <EOL> extra [ attribute ] = None <EOL> else : <EOL> extra [ attribute ] = value <EOL> return extra <EOL> def _to_servers_json ( self , members ) : <EOL> servers = [ ] <EOL> for each in members : <EOL> server = { '<STR_LIT>' : each . id , <EOL> '<STR_LIT>' : '<STR_LIT:100>' } <EOL> if '<STR_LIT>' in each . extra : <EOL> server [ '<STR_LIT>' ] = each . extra [ '<STR_LIT>' ] <EOL> servers . append ( server ) <EOL> try : <EOL> return json . dumps ( servers ) <EOL> except Exception : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> def _to_balancer_attribute ( self , element ) : <EOL> balancer = self . _to_balancer ( element ) <EOL> port_proto_elements = findall ( <EOL> element , '<STR_LIT>' , <EOL> namespace = self . namespace ) <EOL> if len ( port_proto_elements ) > <NUM_LIT:0> : <EOL> listeners = [ self . _to_port_and_protocol ( el ) <EOL> for el in port_proto_elements ] <EOL> else : <EOL> port_elements = findall ( element , '<STR_LIT>' , <EOL> namespace = self . namespace ) <EOL> listeners = [ { '<STR_LIT>' : el . text , '<STR_LIT>' : '<STR_LIT:http>' } <EOL> for el in port_elements ] <EOL> server_elements = findall ( element , <EOL> '<STR_LIT>' , <EOL> namespace = self . namespace ) <EOL> backend_servers = [ self . _to_server_and_weight ( el ) <EOL> for el in server_elements ] <EOL> return SLBLoadBalancerAttribute ( balancer , listeners , backend_servers ) <EOL> def _to_port_and_protocol ( self , el ) : <EOL> port = findtext ( el , '<STR_LIT>' , namespace = self . namespace ) <EOL> protocol = findtext ( el , '<STR_LIT>' , namespace = self . namespace ) <EOL> return { '<STR_LIT>' : port , '<STR_LIT>' : protocol } <EOL> def _to_server_and_weight ( self , el ) : <EOL> server_id = findtext ( el , '<STR_LIT>' , namespace = self . namespace ) <EOL> weight = findtext ( el , '<STR_LIT>' , namespace = self . namespace ) <EOL> return { '<STR_LIT>' : server_id , '<STR_LIT>' : weight } <EOL> def _to_server_certificate ( self , el ) : <EOL> _id = findtext ( el , '<STR_LIT>' , namespace = self . namespace ) <EOL> name = findtext ( el , '<STR_LIT>' , namespace = self . namespace ) <EOL> fingerprint = findtext ( el , '<STR_LIT>' , namespace = self . namespace ) <EOL> return SLBServerCertificate ( id = _id , name = name , <EOL> fingerprint = fingerprint ) <EOL> def _list_to_json ( self , value ) : <EOL> try : <EOL> return json . dumps ( value ) <EOL> except Exception : <EOL> return '<STR_LIT>' </s>
