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

Instance Attribute Summary collapse

Attributes inherited from Connection

#api_key, #api_url, #async_poll_interval, #async_timeout, #debug, #secret_key, #verbose

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, #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
# 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 && @api.command_supports_param?(command["name"], k)
          params[k] = v
        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)
      else
        send_async_request(params)
      end
    end
  end
end