Class: OpenNebula::Client

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

Constant Summary collapse

NO_ONE_AUTH_ERROR =
'ONE_AUTH file not present'
GRPC_NOT_SUPPORTED =
'gRPC API not supported'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize an OpenNebula client

Parameters:

  • secret (String, nil) (defaults to: nil)

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

  • endpoint (String, nil) (defaults to: nil)

    OpenNebula server endpoint or nil to get it form the environment or use the default endpoint

  • options (Hash) (defaults to: {})

    specific XML client options (see XMLClient and GRPCClient for details)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/opennebula/lib/client.rb', line 54

def initialize(secret = nil, endpoint = nil, options = {})
    if options[:subscriber_endpoint]
        @one_zmq = options[:subscriber_endpoint]
    elsif ENV['ONE_ZMQ']
        @one_zmq = ENV['ONE_ZMQ']
    else
        @one_zmq = 'tcp://localhost:2101'
    end

    auth_file     = File.join(ENV['HOME'], '/.one/one_auth')
    one_auth_file = '/var/lib/one/.one/one_auth'

    @one_auth =
        if secret
            secret
        elsif ENV['ONE_AUTH'] && File.file?(ENV['ONE_AUTH'])
            File.read(ENV['ONE_AUTH'])
        elsif File.file?(auth_file)
            File.read(auth_file)
        elsif File.file?(one_auth_file)
            File.read(one_auth_file)
        else
            raise NO_ONE_AUTH_ERROR
        end

    @one_auth = @one_auth.rstrip

    is_grpc = ENV['ONEAPI_PROTOCOL'] == 'grpc' || options.key?(:grpc)

    # Override protocol from endpoint name, if endpoint is defined
    is_grpc = !endpoint.include?('RPC2') if !endpoint.nil? && !endpoint.empty?

    if is_grpc
        begin
            require_relative 'grpc_client'
        rescue LoadError
            raise GRPC_NOT_SUPPORTED
        end
        @one_endpoint = mk_endpoint(endpoint,
                                    'ONE_GRPC',
                                    'localhost:2634').rstrip

        @client = GRPCClient.new(@one_auth, @one_endpoint, options)
    else
        require_relative 'xml_client'
        @one_endpoint = mk_endpoint(endpoint,
                                    'ONE_XMLRPC',
                                    'http://localhost:2633/RPC2').rstrip

        @client = XMLClient.new(@one_auth, @one_endpoint, options)
    end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



42
43
44
# File 'lib/opennebula/lib/client.rb', line 42

def client
  @client
end

#one_authObject (readonly)

Returns the value of attribute one_auth.



42
43
44
# File 'lib/opennebula/lib/client.rb', line 42

def one_auth
  @one_auth
end

#one_endpointObject (readonly)

Returns the value of attribute one_endpoint.



42
43
44
# File 'lib/opennebula/lib/client.rb', line 42

def one_endpoint
  @one_endpoint
end

#one_zmqObject (readonly)

Returns the value of attribute one_zmq.



42
43
44
# File 'lib/opennebula/lib/client.rb', line 42

def one_zmq
  @one_zmq
end

Instance Method Details

#call(action, *args) ⇒ Object



107
108
109
# File 'lib/opennebula/lib/client.rb', line 107

def call(action, *args)
    @client.call(action, *args)
end

#get_versionObject



111
112
113
# File 'lib/opennebula/lib/client.rb', line 111

def get_version
    call('system.version')
end

#mk_endpoint(endpoint, env_var, default) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/opennebula/lib/client.rb', line 115

def mk_endpoint(endpoint, env_var, default)
    ep_file      = File.join(ENV['HOME'], '/.one/one_endpoint')
    one_ep_file  = '/var/lib/one/.one/one_endpoint'

    if endpoint
        endpoint
    elsif ENV[env_var]
        ENV[env_var]
    elsif File.exist?(ep_file)
        File.read(ep_file)
    elsif File.exist?(one_ep_file)
        File.read(one_ep_file)
    else
        default
    end
end