Class: RedfishClient::Resource
- Inherits:
-
Object
- Object
- RedfishClient::Resource
- Defined in:
- lib/redfish_client/resource.rb
Overview
Resource is basic building block of Redfish client and serves as a container for the data that is retrieved from the Redfish service.
When we interact with the Redfish service, resource will wrap the data retrieved from the service API and offer us dot-notation accessors for values stored.
Resource will also load any sub-resource on demand when we access it.
For example, if we have a root Redfish resource stored in root
,
accessing root.SessionService
will automatically fetch the appropriate
resource from the API.
In order to reduce the amount of requests being sent to the service, resource can also utilise caching connector. If we would like to get fresh values from the service, #refresh call will flush the cache and retrieve fresh data from the remote.
Direct Known Subclasses
Defined Under Namespace
Classes: NoODataId, NoResource, Timeout
Instance Attribute Summary collapse
-
#headers ⇒ Hash
readonly
Headers, returned from the service when resource has been constructed.
-
#raw ⇒ Hash
readonly
Raw data that has been used to construct resource by either fetching it from the remote API or by being passed-in as a parameter to constructor.
Instance Method Summary collapse
-
#[](attr) ⇒ Object
Access resource content.
-
#delete(field: "@odata.id", path: nil, payload: nil) ⇒ RedfishClient::Response
Issue a DELETE requests to the endpoint of the resource.
-
#dig(*keys) ⇒ Object
Safely access nested resource content.
-
#get(field: "@odata.id", path: nil) ⇒ RedfishClient::Response
Issue a GET requests to the selected endpoint.
-
#initialize(connector, oid: nil, raw: nil) ⇒ Resource
constructor
Create new resource.
-
#key?(name) ⇒ Boolean
Test if resource contains required key.
-
#method_missing(symbol, *_args, &_block) ⇒ Object
Convenience access for resource data.
-
#patch(field: "@odata.id", path: nil, payload: nil) ⇒ RedfishClient::Response
Issue a PATCH requests to the selected endpoint.
-
#post(field: "@odata.id", path: nil, payload: nil) ⇒ RedfishClient::Response
Issue a POST requests to the selected endpoint.
-
#refresh ⇒ Object
Refresh resource content from the API.
-
#request(method, field, path, payload = nil) ⇒ RedfishClient::Response
Issue a requests to the selected endpoint.
- #respond_to_missing?(symbol, include_private = false) ⇒ Boolean
-
#to_s ⇒ String
Pretty-print the wrapped content.
-
#wait(response, retries: 10, delay: 1) ⇒ RedfishClient::Response
Wait for the potentially async operation to terminate.
Constructor Details
#initialize(connector, oid: nil, raw: nil) ⇒ Resource
Create new resource.
Resource can be created either by passing in OpenData identifier or supplying the content (hash). In the first case, connector will be used to fetch the resource data. In the second case, resource only wraps the passed-in hash and does no fetching.
57 58 59 60 61 62 63 64 |
# File 'lib/redfish_client/resource.rb', line 57 def initialize(connector, oid: nil, raw: nil) @connector = connector if oid initialize_from_service(oid) else @raw = raw end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *_args, &_block) ⇒ Object
Convenience access for resource data.
Calling resource.Value
is exactly the same as resource["Value"]
.
122 123 124 |
# File 'lib/redfish_client/resource.rb', line 122 def method_missing(symbol, *_args, &_block) self[symbol.to_s] end |
Instance Attribute Details
#headers ⇒ Hash (readonly)
Headers, returned from the service when resource has been constructed.
37 38 39 |
# File 'lib/redfish_client/resource.rb', line 37 def headers @headers end |
#raw ⇒ Hash (readonly)
Raw data that has been used to construct resource by either fetching it from the remote API or by being passed-in as a parameter to constructor.
43 44 45 |
# File 'lib/redfish_client/resource.rb', line 43 def raw @raw end |
Instance Method Details
#[](attr) ⇒ Object
Access resource content.
This function offers a way of accessing resource data in the same way that hash exposes its content.
94 95 96 |
# File 'lib/redfish_client/resource.rb', line 94 def [](attr) build_resource(raw[attr]) end |
#delete(field: "@odata.id", path: nil, payload: nil) ⇒ RedfishClient::Response
Issue a DELETE requests to the endpoint of the resource.
If the resource has no @odata.id
field, NoODataId error will be
raised, since deleting non-networked resources makes no sense and
probably indicates bug in library consumer.
224 225 226 |
# File 'lib/redfish_client/resource.rb', line 224 def delete(field: "@odata.id", path: nil, payload: nil) request(:delete, field, path, payload) end |
#dig(*keys) ⇒ Object
Safely access nested resource content.
This function is an equivalent of safe navigation operator that can be used with arbitrary keys.
Calling res.dig("a", "b", "c")
is equivalent to res.a&.b&.c
and
res["a"] && res["a"]["b"] && res["a"]["b"]["c"]
.
107 108 109 |
# File 'lib/redfish_client/resource.rb', line 107 def dig(*keys) keys.reduce(self) { |a, k| a.nil? ? nil : a[k] } end |
#get(field: "@odata.id", path: nil) ⇒ RedfishClient::Response
Issue a GET requests to the selected endpoint.
By default, GET request will be sent to the path, stored in @odata.id
field. Source field can be changed by specifying the field
parameter
when calling this function. Specifying the path
argument will bypass
the field lookup altogether and issue a GET request directly to the
selected path.
If the resource has no lookup field, NoODataId error will be raised, since posting to non-networked resources makes no sense and probably indicates bug in library consumer.
174 175 176 |
# File 'lib/redfish_client/resource.rb', line 174 def get(field: "@odata.id", path: nil) request(:get, field, path) end |
#key?(name) ⇒ Boolean
Test if resource contains required key.
115 116 117 |
# File 'lib/redfish_client/resource.rb', line 115 def key?(name) raw.key?(name.to_s) end |
#patch(field: "@odata.id", path: nil, payload: nil) ⇒ RedfishClient::Response
Issue a PATCH requests to the selected endpoint.
Works exactly the same as the #post method, but issued a PATCH request to the server.
212 213 214 |
# File 'lib/redfish_client/resource.rb', line 212 def patch(field: "@odata.id", path: nil, payload: nil) request(:patch, field, path, payload) end |
#post(field: "@odata.id", path: nil, payload: nil) ⇒ RedfishClient::Response
Issue a POST requests to the selected endpoint.
By default, POST request will be sent to the path, stored in @odata.id
field. Source field can be changed by specifying the field
parameter
when calling this function. Specifying the path
argument will bypass
the field lookup altogether and POST directly to the requested path.
In order to avoid having to manually serialize data to JSON, this function call takes Hash as a payload and encodes it before sending it to the endpoint.
If the resource has no lookup field, NoODataId error will be raised, since posting to non-networked resources makes no sense and probably indicates bug in library consumer.
198 199 200 |
# File 'lib/redfish_client/resource.rb', line 198 def post(field: "@odata.id", path: nil, payload: nil) request(:post, field, path, payload) end |
#refresh ⇒ Object
Refresh resource content from the API
Caling this method will ensure that the resource data is in sync with the Redfis API, invalidating any caches as necessary.
232 233 234 235 236 237 238 239 |
# File 'lib/redfish_client/resource.rb', line 232 def refresh return unless self["@odata.id"] # TODO(@tadeboro): raise more sensible exception if resource cannot be # refreshed. @connector.reset(self["@odata.id"]) initialize_from_service(self["@odata.id"]) end |
#request(method, field, path, payload = nil) ⇒ RedfishClient::Response
Issue a requests to the selected endpoint.
By default, request will be sent to the path, stored in @odata.id
field. Source field can be changed by specifying the field
parameter
when calling this function. Specifying the path
argument will bypass
the field lookup altogether and issue a request directly to the selected
path.
If the resource has no lookup field, NoODataId error will be raised, since posting to non-networked resources makes no sense and probably indicates bug in library consumer.
154 155 156 |
# File 'lib/redfish_client/resource.rb', line 154 def request(method, field, path, payload = nil) @connector.request(method, get_path(field, path), payload) end |
#respond_to_missing?(symbol, include_private = false) ⇒ Boolean
126 127 128 |
# File 'lib/redfish_client/resource.rb', line 126 def respond_to_missing?(symbol, include_private = false) key?(symbol.to_s) || super end |
#to_s ⇒ String
Pretty-print the wrapped content.
133 134 135 |
# File 'lib/redfish_client/resource.rb', line 133 def to_s JSON.pretty_generate(raw) end |
#wait(response, retries: 10, delay: 1) ⇒ RedfishClient::Response
Wait for the potentially async operation to terminate
Note that this can be safely called on response from non-async operations where the function will return immediately and without making any additional requests to the service.
77 78 79 80 81 82 83 84 85 |
# File 'lib/redfish_client/resource.rb', line 77 def wait(response, retries: 10, delay: 1) retries.times do |_i| return response if response.done? sleep(delay) response = get(path: response.monitor) end raise Timeout, "Async operation did not terminate in allotted time" end |