<s> import httplib , urllib <EOL> from zope import interface <EOL> from libcloud . interface import IConnectionUserAndKey , IResponse <EOL> from libcloud . interface import IConnectionKey , IConnectionKeyFactory <EOL> from libcloud . interface import IConnectionUserAndKeyFactory , IResponseFactory <EOL> from libcloud . interface import INodeDriverFactory , INodeDriver <EOL> from libcloud . interface import INodeFactory , INode <EOL> from libcloud . interface import INodeSizeFactory , INodeSize <EOL> from libcloud . interface import INodeImageFactory , INodeImage <EOL> import hashlib <EOL> class Node ( object ) : <EOL> """<STR_LIT>""" <EOL> interface . implements ( INode ) <EOL> interface . classProvides ( INodeFactory ) <EOL> def __init__ ( self , id , name , state , public_ip , private_ip , driver ) : <EOL> self . id = id <EOL> self . name = name <EOL> self . state = state <EOL> self . public_ip = public_ip <EOL> self . private_ip = private_ip <EOL> self . driver = driver <EOL> self . uuid = self . get_uuid ( ) <EOL> def get_uuid ( self ) : <EOL> return hashlib . sha1 ( "<STR_LIT>" % ( self . id , self . driver . type ) ) . hexdigest ( ) <EOL> def reboot ( self ) : <EOL> return self . driver . reboot_node ( self ) <EOL> def destroy ( self ) : <EOL> return self . driver . destroy_node ( self ) <EOL> def __repr__ ( self ) : <EOL> return ( ( '<STR_LIT>' ) <EOL> % ( self . uuid , self . name , self . state , self . public_ip , self . driver . name ) ) <EOL> class NodeSize ( object ) : <EOL> """<STR_LIT>""" <EOL> interface . implements ( INodeSize ) <EOL> interface . classProvides ( INodeSizeFactory ) <EOL> def __init__ ( self , id , name , ram , disk , bandwidth , price , driver ) : <EOL> self . id = id <EOL> self . name = name <EOL> self . ram = ram <EOL> self . disk = disk <EOL> self . bandwidth = bandwidth <EOL> self . price = price <EOL> self . driver = driver <EOL> def __repr__ ( self ) : <EOL> return ( ( '<STR_LIT>' ) <EOL> % ( self . id , self . name , self . ram , self . disk , self . bandwidth , self . price , self . driver . name ) ) <EOL> class NodeImage ( object ) : <EOL> """<STR_LIT>""" <EOL> interface . implements ( INodeImage ) <EOL> interface . classProvides ( INodeImageFactory ) <EOL> def __init__ ( self , id , name , driver ) : <EOL> self . id = id <EOL> self . name = name <EOL> self . driver = driver <EOL> def __repr__ ( self ) : <EOL> return ( ( '<STR_LIT>' ) <EOL> % ( self . id , self . name , self . driver . name ) ) <EOL> class Response ( object ) : <EOL> """<STR_LIT>""" <EOL> interface . implements ( IResponse ) <EOL> interface . classProvides ( IResponseFactory ) <EOL> NODE_STATE_MAP = { } <EOL> object = None <EOL> body = None <EOL> status_code = httplib . OK <EOL> headers = { } <EOL> error = None <EOL> connection = None <EOL> def __init__ ( self , response ) : <EOL> self . body = response . read ( ) <EOL> self . status = response . status <EOL> self . headers = dict ( response . getheaders ( ) ) <EOL> self . error = response . reason <EOL> if not self . success ( ) : <EOL> raise Exception ( self . parse_error ( ) ) <EOL> self . object = self . parse_body ( ) <EOL> def parse_body ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . body <EOL> def parse_error ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . body <EOL> def success ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . status == httplib . OK or self . status == httplib . CREATED <EOL> class ConnectionKey ( object ) : <EOL> """<STR_LIT>""" <EOL> interface . implementsOnly ( IConnectionKey ) <EOL> interface . classProvides ( IConnectionKeyFactory ) <EOL> conn_classes = ( httplib . HTTPConnection , httplib . HTTPSConnection ) <EOL> responseCls = Response <EOL> connection = None <EOL> host = '<STR_LIT:127.0.0.1>' <EOL> port = ( <NUM_LIT> , <NUM_LIT> ) <EOL> secure = <NUM_LIT:1> <EOL> driver = None <EOL> def __init__ ( self , key , secure = True ) : <EOL> """<STR_LIT>""" <EOL> self . key = key <EOL> self . secure = secure and <NUM_LIT:1> or <NUM_LIT:0> <EOL> def connect ( self , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> host = host or self . host <EOL> port = port or self . port [ self . secure ] <EOL> connection = self . conn_classes [ self . secure ] ( host , port ) <EOL> self . connection = connection <EOL> def request ( self , action , params = { } , data = '<STR_LIT>' , headers = { } , method = '<STR_LIT:GET>' ) : <EOL> """<STR_LIT>""" <EOL> params = self . add_default_params ( params ) <EOL> headers = self . add_default_headers ( headers ) <EOL> headers . update ( { '<STR_LIT>' : len ( data ) } ) <EOL> headers . update ( { '<STR_LIT>' : '<STR_LIT>' % ( self . driver . name ) } ) <EOL> headers . update ( { '<STR_LIT>' : self . host } ) <EOL> if data != '<STR_LIT>' : <EOL> data = self . encode_data ( data ) <EOL> url = '<STR_LIT:?>' . join ( ( action , urllib . urlencode ( params ) ) ) <EOL> self . connect ( ) <EOL> self . connection . request ( method = method , url = url , body = data , <EOL> headers = headers ) <EOL> response = self . responseCls ( self . connection . getresponse ( ) ) <EOL> response . connection = self <EOL> return response <EOL> def add_default_params ( self , params ) : <EOL> """<STR_LIT>""" <EOL> return params <EOL> def add_default_headers ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> return headers <EOL> def encode_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return data <EOL> class ConnectionUserAndKey ( ConnectionKey ) : <EOL> """<STR_LIT>""" <EOL> interface . implementsOnly ( IConnectionUserAndKey ) <EOL> interface . classProvides ( IConnectionUserAndKey ) <EOL> user_id = None <EOL> def __init__ ( self , user_id , key , secure = True ) : <EOL> super ( ConnectionUserAndKey , self ) . __init__ ( key , secure ) <EOL> self . user_id = user_id <EOL> class NodeDriver ( object ) : <EOL> """<STR_LIT>""" <EOL> interface . implements ( INodeDriver ) <EOL> interface . classProvides ( INodeDriverFactory ) <EOL> connectionCls = ConnectionKey <EOL> name = None <EOL> type = None <EOL> NODE_STATE_MAP = { } <EOL> def __init__ ( self , key , secret = None , secure = True ) : <EOL> self . key = key <EOL> self . secret = secret <EOL> self . secure = secure <EOL> if self . secret : <EOL> self . connection = self . connectionCls ( key , secret , secure ) <EOL> else : <EOL> self . connection = self . connectionCls ( key , secure ) <EOL> self . connection . driver = self <EOL> self . connection . connect ( ) <EOL> def create_node ( self , name , image , size , ** kwargs ) : <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def destroy_node ( self , node ) : <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def reboot_node ( self , node ) : <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def list_nodes ( self ) : <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def list_images ( self ) : <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def list_sizes ( self ) : <EOL> raise NotImplementedError , '<STR_LIT>' </s>
