train-rest 0.2.1 → 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: 59770f2304519af69df3caaac4f4ef19f964a0a4700952ff6a3b96834867bf13
4
- data.tar.gz: 39546783f42569561a1f740c094d0f2adb1a31c49c710b5c9736f172a961306f
3
+ metadata.gz: 807facd5f597c5fd42ed7044f360601931d8f2ca60cfd79b41969c777e0f631a
4
+ data.tar.gz: 87a040c1f16c24f1efe30f7252e02f2100717b4cb241e4cce974a61f9dba095d
5
5
  SHA512:
6
- metadata.gz: c8b2b55eb1723538a552ecbc7ce452a3545bd8ae3e35a82c263f3bbc16d6bfed161d053a6a711b0e34723c4f332e6d7797a40035d6142fc9957618a10ad6dac6
7
- data.tar.gz: 967e71707994db3086ea1f71e5d987324c405930ecb4473b9ad886ca59b32d15efb2e4c42f4f4075a607d642a11d78d4fe5e4fe6946468eb348735dd24bde514
6
+ metadata.gz: d08b4e78394e02f51701ac0927de49e541b38589508d8f28e4327579a296fb074049c706d56d302dd1cf1b03d930727f1b12720303bdceb986f06fb145e27888
7
+ data.tar.gz: e518d02e65d85cc826c29f19619250246038e8ed329b72a6e3e9825e8078f594bdc7cb1f971d632574b6f766b899d0aee81b01bd5e6dc074444f021b6f858c54
data/README.md CHANGED
@@ -20,7 +20,7 @@ rake install:local
20
20
  | Option | Explanation | Default |
21
21
  | -------------------- | --------------------------------------- | ----------- |
22
22
  | `endpoint` | Endpoint of the REST API | _required_ |
23
- | `validate_ssl` | Check certificate and chain | true |
23
+ | `verify_ssl` | Check certificate and chain | true |
24
24
  | `auth_type` | Authentication type | `anonymous` |
25
25
  | `debug_rest` | Enable debugging of HTTP traffic | false |
26
26
  | `logger` | Alternative logging class | |
@@ -60,6 +60,9 @@ The Redfish standard is defined in <http://www.dmtf.org/standards/redfish> and
60
60
  this handler does initial login, reuses the received session and logs out when
61
61
  closing the transport cleanly.
62
62
 
63
+ Known vendors which implement RedFish based management for their systems include
64
+ HPE, Dell, IBM, SuperMicro, Lenovo, Huawei and others.
65
+
63
66
  ## Debugging and use in Chef
64
67
 
65
68
  You can activate debugging by setting the `debug_rest` flag to `true'. Please
@@ -77,6 +80,46 @@ train = Train.create('rest', {
77
80
  })
78
81
  ```
79
82
 
