Class: CloudstackClient::Connection

Inherits:
Object
  • Object
show all
Includes:
RequestHandling, Utils
Defined in:
lib/cloudstack_client/connection.rb

Direct Known Subclasses

Client

Constant Summary collapse

DEF_POLL_INTERVAL =
2.0
DEF_ASYNC_TIMEOUT =
400
DEF_REQ_TIMEOUT =
60
DEF_REQUEST_RETRIES =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestHandling

#send_request

Methods included from Utils

#camel_case_to_underscore, #print_debug_output, #underscore_to_camel_case

Constructor Details

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

Returns a new instance of Connection.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cloudstack_client/connection.rb', line 24

def initialize(api_url, api_key, secret_key, options = {})
  @api_url = api_url
  @api_key = api_key
  @secret_key = secret_key
  @verbose = options[:quiet] ? false : true
  @debug = options[:debug] ? true : false
  @symbolize_keys = options[:symbolize_keys] ? true : false
  @host = options[:host]
  @verify_ssl = options.fetch(:verify_ssl, true)
  @ca_file = options[:ca_file]
  @read_timeout = options[:read_timeout] || DEF_REQ_TIMEOUT
  @async_poll_interval = options[:async_poll_interval] || DEF_POLL_INTERVAL
  @async_timeout = options[:async_timeout] || DEF_ASYNC_TIMEOUT
  @request_retries = options[:request_retries] || DEF_REQUEST_RETRIES
  @options = options
  validate_input!
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#api_urlObject

Returns the value of attribute api_url.



15
16
17
# File 'lib/cloudstack_client/connection.rb', line 15

def api_url
  @api_url
end

#async_poll_intervalObject

Returns the value of attribute async_poll_interval.



17
18
19
# File 'lib/cloudstack_client/connection.rb', line 17

def async_poll_interval
  @async_poll_interval
end

#async_timeoutObject

Returns the value of attribute async_timeout.



17
18
19
# File 'lib/cloudstack_client/connection.rb', line 17

def async_timeout
  @async_timeout
end

#ca_fileObject

Returns the value of attribute ca_file.



16
17
18
# File 'lib/cloudstack_client/connection.rb', line 16

def ca_file
  @ca_file
end

#debugObject

Returns the value of attribute debug.



15
16
17
# File 'lib/cloudstack_client/connection.rb', line 15

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



15
16
17
# File 'lib/cloudstack_client/connection.rb', line 15

def host
  @host
end

#read_timeoutObject

Returns the value of attribute read_timeout.



15
16
17
# File 'lib/cloudstack_client/connection.rb', line 15

def read_timeout
  @read_timeout
end

#request_retriesObject

Returns the value of attribute request_retries.



17
18
19
# File 'lib/cloudstack_client/connection.rb', line 17

def request_retries
  @request_retries
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



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

def secret_key
  @secret_key
end

#symbolize_keysObject

Returns the value of attribute symbolize_keys.



15
16
17
# File 'lib/cloudstack_client/connection.rb', line 15

def symbolize_keys
  @symbolize_keys
end

#verboseObject

Returns the value of attribute verbose.



15
16
17
# File 'lib/cloudstack_client/connection.rb', line 15

def verbose
  @verbose
end

#verify_sslObject

Returns the value of attribute verify_ssl.



16
17
18
# File 'lib/cloudstack_client/connection.rb', line 16

def verify_ssl
  @verify_ssl
end

Instance Method Details

#send_async_request(params, opts = {}) ⇒ Object

Sends an asynchronous request and waits for the response.

The contents of the 'jobresult' element are returned upon completion of the command.

Raises:



51
52
53
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
# File 'lib/cloudstack_client/connection.rb', line 51

def send_async_request(params, opts = {})
  request_timeout = opts[:async_timeout] || @async_timeout
  poll_interval = opts[:async_poll_interval] || @async_poll_interval
  validate_async_timeouts!(request_timeout, poll_interval)

  data = send_request(params, opts)

  params = {
    'command' => 'queryAsyncJobResult',
    'jobid' => data[k('jobid')]
  }

  max_tries(request_timeout, poll_interval).times do
    data = send_request(params)
    print "." if @verbose

    case data[k('jobstatus')]
    when 1
      return data[k('jobresult')]
    when 2
      result = data[k('jobresult')]
      error_text = result.is_a?(Hash) ? result[k('errortext')] : nil
      error_text ||= "Unknown error"
      raise JobError,
            "Request failed (#{data[k('jobresultcode')]}): #{error_text}."
    end

    STDOUT.flush if @verbose
    sleep poll_interval
  end

  raise TimeoutError, "Asynchronous request timed out."
end