class OpenNebula::Document

All subclasses must define the DOCUMENT_TYPE constant.

@example

require 'opennebula/document'

module OpenNebula
    class CustomObject < Document

        DOCUMENT_TYPE = 400

    end
end

Constants

DOCUMENT_METHODS

Constants and Class Methods

Public Class Methods

build_xml(pe_id=nil) click to toggle source

Creates a Document Object description with just its identifier this method should be used to create plain Document objects. @param [Integer] pe_id the id of the object

@return [Nokogiri::XML::Node, REXML::Element] the empty xml

# File lib/opennebula/document.rb, line 55
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
new(xml, client) click to toggle source

Class constructor

@param [Nokogiri::XML::Node, REXML::Element] xml string

created by the build_xml() method

@param [OpenNebula::Client] client the xml-rpc client

@return [Document] the new object

@example

doc = Document.new(Document.build_xml(3),rpc_client)
Calls superclass method
# File lib/opennebula/document.rb, line 75
def initialize(xml, client)
    super(xml,client)
end

Public Instance Methods

allocate(description) click to toggle source

Allocates a new Document in OpenNebula

@param description [String] The contents of the Document.

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method
# File lib/opennebula/document.rb, line 105
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) click to toggle source

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

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method
# File lib/opennebula/document.rb, line 167
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) click to toggle source

Changes the Document permissions.

@param octet [String] Permissions octed , e.g. 640

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method
# File lib/opennebula/document.rb, line 155
def chmod_octet(octet)
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    super(DOCUMENT_METHODS[:chmod], octet)
end
chown(uid, gid) click to toggle source

Changes the owner/group

@param [Integer] uid the new owner id. Set to -1 to leave the current one @param [Integer] gid the new group id. Set to -1 to leave the current one

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method
# File lib/opennebula/document.rb, line 142
def chown(uid, gid)
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    super(DOCUMENT_METHODS[:chown], uid, gid)
end
clone(name) click to toggle source

Clones this Document into a new one

@param name [String] Name for the new Document.

@return [Integer, OpenNebula::Error] The new Document ID in case

of success, Error otherwise
# File lib/opennebula/document.rb, line 182
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
delete() click to toggle source

Deletes the Document

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
# File lib/opennebula/document.rb, line 113
def delete()
    rc = check_type()
    return rc if OpenNebula.is_error?(rc)

    return call(DOCUMENT_METHODS[:delete], @pe_id)
end
document_type() click to toggle source
# File lib/opennebula/document.rb, line 229
def document_type
    self.class::DOCUMENT_TYPE
end
gid() click to toggle source

Returns the group identifier @return [Integer] the element's group ID

# File lib/opennebula/document.rb, line 209
def gid
    self['GID'].to_i
end
info() click to toggle source

Retrieves the information of the given Document.

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method
# File lib/opennebula/document.rb, line 87
def info()
    rc = super(DOCUMENT_METHODS[:info], 'DOCUMENT')

    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
Also aliased as: info!
info!()
Alias for: info
owner_id() click to toggle source

Returns the owner user ID @return [Integer] the element's owner user ID

# File lib/opennebula/document.rb, line 215
def owner_id
    self['UID'].to_i
end
public?() click to toggle source

Returns true if the GROUP_U permission bit is set @return [true, false] true if the GROUP_U permission bit is set

# File lib/opennebula/document.rb, line 221
def public?
    if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
        true
    else
        false
    end
end
rename(name) click to toggle source

Renames this Document

@param name [String] New name for the Document.

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
# File lib/opennebula/document.rb, line 199
def rename(name)
    return call(DOCUMENT_METHODS[:rename], @pe_id, name)
end
update(new_template, append=false) click to toggle source

Replaces the template contents

@param [String] new_template new template contents @param append [true, false] True to append new attributes instead of

replace the whole template

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method
# File lib/opennebula/document.rb, line 128
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

Private Instance Methods

check_type() click to toggle source
# File lib/opennebula/document.rb, line 241
def check_type()
    type = self['TYPE']

    if type.nil? && @pe_id
        rc = @client.call(DOCUMENT_METHODS[:info], @pe_id)

        return rc if OpenNebula.is_error?(rc)

        xmldoc = XMLElement.new
        xmldoc.initialize_xml(rc, 'DOCUMENT')

        type = xmldoc['TYPE']
    end

    if !type.nil? && type.to_i != document_type
        return OpenNebula::Error.new(
            "[DocumentInfo] Error getting document [#{@pe_id}].")
    end

    return nil
end
set_publish(published) click to toggle source
# File lib/opennebula/document.rb, line 235
def set_publish(published)
    group_u = published ? 1 : 0

    chmod(-1, -1, -1, group_u, -1, -1, -1, -1, -1)
end