1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-11 05:17:41 +03:00

Add JSON documents to OCA

This commit is contained in:
Javi Fontan 2012-08-22 11:52:16 +02:00
parent 7dd75027e3
commit 4874360e9e
3 changed files with 189 additions and 0 deletions

View File

@ -1046,6 +1046,8 @@ RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \
src/oca/ruby/OpenNebula/TemplatePool.rb \
src/oca/ruby/OpenNebula/Document.rb \
src/oca/ruby/OpenNebula/DocumentPool.rb \
src/oca/ruby/OpenNebula/DocumentJSON.rb \
src/oca/ruby/OpenNebula/DocumentPoolJSON.rb \
src/oca/ruby/OpenNebula/Group.rb \
src/oca/ruby/OpenNebula/GroupPool.rb \
src/oca/ruby/OpenNebula/Acl.rb \

View File

@ -0,0 +1,129 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, 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 'json'
module OpenNebula
class DocumentJSON < Document
TEMPLATE_TAG = "BODY"
# Allocate a new Document containing the json inside the TEMPLATE
#
# @param [String] template_json json to be inserted in the TEMPLATE
# of the new resource
# @param [String, nil] name name of the object, this value will be
# processed by the OpenNebula core
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
#
def allocate(template_json, name=nil)
text = build_template_xml(template_json, name)
super(text)
end
# Retrieves the information of the Service and all its Nodes.
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
#
def info
rc = super
if OpenNebula.is_error?(rc)
return rc
end
load_body
end
# Updates the current state of this Service in the OpenNebula DB
#
# @params [String, nil] template_json string to be inserted in the
# template. If nil @body will be used instead
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
#
def update(template_json=nil)
template_json ||= @body.to_json
text = build_template_xml(template_json)
super(text)
end
# Generates a json representing the object
#
# @param [true, false] pretty_generate
# @return [String] json representing the object
#
def to_json(pretty_generate=true)
hash = self.to_hash
body = hash['DOCUMENT']['TEMPLATE']["#{TEMPLATE_TAG}"]
if body
body_hash = JSON.parse(body)
hash['DOCUMENT']['TEMPLATE']["#{TEMPLATE_TAG}"] = body_hash
end
if pretty_generate
JSON.pretty_generate hash
else
hash.to_json
end
end
# Fill the @body hash with the values of the template
def load_body
body_str = self["TEMPLATE/#{TEMPLATE_TAG}"]
if body_str
begin
@body = JSON.parse(body_str)
rescue JSON::JSONError
return OpenNebula::Error.new($!)
end
end
return nil
end
private
# Build an xml string incluiding the provided json
#
# @param [String] template_json The template to be inserted
# @param [String, nil] name The string to be inserted as name
# @return [String] The xml containing the json
#
def build_template_xml(template_json, name=nil)
template_json ||= ""
text = "<TEMPLATE>"
text << "<NAME>#{name}</NAME>" if name
text << "<#{TEMPLATE_TAG}>"
text << "<![CDATA[#{template_json}]]>"
text << "</#{TEMPLATE_TAG}>"
text << "</TEMPLATE>"
text
end
end
end

View File

@ -0,0 +1,58 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, 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 OpenNebula
class DocumentPoolJSON < DocumentPool
TEMPLATE_TAG = "BODY"
def factory(element_xml)
doc = OpenNebula::DocumentJSON.new(element_xml, @client)
doc.load_body
doc
end
# Generates a json representing the object
#
# @param [true, false] pretty_generate
# @return [String] json representing the object
#
def to_json(pretty_generate=true)
hash = self.to_hash
if hash['DOCUMENT_POOL'] && hash['DOCUMENT_POOL']['DOCUMENT']
if !hash['DOCUMENT_POOL']['DOCUMENT'].instance_of?(Array)
array = [hash['DOCUMENT_POOL']['DOCUMENT']]
hash['DOCUMENT_POOL']['DOCUMENT'] = array.compact
end
hash['DOCUMENT_POOL']['DOCUMENT'].each { |doc|
body = doc['TEMPLATE']["#{TEMPLATE_TAG}"]
if body
b_hash = JSON.parse(body)
doc['TEMPLATE']["#{TEMPLATE_TAG}"] = b_hash
end
}
end
if pretty_generate
JSON.pretty_generate hash
else
hash.to_json
end
end
end
end