class OpenNebula::DocumentJSON

Constants

TEMPLATE_TAG

Public Instance Methods

allocate(template_json, name = nil, tag = nil) click to toggle source

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
Calls superclass method OpenNebula::Document#allocate
# File lib/opennebula/document_json.rb, line 42
def allocate(template_json, name = nil, tag = nil)
    @tag = tag if tag

    text = build_template_xml(template_json, name)

    super(text)
end
allocate_xml(xml) click to toggle source

Allocate XML Document

@param xml [String] XML document content

Calls superclass method OpenNebula::Document#allocate_xml
# File lib/opennebula/document_json.rb, line 53
def allocate_xml(xml)
    super(xml)
end
build_template_xml(template_json, name = nil, plain = nil) click to toggle source

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 @param [String, nil] plain information to add to the document @return [String] The xml containing the json

# File lib/opennebula/document_json.rb, line 157
def build_template_xml(template_json, name = nil, plain = nil)
    template_json ||= ""
    plain         ||= @plain
    plain           = plain.to_json if plain && !(plain.is_a? String)

    text = "<TEMPLATE>"

    text << "<NAME>#{name}</NAME>" if name
    text << "<PLAIN><![CDATA[#{plain}]]></PLAIN>" if plain

    text << "<#{template_tag}>"
    text << "<![CDATA[#{template_json}]]>"
    text << "</#{template_tag}>"

    text << "</TEMPLATE>"

    text
end
info(decrypt = false) click to toggle source

Retrieves the information of the Service and all its Nodes.

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method OpenNebula::Document#info
# File lib/opennebula/document_json.rb, line 62
def info(decrypt = false)
    rc = super(decrypt)
    if OpenNebula.is_error?(rc)
        return rc
    end

    load_body
end
Also aliased as: info!
info!(decrypt = false)
Alias for: info
load_body() click to toggle source

Fill the @body hash with the values of the template

# File lib/opennebula/document_json.rb, line 126
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

    plain_str = self['TEMPLATE/PLAIN']

    if plain_str
        begin
            @plain = JSON.parse(plain_str)
        rescue JSON::JSONError
            return OpenNebula::Error.new($!)
        end
    end

    return nil
end
template_tag() click to toggle source

Returns current template tag

# File lib/opennebula/document_json.rb, line 25
def template_tag
    if @tag
        @tag
    else
        TEMPLATE_TAG
    end
end
to_json(pretty_generate=true) click to toggle source

Generates a json representing the object

@param [true, false] pretty_generate @return [String] json representing the object

# File lib/opennebula/document_json.rb, line 108
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
update(template_json=nil, append=false) click to toggle source

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

@param append [true, false] True to append new attributes instead of

replace the whole template

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method OpenNebula::Document#update
# File lib/opennebula/document_json.rb, line 83
def update(template_json=nil, append=false)
    template_json ||= @body.to_json

    text = build_template_xml(template_json)

    super(text, append)
end
update_raw(template_raw, append=false) click to toggle source

Replaces the raw template contents

@param template [String] New template contents, in the form KEY = VAL @param append [true, false] True to append new attributes instead of

replace the whole template

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method OpenNebula::Document#update_raw
# File lib/opennebula/document_json.rb, line 99
def update_raw(template_raw, append=false)
    super(template_raw, append)
end