diff --git a/src/cloud/common/CloudClient.rb b/src/cloud/common/CloudClient.rb index 19e2f90ea3..605b211323 100644 --- a/src/cloud/common/CloudClient.rb +++ b/src/cloud/common/CloudClient.rb @@ -61,7 +61,7 @@ module CloudClient # ######################################################################### def self.get_one_auth if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and - File.file?(ENV["ONE_AUTH"]) + File.file?(ENV["ONE_AUTH"]) one_auth=File.read(ENV["ONE_AUTH"]).strip.split(':') elsif File.file?(DEFAULT_AUTH_FILE) one_auth=File.read(DEFAULT_AUTH_FILE).strip.split(':') @@ -91,24 +91,30 @@ module CloudClient end begin - http.start do |connection| + res = http.start do |connection| block.call(connection) end rescue Errno::ECONNREFUSED => e str = "Error connecting to server (#{e.to_s}).\n" str << "Server: #{url.host}:#{url.port}" - return CloudClient::Error.new(str) + return CloudClient::Error.new(str,"503") rescue Errno::ETIMEDOUT => e str = "Error timeout connecting to server (#{e.to_s}).\n" str << "Server: #{url.host}:#{url.port}" - return CloudClient::Error.new(str) + return CloudClient::Error.new(str,"504") rescue Timeout::Error => e str = "Error timeout while connected to server (#{e.to_s}).\n" str << "Server: #{url.host}:#{url.port}" - return CloudClient::Error.new(str) + return CloudClient::Error.new(str,"504") + end + + if res.is_a?(Net::HTTPSuccess) + res + else + CloudClient::Error.new(res.body, res.code) end end @@ -118,10 +124,12 @@ module CloudClient # ######################################################################### class Error attr_reader :message + attr_reader :code # +message+ a description of the error - def initialize(message=nil) + def initialize(message=nil, code="500") @message=message + @code=code end def to_s() @@ -140,7 +148,7 @@ end # Command line help functions module CloudCLI - def print_xml(xml_text) + def print_xml(xml_text) begin doc = REXML::Document.new(xml_text) rescue REXML::ParseException => e diff --git a/src/cloud/occi/lib/ImageOCCI.rb b/src/cloud/occi/lib/ImageOCCI.rb index 7f3f1dec1a..777876fdcd 100755 --- a/src/cloud/occi/lib/ImageOCCI.rb +++ b/src/cloud/occi/lib/ImageOCCI.rb @@ -23,6 +23,8 @@ class ImageOCCI < Image <%= self.id.to_s %> <%= self.name %> + + <%= self['GNAME'] %> <%= self.state_str %> <% if self['TYPE'] != nil %> <%= self.type_str %> diff --git a/src/cloud/occi/lib/ImagePoolOCCI.rb b/src/cloud/occi/lib/ImagePoolOCCI.rb index 3bdfa8cf9d..ee38636867 100755 --- a/src/cloud/occi/lib/ImagePoolOCCI.rb +++ b/src/cloud/occi/lib/ImagePoolOCCI.rb @@ -14,20 +14,21 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -require 'OpenNebula' - -include OpenNebula +require 'ImageOCCI' class ImagePoolOCCI < ImagePool OCCI_IMAGE_POOL = %q{ <% self.each{ |im| %> + <% if verbose %> + <%= im.to_occi(base_url) %> + <% else %> + <% end %> <% } %> } - # Creates the OCCI representation of a Virtual Machine Pool def to_occi(base_url) begin @@ -40,5 +41,9 @@ class ImagePoolOCCI < ImagePool return occi_text.gsub(/\n\s*/,'') end + + def factory(element_xml) + ImageOCCI.new(element_xml,@client) + end end diff --git a/src/cloud/occi/lib/OCCIClient.rb b/src/cloud/occi/lib/OCCIClient.rb index a5f2d7641d..4b30b9fb2c 100755 --- a/src/cloud/occi/lib/OCCIClient.rb +++ b/src/cloud/occi/lib/OCCIClient.rb @@ -30,11 +30,13 @@ module OCCIClient ##################################################################### class Client + attr_accessor :endpoint + ###################################################################### # Initialize client library ###################################################################### def initialize(endpoint_str=nil, user=nil, pass=nil, - timeout=nil, debug_flag=true) + timeout=nil, debug_flag=true) @debug = debug_flag @timeout = timeout @@ -65,50 +67,30 @@ module OCCIClient # Pool Resource Request Methods # ################################# + def get_root + get('/') + end + + ###################################################################### + # Retieves the available Instance types + ###################################################################### + def get_instance_types + get('/instance_type') + end + ###################################################################### # Post a new VM to the VM Pool - # :instance_type # :xmlfile ###################################################################### def post_vms(xmlfile) - xml=File.read(xmlfile) - - url = URI.parse(@endpoint+"/compute") - - req = Net::HTTP::Post.new(url.path) - req.body=xml - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) do |http| - http.request(req) - end - - if CloudClient::is_error?(res) - return res - else - return res.body - end + post('/compute', xmlfile) end ###################################################################### # Retieves the pool of Virtual Machines ###################################################################### def get_vms - url = URI.parse(@endpoint+"/compute") - req = Net::HTTP::Get.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end + get('/compute') end ###################################################################### @@ -116,44 +98,14 @@ module OCCIClient # :xmlfile xml description of the Virtual Network ###################################################################### def post_network(xmlfile) - xml=File.read(xmlfile) - - url = URI.parse(@endpoint+"/network") - - req = Net::HTTP::Post.new(url.path) - req.body=xml - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) do |http| - http.request(req) - end - - if CloudClient::is_error?(res) - return res - else - return res.body - end + post('/network', xmlfile) end ###################################################################### # Retieves the pool of Virtual Networks ###################################################################### def get_networks - url = URI.parse(@endpoint+"/network") - req = Net::HTTP::Get.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end + get('/network') end ###################################################################### @@ -251,22 +203,10 @@ module OCCIClient # Retieves the pool of Images owned by the user ###################################################################### def get_images - url = URI.parse(@endpoint+"/storage") - req = Net::HTTP::Get.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end + get('/storage') end + #################################### # Entity Resource Request Methods # #################################### @@ -275,20 +215,7 @@ module OCCIClient # :id VM identifier ###################################################################### def get_vm(id) - url = URI.parse(@endpoint+"/compute/" + id.to_s) - req = Net::HTTP::Get.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end + get('/compute/'+id.to_s) end ###################################################################### @@ -296,57 +223,14 @@ module OCCIClient # :xmlfile Compute OCCI xml representation ###################################################################### def put_vm(xmlfile) - xml = File.read(xmlfile) - - begin - vm_info = REXML::Document.new(xml).root - rescue Exception => e - error = CloudClient::Error.new(e.message) - return error - end - - if vm_info.elements['ID'] == nil - return CloudClient::Error.new("Can not find COMPUTE_ID") - end - - vm_id = vm_info.elements['ID'].text - - url = URI.parse(@endpoint+'/compute/' + vm_id) - - req = Net::HTTP::Put.new(url.path) - req.body = xml - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) do |http| - http.request(req) - end - - if CloudClient::is_error?(res) - return res - else - return res.body - end + put('/compute/', xmlfile) end #################################################################### # :id Compute identifier #################################################################### def delete_vm(id) - url = URI.parse(@endpoint+"/compute/" + id.to_s) - req = Net::HTTP::Delete.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end + delete('/compute/'+id.to_s) end ###################################################################### @@ -354,20 +238,7 @@ module OCCIClient # :id Virtual Network identifier ###################################################################### def get_network(id) - url = URI.parse(@endpoint+"/network/" + id.to_s) - req = Net::HTTP::Get.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end + get('/network/'+id.to_s) end ###################################################################### @@ -375,78 +246,22 @@ module OCCIClient # :xmlfile Network OCCI xml representation ###################################################################### def put_network(xmlfile) - xml = File.read(xmlfile) - - begin - vnet_info = REXML::Document.new(xml).root - rescue Exception => e - error = CloudClient::Error.new(e.message) - return error - end - - if vnet_info.elements['ID'] == nil - return CloudClient::Error.new("Can not find NETWORK_ID") - end - - vnet_id = vnet_info.elements['ID'].text - - url = URI.parse(@endpoint+'/network/' + vnet_id) - - req = Net::HTTP::Put.new(url.path) - req.body = xml - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) do |http| - http.request(req) - end - - if CloudClient::is_error?(res) - return res - else - return res.body - end + put('/network/', xmlfile) end ###################################################################### # :id VM identifier ###################################################################### def delete_network(id) - url = URI.parse(@endpoint+"/network/" + id.to_s) - req = Net::HTTP::Delete.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end + delete('/network/'+id.to_s) end - ####################################################################### + ####################################################################### # Retieves an Image # :image_uuid Image identifier ###################################################################### - def get_image(image_uuid) - url = URI.parse(@endpoint+"/storage/"+image_uuid) - req = Net::HTTP::Get.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end + def get_image(id) + get('/storage/'+id.to_s) end ###################################################################### @@ -454,26 +269,67 @@ module OCCIClient # :xmlfile Storage OCCI xml representation ###################################################################### def put_image(xmlfile) - xml = File.read(xmlfile) - + put('/storage/', xmlfile) + end + + ###################################################################### + # :id VM identifier + ###################################################################### + def delete_image(id) + delete('/storage/'+id.to_s) + end + + private + + def get(path) + url = URI.parse(@endpoint+path) + req = Net::HTTP::Get.new(url.path) + + do_request(url, req) + end + + def post(path, xmlfile) + url = URI.parse(@endpoint+path) + req = Net::HTTP::Post.new(url.path) + + req.body=File.read(xmlfile) + + do_request(url, req) + end + + def delete(path, xmlfile) + url = URI.parse(@endpoint+path) + req = Net::HTTP::Delete.new(url.path) + + do_request(url, req) + end + + def put(path, xmlfile) + xml = File.read(xmlfile) + + # Get ID from XML begin - image_info = REXML::Document.new(xml).root + info = REXML::Document.new(xml).root rescue Exception => e - error = OpenNebula::Error.new(e.message) + error = CloudClient::Error.new(e.message) return error end - - if image_info.elements['ID'] == nil - return CloudClient::Error.new("Can not find STORAGE_ID") + + if info.elements['ID'] == nil + return CloudClient::Error.new("Can not find RESOURCE ID") end - image_id = image_info.elements['ID'].text - - url = URI.parse(@endpoint+'/storage/' + image_id) + resource_id = info.elements['ID'].text + url = URI.parse(@endpoint+path + resource_id) req = Net::HTTP::Put.new(url.path) + req.body = xml + do_request(url, req) + end + + def do_request(url, req) req.basic_auth @occiauth[0], @occiauth[1] res = CloudClient::http_start(url, @timeout) do |http| @@ -486,25 +342,5 @@ module OCCIClient return res.body end end - - ###################################################################### - # :id VM identifier - ###################################################################### - def delete_image(id) - url = URI.parse(@endpoint+"/storage/" + id.to_s) - req = Net::HTTP::Delete.new(url.path) - - req.basic_auth @occiauth[0], @occiauth[1] - - res = CloudClient::http_start(url, @timeout) {|http| - http.request(req) - } - - if CloudClient::is_error?(res) - return res - else - return res.body - end - end end -end +end \ No newline at end of file diff --git a/src/cloud/occi/lib/OCCIServer.rb b/src/cloud/occi/lib/OCCIServer.rb index fbe3d210a9..8a4f66cf33 100755 --- a/src/cloud/occi/lib/OCCIServer.rb +++ b/src/cloud/occi/lib/OCCIServer.rb @@ -62,11 +62,11 @@ class OCCIServer < CloudServer # Prepare the OCCI XML Response # resource:: _Pool_ or _PoolElement_ that represents a OCCI resource # [return] _String_,_Integer_ Resource Representation or error, status code - def to_occi_xml(resource, code) - xml_response = resource.to_occi(@base_url) + def to_occi_xml(resource, opts) + xml_response = resource.to_occi(@base_url, opts[:verbose]) return xml_response, 500 if OpenNebula.is_error?(xml_response) - return xml_response, code + return xml_response, opts[:code] end ############################################################################ @@ -122,7 +122,7 @@ class OCCIServer < CloudServer return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] end - return to_occi_xml(vmpool, 200) + return to_occi_xml(vmpool, :status=>200, :verbose=>request.params['verbose']) end @@ -142,7 +142,7 @@ class OCCIServer < CloudServer return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] end - return to_occi_xml(network_pool, 200) + return to_occi_xml(network_pool, :status=>200, :verbose=>request.params['verbose']) end # Gets the pool representation of STORAGES @@ -161,7 +161,7 @@ class OCCIServer < CloudServer return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] end - return to_occi_xml(image_pool, 200) + return to_occi_xml(image_pool, :status=>200, :verbose=>request.params['verbose']) end # Gets the pool representation of USERs @@ -178,7 +178,7 @@ class OCCIServer < CloudServer return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] end - return to_occi_xml(user_pool, 200) + return to_occi_xml(user_pool, :status=>200, :verbose=>request.params['verbose']) end ############################################################################ @@ -214,7 +214,7 @@ class OCCIServer < CloudServer # --- Prepare XML Response --- vm.info - return to_occi_xml(vm, 201) + return to_occi_xml(vm, :status=>201) end # Get the representation of a COMPUTE resource @@ -233,7 +233,7 @@ class OCCIServer < CloudServer return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] end - return to_occi_xml(vm, 200) + return to_occi_xml(vm, :status=>200) end @@ -277,7 +277,7 @@ class OCCIServer < CloudServer return result, code else vm.info - return to_occi_xml(vm, code) + return to_occi_xml(vm, :status=>code) end end @@ -307,7 +307,7 @@ class OCCIServer < CloudServer # --- Prepare XML Response --- network.info - return to_occi_xml(network, 201) + return to_occi_xml(network, :status=>201) end # Retrieves a NETWORK resource @@ -325,7 +325,7 @@ class OCCIServer < CloudServer return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] end - return to_occi_xml(network, 200) + return to_occi_xml(network, :status=>200) end # Deletes a NETWORK resource @@ -371,7 +371,7 @@ class OCCIServer < CloudServer # --- Prepare XML Response --- vnet.info - return to_occi_xml(vnet, 202) + return to_occi_xml(vnet, :status=>202) end ############################################################################ @@ -411,7 +411,7 @@ class OCCIServer < CloudServer # --- Prepare XML Response --- image.info - return to_occi_xml(image, 201) + return to_occi_xml(image, :status=>201) end # Get a STORAGE resource @@ -430,7 +430,7 @@ class OCCIServer < CloudServer end # --- Prepare XML Response --- - return to_occi_xml(image, 200) + return to_occi_xml(image, :status=>200) end # Deletes a STORAGE resource (Not yet implemented) @@ -484,7 +484,7 @@ class OCCIServer < CloudServer # --- Prepare XML Response --- image.info - return to_occi_xml(image, 202) + return to_occi_xml(image, :status=>202) end # Get the representation of a USER @@ -503,6 +503,6 @@ class OCCIServer < CloudServer return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] end - return to_occi_xml(user, 200) + return to_occi_xml(user, :status=>200) end end diff --git a/src/cloud/occi/lib/UserOCCI.rb b/src/cloud/occi/lib/UserOCCI.rb index 7c772c139f..fa8a960ef2 100644 --- a/src/cloud/occi/lib/UserOCCI.rb +++ b/src/cloud/occi/lib/UserOCCI.rb @@ -27,6 +27,7 @@ class UserOCCI < User <%= self.id.to_s %> <%= self.name %> + <%= self['GNAME'] %> <% user_quota.each { |key,value| key_s = key.to_s.upcase diff --git a/src/cloud/occi/lib/UserPoolOCCI.rb b/src/cloud/occi/lib/UserPoolOCCI.rb index bfad719c78..2745020dc7 100644 --- a/src/cloud/occi/lib/UserPoolOCCI.rb +++ b/src/cloud/occi/lib/UserPoolOCCI.rb @@ -14,20 +14,21 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -require 'OpenNebula' - -include OpenNebula +require 'UserOCCI' class UserPoolOCCI < UserPool OCCI_USER_POOL = %q{ <% self.each{ |user| %> + <% if verbose %> + <%= user.to_occi(base_url) %> + <% else %> + <% end %> <% } %> } - # Creates the OCCI representation of a User Pool def to_occi(base_url) begin @@ -40,4 +41,8 @@ class UserPoolOCCI < UserPool return occi_text.gsub(/\n\s*/,'') end + + def factory(element_xml) + UserOCCI.new(element_xml,@client) + end end \ No newline at end of file diff --git a/src/cloud/occi/lib/VirtualMachineOCCI.rb b/src/cloud/occi/lib/VirtualMachineOCCI.rb index c04c9cd1bf..4cb8754653 100755 --- a/src/cloud/occi/lib/VirtualMachineOCCI.rb +++ b/src/cloud/occi/lib/VirtualMachineOCCI.rb @@ -22,6 +22,8 @@ class VirtualMachineOCCI < VirtualMachine OCCI_VM = %q{ <%= self.id.to_s%> + + <%= self['GNAME'] %> <%= self['TEMPLATE/CPU'] %> <%= self['TEMPLATE/MEMORY'] %> <%= self.name%> diff --git a/src/cloud/occi/lib/VirtualMachinePoolOCCI.rb b/src/cloud/occi/lib/VirtualMachinePoolOCCI.rb index 55c9767c12..58a4eb6baa 100755 --- a/src/cloud/occi/lib/VirtualMachinePoolOCCI.rb +++ b/src/cloud/occi/lib/VirtualMachinePoolOCCI.rb @@ -14,22 +14,24 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -require 'OpenNebula' - -include OpenNebula +require 'VirtualMachineOCCI' class VirtualMachinePoolOCCI < VirtualMachinePool OCCI_VM_POOL = %q{ - <% self.each{ |vm| %> + <% self.each{ |vm| %> + <% if verbose %> + <%= vm.to_occi(base_url) %> + <% else %> + <% end %> <% } %> } # Creates the OCCI representation of a Virtual Machine Pool - def to_occi(base_url) + def to_occi(base_url, verbose=false) begin occi = ERB.new(OCCI_VM_POOL) occi_text = occi.result(binding) @@ -40,5 +42,9 @@ class VirtualMachinePoolOCCI < VirtualMachinePool return occi_text.gsub(/\n\s*/,'') end + + def factory(element_xml) + VirtualMachineOCCI.new(element_xml,@client) + end end diff --git a/src/cloud/occi/lib/VirtualNetworkOCCI.rb b/src/cloud/occi/lib/VirtualNetworkOCCI.rb index 22cb93901a..68edb59c7f 100755 --- a/src/cloud/occi/lib/VirtualNetworkOCCI.rb +++ b/src/cloud/occi/lib/VirtualNetworkOCCI.rb @@ -24,6 +24,8 @@ class VirtualNetworkOCCI < VirtualNetwork <%= self.id.to_s %> <%= self.name %> + + <%= self['GNAME'] %> <% if self['TEMPLATE/DESCRIPTION'] != nil %> <%= self['TEMPLATE/DESCRIPTION'] %> <% end %> diff --git a/src/cloud/occi/lib/VirtualNetworkPoolOCCI.rb b/src/cloud/occi/lib/VirtualNetworkPoolOCCI.rb index 6772fefce9..3de2469ae9 100755 --- a/src/cloud/occi/lib/VirtualNetworkPoolOCCI.rb +++ b/src/cloud/occi/lib/VirtualNetworkPoolOCCI.rb @@ -14,15 +14,17 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -require 'OpenNebula' - -include OpenNebula +require 'VirtualNetworkOCCI' class VirtualNetworkPoolOCCI < VirtualNetworkPool OCCI_NETWORK_POOL = %q{ - <% self.each{ |vn| %> + <% self.each{ |vn| %> + <% if verbose %> + <%= vn.to_occi(base_url) %> + <% else %> + <% end %> <% } %> } @@ -39,4 +41,8 @@ class VirtualNetworkPoolOCCI < VirtualNetworkPool return occi_text.gsub(/\n\s*/,'') end + + def factory(element_xml) + VirtualNetworkOCCI.new(element_xml,@client) + end end \ No newline at end of file