Class: CloudstackClient::Connection
- Inherits:
-
Object
- Object
- CloudstackClient::Connection
- Includes:
- Utils
- Defined in:
- lib/cloudstack_client/connection.rb
Direct Known Subclasses
Constant Summary collapse
- DEF_POLL_INTERVAL =
2.0- DEF_ASYNC_TIMEOUT =
400
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.
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.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cloudstack_client/connection.rb', line 18 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] || DEF_POLL_INTERVAL @async_timeout = [:async_timeout] || DEF_ASYNC_TIMEOUT @options = validate_input! 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cloudstack_client/connection.rb', line 83 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.
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 |
# File 'lib/cloudstack_client/connection.rb', line 34 def send_request(params) params['response'] = 'json' params['apiKey'] = @api_key print_debug_output JSON.pretty_generate(params) if @debug data = params_to_data(params) uri = URI.parse "#{@api_url}?#{data}&signature=#{create_signature(data)}" 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 = body['errortext'] rescue body raise ApiError, "Status #{response.code}: #{}." end end |