mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-21 14:50:08 +03:00
Feature #789: First version of ozones api
Slightly tested. Uncommented. To be extended with some utils (like printing helpers).
This commit is contained in:
parent
e20851f044
commit
067879d770
33
src/ozones/Client/lib/api/zona.rb
Normal file
33
src/ozones/Client/lib/api/zona.rb
Normal file
@ -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
|
79
src/ozones/Client/lib/api/zona/OZonesElement.rb
Normal file
79
src/ozones/Client/lib/api/zona/OZonesElement.rb
Normal file
@ -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
|
63
src/ozones/Client/lib/api/zona/OZonesJSON.rb
Normal file
63
src/ozones/Client/lib/api/zona/OZonesJSON.rb
Normal file
@ -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
|
54
src/ozones/Client/lib/api/zona/OZonesPool.rb
Normal file
54
src/ozones/Client/lib/api/zona/OZonesPool.rb
Normal file
@ -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
|
87
src/ozones/Client/lib/api/zona/VDCElement.rb
Normal file
87
src/ozones/Client/lib/api/zona/VDCElement.rb
Normal file
@ -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
|
34
src/ozones/Client/lib/api/zona/VDCPool.rb
Normal file
34
src/ozones/Client/lib/api/zona/VDCPool.rb
Normal file
@ -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
|
52
src/ozones/Client/lib/api/zona/ZoneElement.rb
Normal file
52
src/ozones/Client/lib/api/zona/ZoneElement.rb
Normal file
@ -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
|
36
src/ozones/Client/lib/api/zona/ZonePool.rb
Normal file
36
src/ozones/Client/lib/api/zona/ZonePool.rb
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user