Class: RedfishClient::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/redfish_client/connector.rb

Overview

Connector serves as a low-level wrapper around HTTP calls that are used to retrieve data from the service API. It abstracts away implementation details such as sending the proper headers in request, which do not change between resource fetches.

Library users should treat this class as an implementation detail and use higer-level Resource instead.

Defined Under Namespace

Classes: AuthError

Constant Summary collapse

DEFAULT_HEADERS =

Default headers, as required by Redfish spec https://redfish.dmtf.org/schemas/DSP0266_1.4.0.html#request-headers

{
  "Accept" => "application/json",
  "OData-Version" => "4.0",
}.freeze
BASIC_AUTH_HEADER =

Basic and token authentication header names

"Authorization"
TOKEN_AUTH_HEADER =
"X-Auth-Token"
LOCATION_HEADER =
"Location"

Instance Method Summary collapse

Constructor Details

#initialize(url, verify: true, cache: nil, use_session: true) ⇒ Connector

Create new connector.

By default, connector performs no caching. If caching is desired, Hash should be used as a cache implementation.

It is also possible to pass in custom caching class. Instances of that class should respond to the following four methods:

  1. [](key) - Used to access cached content and should return nil if the key has no associated value.
  2. []=(key, value) - Cache value under the key
  3. clear - Clear the complete cache.
  4. delete(key) - Invalidate cache entry associated with key.

Parameters:

  • url (String)

    base url of the Redfish service

  • verify (Boolean) (defaults to: true)

    verify SSL certificate of the service

  • use_session (Boolean) (defaults to: true)

    Use a session for authentication

  • cache (Object) (defaults to: nil)

    cache backend



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/redfish_client/connector.rb', line 52

def initialize(url, verify: true, cache: nil, use_session: true)
  @url = url
  @headers = DEFAULT_HEADERS.dup
  middlewares = Excon.defaults[:middlewares] +
    [Excon::Middleware::RedirectFollower]
  @connection = Excon.new(@url,
                          ssl_verify_peer: verify,
                          middlewares: middlewares)
  @cache = cache || NilHash.new
  @use_session = use_session
end

Instance Method Details

#add_headers(headers) ⇒ Object

Add HTTP headers to the requests made by the connector.

Parameters:

  • headers (Hash<String, String>)

    headers to be added



67
68
69
# File 'lib/redfish_client/connector.rb', line 67

def add_headers(headers)
  @headers.merge!(headers)
end

#delete(path) ⇒ Response

Issue DELETE requests to the service.

Parameters:

  • path (String)

    path to the resource, relative to the base

Returns:



129
130
131
# File 'lib/redfish_client/connector.rb', line 129

def delete(path)
  request(:delete, path)
end

#get(path) ⇒ Response

Issue GET request to service.

This method will first try to return cached response if available. If cache does not contain entry for this request, data will be fetched from remote and then cached, but only if the response has an OK (200) status.

Parameters:

  • path (String)

    path to the resource, relative to the base url

Returns:



103
104
105
# File 'lib/redfish_client/connector.rb', line 103

def get(path)
  request(:get, path)
end

#loginObject

Authenticate against the service.

Calling this method will try to authenticate against API using credentials provided by ##set_auth_info call. If authentication fails, # AuthError will be raised.

Raises:



172
173
174
# File 'lib/redfish_client/connector.rb', line 172

def 
  @session_path ?  : 
end

#logoutObject

Sign out of the service.



177
178
179
180
181
182
183
184
185
186
# File 'lib/redfish_client/connector.rb', line 177

def logout
  # We bypass request here because we do not want any retries on 401
  # when doing logout.
  if @session_oid
    params = prepare_request_params(:delete, @session_oid)
    @connection.request(params)
    @session_oid = nil
  end
  remove_headers([BASIC_AUTH_HEADER, TOKEN_AUTH_HEADER])
end

#patch(path, data = nil) ⇒ Response

Issue PATCH requests to the service.

Parameters:

  • path (String)

    path to the resource, relative to the base

  • data (Hash) (defaults to: nil)

    data to be sent over the socket

Returns:



121
122
123
# File 'lib/redfish_client/connector.rb', line 121

def patch(path, data = nil)
  request(:patch, path, data)
end

#post(path, data = nil) ⇒ Response

Issue POST requests to the service.

Parameters:

  • path (String)

    path to the resource, relative to the base

  • data (Hash) (defaults to: nil)

    data to be sent over the socket, JSON encoded

Returns:



112
113
114
# File 'lib/redfish_client/connector.rb', line 112

def post(path, data = nil)
  request(:post, path, data)
end

#remove_headers(headers) ⇒ Object

Remove HTTP headers from requests made by the connector.

Headers that are not currently set are silently ignored and no error is raised.

Parameters:

  • headers (List<String>)

    headers to remove



77
78
79
# File 'lib/redfish_client/connector.rb', line 77

def remove_headers(headers)
  headers.each { |h| @headers.delete(h) }
end

#request(method, path, data = nil) ⇒ Response

Issue requests to the service.

Parameters:

  • mathod (Symbol)

    HTTP method (:get, :post, :patch or :delete)

  • path (String)

    path to the resource, relative to the base

  • data (Hash) (defaults to: nil)

    data to be sent over the socket

Returns:



87
88
89
90
91
92
93
# File 'lib/redfish_client/connector.rb', line 87

def request(method, path, data = nil)
  return @cache[path] if method == :get && @cache[path]

  do_request(method, path, data).tap do |r|
    @cache[path] = r if method == :get && r.status == 200
  end
end

#reset(path = nil) ⇒ Object

Clear the cached responses.

If path is passed as a parameter, only one cache entry gets invalidated, else complete cache gets invalidated.

Next GET request will repopulate the cache.

Parameters:

  • path (String) (defaults to: nil)

    path to invalidate



141
142
143
# File 'lib/redfish_client/connector.rb', line 141

def reset(path = nil)
  path.nil? ? @cache.clear : @cache.delete(path)
end

#set_auth_info(username, password, auth_test_path, session_path = nil) ⇒ Object

Set authentication-related variables.

Last parameter controls the kind of login connector will perform. If session_path is nil, basic authentication will be used, otherwise connector will use session-based authentication.

Note that actual login is done lazily. If you need to check for credential validity, call ##login method.

Parameters:

  • username (String)

    API username

  • password (String)

    API password

  • auth_test_path (String)

    API path to test credential's validity

  • session_path (String, nil) (defaults to: nil)

    API session path



158
159
160
161
162
163
# File 'lib/redfish_client/connector.rb', line 158

def set_auth_info(username, password, auth_test_path, session_path = nil)
  @username = username
  @password = password
  @auth_test_path = auth_test_path
  @session_path = @use_session ? session_path : nil
end