Class: CloudstackClient::Connection

Inherits:
Object
  • Object
show all
Includes:
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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cloudstack_client/connection.rb', line 19

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]
  @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
  @options = options
  validate_input!
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.


12
13
14
# File 'lib/cloudstack_client/connection.rb', line 12

def api_key
  @api_key
end

#api_urlObject

Returns the value of attribute api_url.


12
13
14
# File 'lib/cloudstack_client/connection.rb', line 12

def api_url
  @api_url
end

#async_poll_intervalObject

Returns the value of attribute async_poll_interval.


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

def async_poll_interval
  @async_poll_interval
end

#async_timeoutObject

Returns the value of attribute async_timeout.


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

def async_timeout
  @async_timeout
end

#debugObject

Returns the value of attribute debug.


12
13
14
# File 'lib/cloudstack_client/connection.rb', line 12

def debug
  @debug
end

#hostObject

Returns the value of attribute host.


12
13
14
# File 'lib/cloudstack_client/connection.rb', line 12

def host
  @host
end

#read_timeoutObject

Returns the value of attribute read_timeout.


12
13
14
# File 'lib/cloudstack_client/connection.rb', line 12

def read_timeout
  @read_timeout
end

#secret_keyObject

Returns the value of attribute secret_key.


12
13
14
# File 'lib/cloudstack_client/connection.rb', line 12

def secret_key
  @secret_key
end

#symbolize_keysObject

Returns the value of attribute symbolize_keys.


12
13
14
# File 'lib/cloudstack_client/connection.rb', line 12

def symbolize_keys
  @symbolize_keys
end

#verboseObject

Returns the value of attribute verbose.


12
13
14
# File 'lib/cloudstack_client/connection.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#send_async_request(params) ⇒ Object

Sends an asynchronous request and waits for the response.

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

Raises:

[View source]

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cloudstack_client/connection.rb', line 90

def send_async_request(params)
  data = send_request(params)

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

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

    case data[k('jobstatus')]
    when 1
      return data[k('jobresult')]
    when 2
      raise JobError, "Request failed (#{data[k('jobresultcode')]}): #{data[k('jobresult')][k('errortext')]}."
    end

    STDOUT.flush if @verbose
    sleep @async_poll_interval
  end

  raise TimeoutError, "Asynchronous request timed out."
end

#send_request(params) ⇒ Object

Sends a synchronous request to the CloudStack API and returns the response as a Hash.

[View source]

38
39
40
41
42
43
44
45
46
47
48
49
50
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 38

def send_request(params)
  params['response'] = 'json'
  params['apiKey'] = @api_key
  print_debug_output JSON.pretty_generate(params) if @debug

  data = params_to_data(params)
  uri = URI.parse "#{@api_url}?#{data}&signature=#{create_signature(data)}"

  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http.read_timeout = @read_timeout

  begin
    req = Net::HTTP::Get.new(uri.request_uri)
    req['Host'] = host if host.present?
    response = http.request(req)
  rescue
    raise ConnectionError, "API URL \'#{@api_url}\' is not reachable."
  end

  begin
    body = JSON.parse(response.body, symbolize_names: @symbolize_keys).values.first
  rescue JSON::ParserError
    raise ParseError,
          "Response from server is not readable. Check if the API endpoint (#{@api_url}) is valid and accessible."
  end

  if response.is_a?(Net::HTTPOK)
    return body unless body.respond_to?(:keys)
    if body.size == 2 && body.key?(k('count'))
      return body.reject { |key, _| key == k('count') }.values.first
    elsif body.size == 1 && body.values.first.respond_to?(:keys)
      item = body.values.first
      return (item.is_a?(Array) || item.is_a?(Hash)) ? item : []
    else
      body.reject! { |key, _| key == k('count') } if body.key?(k('count'))
      body.size == 0 ? [] : body
    end
  else
    message = body[k('errortext')] rescue body
    raise ApiError, "Status #{response.code}: #{message}."
  end
end