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
- DEF_REQ_TIMEOUT =
60
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.
-
#host ⇒ Object
Returns the value of attribute host.
-
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#symbolize_keys ⇒ Object
Returns the value of attribute symbolize_keys.
-
#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
permalink #initialize(api_url, api_key, secret_key, options = {}) ⇒ Connection
Returns a new instance of Connection.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cloudstack_client/connection.rb', line 19 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 @symbolize_keys = [:symbolize_keys] ? true : false @host = [:host] @read_timeout = [:read_timeout] || DEF_REQ_TIMEOUT @async_poll_interval = [:async_poll_interval] || DEF_POLL_INTERVAL @async_timeout = [:async_timeout] || DEF_ASYNC_TIMEOUT @options = validate_input! end |
Instance Attribute Details
permalink #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 |
permalink #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 |
permalink #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 |
permalink #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 |
permalink #debug ⇒ Object
Returns the value of attribute debug.
12 13 14 |
# File 'lib/cloudstack_client/connection.rb', line 12 def debug @debug end |
permalink #host ⇒ Object
Returns the value of attribute host.
12 13 14 |
# File 'lib/cloudstack_client/connection.rb', line 12 def host @host end |
permalink #read_timeout ⇒ Object
Returns the value of attribute read_timeout.
12 13 14 |
# File 'lib/cloudstack_client/connection.rb', line 12 def read_timeout @read_timeout end |
permalink #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 |
permalink #symbolize_keys ⇒ Object
Returns the value of attribute symbolize_keys.
12 13 14 |
# File 'lib/cloudstack_client/connection.rb', line 12 def symbolize_keys @symbolize_keys end |
permalink #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
permalink #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.
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 |
# File 'lib/cloudstack_client/connection.rb', line 90 def send_async_request(params) data = send_request(params) params = { 'command' => 'queryAsyncJobResult', 'jobid' => data[k('jobid')] } max_tries.times do data = send_request(params) print "." if @verbose case data[k('jobstatus')] when 1 return data[k('jobresult')] when 2 raise JobError, "Request failed (#{data[k('jobresultcode')]}): #{data[k('jobresult')][k('errortext')]}." end STDOUT.flush if @verbose sleep @async_poll_interval end raise TimeoutError, "Asynchronous request timed out." end |
permalink #send_request(params) ⇒ Object
Sends a synchronous request to the CloudStack API and returns the response as a Hash.
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 |
# File 'lib/cloudstack_client/connection.rb', line 38 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 http.read_timeout = @read_timeout begin req = Net::HTTP::Get.new(uri.request_uri) req['Host'] = host if host.present? response = http.request(req) rescue raise ConnectionError, "API URL \'#{@api_url}\' is not reachable." end begin body = JSON.parse(response.body, symbolize_names: @symbolize_keys).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?(k('count')) return body.reject { |key, _| key == k('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 == k('count') } if body.key?(k('count')) body.size == 0 ? [] : body end else = body[k('errortext')] rescue body raise ApiError, "Status #{response.code}: #{}." end end |