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

feature #789: Migrating OzonesServer class. Work in progress

This commit is contained in:
Ruben S. Montero 2011-09-26 23:04:50 +02:00
parent b5298aea6f
commit beb5ee0db9
4 changed files with 185 additions and 201 deletions

View File

@ -96,7 +96,6 @@ module OZones
vdcpass = Digest::SHA1.hexdigest(vdc_data.delete(:vdcadminpass))
@vdc.attributes = vdc_data
puts vdc_data
# Create a group in the zone with the VDC name
group = OpenNebula::Group.new(OpenNebula::Group.build_xml, @client)

View File

@ -96,15 +96,17 @@ module OZones
end
# Create the zone
zone = super(zone_data)
rc = zone.save
begin
zone = Zones.new
zone.raise_on_save_failure = true
if rc
return zone
else
return OZones::Error.new("Error: Couldn't create zone. " \
"Duplicated name?.")
zone.attributes = zone_data
zone.save
rescue => e
return OZones::Error.new(e.message)
end
return zone
end
end

View File

@ -23,212 +23,183 @@ class OzonesServer
############################################################################
# Get methods for the Zones and VDC interface
############################################################################
def get_pool(kind)
rc = 200
pool = case kind
when "vdc" then
OZones::Vdc
when "zone" then
OZones::Zones
else
rc = 400
OZones::Error.new("Error: #{kind} resource not supported")
end
return [rc, pool.to_json]
# Gets all VDCs
def get_vdcs
return 200, OZones::Vdc.to_json
end
# Gets an aggreageted pool for a zone or vdc
# ie All the hosts in all the Zones
def get_aggregated_pool(kind, aggkind)
case kind
when "zone" then
OZones::OpenNebulaZone::all_pools_to_json(aggkind)
else
error = OZones::Error.new("Error: Aggregated view not " \
"supported for #{kind}")
# Gets a VDC
def get_vdc(id)
vdc = OZones::Vdc.get(id)
[404, error.to_json]
if vdc
return [200, vdc.to_json]
else
return [404,
OZones::Error.new("Error:VDC with id #{id} not found").to_json]
end
end
#Gets all Zones
def get_zones
return 200, OZones::Zones.to_json
end
#Gets a zone
def get_zone(id)
zone = OZones::Zones.get(id)
if zone
return [200, zone.to_json]
else
return [404,
OZones::Error.new("Error:Zone with id #{id} not found").to_json]
end
end
# Gets an aggreageted pool for a zone or vdc in json
# ie All the hosts in all the Zones
def get_full_resource(kind, id, aggkind)
case kind
when "zone"
begin
zone = OZones::OpenNebulaZone.new(id)
rc = zone.pool_to_json(aggkind)
rescue => e
return [404, OZones::Error.new(e.message).to_json]
end
else
error = OZones::Error.new("Error: #{kind} resource view " \
"not supported")
rc = [ 404, error.to_json ]
end
return rc
# Gets an aggreageted view of a pool for all zones
def get_zones_pool(pool)
OZones::OpenNebulaZone::all_pools_to_json(pool)
end
# Get a json representation resource with local (DB) info
def get_resource(kind, id)
rc = 200
res = case kind
when "vdc" then
OZones::Vdc.get(id)
when "zone" then
OZones::Zones.get(id)
else
rc = 404
OZones::Error.new("Error: #{kind} resource not supported")
# Gets a pool view of a given zone
def get_zone_pool(id, pool)
begin
zone = OZones::OpenNebulaZone.new(id)
return zone.pool_to_json(pool)
rescue => e
return [404, OZones::Error.new(e.message).to_json]
end
return [rc, res.to_json]
end
############################################################################
# Create resources
############################################################################
# Creates a resource of a kind, and updates the Proxy Rules
def create_resource(kind, data, body, pr)
def create_vdc (data, body,pr)
#Setup POST data
if body.size > 0
result = parse_json(body,kind)
result = parse_json(body,"vdc")
data = result if !OpenNebula.is_error?(result)
end
resource = case kind
when "vdc" then
vdc_data = Hash.new
data.each{|key,value|
vdc_data[key.downcase.to_sym]=value if key!="pool"
}
#Get the Zone that will host the VDC. And check resouces
zoneid = vdc_data.delete(:zoneid)
force = vdc_data.delete(:force)
vdc_data = Hash.new
data.each{|key,value|
vdc_data[key.downcase.to_sym] = value if key!="pool"
}
#Get the Zone that will host the VDC. And check resouces
zoneid = vdc_data.delete(:zoneid)
force = vdc_data.delete(:force)
if !zoneid
return [400, OZones::Error.new("Error: Couldn't create " \
"vdc. Mandatory attribute zoneid missing.").to_json]
end
if !zoneid
return [400, OZones::Error.new("Error: Couldn't create vdc. " \
"Mandatory attribute zoneid missing.").to_json]
end
zone = OZones::Zones.get(zoneid)
if !zone
return [404, OZones::Error.new("Error: Couldn't create " \
"vdc. Zone #{zoneid} not found.").to_json]
end
zone = OZones::Zones.get(zoneid)
if !zone
return [404, OZones::Error.new("Error: Couldn't create vdc. " \
"Zone #{zoneid} not found.").to_json]
end
if (!force or force.upcase!="YES") and
!host_uniqueness?(zone, vdc_data[:hosts])
if (!force or force.upcase!="YES") and
!host_uniqueness?(zone, vdc_data[:hosts])
return [403, OZones::Error.new( "Error: Couldn't create " \
"Hosts are not unique, use force to override").to_json]
end
return [403, OZones::Error.new("Error: Couldn't create vdc. " \
"Hosts are not unique, use force to override").to_json]
end
# Create de VDC
vdc = OZones::OpenNebulaVdc.new(-1,zone)
rc = vdc.create(vdc_data)
# Create de VDC
vdc = OZones::OpenNebulaVdc.new(-1,zone)
rc = vdc.create(vdc_data)
if OpenNebula.is_error?(rc)
return [400, OZones::Error.new("Error: Couldn't create " \
"vdc. Reason: #{rc.message}").to_json]
end
if OpenNebula.is_error?(rc)
return [400, OZones::Error.new("Error: Couldn't create vdc. " \
"Reason: #{rc.message}").to_json]
end
#Update the zone and save the vdc
zone.raise_on_save_failure = true
zone.vdcs << vdc.vdc
#Update the zone and save the vdc
zone.raise_on_save_failure = true
zone.vdcs << vdc.vdc
begin
zone.save
rescue => e
#TODO Rollback VDC creation?
return [400, OZones::Error.new("Error: Couldn't create " \
"vdc. Zone could not be saved: #{e.message}").to_json]
end
begin
zone.save
rescue => e
#TODO Rollback VDC creation
return [400, OZones::Error.new("Error: Couldn't create " \
"vdc. Zone could not be saved: #{e.message}").to_json]
end
pr.update # Rewrite proxy conf file
return [200, vdc.to_json]
when "zone" then
zone = OZones::Zones.create(data)
if OZones.is_error?(zone)
return [400, zone.to_json]
end
pr.update
return [200, zone.to_json]
else
error = OZones::Error.new(
"Error: #{kind.upcase} resource not supported")
return [404, error.to_json]
end
pr.update # Rewrite proxy conf file
return [200, vdc.to_json]
end
def create_zone(data, body, pr)
#Setup POST data
if body.size > 0
result = parse_json(body,"zone")
data = result if !OpenNebula.is_error?(result)
end
zone = OZones::Zones.create(data)
if OZones.is_error?(zone)
return [400, zone.to_json]
end
pr.update
return [200, zone.to_json]
end
############################################################################
# Update resources
############################################################################
# Updates a resource of a kind, and updates the Proxy Rules if needed
def update_resource(kind, data, body, pr)
def update_vdc(data, body, pr)
#Setup PUT data
if body.size > 0
result = parse_json(body,kind)
data = result if !OpenNebula.is_error?(result)
result = parse_json(body,"vdc")
data = result if !OpenNebula.is_error?(result)
end
puts data
vdc_data = Hash.new
vdc_id = nil
data.each{|key,value|
vdc_data[key.downcase.to_sym]=value
}
resource = case kind
when "vdc" then
vdc_data = Hash.new
vdc_id = nil
data.each{|key,value|
vdc_data[key.downcase.to_sym]=value if key!="id"
vdc_id = value if key=="id"
}
vdc_id = vdc_data.delete(:id)
hosts = vdc_data.delete(:hosts)
force = vdc_data.delete(:force)
# Check parameters
if !vdc_data[:hosts] || !vdc_id
return [400, OZones::Error.new(
"Error: Couldn't update resource #{kind}. " +
"Need ID and HOSTS to update.").to_json]
end
# Check parameters
if !hosts || !vdc_id
return [400, OZones::Error.new("Error: Couldn't update vdc. " \
"Missing ID or HOSTS.").to_json]
end
# Check if the referenced Vdc exists
begin
vdc=OZones::OpenNebulaVdc.new(vdc_id, zone)
rescue
return [404, OZones::Error.new("Error: Vdc " \
"#{vdc_id} not found, cannot update Vdc.").to_json]
# Check if the referenced Vdc exists
begin
vdc=OZones::OpenNebulaVdc.new(vdc_id, zone)
rescue
return [404, OZones::Error.new("Error: Couldn't update vdc. " \
"VDC #{vdc_id} not found.").to_json]
end
if (!force or force.upcase!="YES") and
!host_uniqueness?(zone, hosts, vdc_id.to_i)
end
if (!vdc_data[:force] or vdc_data[:force].upcase!="YES") and
!host_uniqueness?(zone, vdc_data[:hosts], vdc_id.to_i)
return [403, OZones::Error.new(
"Error: Couldn't update resource #{kind}. " +
"Hosts are not unique, and no force option" +
" was given.").to_json]
end
rc = vdc.update(vdc_data[:hosts])
if !OpenNebula.is_error?(rc)
return [200, rc]
else
return [500, OZones::Error.new(
"Error: Couldn't update resource #{kind.upcase}." \
" Reason: #{rc.message}").to_json]
end
else
error = OZones::Error.new(
"Error: #{kind.upcase} resource update not supported")
return [404, error.to_json]
end
return [403, OZones::Error.new("Error: Couldn't update vdc. " \
"Hosts are not unique, use force to override").to_json]
end
rc = vdc.update(hosts)
if !OpenNebula.is_error?(rc)
return [200, rc]
else
return [500, OZones::Error.new("Error: Couldn't update vdc. " \
" Reason: #{rc.message}").to_json]
end
end
############################################################################

