<s> """<STR_LIT>""" <EOL> from libcloud . common . runabove import API_ROOT , RunAboveConnection <EOL> from libcloud . compute . base import NodeDriver , NodeSize , Node , NodeLocation <EOL> from libcloud . compute . base import NodeImage , StorageVolume <EOL> from libcloud . compute . types import Provider , StorageVolumeState <EOL> from libcloud . compute . drivers . openstack import OpenStackNodeDriver <EOL> from libcloud . compute . drivers . openstack import OpenStackKeyPair <EOL> class RunAboveNodeDriver ( NodeDriver ) : <EOL> """<STR_LIT>""" <EOL> type = Provider . RUNABOVE <EOL> name = "<STR_LIT>" <EOL> website = '<STR_LIT>' <EOL> connectionCls = RunAboveConnection <EOL> features = { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> api_name = '<STR_LIT>' <EOL> NODE_STATE_MAP = OpenStackNodeDriver . NODE_STATE_MAP <EOL> VOLUME_STATE_MAP = OpenStackNodeDriver . VOLUME_STATE_MAP <EOL> def __init__ ( self , key , secret , ex_consumer_key = None ) : <EOL> """<STR_LIT>""" <EOL> self . datacenter = None <EOL> self . consumer_key = ex_consumer_key <EOL> NodeDriver . __init__ ( self , key , secret , ex_consumer_key = ex_consumer_key ) <EOL> def list_nodes ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = { } <EOL> if location : <EOL> data [ '<STR_LIT>' ] = location . id <EOL> response = self . connection . request ( action , data = data ) <EOL> return self . _to_nodes ( response . object ) <EOL> def ex_get_node ( self , node_id ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' + node_id <EOL> response = self . connection . request ( action , method = '<STR_LIT:GET>' ) <EOL> return self . _to_node ( response . object ) <EOL> def create_node ( self , name , image , size , location , ex_keyname = None ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : image . id , <EOL> '<STR_LIT>' : size . id , <EOL> '<STR_LIT>' : location . id , <EOL> } <EOL> if ex_keyname : <EOL> data [ '<STR_LIT>' ] = ex_keyname <EOL> response = self . connection . request ( action , data = data , method = '<STR_LIT:POST>' ) <EOL> return self . _to_node ( response . object ) <EOL> def destroy_node ( self , node ) : <EOL> action = API_ROOT + '<STR_LIT>' + node . id <EOL> self . connection . request ( action , method = '<STR_LIT>' ) <EOL> return True <EOL> def list_sizes ( self , location = None ) : <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = { } <EOL> if location : <EOL> data [ '<STR_LIT>' ] = location . id <EOL> response = self . connection . request ( action , data = data ) <EOL> return self . _to_sizes ( response . object ) <EOL> def ex_get_size ( self , size_id ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' + size_id <EOL> response = self . connection . request ( action ) <EOL> return self . _to_size ( response . object ) <EOL> def list_images ( self , location = None , ex_size = None ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = { } <EOL> if location : <EOL> data [ '<STR_LIT>' ] = location . id <EOL> if ex_size : <EOL> data [ '<STR_LIT>' ] = ex_size . id <EOL> response = self . connection . request ( action , data = data ) <EOL> return self . _to_images ( response . object ) <EOL> def get_image ( self , image_id ) : <EOL> action = API_ROOT + '<STR_LIT>' + image_id <EOL> response = self . connection . request ( action ) <EOL> return self . _to_image ( response . object ) <EOL> def list_locations ( self ) : <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = self . connection . request ( action ) <EOL> return self . _to_locations ( data . object ) <EOL> def list_key_pairs ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = { } <EOL> if location : <EOL> data [ '<STR_LIT>' ] = location . id <EOL> response = self . connection . request ( action , data = data ) <EOL> return self . _to_key_pairs ( response . object ) <EOL> def get_key_pair ( self , name , location ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' + name <EOL> data = { '<STR_LIT>' : location . id } <EOL> response = self . connection . request ( action , data = data ) <EOL> return self . _to_key_pair ( response . object ) <EOL> def import_key_pair_from_string ( self , name , key_material , location ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = { '<STR_LIT:name>' : name , '<STR_LIT>' : key_material , '<STR_LIT>' : location . id } <EOL> response = self . connection . request ( action , data = data , method = '<STR_LIT:POST>' ) <EOL> return self . _to_key_pair ( response . object ) <EOL> def delete_key_pair ( self , name , location ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' + name <EOL> data = { '<STR_LIT:name>' : name , '<STR_LIT>' : location . id } <EOL> self . connection . request ( action , data = data , method = '<STR_LIT>' ) <EOL> return True <EOL> def create_volume ( self , size , location , name = None , <EOL> ex_volume_type = '<STR_LIT>' , ex_description = None ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : location . id , <EOL> '<STR_LIT:size>' : str ( size ) , <EOL> '<STR_LIT:type>' : ex_volume_type , <EOL> } <EOL> if name : <EOL> data [ '<STR_LIT:name>' ] = name <EOL> if ex_description : <EOL> data [ '<STR_LIT:description>' ] = ex_description <EOL> response = self . connection . request ( action , data = data , method = '<STR_LIT:POST>' ) <EOL> return self . _to_volume ( response . object ) <EOL> def destroy_volume ( self , volume ) : <EOL> action = API_ROOT + '<STR_LIT>' + volume . id <EOL> self . connection . request ( action , method = '<STR_LIT>' ) <EOL> return True <EOL> def list_volumes ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' <EOL> data = { } <EOL> if location : <EOL> data [ '<STR_LIT>' ] = location . id <EOL> response = self . connection . request ( action , data = data ) <EOL> return self . _to_volumes ( response . object ) <EOL> def ex_get_volume ( self , volume_id ) : <EOL> """<STR_LIT>""" <EOL> action = API_ROOT + '<STR_LIT>' + volume_id <EOL> response = self . connection . request ( action ) <EOL> return self . _to_volume ( response . object ) <EOL> def attach_volume ( self , node , volume , device = None ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( API_ROOT , volume . id ) <EOL> data = { '<STR_LIT>' : node . id } <EOL> self . connection . request ( action , data = data , method = '<STR_LIT:POST>' ) <EOL> return True <EOL> def detach_volume ( self , volume , ex_node = None ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( API_ROOT , volume . id ) <EOL> if ex_node is None : <EOL> if len ( volume . extra [ '<STR_LIT>' ] ) != <NUM_LIT:1> : <EOL> err_msg = "<STR_LIT>" <EOL> raise Exception ( err_msg ) <EOL> ex_node = self . ex_get_node ( volume . extra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> data = { '<STR_LIT>' : ex_node . id } <EOL> self . connection . request ( action , data = data , method = '<STR_LIT:POST>' ) <EOL> return True <EOL> def _to_volume ( self , obj ) : <EOL> extra = obj . copy ( ) <EOL> extra . pop ( '<STR_LIT:id>' ) <EOL> extra . pop ( '<STR_LIT:name>' ) <EOL> extra . pop ( '<STR_LIT:size>' ) <EOL> state = self . VOLUME_STATE_MAP . get ( obj . pop ( '<STR_LIT:status>' , None ) , <EOL> StorageVolumeState . UNKNOWN ) <EOL> return StorageVolume ( id = obj [ '<STR_LIT:id>' ] , name = obj [ '<STR_LIT:name>' ] , size = obj [ '<STR_LIT:size>' ] , <EOL> state = state , extra = extra , driver = self ) <EOL> def _to_volumes ( self , objs ) : <EOL> return [ self . _to_volume ( obj ) for obj in objs ] <EOL> def _to_location ( self , obj ) : <EOL> location = self . connection . LOCATIONS [ obj ] <EOL> return NodeLocation ( driver = self , ** location ) <EOL> def _to_locations ( self , objs ) : <EOL> return [ self . _to_location ( obj ) for obj in objs ] <EOL> def _to_node ( self , obj ) : <EOL> extra = obj . copy ( ) <EOL> if '<STR_LIT>' in extra : <EOL> public_ips = [ obj . pop ( '<STR_LIT>' ) ] <EOL> else : <EOL> ip = extra . pop ( '<STR_LIT>' ) <EOL> public_ips = [ ip ] if ip else [ ] <EOL> del extra [ '<STR_LIT>' ] <EOL> del extra [ '<STR_LIT:name>' ] <EOL> return Node ( id = obj [ '<STR_LIT>' ] , name = obj [ '<STR_LIT:name>' ] , <EOL> state = self . NODE_STATE_MAP [ obj [ '<STR_LIT:status>' ] ] , <EOL> public_ips = public_ips , private_ips = [ ] , driver = self , <EOL> extra = extra ) <EOL> def _to_nodes ( self , objs ) : <EOL> return [ self . _to_node ( obj ) for obj in objs ] <EOL> def _to_size ( self , obj ) : <EOL> extra = { '<STR_LIT>' : obj [ '<STR_LIT>' ] , '<STR_LIT:type>' : obj [ '<STR_LIT:type>' ] , <EOL> '<STR_LIT>' : obj [ '<STR_LIT>' ] } <EOL> return NodeSize ( id = obj [ '<STR_LIT:id>' ] , name = obj [ '<STR_LIT:name>' ] , ram = obj [ '<STR_LIT>' ] , <EOL> disk = obj [ '<STR_LIT>' ] , bandwidth = None , price = None , <EOL> driver = self , extra = extra ) <EOL> def _to_sizes ( self , objs ) : <EOL> return [ self . _to_size ( obj ) for obj in objs ] <EOL> def _to_image ( self , obj ) : <EOL> extra = { '<STR_LIT>' : obj [ '<STR_LIT>' ] , '<STR_LIT>' : obj [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : obj [ '<STR_LIT>' ] } <EOL> return NodeImage ( id = obj [ '<STR_LIT:id>' ] , name = obj [ '<STR_LIT:name>' ] , driver = self , <EOL> extra = extra ) <EOL> def _to_images ( self , objs ) : <EOL> return [ self . _to_image ( obj ) for obj in objs ] <EOL> def _to_key_pair ( self , obj ) : <EOL> extra = { '<STR_LIT>' : obj [ '<STR_LIT>' ] } <EOL> return OpenStackKeyPair ( name = obj [ '<STR_LIT:name>' ] , public_key = obj [ '<STR_LIT>' ] , <EOL> driver = self , fingerprint = obj [ '<STR_LIT>' ] , <EOL> extra = extra ) <EOL> def _to_key_pairs ( self , objs ) : <EOL> return [ self . _to_key_pair ( obj ) for obj in objs ] <EOL> def _ex_connection_class_kwargs ( self ) : <EOL> return { '<STR_LIT>' : self . consumer_key } <EOL> def _add_required_headers ( self , headers , method , action , data , timestamp ) : <EOL> timestamp = self . connection . get_timestamp ( ) <EOL> signature = self . connection . make_signature ( method , action , data , <EOL> str ( timestamp ) ) <EOL> headers . update ( { <EOL> '<STR_LIT>' : timestamp , <EOL> '<STR_LIT>' : signature <EOL> } ) </s>
