Module: ILO_SDK::Rest

Included in:
Client
Defined in:
lib/ilo-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

Instance Method Details

#response_handler(response) ⇒ Hash

Handle the response for rest call.

If an asynchronous task was started, this waits for it to complete.

Parameters:

  • HTTP (HTTPResponse)

    response

Returns:

  • (Hash)

    The parsed JSON body

Raises:

  • (RuntimeError)

    if the request failed

  • (RuntimeError)

    if a task was returned that did not complete successfully



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
120
121
122
123
124
# File 'lib/ilo-sdk/rest.rb', line 94

def response_handler(response)
  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) # TODO: Remove when tested
    # TODO: Make this actually wait for the 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
    raise "400 BAD REQUEST #{response.body}"
  when RESPONSE_CODE_UNAUTHORIZED
    raise "401 UNAUTHORIZED #{response.body}"
  when RESPONSE_CODE_NOT_FOUND
    raise "404 NOT FOUND #{response.body}"
  else
    raise "#{response.code} #{response.body}"
  end
end

#rest_api(type, path, options = {}) ⇒ NetHTTPResponse

Make a restful API request to the iLO

Parameters:

  • type (Symbol)

    the rest method/type Options are :get, :post, :put, :patch, and :delete

  • path (String)

    the path for the request. Usually starts with “/rest/”

  • options (Hash) (defaults to: {})

    the options for the request

Options Hash (options):

  • :body (String)

    Hash to be converted into json and set as the request body

  • :Content-Type (String) — default: 'application/json'

    Set to nil or :none to have this option removed

Returns:

  • (NetHTTPResponse)

    The response object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ilo-sdk/rest.rb', line 27

def rest_api(type, path, options = {})
  raise 'Must specify path' unless path
  raise 'Must specify type' unless type
  @logger.debug "Making :#{type} rest call to #{@host}#{path}"

  uri = URI.parse(URI.escape("#{@host}#{path}"))
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @ssl_enabled

  request = build_request(type, uri, options)
  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 cert store"
  msg += ". Using cert store: #{ENV['SSL_CERT_FILE']}" if ENV['SSL_CERT_FILE']
  msg += "\n  2. Set the :ssl_enabled option to false for your ilo client"
  @logger.error msg
  raise e
end

#rest_delete(path, options = {}) ⇒ Object

Make a restful DELETE request Parameters & return value align with those of the rest_api method above



76
77
78
# File 'lib/ilo-sdk/rest.rb', line 76

def rest_delete(path, options = {})
  rest_api(:delete, path, options)
end

#rest_get(path) ⇒ Object

Make a restful GET request Parameters & return value align with those of the rest_api method above



52
53
54
# File 'lib/ilo-sdk/rest.rb', line 52

def rest_get(path)
  rest_api(:get, path, {})
end

#rest_patch(path, options = {}) ⇒ Object

Make a restful PATCH request Parameters & return value align with those of the rest_api method above



70
71
72
# File 'lib/ilo-sdk/rest.rb', line 70

def rest_patch(path, options = {})
  rest_api(:patch, path, options)
end

#rest_post(path, options = {}) ⇒ Object

Make a restful POST request Parameters & return value align with those of the rest_api method above



58
59
60
# File 'lib/ilo-sdk/rest.rb', line 58

def rest_post(path, options = {})
  rest_api(:post, path, options)
end

#rest_put(path, options = {}) ⇒ Object

Make a restful PUT request Parameters & return value align with those of the rest_api method above



64
65
66
# File 'lib/ilo-sdk/rest.rb', line 64

def rest_put(path, options = {})
  rest_api(:put, path, options)
end