From 506f996b1624e88dd3a973c0cee14531efb63b32 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 24 Sep 2011 19:37:27 +0200 Subject: [PATCH] feature #789: Refactor of OCA interaction in OZones. Work in progress --- src/ozones/Server/lib/OZones/Zones.rb | 148 ++++++++++++++++++-- src/ozones/Server/models/OzonesServer.rb | 165 ++++++++--------------- 2 files changed, 194 insertions(+), 119 deletions(-) diff --git a/src/ozones/Server/lib/OZones/Zones.rb b/src/ozones/Server/lib/OZones/Zones.rb index 6f4a2e167a..528ab3dfa3 100644 --- a/src/ozones/Server/lib/OZones/Zones.rb +++ b/src/ozones/Server/lib/OZones/Zones.rb @@ -19,25 +19,38 @@ module OZones class Zones include DataMapper::Resource include OpenNebulaJSON::JSONUtils - extend OpenNebulaJSON::JSONUtils - - property :id, Serial - property :name, String, :required => true, :unique => true - property :onename, String, :required => true - property :onepass, String, :required => true - property :endpoint, String, :required => true - property :sunsendpoint, String + extend OpenNebulaJSON::JSONUtils + + ####################################################################### + # Data Model for the Zone + ####################################################################### + property :id, Serial + property :name, String, :required => true, :unique => true + property :onename, String, :required => true + property :onepass, String, :required => true + property :endpoint, String, :required => true + property :sunsendpoint, String has n, :vdcs + ####################################################################### + # Constants + ####################################################################### + ZONE_ATTRS = [:onename, :onepass, :endpoint, :name] + + ####################################################################### + # JSON Functions + ####################################################################### def self.to_hash zonePoolHash = Hash.new zonePoolHash["ZONE_POOL"] = Hash.new zonePoolHash["ZONE_POOL"]["ZONE"] = Array.new unless self.all.empty? + self.all.each{|zone| zonePoolHash["ZONE_POOL"]["ZONE"] << zone.attributes.merge({:numbervdcs => zone.vdcs.all.size}) } + return zonePoolHash end @@ -45,11 +58,130 @@ module OZones zone_attributes = Hash.new zone_attributes["ZONE"] = attributes zone_attributes["ZONE"][:vdcs] = Array.new + self.vdcs.all.each{|vdc| zone_attributes["ZONE"][:vdcs]< 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 - # TODO build the vdc retrieval - - if kind == "zone" - client = OpenNebula::Client.new( - resource.onename + ":" + resource.onepass, - resource.endpoint, - false) - - simple_pool = case aggkind - when "host" then OpenNebulaJSON::HostPoolJSON.new(client) - when "image" then OpenNebulaJSON::ImagePoolJSON.new(client) - when "user" then OpenNebulaJSON::UserPoolJSON.new(client) - when "vm" then OpenNebulaJSON::VirtualMachinePoolJSON.new(client) - when "vn","vnet" then OpenNebulaJSON::VirtualNetworkPoolJSON.new(client) - when "template","vmtemplate" then OpenNebulaJSON::TemplatePoolJSON.new(client) - else - error = OZones::Error.new( - "Error: #{aggkind} aggregated pool for #{kind} #{id} not supported") - return [404, error.to_json] - end - - simple_pool.info - - return [200, simple_pool.to_json] - end + return rc end # Get a json representation resource with local (DB) info def get_resource(kind, id) resource = retrieve_resource(kind, id) + if OZones.is_error?(resource) return [404, resource.to_json] else @@ -109,25 +89,6 @@ class OzonesServer end end - # Get hold of a object of a particular kind - def retrieve_resource(kind, id) - resource = case kind - when "vdc" then OZones::Vdc.get(id) - when "zone" then OZones::Zones.get(id) - else - return OZones::Error.new( - "Error: #{kind} resource not supported") - end - - if resource - return resource - else - return OZones::Error.new( - "Error: Resource #{kind} with id #{id} not found") - end - end - - ############################################################################ # Create resources ############################################################################ @@ -218,47 +179,14 @@ class OzonesServer end when "zone" then - zone_data=Hash.new - data.each{|key,value| - zone_data[key.downcase.to_sym]=value if key!="pool" - } - - mandatory_params = [:onename, :onepass, :endpoint, :name] - - mandatory_params.each { |param| - if !zone_data[param] - return [400, OZones::Error.new( - "Error: Couldn't create resource #{kind}. " + - "Mandatory attribute '#{param}' is missing.").to_json] - end - } - - # Digest and check credentials - zone_data[:onepass] = - Digest::SHA1.hexdigest(zone_data[:onepass]) - - rc = @ocaInt.check_oneadmin(zone_data[:onename], - zone_data[:onepass], - zone_data[:endpoint]) - - if OpenNebula.is_error?(rc) - return [400, OZones::Error.new( - "Error: Couldn't create resource #{kind}. Reason: "+ - rc.message).to_json] - end - - # Create the zone zone = OZones::Zones.create(zone_data) - rc = zone.save - if rc - pr.update # Rewrite proxy conf file - return [200, zone.to_json] - else - return [400, OZones::Error.new( - "Error: Couldn't create resource #{kind.upcase}." + - " Maybe duplicated name?").to_json] + 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") @@ -375,9 +303,7 @@ class OzonesServer end end - ############################################################################ - # Helper functions - ############################################################################ + private # Check if hosts are already include in any Vdc of the zone def host_uniqueness?(zone, host_list, vdc_id = -1) @@ -397,5 +323,22 @@ class OzonesServer return true end + # Get hold of a object of a particular kind + def retrieve_resource(kind, id) + rc = case kind + when "vdc" then + OZones::Vdc.get(id) + when "zone" then + OZones::Zones.get(id) + else + OZones::Error.new("Error: #{kind} resource not supported") + end + + if rc + return rc + else + return OZones::Error.new("Error: #{kind} with id #{id} not found") + end + end end