Class: CloudstackClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudstack-client/client.rb

Constant Summary collapse

@@async_poll_interval =
2.0
@@async_timeout =
300

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Connection.



23
24
25
26
27
28
29
# File 'lib/cloudstack-client/client.rb', line 23

def initialize(api_url, api_key, secret_key, opts = {})
  @api_url = api_url
  @api_key = api_key
  @secret_key = secret_key
  @use_ssl = api_url.start_with? "https"
  @verbose = !opts[:quiet]
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.



86
87
88
89
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
115
116
117
# File 'lib/cloudstack-client/client.rb', line 86

def send_async_request(params)

  json = send_request(params)

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

  max_tries = (@@async_timeout / @@async_poll_interval).round
  max_tries.times do
    json = send_request(params)
    status = json['jobstatus']

    print "." if @verbose

    if status == 1 then
      return json['jobresult']
    elsif status == 2 then
      puts
      puts "Request failed (#{json['jobresultcode']}): #{json['jobresult']}"
      exit 1
    end

    STDOUT.flush
    sleep @@async_poll_interval
  end

  print "\n"
  puts "Error: Asynchronous request timed out"
  exit 1
end

#send_request(params) ⇒ Object

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

The wrapper element of the response (e.g. mycommandresponse) is discarded and the contents of that element are returned.



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
# File 'lib/cloudstack-client/client.rb', line 37

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

  params_arr = []
  params.sort.each { |elem|
    params_arr << elem[0].to_s + '=' + CGI.escape(elem[1].to_s).gsub('+', '%20').gsub(' ','%20')
  }
  data = params_arr.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)
  http.use_ssl = @use_ssl
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  begin
    response = http.request(Net::HTTP::Get.new(uri.request_uri))
  rescue
    puts "Error connecting to API:"
    puts "#{@api_url} is not reachable"
    exit 1
  end

  if !response.is_a?(Net::HTTPOK) then
    puts "Error #{response.code}: #{response.message}"
    puts JSON.pretty_generate(JSON.parse(response.body))
    puts "URL: #{url}"
    exit 1
  end

  begin 
    json = JSON.parse(response.body)
  rescue JSON::ParserError
    puts "Error parsing response from server."
    exit 1
  end
  json[params['command'].downcase + 'response']
end