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
SECGROUP_POOL DOCUMENT_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



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

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

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

Iterates over pool pages

size

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

state state of objects



297
298
299
300
301
302
303
304
305
# 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) ⇒ Object

Iterates over pool pages to delete them

size

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

state state of objects



311
312
313
314
315
316
317
318
319
320
321
# 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_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.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# 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)) ⇒ 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



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# 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) ⇒ Object

Gets a pool in hash form using pagination

size

Integer size of each page



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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?Boolean

Return true if pool is paginated

Returns:

  • (Boolean)


324
325
326
# File 'lib/opennebula/pool.rb', line 324

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

#loop_page(size, state, extended) ⇒ Object

Iterates over pool page



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# 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_strObject

DO NOT USE - ONLY REXML BACKEND



176
177
178
179
180
181
# File 'lib/opennebula/pool.rb', line 176

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

    return str
end