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.



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

def initialize(api_url, api_key, secret_key, opts = {})
  @api_url = api_url
  @api_key = api_key
  @secret_key = secret_key
  @verbose = opts[:quiet] ? false : true
  @debug = opts[:debug] ? true : false
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#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.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cloudstack_client/client.rb', line 104

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
86
87
88
89
90
91
92
93
94
95
96
97
# 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)

  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
    puts "Error connecting to API:"
    puts "#{@api_url} is not reachable"
    exit 1
  end


  if response.is_a? Net::HTTPOK
    begin
      json = JSON.parse(response.body)
      json = json[params['command'].downcase + 'response']
    rescue JSON::ParserError
      puts "Error parsing response from server:"
      puts response.body
      exit 2
    end
  else
    begin
      json = JSON.parse(response.body)
      puts "Error executing command..."
      puts json if @debug
      json = json[params['command'].downcase + 'response']
      puts "#{json['errorcode']}: #{json['errortext']}"
    rescue
      puts "Error parsing response from server..."
      puts "#{response.code}: #{response.body}"
    end
    exit 3
  end
end