Module: OneviewSDK::Rest
- Included in:
- Client
- Defined in:
- lib/oneview-sdk/rest.rb
Overview
Contains all the methods for making API REST calls
Constant Summary collapse
- RESPONSE_CODE_OK =
200
- RESPONSE_CODE_CREATED =
201
- RESPONSE_CODE_ACCEPTED =
202
- RESPONSE_CODE_NO_CONTENT =
204
- RESPONSE_CODE_BAD_REQUEST =
400
- RESPONSE_CODE_UNAUTHORIZED =
401
- RESPONSE_CODE_NOT_FOUND =
404
Instance Method Summary collapse
-
#response_handler(response, wait_on_task = true) ⇒ Hash
Handle the response from a rest call.
-
#rest_api(type, path, options = {}, api_ver = @api_version) ⇒ NetHTTPResponse
Make a restful API request to OneView.
-
#rest_delete(path, options = {}, api_ver = @api_version) ⇒ Object
Make a restful DELETE request to OneView Parameters & return value align with those of the rest_api method above.
-
#rest_get(path, api_ver = @api_version) ⇒ Object
Make a restful GET request to OneView Parameters & return value align with those of the rest_api method above.
-
#rest_patch(path, options = {}, api_ver = @api_version) ⇒ Object
Make a restful PATCH request to OneView Parameters & return value align with those of the rest_api method above.
-
#rest_post(path, options = {}, api_ver = @api_version) ⇒ Object
Make a restful POST request to OneView Parameters & return value align with those of the rest_api method above.
-
#rest_put(path, options = {}, api_ver = @api_version) ⇒ Object
Make a restful PUT request to OneView Parameters & return value align with those of the rest_api method above.
Instance Method Details
#response_handler(response, wait_on_task = true) ⇒ Hash
Handle the response from a rest call.
If an asynchronous task was started, this waits for it to complete.
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 115 116 117 118 119 |
# File 'lib/oneview-sdk/rest.rb', line 90 def response_handler(response, wait_on_task = true) case response.code.to_i when RESPONSE_CODE_OK # Synchronous read/query begin return JSON.parse(response.body) rescue JSON::ParserError => e @logger.warn "Failed to parse JSON response. #{e}" return response.body end when RESPONSE_CODE_CREATED # Synchronous add return JSON.parse(response.body) when RESPONSE_CODE_ACCEPTED # Asynchronous add, update or delete return JSON.parse(response.body) unless wait_on_task @logger.debug "Waiting for task: response.header['location']" task = wait_for(response.header['location']) return true unless task['associatedResource'] && task['associatedResource']['resourceUri'] resource_data = rest_get(task['associatedResource']['resourceUri']) return JSON.parse(resource_data.body) when RESPONSE_CODE_NO_CONTENT # Synchronous delete return {} when RESPONSE_CODE_BAD_REQUEST fail "400 BAD REQUEST #{response.body}" when RESPONSE_CODE_UNAUTHORIZED fail "401 UNAUTHORIZED #{response.body}" when RESPONSE_CODE_NOT_FOUND fail "404 NOT FOUND #{response.body}" else fail "#{response.code} #{response.body}" end end |
#rest_api(type, path, options = {}, api_ver = @api_version) ⇒ NetHTTPResponse
Make a restful API request to OneView
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/oneview-sdk/rest.rb', line 19 def rest_api(type, path, = {}, api_ver = @api_version) @logger.debug "Making :#{type} rest call to #{@url}#{path}" fail 'Must specify path' unless path uri = URI.parse(URI.escape(@url + path)) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' if @ssl_enabled http.cert_store = @cert_store if @cert_store else http.verify_mode = OpenSSL::SSL::VERIFY_NONE end request = build_request(type, uri, , api_ver) response = http.request(request) @logger.debug " Response: Code=#{response.code}. Headers=#{response.to_hash}\n Body=#{response.body}" response rescue OpenSSL::SSL::SSLError => e msg = 'SSL verification failed for request. Please either:' msg += "\n 1. Install the certificate into your system's cert store" msg += ". Using cert store: #{ENV['SSL_CERT_FILE']}" if ENV['SSL_CERT_FILE'] msg += "\n 2. Run oneview-sdk-ruby cert import #{@url}" msg += "\n 3. Set the :ssl_enabled option to false for your client (NOT RECOMMENDED)" @logger.error msg raise e end |
#rest_delete(path, options = {}, api_ver = @api_version) ⇒ Object
Make a restful DELETE request to OneView Parameters & return value align with those of the rest_api method above
71 72 73 |
# File 'lib/oneview-sdk/rest.rb', line 71 def rest_delete(path, = {}, api_ver = @api_version) rest_api(:delete, path, , api_ver) end |
#rest_get(path, api_ver = @api_version) ⇒ Object
Make a restful GET request to OneView Parameters & return value align with those of the rest_api method above
47 48 49 |
# File 'lib/oneview-sdk/rest.rb', line 47 def rest_get(path, api_ver = @api_version) rest_api(:get, path, {}, api_ver) end |
#rest_patch(path, options = {}, api_ver = @api_version) ⇒ Object
Make a restful PATCH request to OneView Parameters & return value align with those of the rest_api method above
65 66 67 |
# File 'lib/oneview-sdk/rest.rb', line 65 def rest_patch(path, = {}, api_ver = @api_version) rest_api(:patch, path, , api_ver) end |
#rest_post(path, options = {}, api_ver = @api_version) ⇒ Object
Make a restful POST request to OneView Parameters & return value align with those of the rest_api method above
53 54 55 |
# File 'lib/oneview-sdk/rest.rb', line 53 def rest_post(path, = {}, api_ver = @api_version) rest_api(:post, path, , api_ver) end |
#rest_put(path, options = {}, api_ver = @api_version) ⇒ Object
Make a restful PUT request to OneView Parameters & return value align with those of the rest_api method above
59 60 61 |
# File 'lib/oneview-sdk/rest.rb', line 59 def rest_put(path, = {}, api_ver = @api_version) rest_api(:put, path, , api_ver) end |