mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-21 14:50:08 +03:00
feature #789: More work on the Ozones server refactor
This commit is contained in:
parent
cc34893f6d
commit
da42de1d6f
@ -95,7 +95,7 @@ EOT
|
||||
end
|
||||
|
||||
def post_resource_str(kind, tmpl_str)
|
||||
body_str = OZonesClient::to_body(tmpl_str)
|
||||
body_str = OZonesClient::to_body(kind, tmpl_str)
|
||||
|
||||
url = URI.parse("#{@endpoint}/#{kind}")
|
||||
|
||||
@ -112,7 +112,7 @@ EOT
|
||||
end
|
||||
|
||||
def put_resource(kind, id, tmpl_str)
|
||||
body_str = OZonesClient::to_body(tmpl_str)
|
||||
body_str = OZonesClient::to_body(kind, tmpl_str)
|
||||
|
||||
url = URI.parse("#{@endpoint}/#{kind}/#{id}")
|
||||
|
||||
@ -248,16 +248,19 @@ EOT
|
||||
# JSON & Template utils
|
||||
##########################################################################
|
||||
|
||||
def self.to_body(tmpl_str)
|
||||
body_str = ""
|
||||
def self.to_body(kind, tmpl_str)
|
||||
body_str = "{\n"
|
||||
body_str << " \"#{kind}\": {"
|
||||
|
||||
tmpl_str.strip.each_line{|line|
|
||||
line.strip!
|
||||
key,value = line.split("=")
|
||||
body_str = body_str + key + "=" + URI.escape(value) + "&"
|
||||
body_str << "\n \"#{key}\": \"#{value}\","
|
||||
}
|
||||
body_str.chop!
|
||||
|
||||
body_str = body_str[0..-1]
|
||||
body_str << "\n }\n"
|
||||
body_str << "}\n"
|
||||
end
|
||||
|
||||
def self.parse_json(json_str, root_element)
|
||||
|
@ -60,25 +60,33 @@ module OZones
|
||||
VDC_ATTRS = [:vdcadminname, :vdcadminpass, :name, :hosts]
|
||||
|
||||
attr_reader :vdc
|
||||
attr_reader :zone
|
||||
|
||||
#Creates an OpenNebula VDC, using its ID, vdcid and the associated zone
|
||||
def initialize(vdcid, zone = nil)
|
||||
|
||||
if vdcid != -1
|
||||
@vdc = Vdc.get(vdcid)
|
||||
|
||||
if !@vdc
|
||||
raise "Error: VDC with id #{vdcid} not found"
|
||||
raise "VDC with id #{vdcid} not found."
|
||||
end
|
||||
|
||||
zone = OZones::Zones.get(@vdc.zones_id)
|
||||
@zone = OZones::Zones.get(@vdc.zones_id)
|
||||
else
|
||||
@zone = zone
|
||||
end
|
||||
|
||||
@client = OpenNebula::Client.new(
|
||||
"#{zone.onename}:#{zone.onepass}",
|
||||
zone.endpoint,
|
||||
"#{@zone.onename}:#{@zone.onepass}",
|
||||
@zone.endpoint,
|
||||
false)
|
||||
end
|
||||
|
||||
def to_json
|
||||
@vdc.to_json
|
||||
end
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
#######################################################################
|
||||
@ -148,30 +156,37 @@ module OZones
|
||||
}
|
||||
|
||||
# Delete the group
|
||||
rc = OpenNebula::Group.new_with_id(@vdc.group_id, @client).delete
|
||||
OpenNebula::Group.new_with_id(@vdc.group_id, @client).delete
|
||||
|
||||
if OpenNebula.is_error?(rc)
|
||||
return rc
|
||||
else
|
||||
return @vdc.destroy
|
||||
end
|
||||
return @vdc.destroy
|
||||
end
|
||||
|
||||
#Cleans bootstrap operations in a zone
|
||||
def clean_bootstrap
|
||||
delete_acls
|
||||
|
||||
OpenNebula::User.new_with_id(@vdc.vdcadmin_id, @client).delete
|
||||
OpenNebula::Group.new_with_id(@vdc.group_id, @client).delete
|
||||
end
|
||||
|
||||
def update(host_list)
|
||||
# Delete existing host ACLs
|
||||
delete_host_acls
|
||||
|
||||
@vdc.acls =~ /((\d+,){#{HOST_ACL_FIRST_ID}}).*/
|
||||
newacls = $1.chop
|
||||
|
||||
# Create new ACLs. TODO Rollback ACL creation
|
||||
host_acls = get_host_acls(host_list)
|
||||
rc, acls_str = create_acls(host_acls)
|
||||
if !host_list.empty?
|
||||
host_acls = get_host_acls(host_list)
|
||||
rc, acls_str = create_acls(host_acls)
|
||||
|
||||
return rc if OpenNebula.is_error?(rc)
|
||||
return rc if OpenNebula.is_error?(rc)
|
||||
|
||||
#Create the new acl string.
|
||||
@vdc.acls =~ /((\d,){HOST_ACL_FIRST_ID}).*/
|
||||
#Create the new acl string.
|
||||
newacls << "," << acls_str
|
||||
end
|
||||
|
||||
newacls = $1.chop
|
||||
newacls << acls_str
|
||||
|
||||
#Update the VDC Record
|
||||
begin
|
||||
|
@ -73,12 +73,12 @@ module OZones
|
||||
zone_data = Hash.new
|
||||
|
||||
data.each{|key,value|
|
||||
zone_data[key.downcase.to_sym] = value if key!="pool"
|
||||
zone_data[key.downcase.to_sym] = value
|
||||
}
|
||||
|
||||
ZONE_ATTRS.each { |param|
|
||||
if !zone_data[param]
|
||||
return OZones::Error.new("Error: Couldn't create zone." \
|
||||
return OZones::Error.new("Error: Couldn't create zone. " \
|
||||
"Mandatory attribute '#{param}' is missing.")
|
||||
end
|
||||
}
|
||||
@ -92,7 +92,7 @@ module OZones
|
||||
|
||||
if OpenNebula.is_error?(rc)
|
||||
return OZones::Error.new("Error: Couldn't create zone. "\
|
||||
"Reason: #{rc.message}.")
|
||||
"Reason: #{rc.message}")
|
||||
end
|
||||
|
||||
# Create the zone
|
||||
|
@ -75,16 +75,19 @@ class OzonesServer
|
||||
############################################################################
|
||||
# Create resources
|
||||
############################################################################
|
||||
def create_vdc (data, body,pr)
|
||||
def create_vdc (body,pr)
|
||||
#Setup POST data
|
||||
if body.size > 0
|
||||
result = parse_json(body,"vdc")
|
||||
data = result if !OpenNebula.is_error?(result)
|
||||
data = parse_json(body,"vdc")
|
||||
|
||||
if OpenNebula.is_error?(data)
|
||||
return [400, OZones::Error.new("Error: Couldn't update vdc. " \
|
||||
"Reason: #{data.message}.").to_json]
|
||||
end
|
||||
|
||||
vdc_data = Hash.new
|
||||
|
||||
data.each{|key,value|
|
||||
vdc_data[key.downcase.to_sym] = value if key!="pool"
|
||||
vdc_data[key.downcase.to_sym] = value
|
||||
}
|
||||
|
||||
#Get the Zone that will host the VDC. And check resouces
|
||||
@ -125,7 +128,8 @@ class OzonesServer
|
||||
begin
|
||||
zone.save
|
||||
rescue => e
|
||||
#TODO Rollback VDC creation
|
||||
vdc.clean_bootstrap
|
||||
|
||||
return [400, OZones::Error.new("Error: Couldn't create " \
|
||||
"vdc. Zone could not be saved: #{e.message}").to_json]
|
||||
end
|
||||
@ -134,11 +138,13 @@ class OzonesServer
|
||||
return [200, vdc.to_json]
|
||||
end
|
||||
|
||||
def create_zone(data, body, pr)
|
||||
def create_zone(body, pr)
|
||||
#Setup POST data
|
||||
if body.size > 0
|
||||
result = parse_json(body,"zone")
|
||||
data = result if !OpenNebula.is_error?(result)
|
||||
data = parse_json(body,"zone")
|
||||
|
||||
if OpenNebula.is_error?(data)
|
||||
return [400, OZones::Error.new("Error: Couldn't update vdc. " \
|
||||
"Reason: #{data.message}.").to_json]
|
||||
end
|
||||
|
||||
zone = OZones::Zones.create(data)
|
||||
@ -154,39 +160,40 @@ class OzonesServer
|
||||
############################################################################
|
||||
# Update resources
|
||||
############################################################################
|
||||
def update_vdc(data, body, pr)
|
||||
def update_vdc(vdc_id, body)
|
||||
#Setup PUT data
|
||||
if body.size > 0
|
||||
result = parse_json(body,"vdc")
|
||||
data = result if !OpenNebula.is_error?(result)
|
||||
data = parse_json(body,"vdc")
|
||||
|
||||
if OpenNebula.is_error?(data)
|
||||
return [400, OZones::Error.new("Error: Couldn't update vdc. " \
|
||||
"Reason: #{data.message}.").to_json]
|
||||
end
|
||||
|
||||
vdc_data = Hash.new
|
||||
vdc_id = nil
|
||||
|
||||
data.each{|key,value|
|
||||
vdc_data[key.downcase.to_sym]=value
|
||||
}
|
||||
|
||||
vdc_id = vdc_data.delete(:id)
|
||||
hosts = vdc_data.delete(:hosts)
|
||||
force = vdc_data.delete(:force)
|
||||
|
||||
# Check parameters
|
||||
if !hosts || !vdc_id
|
||||
if !hosts
|
||||
return [400, OZones::Error.new("Error: Couldn't update vdc. " \
|
||||
"Missing ID or HOSTS.").to_json]
|
||||
"Missing 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: Couldn't update vdc. " \
|
||||
"VDC #{vdc_id} not found.").to_json]
|
||||
vdc = OZones::OpenNebulaVdc.new(vdc_id)
|
||||
rescue => e
|
||||
return [404, OZones::Error.new("Error: Couldn't update vdc. " \
|
||||
"#{e.message}").to_json]
|
||||
end
|
||||
|
||||
if (!force or force.upcase!="YES") and
|
||||
!host_uniqueness?(zone, hosts, vdc_id.to_i)
|
||||
if (!force or force.upcase != "YES") and
|
||||
!host_uniqueness?(vdc.zone, hosts, vdc_id.to_i)
|
||||
|
||||
return [403, OZones::Error.new("Error: Couldn't update vdc. " \
|
||||
"Hosts are not unique, use force to override").to_json]
|
||||
|
@ -226,7 +226,7 @@ get '/config' do
|
||||
end
|
||||
|
||||
##############################################################################
|
||||
# GETs information
|
||||
# GET information
|
||||
##############################################################################
|
||||
get '/vdc' do
|
||||
@OzonesServer.get_vdcs
|
||||
@ -240,16 +240,16 @@ get '/zone' do
|
||||
@OzonesServer.get_zones
|
||||
end
|
||||
|
||||
get '/zone/:pool' do
|
||||
@OzonesServer.get_zones_pool(params[:pool])
|
||||
get %r{/zone/([\d]+)/([\w]+)} do
|
||||
@OzonesServer.get_zone_pool(params[:captures][0], params[:captures][1])
|
||||
end
|
||||
|
||||
get '/zone/:id' do
|
||||
@OzonesServer.get_zone(params[:id])
|
||||
get %r{/zone/([\d]+)} do
|
||||
@OzonesServer.get_zone(params[:captures].first)
|
||||
end
|
||||
|
||||
get '/zone/:id/:pool' do
|
||||
@OzonesServer.get_zone_pool(params[:id], params[:pool])
|
||||
get %r{/zone/([\w]+)} do
|
||||
@OzonesServer.get_zones_pool(params[:captures].first)
|
||||
end
|
||||
|
||||
##############################################################################
|
||||
@ -257,18 +257,18 @@ end
|
||||
##############################################################################
|
||||
|
||||
post '/vdc' do
|
||||
@OzonesServer.create_vdc(params,request.body.read, @pr)
|
||||
@OzonesServer.create_vdc(request.body.read, @pr)
|
||||
end
|
||||
|
||||
post '/zone' do
|
||||
@OzonesServer.create_zone(params,request.body.read, @pr)
|
||||
@OzonesServer.create_zone(request.body.read, @pr)
|
||||
end
|
||||
|
||||
##############################################################################
|
||||
# Update Resource
|
||||
##############################################################################
|
||||
put '/vdc/:id' do
|
||||
@OzonesServer.update_vdc(params, request.body.read, @pr)
|
||||
@OzonesServer.update_vdc(params[:id], request.body.read)
|
||||
end
|
||||
|
||||
##############################################################################
|
||||
|
Loading…
x
Reference in New Issue
Block a user