class OpenNebula::Pool

The Pool class represents a generic OpenNebula Pool in XML format and provides the basic functionality to handle the Pool elements

Constants

INFO_ALL
INFO_GROUP

Constants for info queries (include/RequestManagerPoolInfoFilter.h)

INFO_MINE
INFO_PRIMARY_GROUP
PAGINATED_POOLS

Attributes

element_name[R]
pool_name[R]

Public Class Methods

new(pool,element,client) click to toggle source
pool

String XML name of the root element

element

String XML name of the Pool elements

client

Client represents a XML-RPC connection

Calls superclass method
# File lib/opennebula/pool.rb, line 37
def initialize(pool,element,client)
    super(nil)

    @pool_name    = pool.upcase
    @element_name = element.upcase

    @client = client
end

Public Instance Methods

each(&block) click to toggle source

Iterates over every PoolElement in the Pool and calls the block with a a PoolElement obtained calling the factory method

block

Block

# File lib/opennebula/pool.rb, line 171
def each(&block)
    each_element(block) if @xml
end
Also aliased as: each_with_xpath
each_page(size, state = -1, extended = false) { |obj| ... } click to toggle source

Iterates over pool pages

size

nil => default page size > 0 => page size

state state of objects

# File lib/opennebula/pool.rb, line 297
def each_page(size, state = -1, extended = false)
    loop_page(size, state, extended) do |element, page|
        page.each("//#{element}") do |obj|
            yield(obj)
        end

        size
    end
end
each_page_delete(size, state = -1, extended = false) { |obj| ... } click to toggle source

Iterates over pool pages to delete them

size

nil => default page size > 0 => page size

state state of objects

# File lib/opennebula/pool.rb, line 311
def each_page_delete(size, state = -1, extended = false)
    loop_page(size, state, extended) do |element, page|
        no_deleted = 0

        page.each("//#{element}") do |obj|
            no_deleted += 1 if !yield(obj)
        end

        no_deleted
    end
end
each_with_xpath(&block)
Alias for: each
get_hash(size=nil) click to toggle source

Gets a hash from a pool

size

nil => default page size < 2 => not paginated >=2 => page size

The default page size can be changed with the environment variable ONE_POOL_PAGE_SIZE. Any value > 2 will set a page size, a non numeric value disables pagination.

# File lib/opennebula/pool.rb, line 192
def get_hash(size=nil)
    allow_paginated = PAGINATED_POOLS.include?(@pool_name)

    if OpenNebula.pool_page_size && allow_paginated &&
            ( ( size && size >= 2 ) || !size )

        size = OpenNebula.pool_page_size if !size
        hash = info_paginated(size)

        return hash if OpenNebula.is_error?(hash)

        { @pool_name => { @element_name => hash } }
    else
        rc = info
        return rc if OpenNebula.is_error?(rc)

        to_hash
    end
end
get_page(size, current, extended = false, state = -1) click to toggle source

Gets a hash from a info page from pool

size

nil => default page size > 0 => page size

current first element of the page extended true to get extended information state state of the objects

hash

return page as a hash

# File lib/opennebula/pool.rb, line 248
def get_page(size, current, extended = false, state = -1)
    rc      = nil
    state ||= -1

    if PAGINATED_POOLS.include?(@pool_name)
        pool_name = @pool_name.delete('_').downcase

        if extended && pool_name == "vmpool"
            method = "#{pool_name}.infoextended"
        else
            method = "#{pool_name}.info"
        end

        size = OpenNebula.pool_page_size if (!size || size == 0)
        rc   = @client.call(method, @user_id, current, -size, state)

        return rc if OpenNebula.is_error?(rc)

        initialize_xml(rc, @pool_name)
    else
        rc = info
    end

    return rc
end
info_paginated(size) click to toggle source

Gets a pool in hash form using pagination

size

Integer size of each page

# File lib/opennebula/pool.rb, line 215
def info_paginated(size)
    array   = Array.new
    current = 0

    parser = ParsePoolSax.new(@pool_name, @element_name)

    while true
        a = @client.call("#{@pool_name.delete('_').downcase}.info",
                @user_id, current, -size, -1)

        return a if OpenNebula.is_error?(a)

        a_array=parser.parse(a)

        array   += a_array
        current += size

        break if !a || a_array.length<size
    end

    array.compact!
    array = nil if array.length == 0

    array
end
is_paginated?() click to toggle source

Return true if pool is paginated

# File lib/opennebula/pool.rb, line 324
def is_paginated?
    PAGINATED_POOLS.include?(@pool_name)
end
loop_page(size, state, extended) { |element, page| ... } click to toggle source

Iterates over pool page