View File

@ -226,48 +226,60 @@ get '/config' do
end
##############################################################################
# GET Pool information
# GETs information
##############################################################################
get '/:pool' do
@OzonesServer.get_pool(params[:pool])
get '/vdc' do
@OzonesServer.get_vdcs
end
get '/vdc/:id' do
@OzonesServer.get_vdc(params[:id])
end
get '/zone' do
@OzonesServer.get_zones
end
get '/zone/:pool' do
@OzonesServer.get_zones_pool(params[:pool])
end
get '/zone/:id' do
@OzonesServer.get_zone(params[:id])
end
get '/zone/:id/:pool' do
@OzonesServer.get_zone_pool(params[:id], params[:pool])
end
##############################################################################
# GET Resource information
# POSTs information
##############################################################################
get %r{/(zone|vdc)/(\d+)/(\w+)} do |kind, id, aggpool|
@OzonesServer.get_full_resource(kind,id,aggpool)
post '/vdc' do
@OzonesServer.create_vdc(params,request.body.read, @pr)
end
get %r{/(zone|vdc)/(\d+)} do |kind, id|
@OzonesServer.get_resource(kind,id)
end
get '/:pool/:aggpool' do
@OzonesServer.get_aggregated_pool(params[:pool], params[:aggpool])
end
##############################################################################
# Create a new Resource
##############################################################################
post '/:pool' do
@OzonesServer.create_resource(params[:pool], params, request.body.read, @pr)
post '/zone' do
@OzonesServer.create_zone(params,request.body.read, @pr)
end
##############################################################################
# Update Resource
##############################################################################
put '/:resource/:id' do
@OzonesServer.update_resource(params[:resource], params,
request.body.read, @pr)
put '/vdc/:id' do
@OzonesServer.update_vdc(params, request.body.read, @pr)
end
##############################################################################
# Delete Resource
##############################################################################
delete '/:resource/:id' do
@OzonesServer.delete_resource(params[:resource], params[:id], @pr)
delete '/vdc/:id' do
@OzonesServer.delete_vdc(params[:id], @pr)
end
delete '/zone/:id' do
@OzonesServer.delete_zone(params[:id], @pr)
end