Class: OpenNebula::DocumentJSON

Inherits:
Document show all
Defined in:
lib/opennebula/document_json.rb

Constant Summary collapse

TEMPLATE_TAG =
"BODY"

Constants inherited from Document

OpenNebula::Document::DOCUMENT_METHODS

Instance Method Summary collapse

Methods inherited from Document

build_xml, #chmod, #chmod_octet, #chown, #clone, #delete, #document_type, #gid, #initialize, #lock, #owner_id, #public?, #rename, #unlock

Methods inherited from PoolElement

#id, #name, new_with_id, #to_str

Methods inherited from XMLElement

#[], #add_element, #attr, build_xml, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize, #initialize_xml, #name, #retrieve_elements, #retrieve_xmlelements, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?

Constructor Details

This class inherits a constructor from OpenNebula::Document

Instance Method Details

#allocate(template_json, name = nil) ⇒ nil, OpenNebula::Error

Allocate a new Document containing the json inside the TEMPLATE

Parameters:

  • template_json (String)

    json to be inserted in the TEMPLATE of the new resource

  • name (String, nil) (defaults to: nil)

    name of the object, this value will be processed by the OpenNebula core

Returns:



33
34
35
36
37
# File 'lib/opennebula/document_json.rb', line 33

def allocate(template_json, name=nil)
    text = build_template_xml(template_json, name)

    super(text)
end

#infonil, OpenNebula::Error Also known as: info!

Retrieves the information of the Service and all its Nodes.

Returns:



44
45
46
47
48
49
50
51
# File 'lib/opennebula/document_json.rb', line 44

def info
    rc = super
    if OpenNebula.is_error?(rc)
        return rc
    end

    load_body
end

#load_bodyObject

Fill the @body hash with the values of the template



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/opennebula/document_json.rb', line 108

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

#to_json(pretty_generate = true) ⇒ String

Generates a json representing the object

Parameters:

  • pretty_generate (true, false) (defaults to: true)

Returns:

  • (String)

    json representing the object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/opennebula/document_json.rb', line 90

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) ⇒ nil, OpenNebula::Error

Updates the current state of this Service in the OpenNebula DB

Parameters:

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



65
66
67
68
69
70
71
# File 'lib/opennebula/document_json.rb', line 65

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) ⇒ nil, OpenNebula::Error

Replaces the raw template contents

Parameters:

  • template (String)

    New template contents, in the form KEY = VAL

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



81
82
83
# File 'lib/opennebula/document_json.rb', line 81

def update_raw(template_raw, append=false)
    super(template_raw, append)
end