1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-23 21:57:43 +03:00

feature #1356: Add attachdisk and detachdisk actions to OCCI

This commit is contained in:
Daniel Molina 2012-08-31 14:58:50 +02:00
parent 9d07a15c9a
commit 985b077c4b
4 changed files with 137 additions and 9 deletions

View File

@ -37,7 +37,7 @@ Usage:
Commands:
* create <occi xml file>
creates a new compute resource described by the provided
creates a new compute resource described by the provided
<occi xml file>
* list
@ -54,6 +54,11 @@ Commands:
* delete <compute id>
deletes the compute resource idenfitied by <compute id>
* attachdisk <compute id> <storage id>
attaches an storage to a given compute
* detachdisk <compute id> <disk id>
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 = "<ACTION>"
xml << " <PERFORM>ATTACHDISK</PERFORM>"
xml << " <PARAMS>"
xml << " <STORAGE href=\"#{url}/storage/#{image_id.to_s}\"/>"
xml << " </PARAMS>"
xml << "</ACTION>"
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 = "<ACTION>"
xml << " <PERFORM>DETACHDISK</PERFORM>"
xml << " <PARAMS>"
xml << " <DISK id=\"#{disk_id}\"/>"
xml << " </PARAMS>"
xml << "</ACTION>"
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)

View File

@ -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

View File

@ -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,

View File

@ -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)