class OpenNebula::ServicePool

ServicePool class

Public Class Methods

new(cloud_auth, client) click to toggle source

Class constructor

@param [OpenNebula::Client] client the xml-rpc client @param [Integer] user_id the filter flag, see

http://docs.opennebula.io/stable/integration/system_interfaces/api.html

@return [DocumentPool] the new object

Calls superclass method
# File lib/opennebula/flow/service_pool.rb, line 51
def initialize(cloud_auth, client)
    # TODO, what if cloud_auth is nil?
    @cloud_auth = cloud_auth
    @client     = client
    @one_pool   = nil

    if @client
        rc = @client.call('user.info', -1)

        unless OpenNebula.is_error?(rc)
            info     = Nokogiri::XML(rc)
            @user_id = Integer(info.xpath('/USER/ID').text)
        end
    end

    super('DOCUMENT_POOL', 'DOCUMENT', @client)
end

Public Instance Methods

client(user_name = nil) click to toggle source
# File lib/opennebula/flow/service_pool.rb, line 69
def client(user_name = nil)
    # If there's a client defined use it
    return @client unless @client.nil?

    # If not, get one via cloud_auth
    if user_name.nil?
        @cloud_auth.client
    else
        @cloud_auth.client(user_name)
    end
end
each(&block) click to toggle source
# File lib/opennebula/flow/service_pool.rb, line 105
def each(&block)
    return if @one_pool.nil?

    @one_pool.each(&block)
end
each_page(size) { |service| ... } click to toggle source

Iterates over pool pages

size

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

state state of objects

# File lib/opennebula/flow/service_pool.rb, line 115
def each_page(size)
    loop_page(size, Service::DOCUMENT_TYPE, false) do |element, page|
        page.each("//#{element}") do |obj|
            service = Service.new_with_id(obj['ID'], @client)
            service.info

            yield(service)
        end

        size
    end
end
get(service_id, external_user = nil, &block) click to toggle source

Retrieves a Service element from OpenNebula. The Service::info() method is called

@param [Integer] service_id Numerical Id of the service to retrieve @yieldparam [Service] this block will have the service's mutex locked.

The mutex will be unlocked after the block execution.

@return [Service, OpenNebula::Error] The Service in case of success

# File lib/opennebula/flow/service_pool.rb, line 136
def get(service_id, external_user = nil, &block)
    service_id = service_id.to_i if service_id
    aux_client = nil

    # WARNING!!!
    # No validation will be performed for external_user, the credentials
    # for this user must be validated previously.
    if external_user.nil?
        aux_client = client
    else
        aux_client = client(external_user)
    end

    service = Service.new_with_id(service_id, aux_client)

    if block_given?
        obj_mutex = nil
        entry     = nil

        @@mutex.synchronize do
            # entry is an array of [Mutex, waiting]
            # waiting is the number of threads waiting on this mutex
            entry = @@mutex_hash[service_id]

            if entry.nil?
                entry = [Mutex.new, 0]
                @@mutex_hash[service_id] = entry
            end

            obj_mutex = entry[0]
            entry[1]  = entry[1] + 1

            if @@mutex_hash.size > 10000
                @@mutex_hash.delete_if do |_s_id, entry_loop|
                    entry_loop[1] == 0
                end
            end
        end

        rc = obj_mutex.synchronize do
            rc = service.info

            if OpenNebula.is_error?(rc)
                return rc
            end

            block.call(service, client)
        end

        @@mutex.synchronize do
            entry[1] = entry[1] - 1
        end

        if OpenNebula.is_error?(rc)
            return rc
        end
    else
        service.info
    end

    service
end
info() click to toggle source
# File lib/opennebula/flow/service_pool.rb, line 81
def info
    osp = OpenNebulaServicePool.new(client)
    rc  = osp.info

    @one_pool = osp

    rc
end
info_all() click to toggle source
# File lib/opennebula/flow/service_pool.rb, line 90
def info_all
    osp = OpenNebulaServicePool.new(client)
    rc  = osp.info_all

    @one_pool = osp

    rc
end
to_json() click to toggle source

rubocop:disable Lint/ToJSON

# File lib/opennebula/flow/service_pool.rb, line 100
def to_json
    # rubocop:enable Lint/ToJSON
    @one_pool.to_json
end