<s> import uuid <EOL> import webob . exc <EOL> import ooi . api . base <EOL> import ooi . api . helpers <EOL> import ooi . api . network as network_api <EOL> from ooi import exception <EOL> from ooi . occi . core import collection <EOL> from ooi . occi . infrastructure import compute <EOL> from ooi . occi . infrastructure import network <EOL> from ooi . occi . infrastructure import storage <EOL> from ooi . occi . infrastructure import storage_link <EOL> from ooi . occi import validator as occi_validator <EOL> from ooi . openstack import contextualization <EOL> from ooi . openstack import helpers <EOL> from ooi . openstack import network as os_network <EOL> from ooi . openstack import templates <EOL> def _create_network_link ( addr , comp ) : <EOL> if addr [ "<STR_LIT>" ] == "<STR_LIT>" : <EOL> net_id = network_api . FLOATING_PREFIX <EOL> else : <EOL> net_id = network_api . FIXED_PREFIX <EOL> net = network . NetworkResource ( title = "<STR_LIT>" , id = net_id ) <EOL> return os_network . OSNetworkInterface ( comp , net , <EOL> addr [ "<STR_LIT>" ] , <EOL> addr [ "<STR_LIT>" ] ) <EOL> class Controller ( ooi . api . base . Controller ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Controller , self ) . __init__ ( * args , ** kwargs ) <EOL> self . compute_actions = compute . ComputeResource . actions <EOL> self . os_helper = ooi . api . helpers . OpenStackHelper ( <EOL> self . app , <EOL> self . openstack_version <EOL> ) <EOL> def _get_compute_resources ( self , servers ) : <EOL> occi_compute_resources = [ ] <EOL> if servers : <EOL> for s in servers : <EOL> s = compute . ComputeResource ( title = s [ "<STR_LIT:name>" ] , id = s [ "<STR_LIT:id>" ] ) <EOL> occi_compute_resources . append ( s ) <EOL> return occi_compute_resources <EOL> def index ( self , req ) : <EOL> servers = self . os_helper . index ( req ) <EOL> occi_compute_resources = self . _get_compute_resources ( servers ) <EOL> return collection . Collection ( resources = occi_compute_resources ) <EOL> def run_action ( self , req , id , body ) : <EOL> action = req . GET . get ( "<STR_LIT:action>" , None ) <EOL> occi_actions = [ a . term for a in compute . ComputeResource . actions ] <EOL> if action is None or action not in occi_actions : <EOL> raise exception . InvalidAction ( action = action ) <EOL> parser = req . get_parser ( ) ( req . headers , req . body ) <EOL> obj = parser . parse ( ) <EOL> server = self . os_helper . get_server ( req , id ) <EOL> if action == "<STR_LIT>" : <EOL> scheme = { "<STR_LIT>" : compute . stop } <EOL> elif action == "<STR_LIT:start>" : <EOL> scheme = { "<STR_LIT>" : compute . start } <EOL> if server [ "<STR_LIT:status>" ] == "<STR_LIT>" : <EOL> action = "<STR_LIT>" <EOL> elif server [ "<STR_LIT:status>" ] == "<STR_LIT>" : <EOL> action = "<STR_LIT>" <EOL> elif action == "<STR_LIT>" : <EOL> scheme = { "<STR_LIT>" : compute . restart } <EOL> elif action == "<STR_LIT>" : <EOL> scheme = { "<STR_LIT>" : compute . suspend } <EOL> else : <EOL> raise exception . NotImplemented <EOL> validator = occi_validator . Validator ( obj ) <EOL> validator . validate ( scheme ) <EOL> self . os_helper . run_action ( req , action , id ) <EOL> return [ ] <EOL> def _build_block_mapping ( self , req , obj ) : <EOL> mappings = [ ] <EOL> for l in obj . get ( "<STR_LIT>" , { } ) . values ( ) : <EOL> if l [ "<STR_LIT>" ] == storage . StorageResource . kind . type_id : <EOL> _ , vol_id = ooi . api . helpers . get_id_with_kind ( <EOL> req , <EOL> l . get ( "<STR_LIT>" ) , <EOL> storage . StorageResource . kind ) <EOL> mapping = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : vol_id , <EOL> "<STR_LIT>" : False , <EOL> } <EOL> try : <EOL> mapping [ '<STR_LIT>' ] = l [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> mappings . append ( mapping ) <EOL> if mappings : <EOL> image = obj [ "<STR_LIT>" ] [ templates . OpenStackOSTemplate . scheme ] [ <NUM_LIT:0> ] <EOL> mappings . insert ( <NUM_LIT:0> , { <EOL> "<STR_LIT>" : "<STR_LIT:image>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : image , <EOL> } ) <EOL> return mappings <EOL> def create ( self , req , body ) : <EOL> parser = req . get_parser ( ) ( req . headers , req . body ) <EOL> scheme = { <EOL> "<STR_LIT>" : compute . ComputeResource . kind , <EOL> "<STR_LIT>" : [ <EOL> templates . OpenStackOSTemplate , <EOL> templates . OpenStackResourceTemplate , <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> contextualization . user_data , <EOL> contextualization . public_key , <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> storage . StorageResource . kind , <EOL> ] <EOL> } <EOL> obj = parser . parse ( ) <EOL> validator = occi_validator . Validator ( obj ) <EOL> validator . validate ( scheme ) <EOL> attrs = obj . get ( "<STR_LIT>" , { } ) <EOL> name = attrs . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> image = obj [ "<STR_LIT>" ] [ templates . OpenStackOSTemplate . scheme ] [ <NUM_LIT:0> ] <EOL> flavor = obj [ "<STR_LIT>" ] [ templates . OpenStackResourceTemplate . scheme ] [ <NUM_LIT:0> ] <EOL> user_data , key_name , key_data = None , None , None <EOL> create_key , create_key_tmp = False , False <EOL> if contextualization . user_data . scheme in obj [ "<STR_LIT>" ] : <EOL> user_data = attrs . get ( "<STR_LIT>" ) <EOL> if contextualization . public_key . scheme in obj [ "<STR_LIT>" ] : <EOL> key_name = attrs . get ( "<STR_LIT>" ) <EOL> key_data = attrs . get ( "<STR_LIT>" ) <EOL> if key_name and key_data : <EOL> create_key = True <EOL> elif not key_name and key_data : <EOL> key_name = uuid . uuid4 ( ) . hex <EOL> create_key = True <EOL> create_key_tmp = True <EOL> if create_key : <EOL> self . os_helper . keypair_create ( req , key_name , <EOL> public_key = key_data ) <EOL> block_device_mapping_v2 = self . _build_block_mapping ( req , obj ) <EOL> server = self . os_helper . create_server ( <EOL> req , <EOL> name , <EOL> image , <EOL> flavor , <EOL> user_data = user_data , <EOL> key_name = key_name , <EOL> block_device_mapping_v2 = block_device_mapping_v2 ) <EOL> server [ "<STR_LIT:name>" ] = name <EOL> occi_compute_resources = self . _get_compute_resources ( [ server ] ) <EOL> if create_key_tmp : <EOL> self . os_helper . keypair_delete ( req , key_name ) <EOL> return collection . Collection ( resources = occi_compute_resources ) <EOL> def show ( self , req , id ) : <EOL> s = self . os_helper . get_server ( req , id ) <EOL> flavor = self . os_helper . get_flavor ( req , s [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] ) <EOL> res_tpl = templates . OpenStackResourceTemplate ( flavor [ "<STR_LIT:id>" ] , <EOL> flavor [ "<STR_LIT:name>" ] , <EOL> flavor [ "<STR_LIT>" ] , <EOL> flavor [ "<STR_LIT>" ] , <EOL> flavor [ "<STR_LIT>" ] ) <EOL> img_id = s [ "<STR_LIT:image>" ] [ "<STR_LIT:id>" ] <EOL> try : <EOL> image = self . os_helper . get_image ( req , img_id ) <EOL> except webob . exc . HTTPNotFound : <EOL> image = { <EOL> "<STR_LIT:id>" : img_id , <EOL> "<STR_LIT:name>" : "<STR_LIT>" % img_id , <EOL> } <EOL> os_tpl = templates . OpenStackOSTemplate ( image [ "<STR_LIT:id>" ] , <EOL> image [ "<STR_LIT:name>" ] ) <EOL> comp = compute . ComputeResource ( title = s [ "<STR_LIT:name>" ] , id = s [ "<STR_LIT:id>" ] , <EOL> cores = flavor [ "<STR_LIT>" ] , <EOL> hostname = s [ "<STR_LIT:name>" ] , <EOL> memory = flavor [ "<STR_LIT>" ] , <EOL> state = helpers . vm_state ( s [ "<STR_LIT:status>" ] ) , <EOL> mixins = [ os_tpl , res_tpl ] ) <EOL> vols = self . os_helper . get_server_volumes_link ( req , s [ "<STR_LIT:id>" ] ) <EOL> for v in vols : <EOL> st = storage . StorageResource ( title = "<STR_LIT>" , id = v [ "<STR_LIT>" ] ) <EOL> comp . add_link ( storage_link . StorageLink ( comp , st , <EOL> deviceid = v [ "<STR_LIT>" ] ) ) <EOL> addresses = s . get ( "<STR_LIT>" , { } ) <EOL> if addresses : <EOL> for addr_set in addresses . values ( ) : <EOL> for addr in addr_set : <EOL> comp . add_link ( _create_network_link ( addr , comp ) ) <EOL> return [ comp ] <EOL> def _get_server_floating_ips ( self , req , server_id ) : <EOL> s = self . os_helper . get_server ( req , server_id ) <EOL> addresses = s . get ( "<STR_LIT>" , { } ) <EOL> floating_ips = [ ] <EOL> if addresses : <EOL> for addr_set in addresses . values ( ) : <EOL> for addr in addr_set : <EOL> if addr [ "<STR_LIT>" ] == "<STR_LIT>" : <EOL> floating_ips . append ( addr [ "<STR_LIT>" ] ) <EOL> return floating_ips <EOL> def _release_floating_ips ( self , req , server_id ) : <EOL> server_ips = self . _get_server_floating_ips ( req , server_id ) <EOL> if server_ips : <EOL> floating_ips = self . os_helper . get_floating_ips ( req ) <EOL> for server_ip in server_ips : <EOL> for ip in floating_ips : <EOL> if server_ip == ip [ "<STR_LIT>" ] : <EOL> self . os_helper . remove_floating_ip ( req , server_id , <EOL> ip [ "<STR_LIT>" ] ) <EOL> self . os_helper . release_floating_ip ( req , ip [ "<STR_LIT:id>" ] ) <EOL> def _delete ( self , req , server_ids ) : <EOL> for server_id in server_ids : <EOL> self . _release_floating_ips ( req , server_id ) <EOL> self . os_helper . delete ( req , server_id ) <EOL> return [ ] <EOL> def delete ( self , req , id ) : <EOL> return self . _delete ( req , [ id ] ) <EOL> def delete_all ( self , req ) : <EOL> ids = [ s [ "<STR_LIT:id>" ] for s in self . os_helper . index ( req ) ] <EOL> return self . _delete ( req , ids ) </s>