# File lib/opennebula/pool.rb, line 275
def loop_page(size, state, extended)
    current = 0
    element = @pool_name.split('_')[0]
    page    = OpenNebula::XMLElement.new

    loop do
        rc = get_page(size, current, extended, state)

        break rc if OpenNebula.is_error?(rc)

        page.initialize_xml(rc, @pool_name)

        break if page["//#{element}"].nil?

        current += yield(element, page)
    end
end
to_str() click to toggle source

DO NOT USE - ONLY REXML BACKEND

# File lib/opennebula/pool.rb, line 176
def to_str
    str = ""
    REXML::Formatters::Pretty.new(1).write(@xml,str)

    return str
end

Protected Instance Methods

factory(element_xml) click to toggle source

Default Factory Method for the Pools. The factory method returns an suitable PoolElement object. Each Pool MUST implement the corresponding factory method

element_xml

XML XML element describing the pool element

return

a PoolElement object

# File lib/opennebula/pool.rb, line 51
def factory(element_xml)
    OpenNebula::PoolElement.new(element_xml,client)
end
info(xml_method) click to toggle source

Gets the pool without any filter. Host, Group and User Pools

xml_method:: _String_ the name of the XML-RPC method
# File lib/opennebula/pool.rb, line 61
def info(xml_method)
    return xmlrpc_info(xml_method)
end
Also aliased as: info!
info!(xml_method)
Alias for: info
info_all(xml_method, *args) click to toggle source
# File lib/opennebula/pool.rb, line 71
def info_all(xml_method, *args)
    return xmlrpc_info(xml_method, INFO_ALL, -1, -1, *args)
end
info_extended(xml_method) click to toggle source
# File lib/opennebula/pool.rb, line 67
def info_extended(xml_method)
    return xmlrpc_info(xml_info)
end
info_filter(xml_method, who, start_id, end_id, *args) click to toggle source
# File lib/opennebula/pool.rb, line 87
def info_filter(xml_method, who, start_id, end_id, *args)
    return xmlrpc_info(xml_method, who, start_id, end_id, *args)
end
info_group(xml_method, *args) click to toggle source
# File lib/opennebula/pool.rb, line 79
def info_group(xml_method, *args)
    return xmlrpc_info(xml_method, INFO_GROUP, -1, -1, *args)
end
info_mine(xml_method, *args) click to toggle source
# File lib/opennebula/pool.rb, line 75
def info_mine(xml_method, *args)
    return xmlrpc_info(xml_method, INFO_MINE, -1, -1, *args)
end
info_primary_group(xml_method, *args) click to toggle source
# File lib/opennebula/pool.rb, line 83
def info_primary_group(xml_method, *args)
    return xmlrpc_info(xml_method, INFO_PRIMARY_GROUP, -1, -1, *args)
end
monitoring(xml_method, xpaths, *args) click to toggle source

Retrieves the monitoring data for all the Objects in the pool

@param [String] xml_method xml-rcp method @param [Array<String>] xpath_expressions Elements to retrieve. @param args arguemnts for the xml_method call

@return [Hash<String, <Hash<String, Array<Array<int>>>>>,

OpenNebula::Error] The first level hash uses the Object ID as keys,
and as value a Hash with the requested xpath expressions,
and an Array of 'timestamp, value'.
# File lib/opennebula/pool.rb, line 101
def monitoring(xml_method, xpaths, *args)

    rc = @client.call(xml_method, *args)

    if ( OpenNebula.is_error?(rc) )
        return rc
    end

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

    hash = {}

    # Get all existing Object IDs
    ids = xmldoc.retrieve_elements('/MONITORING_DATA/MONITORING/ID')

    if ids.nil?
        return hash
    else
        ids.uniq!
    end

    ids.each { |id|
        hash[id] = OpenNebula.process_monitoring(xmldoc, id, xpaths)
    }

    return hash
end
monitoring_last(xml_method, *args) click to toggle source
# File lib/opennebula/pool.rb, line 130
def monitoring_last(xml_method, *args)
    rc = @client.call(xml_method, *args)

    if OpenNebula.is_error?(rc)
        return rc
    end

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

    xmldoc.to_hash
end

Private Instance Methods

xmlrpc_info(xml_method, *args) click to toggle source

Calls to the corresponding info method to retreive the pool representation in XML format

xml_method

String the name of the XML-RPC method

args

Array with additional arguments for the info call

return

nil in case of success or an Error object

# File lib/opennebula/pool.rb, line 149
def xmlrpc_info(xml_method, *args)
    rc = @client.call(xml_method, *args)

    if !OpenNebula.is_error?(rc)
        initialize_xml(rc, @pool_name)

        rc = nil
    end

    return rc
end