Class: OpenNebula::DocumentJSON
- Inherits:
-
Document
- Object
- XMLElement
- PoolElement
- Document
- OpenNebula::DocumentJSON
- Defined in:
- lib/opennebula/document_json.rb
Direct Known Subclasses
Constant Summary collapse
- TEMPLATE_TAG =
"BODY"
Constants inherited from Document
OpenNebula::Document::DOCUMENT_METHODS
Instance Attribute Summary
Attributes inherited from PoolElement
Instance Method Summary collapse
-
#allocate(template_json, name = nil, tag = nil) ⇒ nil, OpenNebula::Error
Allocate a new Document containing the json inside the TEMPLATE.
-
#allocate_xml(xml) ⇒ Object
Allocate XML Document.
-
#build_template_xml(template_json, name = nil, plain = nil) ⇒ String
Build an xml string incluiding the provided json.
-
#info(decrypt = false) ⇒ nil, OpenNebula::Error
(also: #info!)
Retrieves the information of the Service and all its Nodes.
-
#load_body ⇒ Object
Fill the @body hash with the values of the template.
-
#template_tag ⇒ Object
Returns current template tag.
-
#to_json(pretty_generate = true) ⇒ String
Generates a json representing the object.
-
#update(template_json = nil, append = false) ⇒ nil, OpenNebula::Error
Updates the current state of this Service in the OpenNebula DB.
-
#update_raw(template_raw, append = false) ⇒ nil, OpenNebula::Error
Replaces the raw template contents.
Methods inherited from Document
build_xml, #chmod, #chmod_octet, #chown, #clone, #delete, #document_type, #gid, #initialize, #owner_id, #public?, #rename
Methods inherited from PoolElement
#id, new_with_id, #replace, #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, #set_content, #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, tag = nil) ⇒ nil, OpenNebula::Error
Allocate a new Document containing the json inside the TEMPLATE
43 44 45 46 47 48 49 |
# File 'lib/opennebula/document_json.rb', line 43 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) ⇒ Object
Allocate XML Document
54 55 56 |
# File 'lib/opennebula/document_json.rb', line 54 def allocate_xml(xml) super(xml) end |
#build_template_xml(template_json, name = nil, plain = nil) ⇒ String
Build an xml string incluiding the provided json
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/opennebula/document_json.rb', line 158 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) ⇒ nil, OpenNebula::Error Also known as: info!
Retrieves the information of the Service and all its Nodes.
63 64 65 66 67 68 69 70 |
# File 'lib/opennebula/document_json.rb', line 63 def info(decrypt = false) rc = super(decrypt) if OpenNebula.is_error?(rc) return rc end load_body end |
#load_body ⇒ Object
Fill the @body hash with the values of the template
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/opennebula/document_json.rb', line 127 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 ⇒ Object
Returns current template tag
26 27 28 29 30 31 32 |
# File 'lib/opennebula/document_json.rb', line 26 def template_tag if @tag @tag else TEMPLATE_TAG end end |
#to_json(pretty_generate = true) ⇒ String
Generates a json representing the object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/opennebula/document_json.rb', line 109 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
84 85 86 87 88 89 90 |
# File 'lib/opennebula/document_json.rb', line 84 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
100 101 102 |
# File 'lib/opennebula/document_json.rb', line 100 def update_raw(template_raw, append=false) super(template_raw, append) end |