Class: OpenNebula::Pool

Inherits:
XMLPool show all
Includes:
Enumerable
Defined in:
lib/opennebula/pool.rb

Overview

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

Constant Summary collapse

PAGINATED_POOLS =
%w{VM_POOL IMAGE_POOL TEMPLATE_POOL VN_POOL
DOCUMENT_POOL SECGROUP_POOL}
INFO_GROUP =

Constants for info queries (include/RequestManagerPoolInfoFilter.h)

-1
INFO_ALL =
-2
INFO_MINE =
-3
INFO_PRIMARY_GROUP =
-4

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from XMLPool

#each_element

Methods inherited from XMLElement

#[], #add_element, #attr, build_xml, #delete_element, #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?

Instance Attribute Details

#element_nameObject (readonly)

Returns the value of attribute element_name.



28
29
30
# File 'lib/opennebula/pool.rb', line 28

def element_name
  @element_name
end

#pool_nameObject (readonly)

Returns the value of attribute pool_name.



27
28
29
# File 'lib/opennebula/pool.rb', line 27

def pool_name
  @pool_name
end

Instance Method Details

#each(&block) ⇒ Object

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

block

Block



160
161
162
# File 'lib/opennebula/pool.rb', line 160

def each(&block)
    each_element(block) if @xml
end

#each_with_xpathObject



25
# File 'lib/opennebula/pool.rb', line 25

alias_method :each_with_xpath, :each

#get_hash(size = nil) ⇒ Object

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/opennebula/pool.rb', line 181

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) ⇒ Object

Gets a hash from a info page from pool

size

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

current first element of the page

hash

return page as a hash



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/opennebula/pool.rb', line 235

def get_page(size, current)
    rc = nil

    if PAGINATED_POOLS.include?(@pool_name) 
        size = OpenNebula.pool_page_size if (!size || size == 0)
        rc   = @client.call("#{@pool_name.delete('_').downcase}.info",
                    @user_id, current, -size, -1)

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

    return rc 
end

#info_paginated(size) ⇒ Object

Gets a pool in hash form using pagination

size

Integer size of each page



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/opennebula/pool.rb', line 204

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?Boolean

Return true if pool is paginated

Returns:

  • (Boolean)


252
253
254
# File 'lib/opennebula/pool.rb', line 252

def is_paginated?
    PAGINATED_POOLS.include?(@pool_name) 
end

#to_strObject

DO NOT USE - ONLY REXML BACKEND



165
166
167
168
169
170
# File 'lib/opennebula/pool.rb', line 165

def to_str
    str = ""
    REXML::Formatters::Pretty.new(1).write(@xml,str)

    return str
end