redfish_client 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e862a2537bbbd1e6e613e82de07b10ca25dcd1f359642197fc6265b8e2f69bd5
4
- data.tar.gz: 08de46ba2381857fea6e789ff46aee373bb87e014db1cdad70015c4a0ab15d99
3
+ metadata.gz: 45295e03b5e8ec0dda4fd0d7401f95516c92d48eba415337d25d3d2ef210a869
4
+ data.tar.gz: bfe94afbb669fdd6bb4b407b8ddacfaf17695b49f4bdd2d83645290f97f7b5b6
5
5
  SHA512:
6
- metadata.gz: dbaceb5601e1bc015570ee83257379fe442d2dd949d3c7158818d2165a5b28b34392fd669b94419c4ece0fd2a486fb511384cbd1cd1ed69b6f34adda419715f1
7
- data.tar.gz: 58965e7242bcd7805ab003aedaf6e2ea81146a7a1a4d1e37812b23aa7fd8f5266da899db8471a2e3c3e29c9867511037310cf70f50333196fbd44be757947d27
6
+ metadata.gz: c99e489c2328e9a1fb3ada71d3c62f0a3ce9d75e93bf78206e56490a4153d60a20601bc5d7fee351485ede848a6c640310f7df3b29c3c73c138ac4b466f62b3d
7
+ data.tar.gz: 20b278901afbfaa467ec1bd72056f6fbf7420d6ab62f9352193023a46a9e44b7712a37ebb495db1016d6b77a8ca1a7eb729c06ab56c7e8061150e894b64a22f2
@@ -1,12 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "redfish_client/caching_connector"
3
4
  require "redfish_client/connector"
4
5
  require "redfish_client/root"
5
6
  require "redfish_client/version"
6
7
 
7
8
  module RedfishClient
8
- def self.new(url, prefix: "/redfish/v1", verify: true)
9
- con = Connector.new(url, verify)
9
+ # Create new Redfish API client.
10
+ #
11
+ # @param url [String] base URL of Redfish API
12
+ # @param prefix [String] Redfish API prefix
13
+ # @param verify [Boolean] verify certificates for https connections
14
+ # @param use_cache [Boolean] cache API responses
15
+ def self.new(url, prefix: "/redfish/v1", verify: true, use_cache: true)
16
+ con = if use_cache
17
+ CachingConnector.new(url, verify)
18
+ else
19
+ Connector.new(url, verify)
20
+ end
10
21
  Root.new(con, oid: prefix)
11
22
  end
12
23
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "redfish_client/connector"
4
+
5
+ module RedfishClient
6
+ # Variant of {RedfishClient::Connector} that caches GET responses.
7
+ class CachingConnector < Connector
8
+ # Create new caching connector.
9
+ #
10
+ # @param url [String] base url of the Redfish service
11
+ # @param verify [Boolean] verify SSL certificate of the service
12
+ def initialize(url, verify = true)
13
+ super
14
+ @cache = {}
15
+ end
16
+
17
+ # Issue GET request to service.
18
+ #
19
+ # Request is only issued if there is no cache entry for the existing path.
20
+ #
21
+ # @param path [String] path to the resource, relative to the base url
22
+ # @return [Excon::Response] response object
23
+ def get(path)
24
+ @cache[path] ||= super
25
+ end
26
+
27
+ # Clear the cached responses.
28
+ #
29
+ # Next GET request will repopulate the cache.
30
+ def reset(path: nil)
31
+ if path.nil?
32
+ @cache = {}
33
+ else
34
+ @cache.delete(path)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -16,7 +16,7 @@ module RedfishClient
16
16
  # resource from the API.
17
17
  #
18
18
  # In order to reduce the amount of requests being sent to the service,
19
- # resource also caches responses for later reuse. If we would like to get
19
+ # resource can also utilise caching connector. If we would like to get
20
20
  # fresh values from the service, {#reset} call will flush the cache,
21
21
  # causing next access to retrieve fresh data.
22
22
  class Resource
@@ -44,7 +44,6 @@ module RedfishClient
44
44
  # @param content [Hash] content to populate resource with
45
45
  # @raise [NoResource] resource cannot be retrieved from the service
46
46
  def initialize(connector, oid: nil, content: nil)
47
- @cache = {}
48
47
  @connector = connector
49
48
 
50
49
  if oid
@@ -59,22 +58,10 @@ module RedfishClient
59
58
  # This function offers a way of accessing resource data in the same way
60
59
  # that hash exposes its content.
61
60
  #
62
- # In addition to accessing values associated with keys, this function can
63
- # also be used to access members of collection by directly indexing into
64
- # Members array. This means that `res["Members"][3]` can be shortened into
65
- # `res[3]`.
66
- #
67
- # Indexing non-collection resource key will # raise `KeyError`.
68
- #
69
- # @param attr [String, Integer] key or index for accessing data
61
+ # @param attr [String] key for accessing data
70
62
  # @return associated value or `nil` if attr is missing
71
63
  def [](attr)
72
- if attr.is_a?(Integer)
73
- raise(KeyError, "Not a collection.") unless key?("Members")
74
- cache("Members")[attr]
75
- else
76
- cache(attr)
77
- end
64
+ build_resource(@content[attr])
78
65
  end
79
66
 
80
67
  # Safely access nested resource content.
@@ -109,10 +96,11 @@ module RedfishClient
109
96
  key?(symbol.to_s) || super
110
97
  end
111
98
 
112
- # Clear the cached sub-resources. Next sub-resource access will repopulate
113
- # the cache.
99
+ # Clear the cached sub-resources.
100
+ #
101
+ # This method is a no-op if connector in use does not support caching.
114
102
  def reset
115
- @cache = {}
103
+ @connector.reset if @connector.respond_to?(:reset)
116
104
  end
117
105
 
118
106
  # Access raw JSON data that resource wraps.
@@ -195,10 +183,6 @@ module RedfishClient
195
183
  path || @content[field]
196
184
  end
197
185
 
198
- def cache(name)
199
- @cache[name] ||= build_resource(@content[name])
200
- end
201
-
202
186
  def build_resource(data)
203
187
  return nil if data.nil?
204
188
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RedfishClient
4
- VERSION = "0.2.4"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redfish_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tadej Borovšak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-31 00:00:00.000000000 Z
11
+ date: 2018-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon
@@ -142,6 +142,7 @@ files:
142
142
  - bin/console
143
143
  - bin/setup
144
144
  - lib/redfish_client.rb
145
+ - lib/redfish_client/caching_connector.rb
145
146
  - lib/redfish_client/connector.rb
146
147
  - lib/redfish_client/resource.rb
147
148
  - lib/redfish_client/root.rb