Class: OpenNebula::GRPCClient

Inherits:
Object
  • Object
show all
Defined in:
lib/opennebula/lib/grpc_client.rb

Constant Summary collapse

GRPC_MAP =

gRPC mappings from OpenNebula actions to gRPC actions

GRPCMappings::MapLoader.load

Instance Method Summary collapse

Constructor Details

#initialize(secret, endpoint, options = {}) ⇒ OpenNebula::GRPCClient

Creates a new gRPC client object that will be used to call OpenNebula functions.

Parameters:

  • secret (String)

    user credentials (“user:password”) or nil to get the credentials from user auth file

  • endpoint (String)

    OpenNebula gRPC server endpoint (host:2634) or nil to get it from the environment variable ONE_GRPC or use the default endpoint



41
42
43
44
45
# File 'lib/opennebula/lib/grpc_client.rb', line 41

def initialize(secret, endpoint, options = {})
    @one_auth     = secret
    @one_endpoint = endpoint
    @timeout      = options[:timeout] || ENV['ONE_GRPC_TIMEOUT']&.to_i
end

Instance Method Details

#call(action, *args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/opennebula/lib/grpc_client.rb', line 47

def call(action, *args)
    handler = GRPC_MAP[action]
    raise Error.new("Method 'one.#{action}' not defined", Error::EGRPC_CALL) unless handler

    options = {}
    options[:deadline] = Time.now.to_i + @timeout if @timeout

    response = handler.call(@one_auth, @one_endpoint, *args, options)

    if response.respond_to?(:oid)
        response.oid
    elsif response.respond_to?(:xml)
        response.xml
    else
        Error.new("Unexpected response: #{response.inspect}", Error::EGRPC_CALL)
    end
rescue GRPC::DeadlineExceeded
    Error.new("Timeout exceeded for gRPC call '#{action}'", Error::ETIMEOUT)
rescue GRPC::Unavailable => e
    Error.new("#{action}: #{clean_error_message(e.message)}", Error::EGRPC_CALL)
rescue GRPC::BadStatus => e
    Error.new(clean_error_message(e.message), grpc_code_to_one(e.code))
rescue StandardError => e
    Error.new("#{action}: #{e.message}", Error::EGRPC_CALL)
end