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



158
159
160
# File 'lib/opennebula/pool.rb', line 158

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



281
282
283
284
285
286
287
288
289
# File 'lib/opennebula/pool.rb', line 281

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



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/opennebula/pool.rb', line 295

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.



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

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/opennebula/pool.rb', line 235

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



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

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)


308
309
310
# File 'lib/opennebula/pool.rb', line 308

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

#loop_page(size, state, extended) ⇒ Object

Iterates over pool page



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/opennebula/pool.rb', line 259

def loop_page(size, state, extended)
    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?

        current += yield(element, page)
    end
end

#to_strObject

DO NOT USE - ONLY REXML BACKEND



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

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

    return str
end