Class: CloudstackClient::Client

Inherits:
Connection show all
Includes:
Utils
Defined in:
lib/cloudstack_client/client.rb

Constant Summary

Constants inherited from Connection

CloudstackClient::Connection::DEF_ASYNC_TIMEOUT, CloudstackClient::Connection::DEF_POLL_INTERVAL, CloudstackClient::Connection::DEF_REQUEST_RETRIES, CloudstackClient::Connection::DEF_REQ_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Connection

#api_key, #api_url, #async_poll_interval, #async_timeout, #ca_file, #debug, #host, #read_timeout, #request_retries, #secret_key, #symbolize_keys, #verbose, #verify_ssl

Instance Method Summary collapse

Methods included from Utils

#camel_case_to_underscore, #print_debug_output, #underscore_to_camel_case

Methods inherited from Connection

#send_async_request

Methods included from RequestHandling

#send_request

Constructor Details

#initialize(api_url, api_key, secret_key, options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/cloudstack_client/client.rb', line 13

def initialize(api_url, api_key, secret_key, options = {})
  super(api_url, api_key, secret_key, options)
  define_api_methods unless options[:no_api_methods]
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



11
12
13
# File 'lib/cloudstack_client/client.rb', line 11

def api
  @api
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/cloudstack_client/client.rb', line 10

def options
  @options
end

Instance Method Details

#define_api_methodsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cloudstack_client/client.rb', line 18

def define_api_methods
  @api = Api.new(@options)
  @api.commands.each do |name, command|
    method_name = camel_case_to_underscore(command["name"]).to_sym

    define_singleton_method(method_name) do |args = {}, options = {}|
      params = { "command" => command["name"] }

      args.each do |k, v|
        k = k.to_s.gsub("_", "")
        if !v.nil? && @api.command_supports_param?(command["name"], k)
          params[k] = v
        elsif !v.nil? && options.fetch(:strict_params, @options[:strict_params])
          raise ParameterError,
                "#{command['name']} does not support parameter #{k}"
        end
      end

      unless @api.all_required_params?(command["name"], params)
        raise ParameterError, @api.missing_params_msg(command["name"])
      end


      if command["isasync"] == false || options[:sync]
        send_request(params, options)
      else
        send_async_request(params, options)
      end
    end
  end
end