From 067879d770d32ca97551fe20f3517285a3d90ccc Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Tue, 11 Oct 2011 15:21:49 +0200 Subject: [PATCH] Feature #789: First version of ozones api Slightly tested. Uncommented. To be extended with some utils (like printing helpers). --- src/ozones/Client/lib/api/zona.rb | 33 +++++++ .../Client/lib/api/zona/OZonesElement.rb | 79 +++++++++++++++++ src/ozones/Client/lib/api/zona/OZonesJSON.rb | 63 ++++++++++++++ src/ozones/Client/lib/api/zona/OZonesPool.rb | 54 ++++++++++++ src/ozones/Client/lib/api/zona/VDCElement.rb | 87 +++++++++++++++++++ src/ozones/Client/lib/api/zona/VDCPool.rb | 34 ++++++++ src/ozones/Client/lib/api/zona/ZoneElement.rb | 52 +++++++++++ src/ozones/Client/lib/api/zona/ZonePool.rb | 36 ++++++++ 8 files changed, 438 insertions(+) create mode 100644 src/ozones/Client/lib/api/zona.rb create mode 100644 src/ozones/Client/lib/api/zona/OZonesElement.rb create mode 100644 src/ozones/Client/lib/api/zona/OZonesJSON.rb create mode 100644 src/ozones/Client/lib/api/zona/OZonesPool.rb create mode 100644 src/ozones/Client/lib/api/zona/VDCElement.rb create mode 100644 src/ozones/Client/lib/api/zona/VDCPool.rb create mode 100644 src/ozones/Client/lib/api/zona/ZoneElement.rb create mode 100644 src/ozones/Client/lib/api/zona/ZonePool.rb diff --git a/src/ozones/Client/lib/api/zona.rb b/src/ozones/Client/lib/api/zona.rb new file mode 100644 index 0000000000..bdb392371e --- /dev/null +++ b/src/ozones/Client/lib/api/zona.rb @@ -0,0 +1,33 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'rubygems' +require 'OZonesClient.rb' + +require 'zona/OZonesJSON' + +require 'zona/OZonesPool' +require 'zona/OZonesElement' + +require 'zona/ZonePool' +require 'zona/ZoneElement' + +require 'zona/VDCPool' +require 'zona/VDCElement' + +module Zona + +end diff --git a/src/ozones/Client/lib/api/zona/OZonesElement.rb b/src/ozones/Client/lib/api/zona/OZonesElement.rb new file mode 100644 index 0000000000..c375875bdc --- /dev/null +++ b/src/ozones/Client/lib/api/zona/OZonesElement.rb @@ -0,0 +1,79 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module Zona + + class OZonesElement < JSONElement + + protected + + def initialize(hash, client) + @client = client + @json_hash = hash + + @pe_id = self["id"] ? self["id"].to_i : nil + @name = self["name"] ? self["name"] : nil + end + + def info(kind, root_element) + return Error.new('ID not defined') if !@pe_id + + rc = @client.get_resource(kind,@pe_id) + if !OZonesClient.is_error?(rc) + initialize_json(rc.body,root_element) + + rc = nil + + @pe_id = self["id"] ? self["id"].to_i : nil + @name = self["name"] ? self["name"] : nil + + end + rc + end + + def allocate_hash(kind, tmpl_hash) + allocate(kind, tmpl_hash.to_json) + end + + def allocate(kind, tmpl_json) + rc = @client.post_resource(kind, tmpl_json) + + if !OZonesClient.is_error?(rc) + initialize_json(rc.body,kind.upcase) + @pe_id = self["id"].to_i + rc = nil + end + rc + end + + def delete(kind) + return Error.new('ID not defined') if !@pe_id + + rc = @client.delete_resource(kind,@pe_id) + return rc if OZonesClient.is_error?(rc) + nil + end + + public + + attr_reader :pe_id, :name + + def self.new_with_id(id, client=nil) + self.new(self.build_json(id),client) + end + + end +end diff --git a/src/ozones/Client/lib/api/zona/OZonesJSON.rb b/src/ozones/Client/lib/api/zona/OZonesJSON.rb new file mode 100644 index 0000000000..0cfd1fbd17 --- /dev/null +++ b/src/ozones/Client/lib/api/zona/OZonesJSON.rb @@ -0,0 +1,63 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + + +module Zona + + require 'json' + + class JSONElement + def initialize(json_hash=nil) + @json_hash=json_hash + end + + def initialize_json(json_str, root_element) + rc = JSONElement.build_json(json_str,root_element) + @json_hash = rc + + if OZonesClient.is_error?(rc) || (rc.size == 0) + @json_hash=nil + end + end + + def self.build_json(json_str, root_element) + begin + parser = JSON.parser.new(json_str, {:symbolize_names => false}) + hash = parser.parse + hash[root_element] + rescue => e + OZonesClient::Error.new(e.message) + end + end + + def [](key) + @json_hash[key] + end + end + + class JSONPool < JSONElement + def initialize(json_hash=nil) + super(json_hash) + end + + def each_element(block) + @json_hash[@element_name].each do |elem| + block.call self.factory(elem) + end + end + end + +end diff --git a/src/ozones/Client/lib/api/zona/OZonesPool.rb b/src/ozones/Client/lib/api/zona/OZonesPool.rb new file mode 100644 index 0000000000..31952ef0ea --- /dev/null +++ b/src/ozones/Client/lib/api/zona/OZonesPool.rb @@ -0,0 +1,54 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module Zona + + class OZonesPool < JSONPool + + protected + + def initialize(pool,element,client) + super(nil) + + @client = client + @pool_name = pool.upcase + @element_name = element.upcase + end + + def factory(element_json) + Zona::OZonesPoolElement.new(element_json, @client) + end + + def info(kind) + rc = @client.get_pool(kind) + + if !OZonesClient.is_error?(rc) + initialize_json(rc.body,@pool_name) + rc=nil + end + + rc + end + + public + + def each(&block) + each_element(block) if @json_hash + end + + end + +end diff --git a/src/ozones/Client/lib/api/zona/VDCElement.rb b/src/ozones/Client/lib/api/zona/VDCElement.rb new file mode 100644 index 0000000000..73f57b10b0 --- /dev/null +++ b/src/ozones/Client/lib/api/zona/VDCElement.rb @@ -0,0 +1,87 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module Zona + + class VDC < OZonesElement + + VDC_KIND = "vdc" + + def self.build_json(pe_id=nil) + if pe_id + json = "{\"VDC\":{\"id\":#{pe_id}}}" + else + json = '{"VDC":{}}' + end + JSONElement.build_json(json,"VDC") + end + + def initialize(hash, client) + super(hash, client) + end + + def info + super(VDC_KIND,"VDC") + end + + def allocate_hash(template) + super(VDC_KIND,template) + end + + def allocate(template) + super(VDC_KIND,template) + end + + def delete + super(VDC_KIND) + end + + def addhosts(hosts_array,options={}) + return Error.new('VDC not info-ed') if !@json_hash + + # array of hosts, integers + hosts = self["hosts"].split(',').collect!{|x| x.to_i} + hosts.concat(hosts_array).uniq! + + new_hosts = hosts.join(',') + template = {:id => @pe_id, :hosts => new_hosts} + template[:force] = "yes" if options[:force] + + template = {:vdc => template} + + rc = @client.put_resource(VDC_KIND,@pe_id,template.to_json) + return rc if OZonesClient.is_error?(rc) + nil + end + + def delhosts(hosts_array) + return Error.new('VDC not info-ed') if !@json_hash + + hosts = self["hosts"].split(',').collect!{|x| x.to_i} + + new_hosts = (hosts - hosts_array).join(',') + template = {:vdc => {:id => @pe_id, :hosts => new_hosts}} + + rc = @client.put_resource(VDC_KIND,@pe_id,template.to_json) + return rc if OZonesClient.is_error?(rc) + nil + end + + alias :addhost :addhosts + alias :delhost :delhosts + + end +end diff --git a/src/ozones/Client/lib/api/zona/VDCPool.rb b/src/ozones/Client/lib/api/zona/VDCPool.rb new file mode 100644 index 0000000000..c019829ca5 --- /dev/null +++ b/src/ozones/Client/lib/api/zona/VDCPool.rb @@ -0,0 +1,34 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module Zona + + class VDCPool < OZonesPool + VDC_POOL_KIND="vdc" + + def initialize(client) + super("VDC_POOL", "VDC", client) + end + + def factory(element_json) + Zona::VDC.new(element_json,@client) + end + + def info + super(VDC_POOL_KIND) + end + end +end diff --git a/src/ozones/Client/lib/api/zona/ZoneElement.rb b/src/ozones/Client/lib/api/zona/ZoneElement.rb new file mode 100644 index 0000000000..31b891a979 --- /dev/null +++ b/src/ozones/Client/lib/api/zona/ZoneElement.rb @@ -0,0 +1,52 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module Zona + + class Zone < OZonesElement + + ZONE_KIND = "zone" + + def self.build_json(pe_id=nil) + if pe_id + json = "{\"ZONE\":{\"id\":#{pe_id}}}" + else + json = '{"ZONE":{}}' + end + JSONElement.build_json(json,"ZONE") + end + + def initialize(hash, client) + super(hash, client) + end + + def info + super(ZONE_KIND,"ZONE") + end + + def allocate_hash(template) + super(ZONE_KIND,template) + end + + def allocate(template) + super(ZONE_KIND,template) + end + + def delete + super(ZONE_KIND) + end + end +end diff --git a/src/ozones/Client/lib/api/zona/ZonePool.rb b/src/ozones/Client/lib/api/zona/ZonePool.rb new file mode 100644 index 0000000000..1cfe72280d --- /dev/null +++ b/src/ozones/Client/lib/api/zona/ZonePool.rb @@ -0,0 +1,36 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module Zona + + class ZonePool < OZonesPool + + ZONE_POOL_KIND = "zone" + + def initialize(client) + super("ZONE_POOL", "ZONE", client) + end + + def factory(element_json) + Zona::Zone.new(element_json,@client) + end + + def info + super(ZONE_POOL_KIND) + end + end + +end