data_plane_api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +5 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +77 -0
- data/LICENSE.txt +21 -0
- data/README.md +210 -0
- data/Rakefile +16 -0
- data/data_plane_api.gemspec +38 -0
- data/lib/data_plane_api/configuration.rb +119 -0
- data/lib/data_plane_api/server.rb +102 -0
- data/lib/data_plane_api/version.rb +6 -0
- data/lib/data_plane_api.rb +24 -0
- data/sig/data_plane_api.rbs +4 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ef9b77e23457f82223d970f629f3a49cd9d014d842f63b5b09ed69426728dcf5
|
4
|
+
data.tar.gz: 2ddee17d5f8ca74c2c26a371aade290f8eed7318ef0ee4007a1d7bf5c5755250
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8bc942ceebb102d2fb5805b48df7468e0cfcafe33ff7ee3508f9aef3c5b1ed7704dbfa29199fb990d5de992b24868cb274bcb37082a29e52a07fcb72834a6065
|
7
|
+
data.tar.gz: fe7567a34f721e700674a8d0e4e156b0ed368d74897c08de38c150f0bfe5d8fbe399ae6efc9462c22175b0f447e5975f471bdd1f3b88a2f2ef499c03ed1f988e
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in data_plane_api.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'minitest', '~> 5.0' # Tests framework
|
9
|
+
gem 'rake', '~> 13.0' # Ruby automation tasks
|
10
|
+
gem 'rubocop', '~> 1.21' # Ruby linter
|
11
|
+
gem 'rubocop-espago', '~> 1.0' # Custom Ruby linter config
|
12
|
+
gem 'shoulda-context', '~> 2.0' # Testing utilities
|
13
|
+
gem 'debug' # Ruby debugger
|
14
|
+
gem 'vcr' # Save HTTP interactions to files during tests
|
15
|
+
gem 'webmock' # Mock network interactions
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
data_plane_api (0.1.0)
|
5
|
+
faraday (~> 2.7)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.8.1)
|
11
|
+
public_suffix (>= 2.0.2, < 6.0)
|
12
|
+
ast (2.4.2)
|
13
|
+
crack (0.4.5)
|
14
|
+
rexml
|
15
|
+
debug (1.7.0)
|
16
|
+
irb (>= 1.5.0)
|
17
|
+
reline (>= 0.3.1)
|
18
|
+
faraday (2.7.2)
|
19
|
+
faraday-net_http (>= 2.0, < 3.1)
|
20
|
+
ruby2_keywords (>= 0.0.4)
|
21
|
+
faraday-net_http (3.0.2)
|
22
|
+
hashdiff (1.0.1)
|
23
|
+
io-console (0.6.0)
|
24
|
+
irb (1.6.1)
|
25
|
+
reline (>= 0.3.0)
|
26
|
+
json (2.6.3)
|
27
|
+
minitest (5.16.3)
|
28
|
+
parallel (1.22.1)
|
29
|
+
parser (3.1.3.0)
|
30
|
+
ast (~> 2.4.1)
|
31
|
+
public_suffix (5.0.1)
|
32
|
+
rainbow (3.1.1)
|
33
|
+
rake (13.0.6)
|
34
|
+
regexp_parser (2.6.1)
|
35
|
+
reline (0.3.2)
|
36
|
+
io-console (~> 0.5)
|
37
|
+
rexml (3.2.5)
|
38
|
+
rubocop (1.41.1)
|
39
|
+
json (~> 2.3)
|
40
|
+
parallel (~> 1.10)
|
41
|
+
parser (>= 3.1.2.1)
|
42
|
+
rainbow (>= 2.2.2, < 4.0)
|
43
|
+
regexp_parser (>= 1.8, < 3.0)
|
44
|
+
rexml (>= 3.2.5, < 4.0)
|
45
|
+
rubocop-ast (>= 1.23.0, < 2.0)
|
46
|
+
ruby-progressbar (~> 1.7)
|
47
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
48
|
+
rubocop-ast (1.24.0)
|
49
|
+
parser (>= 3.1.1.0)
|
50
|
+
rubocop-espago (1.0.2)
|
51
|
+
rubocop
|
52
|
+
ruby-progressbar (1.11.0)
|
53
|
+
ruby2_keywords (0.0.5)
|
54
|
+
shoulda-context (2.0.0)
|
55
|
+
unicode-display_width (2.3.0)
|
56
|
+
vcr (6.1.0)
|
57
|
+
webmock (3.18.1)
|
58
|
+
addressable (>= 2.8.0)
|
59
|
+
crack (>= 0.3.2)
|
60
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
arm64-darwin-20
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
data_plane_api!
|
67
|
+
debug
|
68
|
+
minitest (~> 5.0)
|
69
|
+
rake (~> 13.0)
|
70
|
+
rubocop (~> 1.21)
|
71
|
+
rubocop-espago (~> 1.0)
|
72
|
+
shoulda-context (~> 2.0)
|
73
|
+
vcr
|
74
|
+
webmock
|
75
|
+
|
76
|
+
BUNDLED WITH
|
77
|
+
2.4.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Espago
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
# DataPlaneApi Ruby
|
2
|
+
|
3
|
+
This gem provides a simple interface for the Community Edition of the HAProxy DataPlane API.
|
4
|
+
Currently, it only supports a handful of requests that we needed.
|
5
|
+
More may be added in the future.
|
6
|
+
|
7
|
+
We encourage you to make Pull Requests with more API endpoints.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add data_plane_api
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install data_plane_api
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The main namespace/module of this gem is `DataPlaneApi`.
|
22
|
+
|
23
|
+
We used [this documentation](https://www.haproxy.com/documentation/dataplaneapi/community/#get-/services/haproxy/configuration/acls) as the reference for implementing this gem.
|
24
|
+
|
25
|
+
Each section of the HAProxy Data Plane API should have its own module under
|
26
|
+
`DataPlaneApi`. Each endpoint is represented as a singleton method of this module.
|
27
|
+
|
28
|
+
eg. `DataPlaneApi::Server.get_runtime_settings`.
|
29
|
+
|
30
|
+
### Configuration
|
31
|
+
|
32
|
+
In order to make requests, you need to configure
|
33
|
+
the API URL, user and password.
|
34
|
+
|
35
|
+
You can configure the gem globally.
|
36
|
+
|
37
|
+
```rb
|
38
|
+
DataPlaneApi.configure do |c|
|
39
|
+
# REQUIRED
|
40
|
+
c.url = 'http://example.com'
|
41
|
+
c.basic_user = 'api_user'
|
42
|
+
c.basic_password = 'api_password'
|
43
|
+
# OPTIONAL
|
44
|
+
c.timeout = 25 # request timeout in seconds
|
45
|
+
c.logger = Logger.new($stdout) # custom logger
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
These settings will be used by default in
|
50
|
+
all requests without local configuration
|
51
|
+
objects.
|
52
|
+
|
53
|
+
You can also create configuration objects
|
54
|
+
and pass them as arguments to particular request
|
55
|
+
method calls.
|
56
|
+
|
57
|
+
```rb
|
58
|
+
# create a config object
|
59
|
+
config = DataPlaneApi::Configuration.new(
|
60
|
+
url: 'http://example.com',
|
61
|
+
basic_user: 'api_user'
|
62
|
+
basic_password: 'api_password'
|
63
|
+
)
|
64
|
+
|
65
|
+
# make a request
|
66
|
+
response = DataPlaneApi::Server.get_runtime_settings(
|
67
|
+
backend: 'foo',
|
68
|
+
config: config # use the configuration object
|
69
|
+
)
|
70
|
+
```
|
71
|
+
|
72
|
+
A configuration object like that may contain
|
73
|
+
incomplete configuration data. Any missing
|
74
|
+
options will be inherited from the global
|
75
|
+
configuration object.
|
76
|
+
|
77
|
+
For example, let's say, that you've got a global
|
78
|
+
configuration like that.
|
79
|
+
|
80
|
+
```rb
|
81
|
+
DataPlaneApi.configure do |c|
|
82
|
+
c.url = 'http://example.com'
|
83
|
+
c.basic_user = 'api_user'
|
84
|
+
c.basic_password = 'api_password'
|
85
|
+
c.timeout = 25 # request timeout in seconds
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
You want to change the timeout to 2 seconds
|
90
|
+
for a single request. You can achieve it
|
91
|
+
by creating a new config object which contains
|
92
|
+
only the new data, that you want to change when
|
93
|
+
compared to the global config. The rest will
|
94
|
+
be inherited.
|
95
|
+
|
96
|
+
```rb
|
97
|
+
config = DataPlaneApi::Configuration.new(timeout: 2)
|
98
|
+
config.timeout #=> 2
|
99
|
+
config.url #=> 'http://example.com'
|
100
|
+
config.basic_user #=> 'api_user'
|
101
|
+
```
|
102
|
+
|
103
|
+
### Response
|
104
|
+
|
105
|
+
You will always get a `Faraday::Response` object as a return value
|
106
|
+
from a request call.
|
107
|
+
|
108
|
+
The response body will be parsed to Ruby structures (Hash, Array, Integer, etc.)
|
109
|
+
|
110
|
+
You may want to look at the [documentation of the faraday gem](https://lostisland.github.io/faraday/usage/) for more
|
111
|
+
details.
|
112
|
+
|
113
|
+
### Server
|
114
|
+
|
115
|
+
Server endpoints live under the `DataPlaneApi::Server` module.
|
116
|
+
|
117
|
+
### Runtime settings
|
118
|
+
|
119
|
+
[Return an array of runtime servers' settings](https://www.haproxy.com/documentation/dataplaneapi/community/#get-/services/haproxy/runtime/servers)
|
120
|
+
|
121
|
+
You can get an array of runtime servers' settings like so.
|
122
|
+
|
123
|
+
```rb
|
124
|
+
# @type [Faraday::Response]
|
125
|
+
response = DataPlaneApi::Server.get_runtime_settings(backend: 'my_backend_name')
|
126
|
+
|
127
|
+
response.status #=> 200
|
128
|
+
response.headers #=> {"server"=>"Fly/c375678 (2021-04-23)", "content-type"=> ...
|
129
|
+
response.body #=> [{"address":"12.0.5.102","admin_state":"ready","id":"1","name":"foo_bar1","operational_state":"up","port":4512}]
|
130
|
+
```
|
131
|
+
|
132
|
+
[Return one server runtime settings](https://www.haproxy.com/documentation/dataplaneapi/community/#get-/services/haproxy/runtime/servers/-name-)
|
133
|
+
|
134
|
+
You can provide the name of a particular server to get only its
|
135
|
+
settings.
|
136
|
+
|
137
|
+
```rb
|
138
|
+
# @type [Faraday::Response]
|
139
|
+
response = DataPlaneApi::Server.get_runtime_settings(backend: 'my_backend_name', name: 'my_server_name')
|
140
|
+
|
141
|
+
response.status #=> 200
|
142
|
+
response.headers #=> {"server"=>"Fly/c375678 (2021-04-23)", "content-type"=> ...
|
143
|
+
response.body #=> [{"address":"12.0.5.102","admin_state":"ready","id":"1","name":"foo_bar1","operational_state":"up","port":4512}]
|
144
|
+
```
|
145
|
+
|
146
|
+
You can override the global config by providing a config object.
|
147
|
+
|
148
|
+
```rb
|
149
|
+
# create a config object
|
150
|
+
config = DataPlaneApi::Configuration.new(timeout: 2)
|
151
|
+
|
152
|
+
response = DataPlaneApi::Server.get_runtime_settings(
|
153
|
+
config: config # use the new config
|
154
|
+
backend: 'my_backend_name',
|
155
|
+
name: 'my_server_name'
|
156
|
+
)
|
157
|
+
```
|
158
|
+
|
159
|
+
### Transient settings
|
160
|
+
|
161
|
+
[Replace server transient settings](https://www.haproxy.com/documentation/dataplaneapi/community/#put-/services/haproxy/runtime/servers/-name-)
|
162
|
+
|
163
|
+
You can replace server transient settings like so.
|
164
|
+
|
165
|
+
```rb
|
166
|
+
# @type [Faraday::Response]
|
167
|
+
response = DataPlaneApi::Server.get_runtime_settings(
|
168
|
+
backend: 'my_backend_name',
|
169
|
+
name: 'my_server_name',
|
170
|
+
settings: {
|
171
|
+
admin_state: 'ready',
|
172
|
+
operational_state: 'up'
|
173
|
+
}
|
174
|
+
)
|
175
|
+
|
176
|
+
response.status #=> 200
|
177
|
+
response.headers #=> {"server"=>"Fly/c375678 (2021-04-23)", "content-type"=> ...
|
178
|
+
response.body #=> [{"address":"12.0.5.102","admin_state":"ready","id":"1","name":"foo_bar1","operational_state":"up","port":4512}]
|
179
|
+
```
|
180
|
+
|
181
|
+
You can override the global config by providing a config object.
|
182
|
+
|
183
|
+
```rb
|
184
|
+
# create a config object
|
185
|
+
config = DataPlaneApi::Configuration.new(timeout: 2)
|
186
|
+
|
187
|
+
response = DataPlaneApi::Server.get_runtime_settings(
|
188
|
+
config: config # use the new config
|
189
|
+
backend: 'my_backend_name',
|
190
|
+
name: 'my_server_name',
|
191
|
+
settings: {
|
192
|
+
admin_state: 'ready',
|
193
|
+
operational_state: 'up'
|
194
|
+
}
|
195
|
+
)
|
196
|
+
```
|
197
|
+
|
198
|
+
## Development
|
199
|
+
|
200
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
201
|
+
|
202
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
203
|
+
|
204
|
+
## Contributing
|
205
|
+
|
206
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/espago/data_plane_api_ruby.
|
207
|
+
|
208
|
+
## License
|
209
|
+
|
210
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
::Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = ::FileList['test/**/*_test.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rubocop/rake_task'
|
13
|
+
|
14
|
+
::RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/data_plane_api/version'
|
4
|
+
|
5
|
+
::Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'data_plane_api'
|
7
|
+
spec.version = ::DataPlaneApi::VERSION
|
8
|
+
spec.authors = ['Mateusz Drewniak', 'Espago']
|
9
|
+
spec.email = ['m.drewniak@espago.com']
|
10
|
+
|
11
|
+
spec.summary = 'Ruby gem which covers a limited subset of the HAProxy Data Plane API.'
|
12
|
+
spec.description = 'Ruby gem which covers a limited subset of the HAProxy Data Plane API.'
|
13
|
+
spec.homepage = 'https://github.com/espago/data_plane_api_ruby'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = '>= 2.7.0'
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/espago/data_plane_api_ruby'
|
19
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = ::Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| ::File.basename(f) }
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
spec.add_dependency 'faraday', '~> 2.7'
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, check out our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
38
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
module DataPlaneApi
|
7
|
+
# Stores configuration options for the HAProxy Data Plane API.
|
8
|
+
class Configuration
|
9
|
+
# @return [String, URI::Generic, nil]
|
10
|
+
attr_writer :url
|
11
|
+
# @return [String, nil] Basic Auth username.
|
12
|
+
attr_writer :basic_user
|
13
|
+
# @return [String, nil] Basic Auth password.
|
14
|
+
attr_writer :basic_password
|
15
|
+
# @return [Logger, nil]
|
16
|
+
attr_writer :logger
|
17
|
+
# @return [Integer, nil]
|
18
|
+
attr_writer :timeout
|
19
|
+
# @return [Boolean] whether this object is used as a global store of settings
|
20
|
+
attr_reader :global
|
21
|
+
|
22
|
+
# @param url [String, nil]
|
23
|
+
# @param global [Boolean] whether this object is used as a global store of settings
|
24
|
+
# @param basic_user [String, nil] Basic Auth username.
|
25
|
+
# @param basic_password [String, nil] Basic Auth password.
|
26
|
+
# @param logger [Logger, nil]
|
27
|
+
# @param timeout [Integer, nil]
|
28
|
+
# @param parent [self, nil]
|
29
|
+
def initialize(
|
30
|
+
url: nil,
|
31
|
+
global: false,
|
32
|
+
basic_user: nil,
|
33
|
+
basic_password: nil,
|
34
|
+
logger: nil,
|
35
|
+
timeout: nil,
|
36
|
+
parent: nil
|
37
|
+
)
|
38
|
+
|
39
|
+
@global = global
|
40
|
+
@url = url
|
41
|
+
@basic_user = basic_user
|
42
|
+
@basic_password = basic_password
|
43
|
+
@logger = logger
|
44
|
+
@timeout = timeout
|
45
|
+
@parent = parent
|
46
|
+
|
47
|
+
return unless global
|
48
|
+
|
49
|
+
@logger ||= ::Logger.new($stdout)
|
50
|
+
@timeout ||= 10
|
51
|
+
end
|
52
|
+
|
53
|
+
# @return [Faraday::Connection]
|
54
|
+
def connection
|
55
|
+
@connection || build_connection
|
56
|
+
end
|
57
|
+
|
58
|
+
# @return [void]
|
59
|
+
def freeze
|
60
|
+
@connection = build_connection
|
61
|
+
super
|
62
|
+
end
|
63
|
+
|
64
|
+
# @return [String, URI::Generic, nil]
|
65
|
+
def url
|
66
|
+
return @url if @global || @url
|
67
|
+
|
68
|
+
parent.url
|
69
|
+
end
|
70
|
+
|
71
|
+
# @return [String, nil] Basic Auth username.
|
72
|
+
def basic_user
|
73
|
+
return @basic_user if @global || @basic_user
|
74
|
+
|
75
|
+
parent.basic_user
|
76
|
+
end
|
77
|
+
|
78
|
+
# @return [String, nil] Basic Auth password.
|
79
|
+
def basic_password
|
80
|
+
return @basic_password if @global || @basic_password
|
81
|
+
|
82
|
+
parent.basic_password
|
83
|
+
end
|
84
|
+
|
85
|
+
# @return [Logger, nil]
|
86
|
+
def logger
|
87
|
+
return @logger if @global || @logger
|
88
|
+
|
89
|
+
parent.logger
|
90
|
+
end
|
91
|
+
|
92
|
+
# @return [Integer, nil]
|
93
|
+
def timeout
|
94
|
+
return @timeout if @global || @timeout
|
95
|
+
|
96
|
+
parent.timeout
|
97
|
+
end
|
98
|
+
|
99
|
+
# @return [self, nil]
|
100
|
+
def parent
|
101
|
+
return if @global
|
102
|
+
|
103
|
+
@parent || CONFIG
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
# @return [Faraday::Connection]
|
109
|
+
def build_connection
|
110
|
+
headers = { 'Content-Type' => 'application/json' }
|
111
|
+
|
112
|
+
::Faraday.new(url: "#{url}/v2/", headers: headers) do |f|
|
113
|
+
f.request :json
|
114
|
+
f.response :json
|
115
|
+
f.request :authorization, :basic, basic_user, basic_password
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'set'
|
5
|
+
|
6
|
+
module DataPlaneApi
|
7
|
+
# Wraps endpoints regarding HAProxy servers.
|
8
|
+
module Server
|
9
|
+
# @return [Set<Symbol>]
|
10
|
+
ADMIN_STATES = ::Set[:ready, :maint, :drain]
|
11
|
+
# @return [Set<Symbol>]
|
12
|
+
OPERATIONAL_STATES = ::Set[:up, :down, :stopping]
|
13
|
+
|
14
|
+
class << self
|
15
|
+
# @param backend [String] Name of the backend
|
16
|
+
# @param name [String, nil] Name of the server whose settings will be returned.
|
17
|
+
# If `nil` then an array of settings of all servers under the passed `backend`
|
18
|
+
# will be returned.
|
19
|
+
# @param config [HaProxy::DataPlaneApi::Configuration, nil]
|
20
|
+
# @return [Faraday::Response]
|
21
|
+
def get_runtime_settings(backend:, name: nil, config: nil)
|
22
|
+
config ||= CONFIG
|
23
|
+
if backend.nil? || backend.empty?
|
24
|
+
raise ::ArgumentError, "`backend` should be present but was `#{backend.inspect}`"
|
25
|
+
end
|
26
|
+
|
27
|
+
path = "services/haproxy/runtime/servers/#{name}".delete_suffix('/')
|
28
|
+
|
29
|
+
send_request(method: :get, path: path, config: config) do |req|
|
30
|
+
req.params[:backend] = backend
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param backend [String] Name of the backend
|
35
|
+
# @param name [String, nil] Name of the server whose transient settings should be updated.
|
36
|
+
# @param settings [Hash]
|
37
|
+
# @param config [HaProxy::DataPlaneApi::Configuration, nil]
|
38
|
+
# @return [Faraday::Response]
|
39
|
+
def update_transient_settings(backend:, name:, settings:, config: nil)
|
40
|
+
config ||= CONFIG
|
41
|
+
if backend.nil? || backend.empty?
|
42
|
+
raise ::ArgumentError, "`backend` should be present but was `#{backend.inspect}`"
|
43
|
+
end
|
44
|
+
raise ::ArgumentError, "`name` should be present but was `#{name.inspect}`" if name.nil? || name.empty?
|
45
|
+
|
46
|
+
path = "services/haproxy/runtime/servers/#{name}"
|
47
|
+
|
48
|
+
send_request(method: :put, path: path, config: config) do |req|
|
49
|
+
req.params[:backend] = backend
|
50
|
+
req.body = settings
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
# @param method [Symbol]
|
57
|
+
# @param path [String, Pathname]
|
58
|
+
# @param config [HaProxy::DataPlaneApi::Configuration]
|
59
|
+
# @yieldparam request [Faraday::Request]
|
60
|
+
# @return [Faraday::Response]
|
61
|
+
def send_request(method:, path:, config:, &block)
|
62
|
+
request = nil
|
63
|
+
response = config.connection.public_send(method, path) do |req|
|
64
|
+
block.call(req)
|
65
|
+
req.options.timeout = config.timeout
|
66
|
+
request = req
|
67
|
+
end
|
68
|
+
|
69
|
+
log_communication(request, response, logger: config.logger)
|
70
|
+
|
71
|
+
response
|
72
|
+
end
|
73
|
+
|
74
|
+
# @param request [Faraday::Request]
|
75
|
+
# @param response [Faraday::Response]
|
76
|
+
# @param logger [Logger]
|
77
|
+
# @return [void]
|
78
|
+
def log_communication(request, response, logger:)
|
79
|
+
request_hash = {
|
80
|
+
method: request.http_method,
|
81
|
+
url: response.env.url,
|
82
|
+
params: request.params,
|
83
|
+
headers: request.headers,
|
84
|
+
body: request.body
|
85
|
+
}
|
86
|
+
response_hash = {
|
87
|
+
status: response.status,
|
88
|
+
body: response.body,
|
89
|
+
headers: response.headers
|
90
|
+
}
|
91
|
+
|
92
|
+
logger.debug <<~REQ
|
93
|
+
HAProxy #{request.http_method.to_s.upcase} #{response.env.url}
|
94
|
+
-----REQUEST-----
|
95
|
+
#{::JSON.pretty_generate request_hash}
|
96
|
+
-----RESPONSE-----
|
97
|
+
#{::JSON.pretty_generate response_hash}
|
98
|
+
REQ
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'data_plane_api/version'
|
4
|
+
require_relative 'data_plane_api/configuration'
|
5
|
+
|
6
|
+
# Contains code which implements a subset of the
|
7
|
+
# HAProxy Data Plane API.
|
8
|
+
module DataPlaneApi
|
9
|
+
class Error < ::StandardError; end
|
10
|
+
|
11
|
+
# @return [DataPlaneAPI]
|
12
|
+
CONFIG = Configuration.new(global: true)
|
13
|
+
|
14
|
+
class << self
|
15
|
+
# @yieldparam config [Configuration]
|
16
|
+
# @return [Configuration]
|
17
|
+
def configure(&block)
|
18
|
+
block.call(CONFIG)
|
19
|
+
CONFIG
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
require_relative 'data_plane_api/server'
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_plane_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mateusz Drewniak
|
8
|
+
- Espago
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2022-12-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '2.7'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '2.7'
|
28
|
+
description: Ruby gem which covers a limited subset of the HAProxy Data Plane API.
|
29
|
+
email:
|
30
|
+
- m.drewniak@espago.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".rubocop.yml"
|
36
|
+
- CHANGELOG.md
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- data_plane_api.gemspec
|
43
|
+
- lib/data_plane_api.rb
|
44
|
+
- lib/data_plane_api/configuration.rb
|
45
|
+
- lib/data_plane_api/server.rb
|
46
|
+
- lib/data_plane_api/version.rb
|
47
|
+
- sig/data_plane_api.rbs
|
48
|
+
homepage: https://github.com/espago/data_plane_api_ruby
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://github.com/espago/data_plane_api_ruby
|
53
|
+
source_code_uri: https://github.com/espago/data_plane_api_ruby
|
54
|
+
rubygems_mfa_required: 'true'
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.7.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.3.26
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Ruby gem which covers a limited subset of the HAProxy Data Plane API.
|
74
|
+
test_files: []
|