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 also caches responses for later reuse. If we would like to get fresh values from the service, #reset call will flush the cache, causing next access to retrieve fresh data.
Direct Known Subclasses
Defined Under Namespace
Classes: NoODataId
Instance Method Summary collapse
-
#[](attr) ⇒ Object
Access resource content.
-
#delete ⇒ Excon::Response
Issue a DELETE requests to the endpoint of the resource.
-
#initialize(connector, oid: nil, content: 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) ⇒ Excon::Response
Issue a PATCH requests to the selected endpoint.
-
#post(field: "@odata.id", path: nil, payload: nil) ⇒ Excon::Response
Issue a POST requests to the selected endpoint.
-
#raw ⇒ Hash
Access raw JSON data that resource wraps.
-
#reset ⇒ Object
Clear the cached sub-resources.
- #respond_to_missing?(symbol, include_private = false) ⇒ Boolean
-
#to_s ⇒ String
Pretty-print the wrapped content.
Constructor Details
#initialize(connector, oid: nil, content: 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.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/redfish_client/resource.rb', line 38 def initialize(connector, oid: nil, content: nil) if oid resp = connector.get(oid) @content = JSON.parse(resp.data[:body]) @content["@odata.id"] = oid @headers = resp.data[:headers] else @content = content end @cache = {} @connector = connector 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"]. The
only difference is that accessing non-existent field will raise
NoMethodError instead of KeyError as [] method does.
89 90 91 92 |
# File 'lib/redfish_client/resource.rb', line 89 def method_missing(symbol, *args, &block) name = symbol.to_s key?(name) ? self[name] : super 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.
In addition to accessing values associated with keys, this function can
also be used to access members of collection by directly indexing into
Members array. This means that res["Members"][3] can be shortened into
res[3].
Accessing non-existent or indexing non-collection resource key will
raise KeyError. Accessing invalid index will raise IndexError.
67 68 69 70 71 72 73 74 |
# File 'lib/redfish_client/resource.rb', line 67 def [](attr) if attr.is_a?(Integer) raise(KeyError, "Not a collection.") unless key?("Members") cache("Members").fetch(attr) else cache(attr) end end |
#delete ⇒ Excon::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.
164 165 166 |
# File 'lib/redfish_client/resource.rb', line 164 def delete @connector.delete(get_path("@odata.id", nil)) end |
#key?(name) ⇒ Boolean
Test if resource contains required key.
80 81 82 |
# File 'lib/redfish_client/resource.rb', line 80 def key?(name) @content.key?(name.to_s) end |
#patch(field: "@odata.id", path: nil, payload: nil) ⇒ Excon::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.
152 153 154 |
# File 'lib/redfish_client/resource.rb', line 152 def patch(field: "@odata.id", path: nil, payload: nil) @connector.patch(get_path(field, path), payload ? payload.to_json : "") end |
#post(field: "@odata.id", path: nil, payload: nil) ⇒ Excon::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.
138 139 140 |
# File 'lib/redfish_client/resource.rb', line 138 def post(field: "@odata.id", path: nil, payload: nil) @connector.post(get_path(field, path), payload ? payload.to_json : "") end |
#raw ⇒ Hash
Access raw JSON data that resource wraps.
107 108 109 |
# File 'lib/redfish_client/resource.rb', line 107 def raw @content end |
#reset ⇒ Object
Clear the cached sub-resources. Next sub-resource access will repopulate the cache.
100 101 102 |
# File 'lib/redfish_client/resource.rb', line 100 def reset @cache = {} end |
#respond_to_missing?(symbol, include_private = false) ⇒ Boolean
94 95 96 |
# File 'lib/redfish_client/resource.rb', line 94 def respond_to_missing?(symbol, include_private = false) key?(symbol.to_s) || super end |
#to_s ⇒ String
Pretty-print the wrapped content.
114 115 116 |
# File 'lib/redfish_client/resource.rb', line 114 def to_s JSON.pretty_generate(@content) end |