Class: OpenNebula::Document

Inherits:
PoolElement show all
Defined in:
lib/opennebula/document.rb

Overview

All subclasses must define the DOCUMENT_TYPE constant.

Examples:

require 'opennebula/document'

module OpenNebula
    class CustomObject < Document

        DOCUMENT_TYPE = 400

    end
end

Direct Known Subclasses

DocumentJSON

Constant Summary collapse

DOCUMENT_METHODS =

Constants and Class Methods

{
    :allocate   => "document.allocate",
    :delete     => "document.delete",
    :info       => "document.info",
    :update     => "document.update",
    :chown      => "document.chown",
    :chmod      => "document.chmod",
    :clone      => "document.clone",
    :rename     => "document.rename",
    :lock       => "document.lock",
    :unlock     => "document.unlock"
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PoolElement

#id, #name, new_with_id, #replace, #to_str

Methods inherited from XMLElement

#[], #add_element, #attr, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #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

#initialize(xml, client) ⇒ Document

Class constructor

Examples:

doc = Document.new(Document.build_xml(3),rpc_client)

Parameters:

  • xml (Nokogiri::XML::Node, REXML::Element)

    string created by the build_xml() method

  • client (OpenNebula::Client)

    the xml-rpc client



78
79
80
81
82
# File 'lib/opennebula/document.rb', line 78

def initialize(xml, client)
    LockableExt.make_lockable(self, DOCUMENT_METHODS)

    super(xml,client)
end

Class Method Details

.build_xml(pe_id = nil) ⇒ Nokogiri::XML::Node, REXML::Element

Creates a Document Object description with just its identifier this method should be used to create plain Document objects.

Parameters:

  • pe_id (Integer) (defaults to: nil)

    the id of the object

Returns:

  • (Nokogiri::XML::Node, REXML::Element)

    the empty xml



58
59
60
61
62
63
64
65
66
# File 'lib/opennebula/document.rb', line 58

def Document.build_xml(pe_id=nil)
    if pe_id
        obj_xml = "<DOCUMENT><ID>#{pe_id}</ID></DOCUMENT>"
    else
        obj_xml = "<DOCUMENT></DOCUMENT>"
    end

    XMLElement.build_xml(obj_xml,'DOCUMENT')
end

Instance Method Details

#allocate(description) ⇒ nil, OpenNebula::Error Also known as: allocate_xml

Allocates a new Document in OpenNebula

Parameters:

  • description (String)

    The contents of the Document.

Returns:



110
111
112
# File 'lib/opennebula/document.rb', line 110

def allocate(description)
    super(DOCUMENT_METHODS[:allocate], description, document_type)
end

#chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) ⇒ nil, OpenNebula::Error

Changes the Document permissions. Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change

Returns:



189
190
191
192
193
194
195
196
# File 'lib/opennebula/document.rb', line 189

def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
        other_m, other_a)
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    super(DOCUMENT_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
        group_m, group_a, other_u, other_m, other_a)
end

#chmod_octet(octet) ⇒ nil, OpenNebula::Error

Changes the Document permissions.

Parameters:

  • octet (String)

    Permissions octed , e.g. 640

Returns:



177
178
179
180
181
182
# File 'lib/opennebula/document.rb', line 177

def chmod_octet(octet)
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    super(DOCUMENT_METHODS[:chmod], octet)
end

#chown(uid, gid) ⇒ nil, OpenNebula::Error

Changes the owner/group

Parameters:

  • uid (Integer)

    the new owner id. Set to -1 to leave the current one

  • gid (Integer)

    the new group id. Set to -1 to leave the current one

Returns:



164
165
166
167
168
169
# File 'lib/opennebula/document.rb', line 164

def chown(uid, gid)
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    super(DOCUMENT_METHODS[:chown], uid, gid)
end

#clone(name) ⇒ Integer, OpenNebula::Error

Clones this Document into a new one

Parameters:

  • name (String)

    Name for the new Document.

Returns:

  • (Integer, OpenNebula::Error)

    The new Document ID in case of success, Error otherwise



204
205
206
207
208
209
210
211
212
213
# File 'lib/opennebula/document.rb', line 204

def clone(name)
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(DOCUMENT_METHODS[:clone], @pe_id, name)

    return rc
end

#deletenil, OpenNebula::Error

Deletes the Document

Returns:



120
121
122
123
124
125
# File 'lib/opennebula/document.rb', line 120

def delete()
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    return call(DOCUMENT_METHODS[:delete], @pe_id)
end

#document_typeObject



251
252
253
# File 'lib/opennebula/document.rb', line 251

def document_type
    self.class::DOCUMENT_TYPE
end

#gidInteger

Returns the group identifier

Returns:

  • (Integer)

    the element’s group ID



231
232
233
# File 'lib/opennebula/document.rb', line 231

def gid
    self['GID'].to_i
end

#info(decrypt = false) ⇒ nil, OpenNebula::Error Also known as: info!

Retrieves the information of the given Document.

Returns:



92
93
94
95
96
97
98
99
100
# File 'lib/opennebula/document.rb', line 92

def info(decrypt = false)
    rc = super(DOCUMENT_METHODS[:info], 'DOCUMENT', decrypt)

    if !OpenNebula.is_error?(rc) && self['TYPE'].to_i != document_type
        return OpenNebula::Error.new("[DocumentInfo] Error getting document [#{@pe_id}].")
    end

    return rc
end

#owner_idInteger

Returns the owner user ID

Returns:

  • (Integer)

    the element’s owner user ID



237
238
239
# File 'lib/opennebula/document.rb', line 237

def owner_id
    self['UID'].to_i
end

#public?true, false

Returns true if the GROUP_U permission bit is set

Returns:

  • (true, false)

    true if the GROUP_U permission bit is set



243
244
245
246
247
248
249
# File 'lib/opennebula/document.rb', line 243

def public?
    if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
        true
    else
        false
    end
end

#rename(name) ⇒ nil, OpenNebula::Error

Renames this Document

Parameters:

  • name (String)

    New name for the Document.

Returns:



221
222
223
# File 'lib/opennebula/document.rb', line 221

def rename(name)
    return call(DOCUMENT_METHODS[:rename], @pe_id, name)
end

#update(new_template, append = false) ⇒ nil, OpenNebula::Error

Replaces the template contents

Parameters:

  • new_template (String)

    new template contents

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

    True to append new attributes instead of replace the whole template

Returns:



135
136
137
138
139
140
# File 'lib/opennebula/document.rb', line 135

def update(new_template, append=false)
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    super(DOCUMENT_METHODS[:update], new_template, append ? 1 : 0)
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:



150
151
152
153
154
155
# File 'lib/opennebula/document.rb', line 150

def update_raw(template_raw, append=false)
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    return call(DOCUMENT_METHODS[:update], @pe_id, template_raw, append ? 1 : 0)
end