Class: CloudstackClient::Connection

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

Direct Known Subclasses

Client

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.



16
17
18
19
20
21
22
23
24
25
# File 'lib/cloudstack_client/connection.rb', line 16

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
  @async_poll_interval = options[:async_poll_interval] || 2.0
  @async_timeout = options[:async_timeout] || 400
  @options = options
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#api_urlObject

Returns the value of attribute api_url.



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

def api_url
  @api_url
end

#async_poll_intervalObject

Returns the value of attribute async_poll_interval.



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

def async_poll_interval
  @async_poll_interval
end

#async_timeoutObject

Returns the value of attribute async_timeout.



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

def async_timeout
  @async_timeout
end

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#secret_keyObject

Returns the value of attribute secret_key.



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

def secret_key
  @secret_key
end

#verboseObject

Returns the value of attribute verbose.



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

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:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cloudstack_client/connection.rb', line 100

def send_async_request(params)
  data = send_request(params)

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

  max_tries.times do
    data = send_request(params)

    print "." if @verbose

    case data['jobstatus']
    when 1
      return data['jobresult']
    when 2
      raise JobError, "Request failed (#{data['jobresultcode']}): #{data['jobresult']['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.



31
32
33
34
35
36
37
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
84
85
86
87
88
89
90
91
92
93
# File 'lib/cloudstack_client/connection.rb', line 31

def send_request(params)
  params['response'] = 'json'
  params['apiKey'] = @api_key

  params_arr = params.sort.map do |key, value|
    # support for maps (Arrays of Hashes)
    if value.is_a?(Array)
      map = []
      value.each_with_index do |items, i|
        items.each {|k, v| map << "#{key}[#{i}].#{k}=#{escape(v)}"}
      end
      map.sort.join("&")
    else
      "#{key}=#{escape(value)}"
    end
  end

  print_debug_output JSON.pretty_generate(params) if @debug

  data = params_arr.sort.join('&')
  signature = OpenSSL::HMAC.digest('sha1', @secret_key, data.downcase)
  signature = Base64.encode64(signature).chomp
  signature = CGI.escape(signature)

  url = "#{@api_url}?#{data}&signature=#{signature}"

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

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

  begin
    body = JSON.parse(response.body).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?('count')
      return body.reject { |key, _| key == '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 == 'count' } if body.key?('count')
      body.size == 0 ? [] : body
    end
  else
    message = body['errortext'] rescue body
    raise ApiError, "Status #{response.code}: #{message}."
  end
end