mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-21 14:50:08 +03:00
Task #908: Integrate OzonesClient in zona.rb api.
This commit deletes OzonesClient.rb. All functionality has been merged to zona.rb and OzonesJSON.rb. All dependent files have been accordingly updated. Cli operations have been manually tested. API operations have not been tested yet.
This commit is contained in:
parent
5412c6fec2
commit
d28dd0b445
@ -372,7 +372,6 @@ INSTALL_CLIENT_FILES=(
|
||||
CLI_LIB_FILES:$LIB_LOCATION/ruby/cli
|
||||
ONE_CLI_LIB_FILES:$LIB_LOCATION/ruby/cli/one_helper
|
||||
ETC_CLIENT_FILES:$ETC_LOCATION
|
||||
OZONES_LIB_CLIENT_FILES:$LIB_LOCATION/ruby
|
||||
OZONES_BIN_CLIENT_FILES:$BIN_LOCATION
|
||||
OZONES_LIB_CLIENT_CLI_FILES:$LIB_LOCATION/ruby/cli
|
||||
OZONES_LIB_CLIENT_CLI_HELPER_FILES:$LIB_LOCATION/ruby/cli/ozones_helper
|
||||
@ -432,7 +431,6 @@ INSTALL_OZONES_FILES=(
|
||||
OZONES_PUBLIC_IMAGES_FILES:$OZONES_LOCATION/public/images
|
||||
OZONES_PUBLIC_CSS_FILES:$OZONES_LOCATION/public/css
|
||||
OZONES_PUBLIC_JS_PLUGINS_FILES:$OZONES_LOCATION/public/js/plugins
|
||||
OZONES_LIB_CLIENT_FILES:$LIB_LOCATION/ruby
|
||||
OZONES_BIN_CLIENT_FILES:$BIN_LOCATION
|
||||
OZONES_LIB_CLIENT_CLI_FILES:$LIB_LOCATION/ruby/cli
|
||||
OZONES_LIB_CLIENT_CLI_HELPER_FILES:$LIB_LOCATION/ruby/cli/ozones_helper
|
||||
@ -1106,8 +1104,6 @@ OZONES_PUBLIC_JS_PLUGINS_FILES="src/ozones/Server/public/js/plugins/zones-tab.js
|
||||
src/ozones/Server/public/js/plugins/aggregated-tab.js \
|
||||
src/ozones/Server/public/js/plugins/dashboard-tab.js"
|
||||
|
||||
OZONES_LIB_CLIENT_FILES="src/ozones/Client/lib/OZonesClient.rb"
|
||||
|
||||
OZONES_LIB_CLIENT_CLI_FILES="src/ozones/Client/lib/cli/ozones_helper.rb"
|
||||
|
||||
OZONES_LIB_CLIENT_CLI_HELPER_FILES="\
|
||||
|
@ -49,8 +49,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
||||
helper.show_resource(args[0],options)
|
||||
end
|
||||
|
||||
command :list, 'Lists VDCs in the pool',
|
||||
:options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do
|
||||
command :list, 'Lists VDCs in the pool' do
|
||||
helper.list_pool(options)
|
||||
end
|
||||
|
||||
|
@ -114,8 +114,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
||||
0
|
||||
end
|
||||
|
||||
command :list, 'Lists Zones in the pool',
|
||||
:options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do
|
||||
command :list, 'Lists Zones in the pool' do
|
||||
helper.list_pool(options)
|
||||
end
|
||||
|
||||
|
@ -1,286 +0,0 @@
|
||||
# -------------------------------------------------------------------------- #
|
||||
# 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 'uri'
|
||||
require 'net/https'
|
||||
require 'json'
|
||||
require 'OpenNebula/Configuration'
|
||||
|
||||
module OZonesClient
|
||||
class Client
|
||||
|
||||
OZONES_VERSION = <<EOT
|
||||
oZones 1.0
|
||||
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
|
||||
EOT
|
||||
|
||||
######################################################################
|
||||
# Initialize client library
|
||||
######################################################################
|
||||
def initialize(user=nil, pass=nil, endpoint_str=nil,
|
||||
timeout=nil, debug_flag=true)
|
||||
@debug = debug_flag
|
||||
@timeout = timeout
|
||||
|
||||
# Server location
|
||||
if endpoint_str
|
||||
@endpoint = endpoint_str
|
||||
elsif ENV["OZONES_URL"]
|
||||
@endpoint = ENV["OZONES_URL"]
|
||||
else
|
||||
@endpoint = "http://localhost:6121"
|
||||
end
|
||||
|
||||
# Autentication
|
||||
if user && pass
|
||||
@ozonesauth = [user, pass]
|
||||
elsif ENV['OZONES_AUTH']
|
||||
@ozonesauth=File.read(ENV['OZONES_AUTH']).strip.split(':')
|
||||
end
|
||||
|
||||
if !@ozonesauth
|
||||
raise "No authorization data present"
|
||||
end
|
||||
|
||||
if @ozonesauth.size != 2
|
||||
raise "Authorization data malformed"
|
||||
end
|
||||
end
|
||||
|
||||
#####################################
|
||||
# General Resource Request Methods #
|
||||
####################################
|
||||
|
||||
######################################################################
|
||||
# Retieves all elements on a pool
|
||||
# :zonetemplate
|
||||
######################################################################
|
||||
def get_pool(kind)
|
||||
url = URI.parse(@endpoint+"/" + kind)
|
||||
req = Net::HTTP::Get.new(url.path)
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = OZonesClient::http_start(url, @timeout) {|http|
|
||||
http.request(req)
|
||||
}
|
||||
|
||||
return OZonesClient::parse_error(res, kind)
|
||||
end
|
||||
|
||||
######################################################################
|
||||
# Post a new Resource to the relevant OZones Pool
|
||||
# :zonetemplate
|
||||
######################################################################
|
||||
def post_resource_file(kind, template)
|
||||
tmpl_str = File.read(template)
|
||||
post_resource_str(kind, tmpl_str)
|
||||
end
|
||||
|
||||
def post_resource_str(kind, tmpl_str)
|
||||
tmpl_json = OZonesClient::tobody(tmpl_str)
|
||||
post_resource(kind, tmpl_json)
|
||||
end
|
||||
|
||||
def post_resource(kind, tmpl_json)
|
||||
url = URI.parse("#{@endpoint}/#{kind}")
|
||||
|
||||
req = Net::HTTP::Post.new(url.path)
|
||||
req.body=tmpl_json
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = OZonesClient::http_start(url, @timeout) do |http|
|
||||
http.request(req)
|
||||
end
|
||||
|
||||
return OZonesClient::parse_error(res, kind)
|
||||
end
|
||||
|
||||
def put_resource_str(kind, id, tmpl_str)
|
||||
tmpl_json = OZonesClient::to_body(kind, tmpl_str)
|
||||
put_resource(kind, id, tmpl_json)
|
||||
end
|
||||
|
||||
def put_resource(kind, id, tmpl_json)
|
||||
url = URI.parse("#{@endpoint}/#{kind}/#{id}")
|
||||
|
||||
req = Net::HTTP::Put.new(url.path)
|
||||
req.body=tmpl_json
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = OZonesClient::http_start(url, @timeout) do |http|
|
||||
http.request(req)
|
||||
end
|
||||
|
||||
return OZonesClient::parse_error(res, kind)
|
||||
end
|
||||
|
||||
def get_resource(kind, id)
|
||||
url = URI.parse("#{@endpoint}/#{kind}/#{id}")
|
||||
req = Net::HTTP::Get.new(url.path)
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = OZonesClient::http_start(url, @timeout) {|http|
|
||||
http.request(req)
|
||||
}
|
||||
|
||||
return OZonesClient::parse_error(res, kind)
|
||||
end
|
||||
|
||||
def delete_resource(kind, id)
|
||||
url = URI.parse("#{@endpoint}/#{kind}/#{id}")
|
||||
req = Net::HTTP::Delete.new(url.path)
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = OZonesClient::http_start(url, @timeout) {|http|
|
||||
http.request(req)
|
||||
}
|
||||
|
||||
return OZonesClient::parse_error(res, kind)
|
||||
end
|
||||
end
|
||||
|
||||
# #########################################################################
|
||||
# The Error Class represents a generic error in the OZones Client
|
||||
# library. It contains a readable representation of the error.
|
||||
# #########################################################################
|
||||
class Error
|
||||
attr_reader :message
|
||||
|
||||
# +message+ a description of the error
|
||||
def initialize(message=nil)
|
||||
@message=message
|
||||
end
|
||||
|
||||
def to_s()
|
||||
@message
|
||||
end
|
||||
end
|
||||
|
||||
# #########################################################################
|
||||
# Error handling functions
|
||||
# #########################################################################
|
||||
def self.is_error?(value)
|
||||
value.class==OZonesClient::Error
|
||||
end
|
||||
|
||||
def self.is_http_error?(value)
|
||||
value.class != Net::HTTPOK
|
||||
end
|
||||
|
||||
def self.parse_error(value, kind)
|
||||
if OZonesClient::is_error?(value)
|
||||
return value
|
||||
else
|
||||
if OZonesClient::is_http_error?(value)
|
||||
str = "Operating with #{kind.upcase} failed with HTTP error "
|
||||
str = str + "code: #{value.code}\n"
|
||||
if value.body
|
||||
# Try to extract error message
|
||||
begin
|
||||
str << "Body: " <<
|
||||
OZonesClient::parse_json(value.body,
|
||||
"error")["message"]
|
||||
rescue
|
||||
str.gsub!("\nBody:","")
|
||||
end
|
||||
end
|
||||
return OZonesClient::Error.new str
|
||||
end
|
||||
end
|
||||
return value # If it is not an error, return it as-is
|
||||
end
|
||||
|
||||
# #########################################################################
|
||||
# Starts an http connection and calls the block provided. SSL flag
|
||||
# is set if needed.
|
||||
# #########################################################################
|
||||
def self.http_start(url, timeout, &block)
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
|
||||
if timeout
|
||||
http.read_timeout = timeout.to_i
|
||||
end
|
||||
|
||||
if url.scheme=='https'
|
||||
http.use_ssl = true
|
||||
http.verify_mode=OpenSSL::SSL::VERIFY_NONE
|
||||
end
|
||||
|
||||
begin
|
||||
http.start do |connection|
|
||||
block.call(connection)
|
||||
end
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
str = "Error connecting to server (#{e.to_s}).\n"
|
||||
str << "Server: #{url.host}:#{url.port}"
|
||||
|
||||
return OZonesClient::Error.new(str)
|
||||
rescue Errno::ETIMEDOUT => e
|
||||
str = "Error timeout connecting to server (#{e.to_s}).\n"
|
||||
str << "Server: #{url.host}:#{url.port}"
|
||||
|
||||
return OZonesClient::Error.new(str)
|
||||
rescue Timeout::Error => e
|
||||
str = "Error timeout while connected to server (#{e.to_s}).\n"
|
||||
str << "Server: #{url.host}:#{url.port}"
|
||||
|
||||
return OZonesClient::Error.new(str)
|
||||
end
|
||||
end
|
||||
|
||||
##########################################################################
|
||||
# JSON & Template utils
|
||||
##########################################################################
|
||||
|
||||
def self.to_body(kind, tmpl_str)
|
||||
tmpl = OpenNebula::Configuration.new(tmpl_str)
|
||||
res = { "#{kind}" => tmpl.conf }
|
||||
|
||||
return JSON::generate(res)
|
||||
end
|
||||
|
||||
def self.parse_json(json_str, root_element)
|
||||
begin
|
||||
hash = JSON.parse(json_str)
|
||||
rescue Exception => e
|
||||
return OZonesClient::Error.new(e.message)
|
||||
end
|
||||
|
||||
if hash.has_key?(root_element)
|
||||
return hash[root_element]
|
||||
else
|
||||
return OZonesClient::Error.new("Error parsing JSON: Wrong resource type")
|
||||
end
|
||||
end
|
||||
|
||||
def self.to_json(hash_to_convert)
|
||||
begin
|
||||
JSON.pretty_generate hash_to_convert
|
||||
rescue Exception => e
|
||||
OZonesClient::Error.new(e.message)
|
||||
end
|
||||
end
|
||||
end
|
@ -15,7 +15,9 @@
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
require 'rubygems'
|
||||
require 'OZonesClient.rb'
|
||||
require 'uri'
|
||||
require 'net/https'
|
||||
require 'OpenNebula/Configuration'
|
||||
|
||||
require 'zona/OZonesJSON'
|
||||
|
||||
@ -30,4 +32,249 @@ require 'zona/VDCElement'
|
||||
|
||||
module Zona
|
||||
|
||||
class Client
|
||||
|
||||
OZONES_VERSION = <<EOT
|
||||
oZones 1.0
|
||||
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
|
||||
EOT
|
||||
|
||||
######################################################################
|
||||
# Initialize client library
|
||||
######################################################################
|
||||
def initialize(user=nil, pass=nil, endpoint_str=nil,
|
||||
timeout=nil, debug_flag=true)
|
||||
@debug = debug_flag
|
||||
@timeout = timeout
|
||||
|
||||
# Server location
|
||||
if endpoint_str
|
||||
@endpoint = endpoint_str
|
||||
elsif ENV["OZONES_URL"]
|
||||
@endpoint = ENV["OZONES_URL"]
|
||||
else
|
||||
@endpoint = "http://localhost:6121"
|
||||
end
|
||||
|
||||
# Autentication
|
||||
if user && pass
|
||||
@ozonesauth = [user, pass]
|
||||
elsif ENV['OZONES_AUTH']
|
||||
@ozonesauth=File.read(ENV['OZONES_AUTH']).strip.split(':')
|
||||
end
|
||||
|
||||
if !@ozonesauth
|
||||
raise "No authorization data present"
|
||||
end
|
||||
|
||||
if @ozonesauth.size != 2
|
||||
raise "Authorization data malformed"
|
||||
end
|
||||
end
|
||||
|
||||
#####################################
|
||||
# General Resource Request Methods #
|
||||
####################################
|
||||
|
||||
######################################################################
|
||||
# Retieves all elements on a pool
|
||||
# :zonetemplate
|
||||
######################################################################
|
||||
def get_pool(kind)
|
||||
url = URI.parse(@endpoint+"/" + kind)
|
||||
req = Net::HTTP::Get.new(url.path)
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = Client.http_start(url, @timeout) {|http|
|
||||
http.request(req)
|
||||
}
|
||||
|
||||
return Client.parse_error(res, kind)
|
||||
end
|
||||
|
||||
######################################################################
|
||||
# Post a new Resource to the relevant OZones Pool
|
||||
# :zonetemplate
|
||||
######################################################################
|
||||
def post_resource_file(kind, template)
|
||||
tmpl_str = File.read(template)
|
||||
post_resource_str(kind, tmpl_str)
|
||||
end
|
||||
|
||||
def post_resource_str(kind, tmpl_str)
|
||||
tmpl_json = Zona.to_body(kind, tmpl_str)
|
||||
post_resource(kind, tmpl_json)
|
||||
end
|
||||
|
||||
def post_resource(kind, tmpl_json)
|
||||
url = URI.parse("#{@endpoint}/#{kind}")
|
||||
|
||||
req = Net::HTTP::Post.new(url.path)
|
||||
req.body=tmpl_json
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = Client.http_start(url, @timeout) do |http|
|
||||
http.request(req)
|
||||
end
|
||||
|
||||
return Client.parse_error(res, kind)
|
||||
end
|
||||
|
||||
def put_resource_str(kind, id, tmpl_str)
|
||||
tmpl_json = Client.to_body(kind, tmpl_str)
|
||||
put_resource(kind, id, tmpl_json)
|
||||
end
|
||||
|
||||
def put_resource(kind, id, tmpl_json)
|
||||
url = URI.parse("#{@endpoint}/#{kind}/#{id}")
|
||||
|
||||
req = Net::HTTP::Put.new(url.path)
|
||||
req.body=tmpl_json
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = Client.http_start(url, @timeout) do |http|
|
||||
http.request(req)
|
||||
end
|
||||
|
||||
return Client.parse_error(res, kind)
|
||||
end
|
||||
|
||||
def get_resource(kind, id)
|
||||
url = URI.parse("#{@endpoint}/#{kind}/#{id}")
|
||||
req = Net::HTTP::Get.new(url.path)
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = Client.http_start(url, @timeout) {|http|
|
||||
http.request(req)
|
||||
}
|
||||
|
||||
return Client.parse_error(res, kind)
|
||||
end
|
||||
|
||||
def delete_resource(kind, id)
|
||||
url = URI.parse("#{@endpoint}/#{kind}/#{id}")
|
||||
req = Net::HTTP::Delete.new(url.path)
|
||||
|
||||
req.basic_auth @ozonesauth[0], @ozonesauth[1]
|
||||
|
||||
res = Client.http_start(url, @timeout) {|http|
|
||||
http.request(req)
|
||||
}
|
||||
|
||||
return Client.parse_error(res, kind)
|
||||
end
|
||||
|
||||
|
||||
# #########################################################################
|
||||
# Starts an http connection and calls the block provided. SSL flag
|
||||
# is set if needed.
|
||||
# #########################################################################
|
||||
def self.http_start(url, timeout, &block)
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
|
||||
if timeout
|
||||
http.read_timeout = timeout.to_i
|
||||
end
|
||||
|
||||
if url.scheme=='https'
|
||||
http.use_ssl = true
|
||||
http.verify_mode=OpenSSL::SSL::VERIFY_NONE
|
||||
end
|
||||
|
||||
begin
|
||||
http.start do |connection|
|
||||
block.call(connection)
|
||||
end
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
str = "Error connecting to server (#{e.to_s}).\n"
|
||||
str << "Server: #{url.host}:#{url.port}"
|
||||
return Error.new(str)
|
||||
rescue Errno::ETIMEDOUT => e
|
||||
str = "Error timeout connecting to server (#{e.to_s}).\n"
|
||||
str << "Server: #{url.host}:#{url.port}"
|
||||
return Error.new(str)
|
||||
rescue Timeout::Error => e
|
||||
str = "Error timeout while connected to server (#{e.to_s}).\n"
|
||||
str << "Server: #{url.host}:#{url.port}"
|
||||
return Error.new(str)
|
||||
rescue Errno::ENETUNREACH => e
|
||||
str = "Error trying to reach network (#{e.to_s}).\n"
|
||||
str << "Server: #{url.host}:#{url.port}"
|
||||
return Error.new(str)
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_error(value, kind)
|
||||
if Zona.is_error?(value)
|
||||
return value
|
||||
else
|
||||
if Zona.is_http_error?(value)
|
||||
str = "Operating with #{kind.upcase} failed with HTTP error"
|
||||
str = " " + str + "code: #{value.code}\n"
|
||||
if value.body
|
||||
# Try to extract error message
|
||||
begin
|
||||
str << "Body: " <<
|
||||
OZonesJSON.parse_json(value.body,
|
||||
"error")["message"]
|
||||
rescue
|
||||
str.gsub!("\nBody:","")
|
||||
end
|
||||
end
|
||||
return Error.new(str)
|
||||
end
|
||||
end
|
||||
value # If it is not an error, return it as-is
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# ############################################
|
||||
# Template helpers
|
||||
# ############################################
|
||||
|
||||
def self.to_body(kind, tmpl_str)
|
||||
tmpl = OpenNebula::Configuration.new(tmpl_str)
|
||||
res = { "#{kind}" => tmpl.conf }
|
||||
|
||||
return OZonesJSON.to_json(res)
|
||||
end
|
||||
|
||||
# #########################################################################
|
||||
# Error handling functions
|
||||
# #########################################################################
|
||||
|
||||
def self.is_error?(value)
|
||||
value.class==Zona::Error
|
||||
end
|
||||
|
||||
def self.is_http_error?(value)
|
||||
value.class != Net::HTTPOK
|
||||
end
|
||||
|
||||
# #########################################################################
|
||||
# The Error Class represents a generic error in the Zona
|
||||
# library. It contains a readable representation of the error.
|
||||
# #########################################################################
|
||||
class Error
|
||||
attr_reader :message
|
||||
|
||||
# +message+ a description of the error
|
||||
def initialize(message=nil)
|
||||
@message=message
|
||||
end
|
||||
|
||||
def to_s()
|
||||
@message
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -17,7 +17,7 @@
|
||||
module Zona
|
||||
|
||||
class OZonesElement < JSONElement
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def initialize(hash, client)
|
||||
@ -30,16 +30,16 @@ module Zona
|
||||
|
||||
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)
|
||||
if !Zona.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
|
||||
@ -50,8 +50,8 @@ module Zona
|
||||
|
||||
def allocate(kind, tmpl_json)
|
||||
rc = @client.post_resource(kind, tmpl_json)
|
||||
|
||||
if !OZonesClient.is_error?(rc)
|
||||
|
||||
if !Zona.is_error?(rc)
|
||||
initialize_json(rc.body,kind.upcase)
|
||||
@pe_id = self["id"].to_i
|
||||
rc = nil
|
||||
@ -63,7 +63,7 @@ module Zona
|
||||
return Error.new('ID not defined') if !@pe_id
|
||||
|
||||
rc = @client.delete_resource(kind,@pe_id)
|
||||
return rc if OZonesClient.is_error?(rc)
|
||||
return rc if Zona.is_error?(rc)
|
||||
nil
|
||||
end
|
||||
|
||||
|
@ -19,33 +19,58 @@ module Zona
|
||||
|
||||
require 'json'
|
||||
|
||||
class OZonesJSON
|
||||
|
||||
def self.build_json(json_str, root_element)
|
||||
begin
|
||||
parser = JSON.parser.new(json_str, {:symbolize_names => false})
|
||||
hash = parser.parse
|
||||
|
||||
if hash.has_key?(root_element)
|
||||
return hash[root_element]
|
||||
end
|
||||
|
||||
Error.new("Error parsing JSON:\ root element not present")
|
||||
|
||||
rescue => e
|
||||
Error.new(e.message)
|
||||
end
|
||||
end
|
||||
|
||||
# Alias for compatibility
|
||||
def self.parse_json(json_str, root_element)
|
||||
OZonesJSON.build_json(json_str, root_element)
|
||||
end
|
||||
|
||||
|
||||
def self.to_json(hash_to_convert)
|
||||
begin
|
||||
JSON.pretty_generate(hash_to_convert)
|
||||
rescue Exception => e
|
||||
Error.new(e.message)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
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)
|
||||
rc = OZonesJSON.build_json(json_str,root_element)
|
||||
@json_hash = rc
|
||||
|
||||
if OZonesClient.is_error?(rc) || (rc.size == 0)
|
||||
if Zona.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
|
||||
|
@ -19,7 +19,7 @@ module Zona
|
||||
class OZonesPool < JSONPool
|
||||
|
||||
protected
|
||||
|
||||
|
||||
def initialize(pool,element,client)
|
||||
super(nil)
|
||||
|
||||
@ -29,13 +29,13 @@ module Zona
|
||||
end
|
||||
|
||||
def factory(element_json)
|
||||
Zona::OZonesPoolElement.new(element_json, @client)
|
||||
OZonesPoolElement.new(element_json, @client)
|
||||
end
|
||||
|
||||
def info(kind)
|
||||
rc = @client.get_pool(kind)
|
||||
|
||||
if !OZonesClient.is_error?(rc)
|
||||
if !Zona.is_error?(rc)
|
||||
initialize_json(rc.body,@pool_name)
|
||||
rc=nil
|
||||
end
|
||||
@ -44,7 +44,7 @@ module Zona
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
|
||||
def each(&block)
|
||||
each_element(block) if @json_hash
|
||||
end
|
||||
|
@ -26,13 +26,13 @@ module Zona
|
||||
else
|
||||
json = '{"VDC":{}}'
|
||||
end
|
||||
JSONElement.build_json(json,"VDC")
|
||||
OZonesJSON.build_json(json,"VDC")
|
||||
end
|
||||
|
||||
def initialize(hash, client)
|
||||
super(hash, client)
|
||||
end
|
||||
|
||||
|
||||
def info
|
||||
super(VDC_KIND,"VDC")
|
||||
end
|
||||
@ -51,11 +51,11 @@ module Zona
|
||||
|
||||
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]
|
||||
@ -63,7 +63,7 @@ module Zona
|
||||
template = {:vdc => template}
|
||||
|
||||
rc = @client.put_resource(VDC_KIND,@pe_id,template.to_json)
|
||||
return rc if OZonesClient.is_error?(rc)
|
||||
return rc if Zona.is_error?(rc)
|
||||
nil
|
||||
end
|
||||
|
||||
@ -76,7 +76,7 @@ module Zona
|
||||
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)
|
||||
return rc if Zona.is_error?(rc)
|
||||
nil
|
||||
end
|
||||
|
||||
|
@ -24,7 +24,7 @@ module Zona
|
||||
end
|
||||
|
||||
def factory(element_json)
|
||||
Zona::VDC.new(element_json,@client)
|
||||
VDC.new(element_json,@client)
|
||||
end
|
||||
|
||||
def info
|
||||
|
@ -17,7 +17,7 @@
|
||||
module Zona
|
||||
|
||||
class Zone < OZonesElement
|
||||
|
||||
|
||||
ZONE_KIND = "zone"
|
||||
|
||||
def self.build_json(pe_id=nil)
|
||||
@ -26,13 +26,13 @@ module Zona
|
||||
else
|
||||
json = '{"ZONE":{}}'
|
||||
end
|
||||
JSONElement.build_json(json,"ZONE")
|
||||
OZonesJSON.build_json(json,"ZONE")
|
||||
end
|
||||
|
||||
def initialize(hash, client)
|
||||
super(hash, client)
|
||||
end
|
||||
|
||||
|
||||
def info
|
||||
super(ZONE_KIND,"ZONE")
|
||||
end
|
||||
|
@ -17,7 +17,7 @@
|
||||
module Zona
|
||||
|
||||
class ZonePool < OZonesPool
|
||||
|
||||
|
||||
ZONE_POOL_KIND = "zone"
|
||||
|
||||
def initialize(client)
|
||||
@ -25,7 +25,7 @@ module Zona
|
||||
end
|
||||
|
||||
def factory(element_json)
|
||||
Zona::Zone.new(element_json,@client)
|
||||
Zone.new(element_json,@client)
|
||||
end
|
||||
|
||||
def info
|
||||
|
@ -14,14 +14,14 @@
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
require 'OZonesClient'
|
||||
require 'zona'
|
||||
|
||||
module OZonesHelper
|
||||
|
||||
class OZHelper
|
||||
def initialize(user=nil, pass=nil, endpoint_str=nil,
|
||||
timeout=nil, debug_flag=true)
|
||||
@client = OZonesClient::Client.new(user,
|
||||
@client = Zona::Client.new(user,
|
||||
pass,
|
||||
endpoint_str,
|
||||
timeout,
|
||||
@ -31,7 +31,7 @@ module OZonesHelper
|
||||
def create_resource(kind, template)
|
||||
rc = @client.post_resource_file(kind, template)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona::is_error?(rc)
|
||||
[-1, rc.message]
|
||||
else
|
||||
id = get_id(rc)
|
||||
@ -42,10 +42,10 @@ module OZonesHelper
|
||||
def list_pool(kind, options)
|
||||
rc = @client.get_pool(kind)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona::is_error?(rc)
|
||||
[-1, rc.message]
|
||||
else
|
||||
pool=OZonesClient::parse_json(rc.body, kind.upcase + "_POOL")
|
||||
pool=Zona::OZonesJSON.parse_json(rc.body, kind.upcase + "_POOL")
|
||||
format_pool(pool, options)
|
||||
end
|
||||
end
|
||||
@ -53,10 +53,10 @@ module OZonesHelper
|
||||
def show_resource(kind, id, options)
|
||||
rc = @client.get_resource(kind, id)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona::is_error?(rc)
|
||||
[-1, rc.message]
|
||||
else
|
||||
resource=OZonesClient::parse_json(rc.body, kind.upcase)
|
||||
resource=Zona::OZonesJSON.parse_json(rc.body, kind.upcase)
|
||||
format_resource(resource, options)
|
||||
end
|
||||
end
|
||||
@ -64,10 +64,10 @@ module OZonesHelper
|
||||
def delete_resource(kind, id, options)
|
||||
rc = @client.delete_resource(kind, id)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona::is_error?(rc)
|
||||
[-1, rc.message]
|
||||
else
|
||||
message=OZonesClient::parse_json(rc.body, "message")
|
||||
message=Zona::OZonesJSON.parse_json(rc.body, "message")
|
||||
[0, "#{message}"]
|
||||
end
|
||||
end
|
||||
|
@ -33,7 +33,7 @@ class VDCHelper < OZonesHelper::OZHelper
|
||||
|
||||
rc = @client.post_resource_str(@vdc_str, tmpl_str)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona::is_error?(rc)
|
||||
[-1, rc.message]
|
||||
else
|
||||
id = get_id(rc)
|
||||
@ -56,10 +56,10 @@ class VDCHelper < OZonesHelper::OZHelper
|
||||
def addhost(id, host_array, options)
|
||||
rc = @client.get_resource(@vdc_str, id)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona::is_error?(rc)
|
||||
return [-1, rc.message]
|
||||
else
|
||||
vdc = OZonesClient::parse_json(rc.body, @vdc_str.upcase)
|
||||
vdc = Zona::OZonesJSON.parse_json(rc.body, @vdc_str.upcase)
|
||||
end
|
||||
|
||||
hosts = vdc['hosts'].split(',').collect!{|x| x.to_i}
|
||||
@ -74,7 +74,7 @@ class VDCHelper < OZonesHelper::OZHelper
|
||||
|
||||
rc = @client.put_resource_str(@vdc_str, id, template)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona::is_error?(rc)
|
||||
return [-1, rc.message]
|
||||
end
|
||||
|
||||
@ -84,10 +84,10 @@ class VDCHelper < OZonesHelper::OZHelper
|
||||
def delhost(id, host_array, options)
|
||||
rc = @client.get_resource(@vdc_str, id)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona::is_error?(rc)
|
||||
return [-1, rc.message]
|
||||
else
|
||||
vdc = OZonesClient::parse_json(rc.body, @vdc_str.upcase)
|
||||
vdc = Zona::OZonesJSON.parse_json(rc.body, @vdc_str.upcase)
|
||||
end
|
||||
|
||||
hosts = vdc['hosts'].split(',').collect!{|x| x.to_i}
|
||||
@ -97,7 +97,7 @@ class VDCHelper < OZonesHelper::OZHelper
|
||||
|
||||
rc = @client.put_resource_str(@vdc_str, id, template)
|
||||
|
||||
if OZonesClient::is_error?(rc)
|
||||
if Zona.is_error?(rc)
|
||||
return [-1, rc.message]
|
||||
end
|
||||
|
||||
|
@ -60,7 +60,7 @@ module OZones
|
||||
zone_attributes["ZONE"][:vdcs] = Array.new
|
||||
|
||||
self.vdcs.all.each{|vdc|
|
||||
zone_attributes["ZONE"][:vdcs]<<vdc.attributes
|
||||
zone_attributes["ZONE"][:vdcs]<< vdc.attributes
|
||||
}
|
||||
|
||||
return zone_attributes
|
||||
|
Loading…
x
Reference in New Issue
Block a user