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
42 43 44 45 46 47 48 |
# 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) ⇒ Object
Allocate XML Document
53 54 55 |
# File 'lib/opennebula/document_json.rb', line 53 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
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# 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) ⇒ nil, OpenNebula::Error Also known as: info!
Retrieves the information of the Service and all its Nodes.
62 63 64 65 66 67 68 69 |
# 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 |
#load_body ⇒ Object
Fill the @body hash with the values of the template
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# 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 ⇒ Object
Returns current template tag
25 26 27 28 29 30 31 |
# File 'lib/opennebula/document_json.rb', line 25 def template_tag if @tag @tag else TEMPLATE_TAG end end |
#to_json(pretty_generate = true) ⇒ String
Generates a json representing the object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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) ⇒ nil, OpenNebula::Error
Updates the current state of this Service in the OpenNebula DB
83 84 85 86 87 88 89 |
# 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) ⇒ nil, OpenNebula::Error
Replaces the raw template contents
99 100 101 |
# File 'lib/opennebula/document_json.rb', line 99 def update_raw(template_raw, append=false) super(template_raw, append) end |