<s> """<STR_LIT>""" <EOL> import time <EOL> from libcloud . utils . py3 import httplib <EOL> from libcloud . utils . py3 import urlencode <EOL> from libcloud . common . base import ConnectionKey , JsonResponse <EOL> from libcloud . compute . types import Provider , NodeState <EOL> from libcloud . common . types import LibcloudError , InvalidCredsError <EOL> from libcloud . compute . base import NodeDriver <EOL> from libcloud . compute . base import Node , NodeImage , NodeSize , NodeLocation <EOL> class VultrResponse ( JsonResponse ) : <EOL> def parse_error ( self ) : <EOL> if self . status == httplib . OK : <EOL> body = self . parse_body ( ) <EOL> return body <EOL> elif self . status == httplib . FORBIDDEN : <EOL> raise InvalidCredsError ( self . body ) <EOL> else : <EOL> raise LibcloudError ( self . body ) <EOL> class SSHKey ( object ) : <EOL> def __init__ ( self , id , name , pub_key ) : <EOL> self . id = id <EOL> self . name = name <EOL> self . pub_key = pub_key <EOL> def __repr__ ( self ) : <EOL> return ( ( '<STR_LIT>' ) % <EOL> ( self . id , self . name , self . pub_key ) ) <EOL> class VultrConnection ( ConnectionKey ) : <EOL> """<STR_LIT>""" <EOL> host = '<STR_LIT>' <EOL> responseCls = VultrResponse <EOL> def add_default_params ( self , params ) : <EOL> """<STR_LIT>""" <EOL> params [ '<STR_LIT>' ] = self . key <EOL> return params <EOL> def encode_data ( self , data ) : <EOL> return urlencode ( data ) <EOL> def get ( self , url ) : <EOL> return self . request ( url ) <EOL> def post ( self , url , data ) : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> return self . request ( url , data = data , headers = headers , method = '<STR_LIT:POST>' ) <EOL> class VultrNodeDriver ( NodeDriver ) : <EOL> """<STR_LIT>""" <EOL> connectionCls = VultrConnection <EOL> type = Provider . VULTR <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> NODE_STATE_MAP = { '<STR_LIT>' : NodeState . PENDING , <EOL> '<STR_LIT>' : NodeState . RUNNING } <EOL> def list_nodes ( self ) : <EOL> return self . _list_resources ( '<STR_LIT>' , self . _to_node ) <EOL> def list_key_pairs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _list_resources ( '<STR_LIT>' , self . _to_ssh_key ) <EOL> def list_locations ( self ) : <EOL> return self . _list_resources ( '<STR_LIT>' , self . _to_location ) <EOL> def list_sizes ( self ) : <EOL> return self . _list_resources ( '<STR_LIT>' , self . _to_size ) <EOL> def list_images ( self ) : <EOL> return self . _list_resources ( '<STR_LIT>' , self . _to_image ) <EOL> def create_node ( self , name , size , image , location , ex_ssh_key_ids = None ) : <EOL> params = { '<STR_LIT>' : location . id , '<STR_LIT>' : size . id , <EOL> '<STR_LIT>' : image . id , '<STR_LIT:label>' : name } <EOL> if ex_ssh_key_ids is not None : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( ex_ssh_key_ids ) <EOL> result = self . connection . post ( '<STR_LIT>' , params ) <EOL> if result . status != httplib . OK : <EOL> return False <EOL> subid = result . object [ '<STR_LIT>' ] <EOL> retry_count = <NUM_LIT:3> <EOL> created_node = None <EOL> for i in range ( retry_count ) : <EOL> try : <EOL> nodes = self . list_nodes ( ) <EOL> created_node = [ n for n in nodes if n . id == subid ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> pass <EOL> else : <EOL> break <EOL> return created_node <EOL> def reboot_node ( self , node ) : <EOL> params = { '<STR_LIT>' : node . id } <EOL> res = self . connection . post ( '<STR_LIT>' , params ) <EOL> return res . status == httplib . OK <EOL> def destroy_node ( self , node ) : <EOL> params = { '<STR_LIT>' : node . id } <EOL> res = self . connection . post ( '<STR_LIT>' , params ) <EOL> return res . status == httplib . OK <EOL> def _list_resources ( self , url , tranform_func ) : <EOL> data = self . connection . get ( url ) . object <EOL> sorted_key = sorted ( data ) <EOL> return [ tranform_func ( data [ key ] ) for key in sorted_key ] <EOL> def _to_node ( self , data ) : <EOL> if '<STR_LIT:status>' in data : <EOL> state = self . NODE_STATE_MAP . get ( data [ '<STR_LIT:status>' ] , NodeState . UNKNOWN ) <EOL> if state == NodeState . RUNNING and data [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> state = NodeState . STOPPED <EOL> else : <EOL> state = NodeState . UNKNOWN <EOL> if '<STR_LIT>' in data and data [ '<STR_LIT>' ] is not None : <EOL> public_ips = [ data [ '<STR_LIT>' ] ] <EOL> else : <EOL> public_ips = [ ] <EOL> extra_keys = [ ] <EOL> extra = { } <EOL> for key in extra_keys : <EOL> if key in data : <EOL> extra [ key ] = data [ key ] <EOL> node = Node ( id = data [ '<STR_LIT>' ] , name = data [ '<STR_LIT:label>' ] , state = state , <EOL> public_ips = public_ips , private_ips = None , extra = extra , <EOL> driver = self ) <EOL> return node <EOL> def _to_location ( self , data ) : <EOL> return NodeLocation ( id = data [ '<STR_LIT>' ] , name = data [ '<STR_LIT:name>' ] , <EOL> country = data [ '<STR_LIT>' ] , driver = self ) <EOL> def _to_size ( self , data ) : <EOL> extra = { '<STR_LIT>' : int ( data [ '<STR_LIT>' ] ) } <EOL> ram = int ( data [ '<STR_LIT>' ] ) <EOL> disk = int ( data [ '<STR_LIT>' ] ) <EOL> bandwidth = float ( data [ '<STR_LIT>' ] ) <EOL> price = float ( data [ '<STR_LIT>' ] ) <EOL> return NodeSize ( id = data [ '<STR_LIT>' ] , name = data [ '<STR_LIT:name>' ] , <EOL> ram = ram , disk = disk , <EOL> bandwidth = bandwidth , price = price , <EOL> extra = extra , driver = self ) <EOL> def _to_image ( self , data ) : <EOL> extra = { '<STR_LIT>' : data [ '<STR_LIT>' ] , '<STR_LIT>' : data [ '<STR_LIT>' ] } <EOL> return NodeImage ( id = data [ '<STR_LIT>' ] , name = data [ '<STR_LIT:name>' ] , extra = extra , <EOL> driver = self ) <EOL> def _to_ssh_key ( self , data ) : <EOL> return SSHKey ( id = data [ '<STR_LIT>' ] , name = data [ '<STR_LIT:name>' ] , <EOL> pub_key = data [ '<STR_LIT>' ] ) </s>
