train-rest 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/train-rest/version.rb +1 -1
- data/train-rest.gemspec +4 -4
- metadata +7 -9
- data/Gemfile +0 -3
- data/README.md +0 -280
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a003dbd0ea47ee7eb23e2173036951f9104493a761a7e1d8d9e1798ce933ddc3
|
4
|
+
data.tar.gz: ee585ad7ed98f701e20fe3d48384260073df3f69eb102faa05d79eb118a63f95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6440c9ff603fe5af0042cd925c4c3729552fb65cc8a41162e4991e9301149ba3795bbf5fffcff87990d52f784331839ff70f99b20d9fceb230785d4e9cc1ba38
|
7
|
+
data.tar.gz: e92001e986dce6ead7361cb6b3e55213a1c17ba517a62ed99ce9b0275dc689857cec05e05d9b58a6039e1971c3ec7773a9622bb271d4d8f5c495d4bb1c6f6306
|
data/lib/train-rest/version.rb
CHANGED
data/train-rest.gemspec
CHANGED
@@ -9,21 +9,21 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.email = ["theinen@tecracer.de"]
|
10
10
|
spec.summary = "Train transport for REST"
|
11
11
|
spec.description = "Provides a transport to communicate easily with RESTful APIs."
|
12
|
-
spec.homepage = "https://github.com/
|
12
|
+
spec.homepage = "https://github.com/tecracer-chef/train-rest"
|
13
13
|
spec.license = "Apache-2.0"
|
14
14
|
|
15
15
|
spec.files = %w{
|
16
|
-
|
16
|
+
train-rest.gemspec
|
17
17
|
} + Dir.glob(
|
18
18
|
"lib/**/*", File::FNM_DOTMATCH
|
19
19
|
).reject { |f| File.directory?(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency "train", "~> 3.0"
|
22
|
+
spec.add_dependency "train-core", "~> 3.0"
|
23
23
|
spec.add_dependency "rest-client", "~> 2.1"
|
24
24
|
|
25
25
|
spec.add_development_dependency "bump", "~> 0.9"
|
26
|
-
spec.add_development_dependency "chefstyle", "~>
|
26
|
+
spec.add_development_dependency "chefstyle", "~> 2.2"
|
27
27
|
spec.add_development_dependency "guard", "~> 2.16"
|
28
28
|
spec.add_development_dependency "mdl", "~> 0.9"
|
29
29
|
spec.add_development_dependency "rake", "~> 13.0"
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: train-rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Heinen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: train
|
14
|
+
name: train-core
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '2.2'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '2.2'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: guard
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -115,8 +115,6 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
-
- Gemfile
|
119
|
-
- README.md
|
120
118
|
- lib/train-rest.rb
|
121
119
|
- lib/train-rest/auth_handler.rb
|
122
120
|
- lib/train-rest/auth_handler/anonymous.rb
|
@@ -129,7 +127,7 @@ files:
|
|
129
127
|
- lib/train-rest/transport.rb
|
130
128
|
- lib/train-rest/version.rb
|
131
129
|
- train-rest.gemspec
|
132
|
-
homepage: https://github.com/
|
130
|
+
homepage: https://github.com/tecracer-chef/train-rest
|
133
131
|
licenses:
|
134
132
|
- Apache-2.0
|
135
133
|
metadata: {}
|
@@ -148,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
146
|
- !ruby/object:Gem::Version
|
149
147
|
version: '0'
|
150
148
|
requirements: []
|
151
|
-
rubygems_version: 3.0.3
|
149
|
+
rubygems_version: 3.0.3.1
|
152
150
|
signing_key:
|
153
151
|
specification_version: 4
|
154
152
|
summary: Train transport for REST
|
data/Gemfile
DELETED
data/README.md
DELETED
@@ -1,280 +0,0 @@
|
|
1
|
-
# train-rest - Train transport
|
2
|
-
|
3
|
-
Provides a transport to communicate easily with RESTful APIs.
|
4
|
-
|
5
|
-
## Requirements
|
6
|
-
|
7
|
-
- Gem `rest-client` in Version 2.1
|
8
|
-
|
9
|
-
## Installation
|
10
|
-
|
11
|
-
You will have to build this gem yourself to install it as it is not yet on
|
12
|
-
Rubygems.Org. For this there is a rake task which makes this a one-liner:
|
13
|
-
|
14
|
-
```bash
|
15
|
-
rake install:local
|
16
|
-
```
|
17
|
-
|
18
|
-
## Transport parameters
|
19
|
-
|
20
|
-
| Option | Explanation | Default |
|
21
|
-
| -------------------- | --------------------------------------- | ----------- |
|
22
|
-
| `endpoint` | Endpoint of the REST API | _required_ |
|
23
|
-
| `verify_ssl` | Check certificate and chain | true |
|
24
|
-
| `auth_type` | Authentication type | `anonymous` |
|
25
|
-
| `debug_rest` | Enable debugging of HTTP traffic | false |
|
26
|
-
| `logger` | Alternative logging class | |
|
27
|
-
|
28
|
-
## Authenticators
|
29
|
-
|
30
|
-
### Anonymous
|
31
|
-
|
32
|
-
Identifier: `auth_type: :anonymous`
|
33
|
-
|
34
|
-
No actions for authentication, logging in/out or session handing are made. This
|
35
|
-
assumes a public API.
|
36
|
-
|
37
|
-
### Authtype Apikey
|
38
|
-
|
39
|
-
This will inject a HTTP header `Authorization: Apikey xxxx` with the passed
|
40
|
-
API key into requests.
|
41
|
-
|
42
|
-
Identifier: `auth_type: :authtype_apikey`
|
43
|
-
|
44
|
-
| Option | Explanation | Default |
|
45
|
-
| -------------------- | --------------------------------------- | ----------- |
|
46
|
-
| `apikey` | API Key for authentication | _required_ |
|
47
|
-
|
48
|
-
### Basic (RFC 2617)
|
49
|
-
|
50
|
-
Identifier: `auth_type: :basic`
|
51
|
-
|
52
|
-
| Option | Explanation | Default |
|
53
|
-
| -------------------- | --------------------------------------- | ----------- |
|
54
|
-
| `username` | Username for `basic` authentication | _required_ |
|
55
|
-
| `password` | Password for `basic` authentication | _required_ |
|
56
|
-
|
57
|
-
If you supply a `username` and a `password`, authentication will automatically
|
58
|
-
switch to `basic`.
|
59
|
-
|
60
|
-
### Bearer (RFC 7650)
|
61
|
-
|
62
|
-
This will inject a HTTP header `Authorization: Bearer xxxx` with the passed
|
63
|
-
token into requests.
|
64
|
-
|
65
|
-
Identifier: `auth_type: :bearer`
|
66
|
-
|
67
|
-
| Option | Explanation | Default |
|
68
|
-
| -------------------- | --------------------------------------- | ----------- |
|
69
|
-
| `token` | Tokenb to pass | _required_ |
|
70
|
-
|
71
|
-
### Header-based
|
72
|
-
|
73
|
-
This will inject an additional HTTP header with the passed value. If no name
|
74
|
-
for the header is passed, it will default to `X-API-Key`.
|
75
|
-
|
76
|
-
Identifier: `auth_type: :header`
|
77
|
-
|
78
|
-
| Option | Explanation | Default |
|
79
|
-
| -------------------- | --------------------------------------- | ----------- |
|
80
|
-
| `apikey` | API Key for authentication | _required_ |
|
81
|
-
| `header` | Name of the HTTP header to include | `X-API-Key` |
|
82
|
-
|
83
|
-
### Redfish
|
84
|
-
|
85
|
-
Identifier: `auth_type: :redfish`
|
86
|
-
|
87
|
-
| Option | Explanation | Default |
|
88
|
-
| -------------------- | --------------------------------------- | ----------- |
|
89
|
-
| `username` | Username for `redfish` authentication | _required_ |
|
90
|
-
| `password` | Password for `redfish` authentication | _required_ |
|
91
|
-
|
92
|
-
For access to integrated management controllers on standardized server hardware.
|
93
|
-
The Redfish standard is defined in <http://www.dmtf.org/standards/redfish> and
|
94
|
-
this handler does initial login, reuses the received session and logs out when
|
95
|
-
closing the transport cleanly.
|
96
|
-
|
97
|
-
Known vendors which implement RedFish based management for their systems include
|
98
|
-
HPE, Dell, IBM, SuperMicro, Lenovo, Huawei and others.
|
99
|
-
|
100
|
-
## Debugging and use in Chef
|
101
|
-
|
102
|
-
You can activate debugging by setting the `debug_rest` flag to `true'. Please
|
103
|
-
note, that this will also log any confidential parts of HTTP traffic as well.
|
104
|
-
|
105
|
-
For better integration into Chef custom resources, all REST debug output will
|
106
|
-
be printed on `info` level. This allows debugging Chef resources without
|
107
|
-
crawling through all other debug output:
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
train = Train.create('rest', {
|
111
|
-
endpoint: 'https://api.example.com/v1/',
|
112
|
-
debug_rest: true,
|
113
|
-
logger: Chef::Log
|
114
|
-
})
|
115
|
-
```
|
116
|
-
|
117
|
-
## Request Methods
|
118
|
-
|
119
|
-
This transport does not implement the `run_command` method, as there is no
|
120
|
-
line-based protocol to execute commands against. Instead, it implements its own
|
121
|
-
custom methods which suit REST interfaces. Trying to call this method will
|
122
|
-
throw an Exception.
|
123
|
-
|
124
|
-
### Generic Request
|
125
|
-
|
126
|
-
The `request` methods allows to send free-form requests against any defined or
|
127
|
-
custom methods.
|
128
|
-
|
129
|
-
`request(path, method = :get, request_parameters: {}, data: nil, headers: {},
|
130
|
-
json_processing: true)`
|
131
|
-
|
132
|
-
- `path`: The path to request, which will be appended to the `endpoint`
|
133
|
-
- `method`: The HTTP method in Ruby Symbol syntax
|
134
|
-
- `request_parameters`: A hash of parameters to the `rest-client` request
|
135
|
-
method for additional settings
|
136
|
-
- `data`: Data for actions like `:post` or `:put`. Not all methods accept
|
137
|
-
a data body.
|
138
|
-
- `headers`: Additional headers for the request
|
139
|
-
- `json_processing`: If the response is a JSON and you want to receive a
|
140
|
-
processed Hash/Array instead of text
|
141
|
-
|
142
|
-
For `request_parameters` and `headers`, there is data mixed in to add
|
143
|
-
authenticator responses, JSON processing etc. Please check the
|
144
|
-
implementation in `connection.rb` for details.
|
145
|
-
|
146
|
-
### Convenience Methods
|
147
|
-
|
148
|
-
Simplified wrappers are generated for the most common request types:
|
149
|
-
|
150
|
-
- `delete(path, request_parameters: {}, headers: {}, json_processing: true)`
|
151
|
-
- `head(path, request_parameters: {}, headers: {}, json_processing: true)`
|
152
|
-
- `get(path, request_parameters: {}, headers: {}, json_processing: true)`
|
153
|
-
- `post(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
|
154
|
-
- `put(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
|
155
|
-
- `patch(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
|
156
|
-
|
157
|
-
## Example use
|
158
|
-
|
159
|
-
```ruby
|
160
|
-
require 'train-rest'
|
161
|
-
|
162
|
-
train = Train.create('rest', {
|
163
|
-
endpoint: 'https://api.example.com/v1/',
|
164
|
-
|
165
|
-
logger: Logger.new($stdout, level: :info)
|
166
|
-
})
|
167
|
-
conn = train.connection
|
168
|
-
|
169
|
-
# Get some hypothetical data
|
170
|
-
data = conn.get('device/1/settings')
|
171
|
-
|
172
|
-
# Modify + Patch
|
173
|
-
data['disabled'] = false
|
174
|
-
conn.patch('device/1/settings', data)
|
175
|
-
|
176
|
-
conn.close
|
177
|
-
```
|
178
|
-
|
179
|
-
Example for basic authentication:
|
180
|
-
|
181
|
-
```ruby
|
182
|
-
require 'train-rest'
|
183
|
-
|
184
|
-
# This will immediately do a login and add headers
|
185
|
-
train = Train.create('rest', {
|
186
|
-
endpoint: 'https://api.example.com/v1/',
|
187
|
-
|
188
|
-
auth_type: :basic,
|
189
|
-
username: 'admin',
|
190
|
-
password: '*********'
|
191
|
-
})
|
192
|
-
conn = train.connection
|
193
|
-
|
194
|
-
# ... do work, each request will resend Basic authentication headers ...
|
195
|
-
|
196
|
-
conn.close
|
197
|
-
```
|
198
|
-
|
199
|
-
Example for logging into a RedFish based system. Please note that the RedFish
|
200
|
-
authentication handler will append `redfish/v1` to the endpoint automatically,
|
201
|
-
if it is not present.
|
202
|
-
|
203
|
-
Due to this, you can use RedFish systems either with a base URL like in the
|
204
|
-
example below or with a full one. Your own code needs to match the style
|
205
|
-
you choose.
|
206
|
-
|
207
|
-
```ruby
|
208
|
-
require 'train-rest'
|
209
|
-
|
210
|
-
# This will immediately do a login and add headers
|
211
|
-
train = Train.create('rest', {
|
212
|
-
endpoint: 'https://10.20.30.40',
|
213
|
-
verify_ssl: false,
|
214
|
-
|
215
|
-
auth_type: :redfish,
|
216
|
-
username: 'iloadmin',
|
217
|
-
password: '*********'
|
218
|
-
})
|
219
|
-
conn = train.connection
|
220
|
-
|
221
|
-
# ... do work ...
|
222
|
-
|
223
|
-
# Handles logout as well
|
224
|
-
conn.close
|
225
|
-
```
|
226
|
-
|
227
|
-
## Use with Redfish, Your Custom Resources and Chef Target Mode
|
228
|
-
|
229
|
-
1. Set up a credentials file under `~/.chef/credentials` or `/etc/chef/credentials`:
|
230
|
-
|
231
|
-
```toml
|
232
|
-
['10.0.0.1']
|
233
|
-
endpoint = 'https://10.0.0.1/redfish/v1/'
|
234
|
-
username = 'user'
|
235
|
-
password = 'pass'
|
236
|
-
verify_ssl = false
|
237
|
-
auth_type = 'redfish'
|
238
|
-
```
|
239
|
-
|
240
|
-
1. Configure Chef to use the REST transport in `client.rb`:
|
241
|
-
|
242
|
-
```toml
|
243
|
-
target_mode.protocol = "rest"
|
244
|
-
```
|
245
|
-
|
246
|
-
1. Write your custom resources for REST APIs
|
247
|
-
1. Mark them up using the REST methods for target mode:
|
248
|
-
|
249
|
-
```ruby
|
250
|
-
provides :rest_resource, target_mode: true, platform: 'rest'
|
251
|
-
```
|
252
|
-
|
253
|
-
1. Run against the defiend targets via Chef Target Mode:
|
254
|
-
|
255
|
-
```shell
|
256
|
-
chef-client --local-mode --target 10.0.0.1 --runlist 'recipe[my-cookbook::setup]'
|
257
|
-
```
|
258
|
-
|
259
|
-
## Use with Prerecorded API responses
|
260
|
-
|
261
|
-
For testing during and after development, not all APIs can be used to verify your solution against.
|
262
|
-
The VCR gem offers the possibility to hook into web requests and intercept them to play back canned
|
263
|
-
responses.
|
264
|
-
|
265
|
-
Please read the documentation of the VCR gem on how to record your API and the concepts like
|
266
|
-
"cassettes", "libraries" and matchers.
|
267
|
-
|
268
|
-
The following options are available in train-rest for this:
|
269
|
-
|
270
|
-
| Option | Explanation | Default |
|
271
|
-
| -------------------- | --------------------------------------- | ------------ |
|
272
|
-
| `vcr_cassette` | Name of the response file | nil |
|
273
|
-
| `vcr_library` | Directory to search responses in | `vcr` |
|
274
|
-
| `vcr_match_on` | Elements to match request by | `method uri` |
|
275
|
-
| `vcr_record` | Recording mode | `none` |
|
276
|
-
| `vcr_hook_into` | Base library for intercepting | `webmock` |
|
277
|
-
|
278
|
-
VCR will only be required as a Gem and activated, if you supply a cassette name.
|
279
|
-
|
280
|
-
You can use all these settings in your Chef Target Mode `credentials` file as well.
|