<s> """<STR_LIT>""" <EOL> try : <EOL> import json <EOL> except : <EOL> import simplejson as json <EOL> from libcloud . common . base import ConnectionKey , Response <EOL> from libcloud . common . types import InvalidCredsError <EOL> from libcloud . compute . base import Node , NodeDriver , NodeLocation , NodeSize <EOL> from libcloud . compute . base import NodeImage <EOL> from libcloud . compute . types import Provider , NodeState <EOL> """<STR_LIT>""" <EOL> DH_PS_SIZES = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:15> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:200> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } , <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT:default>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1000> , <EOL> '<STR_LIT>' : <NUM_LIT:50> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } , <EOL> } <EOL> class DreamhostAPIException ( Exception ) : <EOL> def __str__ ( self ) : <EOL> return self . args [ <NUM_LIT:0> ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . args [ <NUM_LIT:0> ] ) <EOL> class DreamhostResponse ( Response ) : <EOL> """<STR_LIT>""" <EOL> def parse_body ( self ) : <EOL> resp = json . loads ( self . body ) <EOL> if resp [ '<STR_LIT:result>' ] != '<STR_LIT:success>' : <EOL> raise Exception ( self . _api_parse_error ( resp ) ) <EOL> return resp [ '<STR_LIT:data>' ] <EOL> def parse_error ( self ) : <EOL> raise Exception <EOL> def _api_parse_error ( self , response ) : <EOL> if '<STR_LIT:data>' in response : <EOL> if response [ '<STR_LIT:data>' ] == '<STR_LIT>' : <EOL> raise InvalidCredsError ( "<STR_LIT>" ) <EOL> else : <EOL> raise DreamhostAPIException ( response [ '<STR_LIT:data>' ] ) <EOL> else : <EOL> raise DreamhostAPIException ( "<STR_LIT>" % ( self . body ) ) <EOL> class DreamhostConnection ( ConnectionKey ) : <EOL> """<STR_LIT>""" <EOL> host = '<STR_LIT>' <EOL> responseCls = DreamhostResponse <EOL> format = '<STR_LIT>' <EOL> def add_default_params ( self , params ) : <EOL> """<STR_LIT>""" <EOL> params [ '<STR_LIT:key>' ] = self . key <EOL> params [ '<STR_LIT>' ] = self . format <EOL> return params <EOL> class DreamhostNodeDriver ( NodeDriver ) : <EOL> """<STR_LIT>""" <EOL> type = Provider . DREAMHOST <EOL> name = "<STR_LIT>" <EOL> connectionCls = DreamhostConnection <EOL> _sizes = DH_PS_SIZES <EOL> def create_node ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> size = kwargs [ '<STR_LIT:size>' ] . ram <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:type>' : kwargs [ '<STR_LIT:image>' ] . name , <EOL> '<STR_LIT:size>' : size <EOL> } <EOL> data = self . connection . request ( '<STR_LIT:/>' , params ) . object <EOL> return Node ( <EOL> id = data [ '<STR_LIT>' ] , <EOL> name = data [ '<STR_LIT>' ] , <EOL> state = NodeState . PENDING , <EOL> public_ip = [ ] , <EOL> private_ip = [ ] , <EOL> driver = self . connection . driver , <EOL> extra = { <EOL> '<STR_LIT:type>' : kwargs [ '<STR_LIT:image>' ] . name <EOL> } <EOL> ) <EOL> def destroy_node ( self , node ) : <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : node . id <EOL> } <EOL> try : <EOL> return self . connection . request ( '<STR_LIT:/>' , params ) . success ( ) <EOL> except DreamhostAPIException : <EOL> return False <EOL> def reboot_node ( self , node ) : <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : node . id <EOL> } <EOL> try : <EOL> return self . connection . request ( '<STR_LIT:/>' , params ) . success ( ) <EOL> except DreamhostAPIException : <EOL> return False <EOL> def list_nodes ( self , ** kwargs ) : <EOL> data = self . connection . request ( '<STR_LIT:/>' , { '<STR_LIT>' : '<STR_LIT>' } ) . object <EOL> return [ self . _to_node ( n ) for n in data ] <EOL> def list_images ( self , ** kwargs ) : <EOL> data = self . connection . request ( '<STR_LIT:/>' , { '<STR_LIT>' : '<STR_LIT>' } ) . object <EOL> images = [ ] <EOL> for img in data : <EOL> images . append ( NodeImage ( <EOL> id = img [ '<STR_LIT:image>' ] , <EOL> name = img [ '<STR_LIT:image>' ] , <EOL> driver = self . connection . driver <EOL> ) ) <EOL> return images <EOL> def list_sizes ( self , ** kwargs ) : <EOL> return [ NodeSize ( driver = self . connection . driver , ** i ) <EOL> for i in self . _sizes . values ( ) ] <EOL> def list_locations ( self , ** kwargs ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def _resize_node ( self , node , size ) : <EOL> if ( size < <NUM_LIT> or size > <NUM_LIT> ) : <EOL> return False <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : node . id , <EOL> '<STR_LIT:size>' : size <EOL> } <EOL> try : <EOL> return self . connection . request ( '<STR_LIT:/>' , params ) . success ( ) <EOL> except DreamhostAPIException : <EOL> return False <EOL> def _to_node ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return Node ( <EOL> id = data [ '<STR_LIT>' ] , <EOL> name = data [ '<STR_LIT>' ] , <EOL> state = NodeState . UNKNOWN , <EOL> public_ip = [ data [ '<STR_LIT>' ] ] , <EOL> private_ip = [ ] , <EOL> driver = self . connection . driver , <EOL> extra = { <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:type>' : data [ '<STR_LIT:type>' ] <EOL> } <EOL> ) </s>
