Class: CloudstackClient::Connection
- Inherits:
-
Object
- Object
- CloudstackClient::Connection
- Defined in:
- lib/cloudstack_client/connection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#async_poll_interval ⇒ Object
Returns the value of attribute async_poll_interval.
-
#async_timeout ⇒ Object
Returns the value of attribute async_timeout.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#initialize(api_url, api_key, secret_key, options = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#send_async_request(params) ⇒ Object
Sends an asynchronous request and waits for the response.
-
#send_request(params) ⇒ Object
Sends a synchronous request to the CloudStack API and returns the response as a Hash.
Constructor Details
#initialize(api_url, api_key, secret_key, options = {}) ⇒ Connection
Returns a new instance of Connection.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cloudstack_client/connection.rb', line 15 def initialize(api_url, api_key, secret_key, = {}) @api_url = api_url @api_key = api_key @secret_key = secret_key @verbose = [:quiet] ? false : true @debug = [:debug] ? true : false @async_poll_interval = [:async_poll_interval] || 2.0 @async_timeout = [:async_timeout] || 400 = end |
Instance Attribute Details
#api_key ⇒ Object
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_url ⇒ Object
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_interval ⇒ Object
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_timeout ⇒ Object
Returns the value of attribute async_timeout.
13 14 15 |
# File 'lib/cloudstack_client/connection.rb', line 13 def async_timeout @async_timeout end |
#debug ⇒ Object
Returns the value of attribute debug.
12 13 14 |
# File 'lib/cloudstack_client/connection.rb', line 12 def debug @debug end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
12 13 14 |
# File 'lib/cloudstack_client/connection.rb', line 12 def secret_key @secret_key end |
#verbose ⇒ Object
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.
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/connection.rb', line 91 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']}." 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.
30 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 |
# File 'lib/cloudstack_client/connection.rb', line 30 def send_request(params) params['response'] = 'json' params['apiKey'] = @api_key params_arr = params.sort.map do |key, value| value = CGI.escape(value.to_s).gsub('+', '%20').gsub(' ','%20') "#{key}=#{value}" end 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 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 correct 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 : [] else body.reject! { |key, _| key == 'count' } if body.key?('count') body.size == 0 ? [] : body end else = body['errortext'] rescue body raise ApiError, "Status #{response.code}: #{message}." end end |