Class: OpenNebula::XMLClient

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

Overview

The openNebula XMLRPC client class, represents the connection with the core and handles the xml-rpc calls.

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

  • secret (String, nil)

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

  • endpoint (String, nil)

    OpenNebula server endpoint (host:2633/RPC2) or nil to get it from the environment variable ONE_XMLRPC or use the default endpoint

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

    a customizable set of options



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/opennebula/lib/xml_client.rb', line 114

def initialize(secret, endpoint, options = {})
    @one_auth = secret
    @one_endpoint = endpoint

    @async = !options[:sync]

    http_proxy = options[:http_proxy]
    timeout    = options[:timeout] || ENV['ONE_XMLRPC_TIMEOUT']&.to_i

    @server    = XMLRPC::Client.new2(@one_endpoint, http_proxy, timeout)
    @server.http_header_extra = { 'accept-encoding' => 'identity' }

    http = @server.instance_variable_get('@http')

    if options[:cert_dir] || ENV['ONE_CERT_DIR']
        raise "SSL options don't work in async mode" if @async

        cert_dir = options[:cert_dir] || ENV['ONE_CERT_DIR']
        cert_files = Dir["#{cert_dir}/*"]

        cert_store = OpenSSL::X509::Store.new
        cert_store.set_default_paths
        cert_files.each {|cert| cert_store.add_file(cert) }

        http.cert_store = cert_store
    end

    if options[:disable_ssl_verify] || ENV['ONE_DISABLE_SSL_VERIFY']
        raise "SSL options don't work in async mode" if @async

        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    if defined?(OxStreamParser)
        @server.set_parser(OxStreamParser.new)
    elsif OpenNebula::NOKOGIRI
        @server.set_parser(NokogiriStreamParser.new)
    elsif XMLPARSER
        @server.set_parser(XMLRPC::XMLParser::XMLStreamParser.new)
    end
end

Instance Method Details

#call(action, *args) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/opennebula/lib/xml_client.rb', line 156

def call(action, *args)
    begin
        if @async
            response = @server.call_async("one.#{action}", @one_auth, *args)
        else
            response = @server.call("one.#{action}", @one_auth, *args)
        end

        if response[0] == false
            Error.new(response[1], response[2])
        else
            response[1] # response[1..-1]
        end
    rescue StandardError => e
        Error.new(e.message, Error::EXML_RPC_CALL)
    end
end