83
+ ## Request Methods
84
+
85
+ This transport does not implement the `run_command` method, as there is no
86
+ line-based protocol to execute commands against. Instead, it implements its own
87
+ custom methods which suit REST interfaces. Trying to call this method will
88
+ throw an Exception.
89
+
90
+ ### Generic Request
91
+
92
+ The `request` methods allows to send free-form requests against any defined or
93
+ custom methods.
94
+
95
+ `request(path, method = :get, request_parameters: {}, data: nil, headers: {},
96
+ json_processing: true)`
97
+
98
+ - `path`: The path to request, which will be appended to the `endpoint`
99
+ - `method`: The HTTP method in Ruby Symbol syntax
100
+ - `request_parameters`: A hash of parameters to the `rest-client` request
101
+ method for additional settings
102
+ - `data`: Data for actions like `:post` or `:put`. Not all methods accept
103
+ a data body.
104
+ - `headers`: Additional headers for the request
105
+ - `json_processing`: If the response is a JSON and you want to receive a
106
+ processed Hash/Array instead of text
107
+
108
+ For `request_parameters` and `headers`, there is data mixed in to add
109
+ authenticator responses, JSON processing etc. Please check the
110
+ implementation in `connection.rb` for details.
111
+
112
+ ### Convenience Methods
113
+
114
+ Simplified wrappers are generated for the most common request types:
115
+
116
+ - `delete(path, request_parameters: {}, headers: {}, json_processing: true)`
117
+ - `head(path, request_parameters: {}, headers: {}, json_processing: true)`
118
+ - `get(path, request_parameters: {}, headers: {}, json_processing: true)`
119
+ - `post(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
120
+ - `put(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
121
+ - `patch(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
122
+
80
123
  ## Example use
81
124
 
82
125
  ```ruby
@@ -133,7 +176,7 @@ require 'train-rest'
133
176
  # This will immediately do a login and add headers
134
177
  train = Train.create('rest', {
135
178
  endpoint: 'https://10.20.30.40',
136
- validate_ssl: false,
179
+ verify_ssl: false,
137
180
 
138
181
  auth_type: :redfish,
139
182
  username: 'iloadmin',
@@ -176,5 +219,28 @@ conn.close
176
219
  1. Run against the defiend targets via Chef Target Mode:
177
220
 
178
221
  ```shell
179
- chef-client --local-mode --target 10.0.0.1 --runlist 'recipe[my-cookbook:setup]'
222
+ chef-client --local-mode --target 10.0.0.1 --runlist 'recipe[my-cookbook::setup]'
180
223
  ```
224
+
225
+ ## Use with Prerecorded API responses
226
+
227
+ For testing during and after development, not all APIs can be used to verify your solution against.
228
+ The VCR gem offers the possibility to hook into web requests and intercept them to play back canned
229
+ responses.
230
+
231
+ Please read the documentation of the VCR gem on how to record your API and the concepts like
232
+ "cassettes", "libraries" and matchers.
233
+
234
+ The following options are available in train-rest for this:
235
+
236
+ | Option | Explanation | Default |
237
+ | -------------------- | --------------------------------------- | ------------ |
238
+ | `vcr_cassette` | Name of the response file | nil |
239
+ | `vcr_library` | Directory to search responses in | `vcr` |
240
+ | `vcr_match_on` | Elements to match request by | `method uri` |
241
+ | `vcr_record` | Recording mode | `none` |
242
+ | `vcr_hook_into` | Base library for intercepting | `webmock` |
243
+
244
+ VCR will only be required as a Gem and activated, if you supply a cassette name.
245
+
246
+ You can use all these settings in your Chef Target Mode `credentials` file as well.
@@ -19,9 +19,35 @@ module TrainPlugins
19
19
  # Accept string (CLI) and boolean (API) options
20
20
  options[:verify_ssl] = options[:verify_ssl].to_s == "false" ? false : true
21
21
 
22
+ setup_vcr
23
+
22
24
  connect
23
25
  end
24
26
 
27
+ def setup_vcr
28
+ return unless options[:vcr_cassette]
29
+
30
+ require "vcr"
31
+
32
+ # TODO: Starts from "/" :(
33
+ library = options[:vcr_library]
34
+ match_on = options[:vcr_match_on].split.map(&:to_sym)
35
+
36
+ VCR.configure do |config|
37
+ config.cassette_library_dir = library
38
+ config.hook_into options[:vcr_hook_into]
39
+ config.default_cassette_options = {
40
+ record: options[:vcr_record].to_sym,
41
+ match_requests_on: match_on,
42
+ }
43
+ end
44
+
45
+ VCR.insert_cassette options[:vcr_cassette]
46
+ rescue LoadError
47
+ logger.fatal "Install the vcr gem to use HTTP(S) playback capability"
48
+ raise
49
+ end
50
+
25
51
  def connect
26
52
  login if auth_handlers.include? auth_type
27
53
  end
@@ -1,3 +1,5 @@
1
+ require "rubygems"
2
+
1
3
  require "train-rest/connection"
2
4
 
3
5
  module TrainPlugins
@@ -16,9 +18,32 @@ module TrainPlugins
16
18
  option :password, default: nil
17
19
  option :debug_rest, default: false
18
20
 
21
+ option :vcr_cassette, default: nil
22
+ option :vcr_library, default: "vcr"
23
+ option :vcr_match_on, default: "method uri"
24
+ option :vcr_record, default: "none"
25
+ option :vcr_hook_into, default: "webmock"
26
+
19
27
  def connection(_instance_opts = nil)
28
+ dependency_checks
29
+
20
30
  @connection ||= TrainPlugins::Rest::Connection.new(@options)
21
31
  end
32
+
33
+ private
34
+
35
+ def dependency_checks
36
+ return unless @options[:vcr_cassette]
37
+
38
+ raise Gem::LoadError.new("Install VCR Gem for API playback capability") unless gem_installed?("vcr")
39
+
40
+ stubber = @options[:vcr_hook_into]
41
+ raise Gem::LoadError.new("Install #{stubber} Gem for API playback capability") unless gem_installed?(stubber)
42
+ end
43
+
44
+ def gem_installed?(name)
45
+ Gem::Specification.find_all_by_name(name).any?
46
+ end
22
47
  end
23
48
  end
24
49
  end
@@ -1,5 +1,5 @@
1
1
  module TrainPlugins
2
2
  module Rest
3
- VERSION = "0.2.1".freeze
3
+ VERSION = "0.3.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: train-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Heinen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-24 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: train