1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

feature #990: Add instance_type and collections methods to OCCI API(cherry picked from commit 36c045992c761b6c66db56fd09e56bdf79bec4d9)

Conflicts:

	src/cloud/occi/etc/occi-server.conf
This commit is contained in:
Daniel Molina 2011-11-23 19:03:27 +01:00 committed by Ruben S. Montero
parent 5c5e3df525
commit 5028be8b1f
5 changed files with 61 additions and 42 deletions

View File

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

View File

@ -41,4 +41,4 @@
]
<% end %>
INSTANCE_TYPE = <%= @vm_info['INSTANCE_TYPE']%>
INSTANCE_TYPE = <%= @itype %>

View File

@ -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 = "<COLLECTIONS>\n"
COLLECTIONS.each { |c|
xml_resp << "\t<#{c.upcase}_COLLECTION href=\"#{@base_url}/#{c}\">\n"
}
xml_resp << "</COLLECTIONS>"
return xml_resp, 200
end
def get_instance_types(request)
xml_resp = "<INSTANCE_TYPE_COLLECTION>\n"
@config[:instance_types].each { |k, v|
xml_resp << "\t<INSTANCE_TYPE href=\"#{@base_url}/instance_type/#{k.to_s}\">\n"
xml_resp << "\t\t<NAME>#{k.to_s}</NAME>\n"
v.each { |elem, value|
next if elem==:template
str = elem.to_s.upcase
xml_resp << "\t\t<#{str}>#{value}</#{str}>\n"
}
xml_resp << "\t</INSTANCE_TYPE>\n"
}
xml_resp << "</INSTANCE_TYPE_COLLECTION>"
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 = "<ALLOWED_COMPUTE_TYPES>\n"
Dir[etc_location + "/occi_templates/**"].each{| filename |
next if File.basename(filename) == "common.erb"
xml_response += "\t<COMPUTE_TYPE>"
xml_response += "\t\t<NAME>#{File.basename(filename)[0..-5]}</NAME>"
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<NAME>#{match[2].strip}</NAME>"
when "CPU"
xml_response += "\t\t<vCPU>#{match[2].strip}</vCPU>"
when "MEMORY"
xml_response += "\t\t<vMEM>#{match[2].strip}</vMEM>"
end
}
xml_response += "\t</COMPUTE_TYPE>"
}
xml_response += "</ALLOWED_COMPUTE_TYPES>"
return xml_response, 200
rescue Exception => e
return "Error getting the instance types. Reason: #{e.message}", 500
end
end
end

View File

@ -26,7 +26,7 @@ class VirtualMachineOCCI < VirtualMachine
<MEMORY><%= self['TEMPLATE/MEMORY'] %></MEMORY>
<NAME><%= self.name%></NAME>
<% if self['TEMPLATE/INSTANCE_TYPE'] %>
<INSTANCE_TYPE><%= self['TEMPLATE/INSTANCE_TYPE'] %></INSTANCE_TYPE>
<INSTANCE_TYPE href="<%= base_url %>/instance_type/<%= self['TEMPLATE/INSTANCE_TYPE'] %>"><%= self['TEMPLATE/INSTANCE_TYPE'] %></INSTANCE_TYPE>
<% end %>
<STATE><%= self.state_str %></STATE>
<% 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

View File

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