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

feature #795: Adapt OCCI to the new Cloud Auth system

This commit is contained in:
Daniel Molina 2011-09-20 18:25:57 +02:00
parent afdf9d23c5
commit 196c021caf
3 changed files with 62 additions and 140 deletions

View File

@ -24,6 +24,8 @@ PORT=4567
# SSL proxy that serves the API (set if is being used)
#SSL_SERVER=https://localhost:443
AUTH=basic
# Configuration for OpenNebula's Virtual Networks
BRIDGE=<NAME OF DEFAULT BRIDGE>

View File

@ -15,12 +15,9 @@
#--------------------------------------------------------------------------- #
# Common cloud libs
require 'rubygems'
require 'sinatra'
require 'CloudServer'
# OCA
require 'OpenNebula'
include OpenNebula
# OCCI libs
@ -43,29 +40,13 @@ class OCCIServer < CloudServer
# Server initializer
# config_file:: _String_ path of the config file
# template:: _String_ path to the location of the templates
def initialize(config_file,template)
super(config_file)
def initialize(config)
super(config)
@config.add_configuration_value("TEMPLATE_LOCATION",template)
if @config[:ssl_server]
@base_url=@config[:ssl_server]
if config[:ssl_server]
@base_url=config[:ssl_server]
else
@base_url="http://#{@config[:server]}:#{@config[:port]}"
end
print_configuration
end
# Retrieve a client with the user credentials
# requestenv:: _Hash_ Hash containing the environment of the request
# [return] _Client_ client with the user credentials
def get_client(requestenv)
auth = Rack::Auth::Basic::Request.new(requestenv)
if auth
return one_client_user(auth.credentials[0], auth.credentials[1])
else
return nil
@base_url="http://#{config[:server]}:#{config[:port]}"
end
end
@ -92,13 +73,8 @@ class OCCIServer < CloudServer
# --- Get User's VMs ---
user_flag = -1
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
vmpool = VirtualMachinePoolOCCI.new(
one_client,
self.client,
user_flag)
# --- Prepare XML Response ---
@ -124,13 +100,8 @@ class OCCIServer < CloudServer
# --- Get User's VNETs ---
user_flag = -1
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
network_pool = VirtualNetworkPoolOCCI.new(
one_client,
self.client,
user_flag)
# --- Prepare XML Response ---
@ -155,13 +126,8 @@ class OCCIServer < CloudServer
# --- Get User's Images ---
user_flag = -1
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
image_pool = ImagePoolOCCI.new(
one_client,
self.client,
user_flag)
# --- Prepare XML Response ---
@ -193,16 +159,11 @@ class OCCIServer < CloudServer
# [return] _String_,_Integer_ COMPUTE Representation or error, status code
def post_compute(request)
# --- Create the new Instance ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
vm = VirtualMachineOCCI.new(
VirtualMachine.build_xml,
one_client,
self.client,
request.body.read,
@instance_types,
@config[:instance_types],
@config[:template_location])
# --- Generate the template and Allocate the new Instance ---
@ -223,14 +184,9 @@ class OCCIServer < CloudServer
# status code
def get_compute(request, params)
# --- Get the VM ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
vm = VirtualMachineOCCI.new(
VirtualMachine.build_xml(params[:id]),
one_client)
self.client)
# --- Prepare XML Response ---
rc = vm.info
@ -253,14 +209,9 @@ class OCCIServer < CloudServer
# status code
def delete_compute(request, params)
# --- Get the VM ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
vm = VirtualMachineOCCI.new(
VirtualMachine.build_xml(params[:id]),
one_client)
self.client)
rc = vm.info
return rc, 404 if OpenNebula::is_error?(rc)
@ -278,14 +229,9 @@ class OCCIServer < CloudServer
# status code
def put_compute(request, params)
# --- Get the VM ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
vm = VirtualMachineOCCI.new(
VirtualMachine.build_xml(params[:id]),
one_client)
self.client)
rc = vm.info
return rc, 400 if OpenNebula.is_error?(rc)
@ -362,14 +308,9 @@ class OCCIServer < CloudServer
# [return] _String_,_Integer_ Network Representation or error, status code
def post_network(request)
# --- Create the new Instance ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
network = VirtualNetworkOCCI.new(
VirtualNetwork.build_xml,
one_client,
self.client,
request.body,
@config[:bridge])
@ -390,15 +331,9 @@ class OCCIServer < CloudServer
# [return] _String_,_Integer_ NETWORK occi representation or error,
# status code
def get_network(request, params)
# --- Get the VNET ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
network = VirtualNetworkOCCI.new(
VirtualNetwork.build_xml(params[:id]),
one_client)
self.client)
# --- Prepare XML Response ---
rc = network.info
@ -419,15 +354,9 @@ class OCCIServer < CloudServer
# [return] _String_,_Integer_ Delete confirmation msg or error,
# status code
def delete_network(request, params)
# --- Get the VNET ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
network = VirtualNetworkOCCI.new(
VirtualNetwork.build_xml(params[:id]),
one_client)
self.client)
rc = network.info
return rc, 404 if OpenNebula::is_error?(rc)
@ -446,15 +375,10 @@ class OCCIServer < CloudServer
def put_network(request, params)
xmldoc = XMLElement.build_xml(request.body, 'NETWORK')
vnet_info = XMLElement.new(xmldoc) if xmldoc != nil
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
vnet = VirtualNetworkOCCI.new(
VirtualNetwork.build_xml(params[:id]),
one_client)
self.client)
rc = vnet.info
return rc, 400 if OpenNebula.is_error?(rc)
@ -487,11 +411,6 @@ class OCCIServer < CloudServer
error = OpenNebula::Error.new(error_msg)
return error, 400
end
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
# --- Create and Add the new Image ---
occixml = request.params['occixml']
@ -499,7 +418,7 @@ class OCCIServer < CloudServer
image = ImageOCCI.new(
Image.build_xml,
one_client,
self.client,
occixml,
request.params['file'])
@ -521,14 +440,9 @@ class OCCIServer < CloudServer
# status code
def get_storage(request, params)
# --- Get the Image ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
image = ImageOCCI.new(
Image.build_xml(params[:id]),
one_client)
self.client)
rc = image.info
@ -550,20 +464,15 @@ class OCCIServer < CloudServer
# status code
def delete_storage(request, params)
# --- Get the Image ---
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
image = ImageOCCI.new(
Image.build_xml(params[:id]),
one_client)
self.client)
rc = image.info
return rc, 404 if OpenNebula::is_error?(rc)
# --- Delete the Image ---
rc = @img_repo.delete(image)
rc = image.delete
return rc, 500 if OpenNebula::is_error?(rc)
return "", 204
@ -576,15 +485,10 @@ class OCCIServer < CloudServer
def put_storage(request, params)
xmldoc = XMLElement.build_xml(request.body, 'STORAGE')
image_info = XMLElement.new(xmldoc) if xmldoc != nil
one_client = get_client(request.env)
if !one_client
return "No authorization data present", 401
end
image = ImageOCCI.new(
Image.build_xml(params[:id]),
one_client)
self.client)
rc = image.info
return rc, 400 if OpenNebula.is_error?(rc)

