diff --git a/src/cloud/occi/bin/occi-compute b/src/cloud/occi/bin/occi-compute index 722ee2a91c..0f77ee05b8 100755 --- a/src/cloud/occi/bin/occi-compute +++ b/src/cloud/occi/bin/occi-compute @@ -37,7 +37,7 @@ Usage: Commands: * create - creates a new compute resource described by the provided + creates a new compute resource described by the provided * list @@ -54,6 +54,11 @@ Commands: * delete deletes the compute resource idenfitied by +* attachdisk + attaches an storage to a given compute + +* detachdisk + detaches the target disk form the compute Options: @@ -133,7 +138,7 @@ begin end rescue Exception => e exit(-1) -end +end begin occi_client = OCCIClient::Client.new(url,username,password, timeout, debug, plain) @@ -146,7 +151,7 @@ if !ARGV[0] puts "#{cmd_name}: [COMMAND] not present" puts "#{cmd_name}: Execute #{cmd_name} -h for help." exit(-1) -end +end case ARGV[0].downcase when 'list' @@ -165,13 +170,13 @@ case ARGV[0].downcase when 'show' vm_id = ARGV[1] - if !vm_id + if !vm_id puts "#{cmd_name} show: missing VM-ID parameter" exit(-1) end rc = occi_client.get_vm(vm_id) - + when 'update' vm_xml = ARGV[1] @@ -181,17 +186,53 @@ case ARGV[0].downcase end rc = occi_client.put_vm(vm_xml) - + when 'delete' vm_id = ARGV[1] - if !vm_id + if !vm_id puts "#{cmd_name} delete: missing VM-ID parameter" exit -1 end rc = occi_client.delete_vm(vm_id) - + + when 'attachdisk' + vm_id = ARGV[1] + image_id = ARGV[2] + + if !image_id + puts "#{cmd_name} show: missing storage id parameter" + exit(-1) + end + + xml = "" + xml << " ATTACHDISK" + xml << " " + xml << " " + xml << " " + xml << "" + + rc = occi_client.action_vm(vm_id, xml) + + when 'detachdisk' + vm_id = ARGV[1] + disk_id = ARGV[2] + + if !disk_id + puts "#{cmd_name} show: missing disk id parameter" + exit(-1) + end + + xml = "" + xml << " DETACHDISK" + xml << " " + xml << " " + xml << " " + xml << "" + + rc = occi_client.action_vm(vm_id, xml) + else puts "Command #{ARGV[0]} not valid." exit(-1) @@ -200,7 +241,7 @@ end if CloudClient::is_error?(rc) puts rc.to_s() exit(-1) -else +else str, code = print_xml(rc) puts str exit(code) diff --git a/src/cloud/occi/bin/occi-storage b/src/cloud/occi/bin/occi-storage index ad70987f71..ac68b46ae1 100755 --- a/src/cloud/occi/bin/occi-storage +++ b/src/cloud/occi/bin/occi-storage @@ -51,10 +51,14 @@ Commands: * update updates the representation of the storage resource represented by the provided - + * delete deletes the storage resource idenfitied by +* clone + clones the storage resource idenfitied by in a new one + using the given + Options: @@ -205,6 +209,28 @@ case ARGV[0].downcase rc = occi_client.delete_image(image_id) + when 'clone' + image_id = ARGV[1] + name = ARGV[2] + + if !image_id + puts "#{cmd_name} clone: missing storage id parameter" + exit(-1) + end + + if !name + puts "#{cmd_name} clone: missing name parameter" + exit(-1) + end + + xml = "" + xml << " CLONE" + xml << " " + xml << " #{name}" + xml << " " + xml << "" + + rc = occi_client.action_image(image_id, xml) else puts "Command #{ARGV[0]} not valid." exit(-1) @@ -213,7 +239,7 @@ end if CloudClient::is_error?(rc) puts rc.to_s() exit(-1) -else +else str, code = print_xml(rc) puts str exit(code) diff --git a/src/cloud/occi/lib/OCCIClient.rb b/src/cloud/occi/lib/OCCIClient.rb index 9b1ffbf600..5d5eb2a6a9 100755 --- a/src/cloud/occi/lib/OCCIClient.rb +++ b/src/cloud/occi/lib/OCCIClient.rb @@ -233,6 +233,10 @@ module OCCIClient delete('/compute/'+id.to_s) end + def action_vm(id, xml) + post('/compute/'+id.to_s+'/action', xml, false) + end + ###################################################################### # Retrieves a Virtual Network # :id Virtual Network identifier @@ -279,6 +283,10 @@ module OCCIClient delete('/storage/'+id.to_s) end + def action_image(id, xml) + post('/storage/'+id.to_s+'/action', xml, false) + end + private def get(path, verbose=false) @@ -289,11 +297,15 @@ module OCCIClient do_request(url, req) end - def post(path, xmlfile) + def post(path, xmlfile, file=true) url = URI.parse(@endpoint+path) req = Net::HTTP::Post.new(url.path) - req.body=File.read(xmlfile) + if file + req.body=File.read(xmlfile) + else + req.body=xmlfile + end do_request(url, req) end diff --git a/src/cloud/occi/lib/OCCIServer.rb b/src/cloud/occi/lib/OCCIServer.rb index 617280547b..9c7ec17a92 100644 --- a/src/cloud/occi/lib/OCCIServer.rb +++ b/src/cloud/occi/lib/OCCIServer.rb @@ -298,6 +298,73 @@ class OCCIServer < CloudServer return "", 204 end + # Attach a disk to an existing COMPUTE + # @param [Hash] request hash containing the data of the request + # @param [Hash] params hash containing the params of the request + # @param [OpenNebula::XMLElement] action_xml contains the body of the request + # @return [[String, OpenNebula:Error], Integer] + def attach_disk(request, params, action_xml) + # --- Get the VM --- + vm = VirtualMachineOCCI.new( + VirtualMachine.build_xml(params[:id]), + @client) + + image_href = action_xml.attr('PARAMS/STORAGE','href') + if !image_href + error = OpenNebula::Error.new("You have to specify an STORAGE " << + "to be attached") + return error, CloudServer::HTTP_ERROR_CODE[error.errno] + end + + image_id = image_href.split('/').last + target = action_xml['PARAMS/TARGET'] + + if target + template = "DISK = [ IMAGE_ID = #{image_id}, TARGET = #{target} ]" + else + template = "DISK = [ IMAGE_ID = #{image_id}, DEV_PREFIX = sd ]" + end + + # --- Attach DISK --- + result = vm.attachdisk(template) + if OpenNebula.is_error?(result) + return result, CloudServer::HTTP_ERROR_CODE[result.errno] + end + + vm.info + + return to_occi_xml(vm, :code=>202) + end + + # Detach a DISK from an existing COMOUTE + # @param [Hash] request hash containing the data of the request + # @param [Hash] params hash containing the params of the request + # @param [OpenNebula::XMLElement] action_xml contains the body of the request + # @return [[String, OpenNebula:Error], Integer] + def detach_disk(request, params, action_xml) + # --- Get the VM --- + vm = VirtualMachineOCCI.new( + VirtualMachine.build_xml(params[:id]), + @client) + + disk_id = action_xml.attr('PARAMS/DISK','id') + if !disk_id + error = OpenNebula::Error.new("You have to specify a DISK " << + "to be detached") + return error, CloudServer::HTTP_ERROR_CODE[error.errno] + end + + # --- Detach DISK --- + result = vm.detachdisk(disk_id.to_i) + if OpenNebula.is_error?(result) + return result, CloudServer::HTTP_ERROR_CODE[result.errno] + end + + vm.info + + return to_occi_xml(vm, :code=>202) + end + # Updates a COMPUTE resource # request:: _Hash_ hash containing the data of the request # [return] _String_,_Integer_ Update confirmation msg or error, @@ -481,6 +548,31 @@ class OCCIServer < CloudServer return to_occi_xml(image, :code=>200) end + # Clone a STORAGE resource + # request:: _Hash_ hash containing the data of the request + # [return] _String_,_Integer_ STORAGE occi representation or error, + # status code + def clone_storage(request, params, action_xml) + # --- Get the Image --- + image = ImageOCCI.new( + Image.build_xml(params[:id]), + @client) + + rc = image.clone(action_xml["PARAMS/NAME"]) + if OpenNebula.is_error?(rc) + return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] + end + + new_image = ImageOCCI.new( + Image.build_xml(rc), + @client) + + new_image.info + + # --- Prepare XML Response --- + return to_occi_xml(new_image, :code=>201) + end + # Deletes a STORAGE resource (Not yet implemented) # request:: _Hash_ hash containing the data of the request # [return] _String_,_Integer_ Delete confirmation msg or error, diff --git a/src/cloud/occi/lib/occi-server.rb b/src/cloud/occi/lib/occi-server.rb index 68e2b60df0..1cb6136ee0 100644 --- a/src/cloud/occi/lib/occi-server.rb +++ b/src/cloud/occi/lib/occi-server.rb @@ -315,6 +315,22 @@ get '/compute/:id' do treat_response(result,rc) end +post '/compute/:id/action' do + xml = XMLElement.new + xml.initialize_xml(request.body, "ACTION") + + result,rc = case xml['PERFORM'].downcase + when 'attachdisk' then + @occi_server.attach_disk(request, params, xml) + when 'detachdisk' then + @occi_server.detach_disk(request, params, xml) + else + halt 403, "Action #{xml['PERFORM']} not supported" + end + + treat_response(result,rc) +end + delete '/compute/:id' do result,rc = @occi_server.delete_compute(request, params) treat_response(result,rc) @@ -345,6 +361,20 @@ get '/storage/:id' do treat_response(result,rc) end +post '/storage/:id/action' do + xml = XMLElement.new + xml.initialize_xml(request.body, "ACTION") + + result, rc = case xml['PERFORM'].downcase + when 'clone' then + @occi_server.clone_storage(request, params, xml) + else + halt 403, "Action #{xml['PERFORM']} not supported" + end + + treat_response(result,rc) +end + delete '/storage/:id' do result,rc = @occi_server.delete_storage(request, params) treat_response(result,rc)