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 =
400

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Connection.



25
26
27
28
29
30
31
32
# File 'lib/cloudstack_client/client.rb', line 25

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] ? false : true
  @debug = opts[:debug] ? true : false
end

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



23
24
25
# File 'lib/cloudstack_client/client.rb', line 23

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.



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
118
119
120
121
122
123
# File 'lib/cloudstack_client/client.rb', line 92

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
      return json['jobresult']
    elsif status == 2
      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.



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

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')
  }

  debug_output JSON.pretty_generate(params) if @debug

  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)
    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