View File

@ -43,21 +43,29 @@ $: << RUBY_LIB_LOCATION+"/cloud" # For the Repository Manager
################################################
require 'rubygems'
require 'sinatra'
require 'OCCIServer'
require 'OpenNebula'
require 'OCCIServer'
require 'Configuration'
include OpenNebula
begin
$occi_server = OCCIServer.new(CONFIGURATION_FILE, TEMPLATE_LOCATION)
config = Configuration.new(CONFIGURATION_FILE)
config.add_configuration_value("TEMPLATE_LOCATION", TEMPLATE_LOCATION)
instance_types = CloudServer.get_instance_types(config)
config.add_configuration_value("INSTANCE_TYPES", instance_types)
CloudServer.print_configuration(config)
set :config, config
rescue Exception => e
puts "Error starting server: #{e}"
exit(-1)
end
if CloudServer.is_port_open?($occi_server.config[:server],
$occi_server.config[:port])
if CloudServer.is_port_open?(settings.config[:server],
settings.config[:port])
puts "Port busy, please shutdown the service or move occi server port."
exit
end
@ -65,13 +73,21 @@ end
##############################################################################
# Sinatra Configuration
##############################################################################
set :host, $occi_server.config[:server]
set :port, $occi_server.config[:port]
set :host, settings.config[:server]
set :port, settings.config[:port]
##############################################################################
# Helpers
##############################################################################
before do
@occi_server = OCCIServer.new(settings.config)
result = @occi_server.authenticate(request.env)
if result
error 401, result
end
end
# Response treatment
helpers do
def treat_response(result,rc)
@ -93,32 +109,32 @@ end
###################################################
post '/compute' do
result,rc = $occi_server.post_compute(request)
result,rc = @occi_server.post_compute(request)
treat_response(result,rc)
end
get '/compute' do
result,rc = $occi_server.get_computes(request)
result,rc = @occi_server.get_computes(request)
treat_response(result,rc)
end
post '/network' do
result,rc = $occi_server.post_network(request)
result,rc = @occi_server.post_network(request)
treat_response(result,rc)
end
get '/network' do
result,rc = $occi_server.get_networks(request)
result,rc = @occi_server.get_networks(request)
treat_response(result,rc)
end
post '/storage' do
result,rc = $occi_server.post_storage(request)
result,rc = @occi_server.post_storage(request)
treat_response(result,rc)
end
get '/storage' do
result,rc = $occi_server.get_storages(request)
result,rc = @occi_server.get_storages(request)
treat_response(result,rc)
end
@ -127,46 +143,46 @@ end
###################################################
get '/compute/:id' do
result,rc = $occi_server.get_compute(request, params)
result,rc = @occi_server.get_compute(request, params)
treat_response(result,rc)
end
delete '/compute/:id' do
result,rc = $occi_server.delete_compute(request, params)
result,rc = @occi_server.delete_compute(request, params)
treat_response(result,rc)
end
put '/compute/:id' do
result,rc = $occi_server.put_compute(request, params)
result,rc = @occi_server.put_compute(request, params)
treat_response(result,rc)
end
get '/network/:id' do
result,rc = $occi_server.get_network(request, params)
result,rc = @occi_server.get_network(request, params)
treat_response(result,rc)
end
delete '/network/:id' do
result,rc = $occi_server.delete_network(request, params)
result,rc = @occi_server.delete_network(request, params)
treat_response(result,rc)
end
put '/network/:id' do
result,rc = $occi_server.put_network(request, params)
result,rc = @occi_server.put_network(request, params)
treat_response(result,rc)
end
get '/storage/:id' do
result,rc = $occi_server.get_storage(request, params)
result,rc = @occi_server.get_storage(request, params)
treat_response(result,rc)
end
delete '/storage/:id' do
result,rc = $occi_server.delete_storage(request, params)
result,rc = @occi_server.delete_storage(request, params)
treat_response(result,rc)
end
put '/storage/:id' do
result,rc = $occi_server.put_storage(request, params)
result,rc = @occi_server.put_storage(request, params)
treat_response(result,rc)
end