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



164
165
166
# File 'lib/opennebula/pool.rb', line 164

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

#each_page(size, state = -1,, extended = false, delete = false) ⇒ Object

Iterates over pool pages

size

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

state state of objects delete true to take always the first page



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/opennebula/pool.rb', line 269

def each_page(size, state = -1, extended = false, delete = false)
    current = 0
    element = @pool_name.split('_')[0]
    page    = OpenNebula::XMLElement.new

    loop do
        page.initialize_xml(get_page(size, current, extended, state),
                                     @pool_name)

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

        page.each("//#{element}") do |obj|
            yield(obj)
        end

        current += size unless delete
    end
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.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/opennebula/pool.rb', line 185

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

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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/opennebula/pool.rb', line 241

def get_page(size, current, extended = false, state = -1)
    rc = nil

    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)

        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



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

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)


289
290
291
# File 'lib/opennebula/pool.rb', line 289

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

#to_strObject

DO NOT USE - ONLY REXML BACKEND



169
170
171
172
173
174
# File 'lib/opennebula/pool.rb', line 169

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

    return str
end