Class: OpenNebula::DocumentJSON

Inherits:
Document show all
Defined in:
OpenNebula/DocumentJSON.rb

Constant Summary

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, #owner_id, #public?

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, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml

Constructor Details

This class inherits a constructor from OpenNebula::Document

Instance Method Details

- (nil, OpenNebula::Error) allocate(template_json, name = nil)

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 'OpenNebula/DocumentJSON.rb', line 33

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

    super(text)
end

- (nil, OpenNebula::Error) info

Retrieves the information of the Service and all its Nodes.

Returns:



44
45
46
47
48
49
50
51
# File 'OpenNebula/DocumentJSON.rb', line 44

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

    load_body
end

- (Object) load_body

Fill the @body hash with the values of the template



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'OpenNebula/DocumentJSON.rb', line 91

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

- (String) to_json(pretty_generate = true)

Generates a json representing the object

Parameters:

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

Returns:

  • (String)

    json representing the object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'OpenNebula/DocumentJSON.rb', line 73

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

- (nil, OpenNebula::Error) update(template_json = nil)

Updates the current state of this Service in the OpenNebula DB

Returns:



60
61
62
63
64
65
66
# File 'OpenNebula/DocumentJSON.rb', line 60

def update(template_json=nil)
    template_json ||= @body.to_json

    text = build_template_xml(template_json)

    super(text)
end