<s> """<STR_LIT>""" <EOL> from libcloud . utils . py3 import httplib <EOL> from libcloud . utils . py3 import b <EOL> from libcloud . common . brightbox import BrightboxConnection <EOL> from libcloud . compute . types import Provider , NodeState <EOL> from libcloud . compute . base import NodeDriver <EOL> from libcloud . compute . base import Node , NodeImage , NodeSize , NodeLocation <EOL> import base64 <EOL> API_VERSION = '<STR_LIT:1.0>' <EOL> def _extract ( d , keys ) : <EOL> return dict ( ( k , d [ k ] ) for k in keys if k in d and d [ k ] is not None ) <EOL> class BrightboxNodeDriver ( NodeDriver ) : <EOL> """<STR_LIT>""" <EOL> connectionCls = BrightboxConnection <EOL> type = Provider . BRIGHTBOX <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> NODE_STATE_MAP = { '<STR_LIT>' : NodeState . PENDING , <EOL> '<STR_LIT>' : NodeState . RUNNING , <EOL> '<STR_LIT>' : NodeState . UNKNOWN , <EOL> '<STR_LIT>' : NodeState . UNKNOWN , <EOL> '<STR_LIT>' : NodeState . TERMINATED , <EOL> '<STR_LIT>' : NodeState . UNKNOWN , <EOL> '<STR_LIT>' : NodeState . UNKNOWN } <EOL> def __init__ ( self , key , secret = None , secure = True , host = None , port = None , <EOL> api_version = API_VERSION , ** kwargs ) : <EOL> super ( BrightboxNodeDriver , self ) . __init__ ( key = key , secret = secret , <EOL> secure = secure , <EOL> host = host , port = port , <EOL> api_version = api_version , <EOL> ** kwargs ) <EOL> def _to_node ( self , data ) : <EOL> extra_data = _extract ( data , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> extra_data [ '<STR_LIT>' ] = self . _to_location ( data [ '<STR_LIT>' ] ) <EOL> ipv6_addresses = [ interface [ '<STR_LIT>' ] for interface <EOL> in data [ '<STR_LIT>' ] if '<STR_LIT>' in interface ] <EOL> private_ips = [ interface [ '<STR_LIT>' ] <EOL> for interface in data [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in interface ] <EOL> public_ips = [ cloud_ip [ '<STR_LIT>' ] for cloud_ip in data [ '<STR_LIT>' ] ] <EOL> public_ips += ipv6_addresses <EOL> return Node ( <EOL> id = data [ '<STR_LIT:id>' ] , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> state = self . NODE_STATE_MAP [ data [ '<STR_LIT:status>' ] ] , <EOL> private_ips = private_ips , <EOL> public_ips = public_ips , <EOL> driver = self . connection . driver , <EOL> size = self . _to_size ( data [ '<STR_LIT>' ] ) , <EOL> image = self . _to_image ( data [ '<STR_LIT:image>' ] ) , <EOL> extra = extra_data <EOL> ) <EOL> def _to_image ( self , data ) : <EOL> extra_data = _extract ( data , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:description>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:source>' , <EOL> '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT:username>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if data . get ( '<STR_LIT>' , None ) : <EOL> extra_data [ '<STR_LIT>' ] = self . _to_image ( data [ '<STR_LIT>' ] ) <EOL> return NodeImage ( <EOL> id = data [ '<STR_LIT:id>' ] , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> driver = self , <EOL> extra = extra_data <EOL> ) <EOL> def _to_size ( self , data ) : <EOL> return NodeSize ( <EOL> id = data [ '<STR_LIT:id>' ] , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> ram = data [ '<STR_LIT>' ] , <EOL> disk = data [ '<STR_LIT>' ] , <EOL> bandwidth = <NUM_LIT:0> , <EOL> price = <NUM_LIT:0> , <EOL> driver = self <EOL> ) <EOL> def _to_location ( self , data ) : <EOL> if data : <EOL> return NodeLocation ( <EOL> id = data [ '<STR_LIT:id>' ] , <EOL> name = data [ '<STR_LIT>' ] , <EOL> country = '<STR_LIT>' , <EOL> driver = self <EOL> ) <EOL> else : <EOL> return None <EOL> def _post ( self , path , data = { } ) : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> return self . connection . request ( path , data = data , headers = headers , <EOL> method = '<STR_LIT:POST>' ) <EOL> def _put ( self , path , data = { } ) : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> return self . connection . request ( path , data = data , headers = headers , <EOL> method = '<STR_LIT>' ) <EOL> def create_node ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT:name>' : kwargs [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : kwargs [ '<STR_LIT:size>' ] . id , <EOL> '<STR_LIT:image>' : kwargs [ '<STR_LIT:image>' ] . id , <EOL> } <EOL> if '<STR_LIT>' in kwargs : <EOL> data [ '<STR_LIT>' ] = base64 . b64encode ( b ( kwargs [ '<STR_LIT>' ] ) ) . decode ( '<STR_LIT:ascii>' ) <EOL> if '<STR_LIT:location>' in kwargs : <EOL> data [ '<STR_LIT>' ] = kwargs [ '<STR_LIT:location>' ] . id <EOL> if '<STR_LIT>' in kwargs : <EOL> if not isinstance ( kwargs [ '<STR_LIT>' ] , list ) : <EOL> kwargs [ '<STR_LIT>' ] = [ kwargs [ '<STR_LIT>' ] ] <EOL> data [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> data = self . _post ( '<STR_LIT>' % self . api_version , data ) . object <EOL> return self . _to_node ( data ) <EOL> def destroy_node ( self , node ) : <EOL> response = self . connection . request ( <EOL> '<STR_LIT>' % ( self . api_version , node . id ) , <EOL> method = '<STR_LIT>' ) <EOL> return response . status == httplib . ACCEPTED <EOL> def list_nodes ( self ) : <EOL> data = self . connection . request ( '<STR_LIT>' % self . api_version ) . object <EOL> return list ( map ( self . _to_node , data ) ) <EOL> def list_images ( self , location = None ) : <EOL> data = self . connection . request ( '<STR_LIT>' % self . api_version ) . object <EOL> return list ( map ( self . _to_image , data ) ) <EOL> def list_sizes ( self ) : <EOL> data = self . connection . request ( '<STR_LIT>' % self . api_version ) . object <EOL> return list ( map ( self . _to_size , data ) ) <EOL> def list_locations ( self ) : <EOL> data = self . connection . request ( '<STR_LIT>' % self . api_version ) . object <EOL> return list ( map ( self . _to_location , data ) ) <EOL> def ex_list_cloud_ips ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . request ( '<STR_LIT>' % self . api_version ) . object <EOL> def ex_create_cloud_ip ( self , reverse_dns = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if reverse_dns : <EOL> params [ '<STR_LIT>' ] = reverse_dns <EOL> return self . _post ( '<STR_LIT>' % self . api_version , params ) . object <EOL> def ex_update_cloud_ip ( self , cloud_ip_id , reverse_dns ) : <EOL> """<STR_LIT>""" <EOL> response = self . _put ( '<STR_LIT>' % ( self . api_version , <EOL> cloud_ip_id ) , <EOL> { '<STR_LIT>' : reverse_dns } ) <EOL> return response . status == httplib . OK <EOL> def ex_map_cloud_ip ( self , cloud_ip_id , interface_id ) : <EOL> """<STR_LIT>""" <EOL> response = self . _post ( '<STR_LIT>' % ( self . api_version , <EOL> cloud_ip_id ) , <EOL> { '<STR_LIT>' : interface_id } ) <EOL> return response . status == httplib . ACCEPTED <EOL> def ex_unmap_cloud_ip ( self , cloud_ip_id ) : <EOL> """<STR_LIT>""" <EOL> response = self . _post ( '<STR_LIT>' % ( self . api_version , <EOL> cloud_ip_id ) ) <EOL> return response . status == httplib . ACCEPTED <EOL> def ex_destroy_cloud_ip ( self , cloud_ip_id ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . request ( <EOL> '<STR_LIT>' % ( self . api_version , <EOL> cloud_ip_id ) , <EOL> method = '<STR_LIT>' ) <EOL> return response . status == httplib . OK </s>
