From 5028be8b1f1debc4a27f06b1812b521e510c7ca0 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 23 Nov 2011 19:03:27 +0100 Subject: [PATCH] feature #990: Add instance_type and collections methods to OCCI API(cherry picked from commit 36c045992c761b6c66db56fd09e56bdf79bec4d9) Conflicts: src/cloud/occi/etc/occi-server.conf --- src/cloud/occi/etc/occi-server.conf | 8 ++- src/cloud/occi/etc/templates/common.erb | 2 +- src/cloud/occi/lib/OCCIServer.rb | 69 ++++++++++++------------ src/cloud/occi/lib/VirtualMachineOCCI.rb | 12 +++-- src/cloud/occi/lib/occi-server.rb | 12 ++++- 5 files changed, 61 insertions(+), 42 deletions(-) diff --git a/src/cloud/occi/etc/occi-server.conf b/src/cloud/occi/etc/occi-server.conf index 6304fa24f5..859329f2df 100644 --- a/src/cloud/occi/etc/occi-server.conf +++ b/src/cloud/occi/etc/occi-server.conf @@ -39,11 +39,15 @@ # VM types allowed and its template file (inside templates directory) :instance_types: - :custom: - :template: custom.erb :small: :template: small.erb + :cpu: 1 + :memory: 1024 :medium: :template: medium.erb + :cpu: 4 + :memory: 4096 :large: :template: large.erb + :cpu: 8 + :memory: 8192 diff --git a/src/cloud/occi/etc/templates/common.erb b/src/cloud/occi/etc/templates/common.erb index 459ea02e69..4dd2fbae33 100644 --- a/src/cloud/occi/etc/templates/common.erb +++ b/src/cloud/occi/etc/templates/common.erb @@ -41,4 +41,4 @@ ] <% end %> -INSTANCE_TYPE = <%= @vm_info['INSTANCE_TYPE']%> \ No newline at end of file +INSTANCE_TYPE = <%= @itype %> \ No newline at end of file diff --git a/src/cloud/occi/lib/OCCIServer.rb b/src/cloud/occi/lib/OCCIServer.rb index 954248d3db..613ecd5333 100755 --- a/src/cloud/occi/lib/OCCIServer.rb +++ b/src/cloud/occi/lib/OCCIServer.rb @@ -37,6 +37,9 @@ require 'pp' # The OCCI Server provides an OCCI implementation based on the # OpenNebula Engine ############################################################################## + +COLLECTIONS = ["compute", "instance_type", "network", "storage"] + class OCCIServer < CloudServer # Server initializer # config_file:: _String_ path of the config file @@ -69,6 +72,38 @@ class OCCIServer < CloudServer ############################################################################ ############################################################################ + def get_collections(request) + xml_resp = "\n" + + COLLECTIONS.each { |c| + xml_resp << "\t<#{c.upcase}_COLLECTION href=\"#{@base_url}/#{c}\">\n" + } + + xml_resp << "" + + return xml_resp, 200 + end + + def get_instance_types(request) + xml_resp = "\n" + + @config[:instance_types].each { |k, v| + xml_resp << "\t\n" + xml_resp << "\t\t#{k.to_s}\n" + v.each { |elem, value| + next if elem==:template + str = elem.to_s.upcase + xml_resp << "\t\t<#{str}>#{value}\n" + } + xml_resp << "\t\n" + } + + xml_resp << "" + + return xml_resp, 200 + end + + # Gets the pool representation of COMPUTES # request:: _Hash_ hash containing the data of the request # [return] _String_,_Integer_ Pool Representation or error, status code @@ -504,38 +539,4 @@ class OCCIServer < CloudServer return to_occi_xml(user, 200) end - - def get_computes_types - etc_location=ONE_LOCATION ? ONE_LOCATION+"/etc" : "/etc/one" - begin - xml_response = "\n" - - Dir[etc_location + "/occi_templates/**"].each{| filename | - next if File.basename(filename) == "common.erb" - xml_response += "\t" - xml_response += "\t\t#{File.basename(filename)[0..-5]}" - file = File.open(filename, "r") - file.each_line{|line| - next if line.match(/^#/) - match=line.match(/^(.*)=(.*)/) - next if !match - case match[1].strip - when "NAME" - xml_response += "\t\t#{match[2].strip}" - when "CPU" - xml_response += "\t\t#{match[2].strip}" - when "MEMORY" - xml_response += "\t\t#{match[2].strip}" - end - } - xml_response += "\t" - } - - xml_response += "" - - return xml_response, 200 - rescue Exception => e - return "Error getting the instance types. Reason: #{e.message}", 500 - end - end end diff --git a/src/cloud/occi/lib/VirtualMachineOCCI.rb b/src/cloud/occi/lib/VirtualMachineOCCI.rb index 1f00212020..73274ba9b0 100755 --- a/src/cloud/occi/lib/VirtualMachineOCCI.rb +++ b/src/cloud/occi/lib/VirtualMachineOCCI.rb @@ -26,7 +26,7 @@ class VirtualMachineOCCI < VirtualMachine <%= self['TEMPLATE/MEMORY'] %> <%= self.name%> <% if self['TEMPLATE/INSTANCE_TYPE'] %> - <%= self['TEMPLATE/INSTANCE_TYPE'] %> + <%= self['TEMPLATE/INSTANCE_TYPE'] %> <% end %> <%= self.state_str %> <% self.each('TEMPLATE/DISK') do |disk| %> @@ -84,10 +84,14 @@ class VirtualMachineOCCI < VirtualMachine end if @vm_info != nil - itype = @vm_info['INSTANCE_TYPE'] + if href = @vm_info.attr('INSTANCE_TYPE','href') + @itype = href.split('/').last + else + @itype = @vm_info['INSTANCE_TYPE'] + end - if itype != nil and types[itype.to_sym] != nil - @template = base + "/#{types[itype.to_sym][:template]}" + if @itype != nil and types[@itype.to_sym] != nil + @template = base + "/#{types[@itype.to_sym][:template]}" end end diff --git a/src/cloud/occi/lib/occi-server.rb b/src/cloud/occi/lib/occi-server.rb index e650e861cd..b2675a005d 100755 --- a/src/cloud/occi/lib/occi-server.rb +++ b/src/cloud/occi/lib/occi-server.rb @@ -128,10 +128,20 @@ end # Actions ############################################################################## +get '/' do + result,rc = @occi_server.get_collections(request) + treat_response(result,rc) +end + ################################################### # Pool Resources methods ################################################### +get '/instance_type' do + result,rc = @occi_server.get_instance_types(request) + treat_response(result,rc) +end + post '/compute' do result,rc = @occi_server.post_compute(request) treat_response(result,rc) @@ -172,7 +182,7 @@ end ################################################### get '/compute/:id' do - if params[:id] == "types" + if params[:id] == "types" result,rc = @occi_server.get_computes_types else result,rc = @occi_server.get_compute(request, params)