nucleus 0.2.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/CHANGELOG.md +9 -0
  4. data/README.md +43 -72
  5. data/lib/nucleus/adapter_resolver.rb +3 -3
  6. data/lib/nucleus/adapters/base_adapter.rb +109 -109
  7. data/lib/nucleus/adapters/v1/cloud_foundry_v2/application.rb +111 -111
  8. data/lib/nucleus/adapters/v1/cloud_foundry_v2/cloud_foundry_v2.rb +141 -141
  9. data/lib/nucleus/adapters/v1/cloud_foundry_v2/data.rb +97 -97
  10. data/lib/nucleus/adapters/v1/cloud_foundry_v2/domains.rb +5 -5
  11. data/lib/nucleus/adapters/v1/cloud_foundry_v2/lifecycle.rb +41 -41
  12. data/lib/nucleus/adapters/v1/cloud_foundry_v2/logs.rb +6 -6
  13. data/lib/nucleus/adapters/v1/cloud_foundry_v2/regions.rb +33 -33
  14. data/lib/nucleus/adapters/v1/cloud_foundry_v2/services.rb +6 -6
  15. data/lib/nucleus/adapters/v1/cloud_foundry_v2/vars.rb +80 -80
  16. data/lib/nucleus/adapters/v1/heroku/app_states.rb +57 -57
  17. data/lib/nucleus/adapters/v1/heroku/data.rb +78 -78
  18. data/lib/nucleus/adapters/v1/heroku/heroku.rb +146 -146
  19. data/lib/nucleus/adapters/v1/heroku/lifecycle.rb +51 -51
  20. data/lib/nucleus/adapters/v1/heroku/logs.rb +2 -2
  21. data/lib/nucleus/adapters/v1/heroku/regions.rb +42 -42
  22. data/lib/nucleus/adapters/v1/heroku/services.rb +168 -168
  23. data/lib/nucleus/adapters/v1/heroku/vars.rb +65 -65
  24. data/lib/nucleus/adapters/v1/openshift_v2/app_states.rb +68 -68
  25. data/lib/nucleus/adapters/v1/openshift_v2/application.rb +1 -1
  26. data/lib/nucleus/adapters/v1/openshift_v2/data.rb +96 -96
  27. data/lib/nucleus/adapters/v1/openshift_v2/lifecycle.rb +60 -60
  28. data/lib/nucleus/adapters/v1/openshift_v2/logs.rb +106 -106
  29. data/lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb +125 -125
  30. data/lib/nucleus/adapters/v1/openshift_v2/regions.rb +58 -58
  31. data/lib/nucleus/adapters/v1/openshift_v2/services.rb +173 -173
  32. data/lib/nucleus/adapters/v1/openshift_v2/vars.rb +49 -49
  33. data/lib/nucleus/adapters/v1/stub_adapter.rb +464 -464
  34. data/lib/nucleus/core/adapter_extensions/auth/auth_client.rb +44 -44
  35. data/lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb +53 -53
  36. data/lib/nucleus/core/adapter_extensions/auth/http_basic_auth_client.rb +3 -3
  37. data/lib/nucleus/core/adapter_extensions/auth/o_auth2_auth_client.rb +95 -95
  38. data/lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb +36 -36
  39. data/lib/nucleus/core/adapter_extensions/http_client.rb +5 -5
  40. data/lib/nucleus/core/common/files/archive_extractor.rb +1 -1
  41. data/lib/nucleus/core/common/files/archiver.rb +2 -2
  42. data/lib/nucleus/core/file_handling/file_manager.rb +64 -64
  43. data/lib/nucleus/core/file_handling/git_deployer.rb +133 -133
  44. data/lib/nucleus/core/import/adapter_configuration.rb +53 -53
  45. data/lib/nucleus/scripts/initialize_config_defaults.rb +26 -26
  46. data/lib/nucleus/version.rb +1 -1
  47. data/nucleus.gemspec +2 -2
  48. data/spec/integration/api/auth_spec.rb +3 -3
  49. data/spec/spec_helper.rb +98 -98
  50. data/spec/test_suites.rake +1 -1
  51. data/spec/unit/adapters/git_deployer_spec.rb +262 -262
  52. data/spec/unit/common/helpers/auth_helper_spec.rb +1 -1
  53. data/tasks/evaluation.rake +1 -1
  54. data/wiki/adapter_tests.md +0 -7
  55. data/wiki/implement_new_adapter.md +1 -1
  56. metadata +4 -20
  57. data/config/adapters/cloud_control.yml +0 -32
  58. data/lib/nucleus/adapters/v1/cloud_control/application.rb +0 -108
  59. data/lib/nucleus/adapters/v1/cloud_control/authentication.rb +0 -27
  60. data/lib/nucleus/adapters/v1/cloud_control/buildpacks.rb +0 -23
  61. data/lib/nucleus/adapters/v1/cloud_control/cloud_control.rb +0 -153
  62. data/lib/nucleus/adapters/v1/cloud_control/data.rb +0 -76
  63. data/lib/nucleus/adapters/v1/cloud_control/domains.rb +0 -68
  64. data/lib/nucleus/adapters/v1/cloud_control/lifecycle.rb +0 -27
  65. data/lib/nucleus/adapters/v1/cloud_control/log_poller.rb +0 -71
  66. data/lib/nucleus/adapters/v1/cloud_control/logs.rb +0 -103
  67. data/lib/nucleus/adapters/v1/cloud_control/regions.rb +0 -32
  68. data/lib/nucleus/adapters/v1/cloud_control/scaling.rb +0 -17
  69. data/lib/nucleus/adapters/v1/cloud_control/semantic_errors.rb +0 -31
  70. data/lib/nucleus/adapters/v1/cloud_control/services.rb +0 -162
  71. data/lib/nucleus/adapters/v1/cloud_control/token.rb +0 -17
  72. data/lib/nucleus/adapters/v1/cloud_control/vars.rb +0 -88
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a510c433f1548f80c42ce3d32804e73b7c71640
4
- data.tar.gz: 46ff6af1d76e37cc945ea45a455968dded34533e
3
+ metadata.gz: 07e7129693ceadca0091a9e0f2e47260aa1ada94
4
+ data.tar.gz: b6383e24f2e138f8f9d6062546bf0663de185f2d
5
5
  SHA512:
6
- metadata.gz: 44ffe6b03100aebabed7d6dbbc25cf714252b5985bbbc8708620688b84367082bdd2b39af34aa0c32094f0b155cf687a5e182385686dc505fbc6c1ab0f9a4bb4
7
- data.tar.gz: 5668c6da41e685a1e6b65e0d7d6c0f84a93ac70d1f936e89227aa93558c76d949ffa2a8d7164ba18bb4ec68a7ce7692874279201dae9476ec7613f94e6ddb239
6
+ metadata.gz: 27460f6056c47b7faaa142478bff8990b51bd3ac580b3e3b782c94825fca2edf987f853e64ddae9778e07f0271375fb4303be96313e6c057ce8c8bcecdd29dc9
7
+ data.tar.gz: 276f1fff90199a3c0291279381eca3782182138df6c928dd8b452aae18d392b2c4df92bd2f7e237bdaecd55dac266fc2e10e34fddd8176942511c4bff6e9d72e
@@ -36,6 +36,9 @@ Style/Documentation:
36
36
  Style/AlignHash:
37
37
  Enabled: false
38
38
 
39
+ Style/GuardClause:
40
+ Enabled: false
41
+
39
42
  Style/ExtraSpacing:
40
43
  Exclude:
41
44
  - nucleus.gemspec
@@ -8,9 +8,16 @@ This project adheres to [Semantic Versioning](http://semver.org/).
8
8
 
9
9
  ### Fixed
10
10
 
11
+ ### Removed
12
+
13
+
14
+ ## [0.3.1] - 2016-03-07
11
15
 
12
16
  ### Removed
17
+ * Removed cloudControl adapter due to bankruptcy of the vendor
13
18
 
19
+ ## [0.3.0] - 2016-03-07
20
+ * Yanked due to unnecessary Gem contents increasing the overall file size
14
21
 
15
22
  ## [0.2.0] - 2016-01-22
16
23
 
@@ -31,3 +38,5 @@ This project adheres to [Semantic Versioning](http://semver.org/).
31
38
 
32
39
 
33
40
  [0.1.0]: https://github.com/stefan-kolb/nucleus/releases/tag/0.1.0
41
+ [0.2.0]: https://github.com/stefan-kolb/nucleus/releases/tag/0.2.0
42
+ [0.3.0]: https://github.com/stefan-kolb/nucleus/releases/tag/0.3.0
data/README.md CHANGED
@@ -34,7 +34,6 @@ It provides fully compliant [swagger](http://swagger.io/) schemas that serve for
34
34
  * [Heroku](#heroku)
35
35
  * [Cloud Foundry v2](#cloud-foundry-v2)
36
36
  * [Openshift v2](#openshift-v2)
37
- * [cloudControl](#cloudcontrol)
38
37
  * [Configuration](#configuration)
39
38
  * [Vendors, Providers and Endpoints](#vendors-providers-and-endpoints)
40
39
  * [Application configuration](#application-configuration)
@@ -57,8 +56,6 @@ It provides fully compliant [swagger](http://swagger.io/) schemas that serve for
57
56
  - [AppFog][appfog], [Anynines][anynines], [IBM Bluemix][bluemix], [Pivotal Web Services][pivotal_ws], [HP Helion][hp_helion]
58
57
  - [Openshift][openshift_v2] (v2)
59
58
  - [OpenShift Online][openshift_online], [getup Cloud][getup]
60
- - [cloudControl][cloudcontrol]
61
- - [dotCloud][dotcloud], [Cloud&Heat App Elevator][cloud&heat], [exoscale Apps][exoscale]
62
59
 
63
60
  [heroku]: https://www.heroku.com
64
61
 
@@ -73,11 +70,6 @@ It provides fully compliant [swagger](http://swagger.io/) schemas that serve for
73
70
  [openshift_online]: https://www.openshift.com/features/index.html
74
71
  [getup]: https://getupcloud.com/index_en.html
75
72
 
76
- [cloudcontrol]: https://www.cloudcontrol.com
77
- [dotcloud]: https://www.dotcloud.com/
78
- [cloud&heat]: https://www.cloudandheat.com/de/paas.html
79
- [exoscale]: https://www.exoscale.ch/add-on/apps/
80
-
81
73
  More information on the vendors and the associated adapter can be found in the [adapters section](#adapters).
82
74
 
83
75
  ## Usage
@@ -186,13 +178,13 @@ resolver.adapters
186
178
  ```
187
179
 
188
180
  ```ruby
189
- { "cloudcontrol"=>Nucleus::Adapters::V1::CloudControl, "cloud_foundry_v2"=>Nucleus::Adapters::V1::CloudFoundryV2, "heroku"=>Nucleus::Adapters::V1::Heroku, "openshift_v2"=>Nucleus::Adapters::V1::OpenshiftV2 }
181
+ { "cloud_foundry_v2"=>Nucleus::Adapters::V1::CloudFoundryV2, "heroku"=>Nucleus::Adapters::V1::Heroku, "openshift_v2"=>Nucleus::Adapters::V1::OpenshiftV2 }
190
182
  ```
191
183
 
192
184
  5) Load your desired adapter implementation:
193
185
 
194
186
  ```ruby
195
- adapter = resolver.load('cloudcontrol', 'api.cloudcontrol.com', 'your_username', 'your_password')
187
+ adapter = resolver.load('cloud_foundry_v2', 'api.pivotal.io', 'your_username', 'your_password')
196
188
  ```
197
189
 
198
190
  By default, the adapter will be populated with the default configuration options that are defined in the vendor's configuration for the selected endpoint_url.
@@ -263,47 +255,47 @@ This list can be auto-generated via:
263
255
  $ bundle exec rake evaluation:compatibility:markdown
264
256
  ```
265
257
 
266
- **State: 10/14/2015**
267
-
268
- Method / Vendor|cloudControl|Cloud Foundry v2|Heroku|Openshift v2
269
- :--|:-:|:-:|:-:|:-:
270
- auth_client|✓|✓|✓|✓
271
- regions|✓|✓|✓|✓
272
- region|✓|✓|✓|✓
273
- applications|✓|✓|✓|✓
274
- application|✓|✓|✓|✓
275
- create_application|✓|✓|✓|✓
276
- update_application|✗|✓|✓|✗
277
- delete_application|✓|✓|✓|✓
278
- domains|✓|✓|✓|✓
279
- domain|✓|✓|✓|✓
280
- create_domain|✓|✓|✓|✓
281
- delete_domain|✓|✓|✓|✓
282
- env_vars|✓|✓|✓|✓
283
- env_var|✓|✓|✓|✓
284
- create_env_var|✓|✓|✓|✓
285
- update_env_var|✓|✓|✓|✓
286
- delete_env_var|✓|✓|✓|✓
287
- start|✓|✓|✓|✓
288
- stop|✗|✓|✓|✓
289
- restart|✗|✓|✓|✓
290
- deploy|✓|✓|✓|✓
291
- rebuild|✓|✓|✓|✓
292
- download|✓|✓|✓|✓
293
- scale|✓|✓|✓|✓
294
- log?|✓|✓|✓|✓
295
- logs|✓|✓|✓|✓
296
- log_entries|✓|✓|✓|✓
297
- tail|✓|✓|✓|✗
298
- services|✓|✓|✓|✓
299
- service|✓|✓|✓|✓
300
- service_plans|✓|✓|✓|✓
301
- service_plan|✓|✓|✓|✓
302
- installed_services|✓|✓|✓|✓
303
- installed_service|✓|✓|✓|✓
304
- add_service|✓|✓|✓|✓
305
- change_service|✓|✓|✓|✗
306
- remove_service|✓|✓|✓|✓
258
+ **State: 3/7/2016**
259
+
260
+ Method / Vendor|Cloud Foundry v2|Heroku|Openshift v2
261
+ :--|:-:|:-:|:-:
262
+ auth_client|✓|✓|✓
263
+ regions|✓|✓|✓
264
+ region|✓|✓|✓
265
+ applications|✓|✓|✓
266
+ application|✓|✓|✓
267
+ create_application|✓|✓|✓
268
+ update_application|✓|✓|✗
269
+ delete_application|✓|✓|✓
270
+ domains|✓|✓|✓
271
+ domain|✓|✓|✓
272
+ create_domain|✓|✓|✓
273
+ delete_domain|✓|✓|✓
274
+ env_vars|✓|✓|✓
275
+ env_var|✓|✓|✓
276
+ create_env_var|✓|✓|✓
277
+ update_env_var|✓|✓|✓
278
+ delete_env_var|✓|✓|✓
279
+ start|✓|✓|✓
280
+ stop|✓|✓|✓
281
+ restart|✓|✓|✓
282
+ deploy|✓|✓|✓
283
+ rebuild|✓|✓|✓
284
+ download|✓|✓|✓
285
+ scale|✓|✓|✓
286
+ log?|✓|✓|✓
287
+ logs|✓|✓|✓
288
+ log_entries|✓|✓|✓
289
+ tail|✓|✓|✓
290
+ services|✓|✓|✓
291
+ service|✓|✓|✓
292
+ service_plans|✓|✓|✓
293
+ service_plan|✓|✓|✓
294
+ installed_services|✓|✓|✓
295
+ installed_service|✓|✓|✓
296
+ add_service|✓|✓|✓
297
+ change_service|✓|✓|✗
298
+ remove_service|✓|✓|✓
307
299
 
308
300
  ### Core constructs
309
301
 
@@ -477,27 +469,6 @@ further configuration are not supported as of now.
477
469
 
478
470
  Recording is really slow. Even worse, actions quite often fail with Openshift internal timeouts.
479
471
 
480
- ### cloudControl
481
-
482
- Providers: [cloudControl][cloudcontrol], [dotCloud][dotcloud], [Cloud&Heat App Elevator][cloud&heat], [exoscale Apps][exoscale]
483
-
484
- #### Issues
485
-
486
- **Application update**
487
-
488
- An application can't be updated, the `name` and `runtimes` can't be changed once created.
489
-
490
- **Application lifecycle**
491
-
492
- Applications on cloudControl can't be explicitly stopped or restarted.
493
- They start after a successful build of the application, which is therefore postponed to the first invocation of the start operation.
494
- Applications only stop once the corresponding _deployment_ has been deleted.
495
-
496
- **Logs**
497
-
498
- Log messages, for instance the request entries, do not appear instantly in the log.
499
- It may take some seconds or even minutes for them to show up.
500
-
501
472
  ## Configuration
502
473
 
503
474
  Several parts of Nucleus can be configured, e.g. whether to persist your data or always start with a clean instance.
@@ -5,7 +5,7 @@ module Nucleus
5
5
  include Nucleus::UrlConverter
6
6
 
7
7
  def initialize(requested_version)
8
- fail 'No such version supported' unless Nucleus::VersionDetector.api_versions.include?(requested_version)
8
+ raise 'No such version supported' unless Nucleus::VersionDetector.api_versions.include?(requested_version)
9
9
  @api_version = requested_version
10
10
  end
11
11
 
@@ -33,7 +33,7 @@ module Nucleus
33
33
  # @return [Nucleus::Adapters::BaseAdapter] loaded adapter implementation
34
34
  def load(vendor, username, password, options = {})
35
35
  setup
36
- fail StandardError, "Could not find adapter for vendor '#{vendor}'" unless @adapters.key?(vendor)
36
+ raise StandardError, "Could not find adapter for vendor '#{vendor}'" unless @adapters.key?(vendor)
37
37
 
38
38
  # load the endpoint's HTTPS enabled API URL
39
39
  endpoint_url = load_endpoint(vendor, options)
@@ -73,7 +73,7 @@ module Nucleus
73
73
  # use default endpoint
74
74
  endpoint_url = @configurations[vendor].keys.first
75
75
  else
76
- fail StandardError, "Could not identify an API endpoint for the vendor '#{vendor}'. "\
76
+ raise StandardError, "Could not identify an API endpoint for the vendor '#{vendor}'. "\
77
77
  "Please specify the API URL to use with the ':api_url' option."
78
78
  end
79
79
 
@@ -1,109 +1,109 @@
1
- module Nucleus
2
- # The {Adapters} module combines all application logic to communicate with the different vendor platforms
3
- # and created the unified API.
4
- module Adapters
5
- # The {BaseAdapter} is an abstract class that shall be extended by all actual Adapters.
6
- # It provides methods to common functionality:<br>
7
- # * authentication (+cache)
8
- # * http client with general error handling
9
- # * native platform API calls
10
- # @abstract
11
- class BaseAdapter
12
- include HttpClient
13
- include HttpTailClient
14
- include Logging
15
-
16
- attr_reader :endpoint_url
17
-
18
- def initialize(endpoint_url, endpoint_app_domain = nil, check_certificates = true)
19
- fail ArgumentError, "'endpoint_url' must be a valid URL" unless endpoint_url =~ /\A#{URI.regexp(['https'])}\z/
20
- @endpoint_url = endpoint_url
21
- @endpoint_app_domain = endpoint_app_domain
22
- @check_certificates = check_certificates
23
- end
24
-
25
- # thread-based cache for the api authorization headers
26
- thread_config_accessor :auth_objects_cache, default: {}
27
-
28
- # Cache the auth information.
29
- # @param [String] key cache key
30
- # @param [Nucleus::Adapters::AuthClient] auth_object authentication client to be cached
31
- # @return [void]
32
- def cache(key, auth_object)
33
- auth_objects_cache[key] = auth_object
34
- end
35
-
36
- # Are there cached information for this key?
37
- # @param [String] key cache key
38
- # @return [true, false] true if has cached auth info, else false
39
- def cache?(key)
40
- auth_objects_cache.key? key
41
- end
42
-
43
- # Get the currently cached authentication object.
44
- # @param [String] key cache key
45
- # @return [Hash<String,String>, Nucleus::Adapters::AuthClient] cached authentication client
46
- def cached(key)
47
- return nil unless cache?(key)
48
- auth_objects_cache[key]
49
- end
50
-
51
- # Create the cache key for the username / password combination and save it in the {::RequestStore} to make it
52
- # available throughout the current request.
53
- # @param [String] username the username for the authentication
54
- # @param [String] password the password for the authentication
55
- # @return [String] calculated hash key for the input values
56
- def cache_key(username, password)
57
- # calculate the cache only once per request
58
- return RequestStore.store[:cache_key] if RequestStore.exist?(:cache_key)
59
- key = Digest::SHA256.hexdigest "#{endpoint_url}#{username}:#{password}"
60
- RequestStore.store[:cache_key] = key
61
- key
62
- end
63
-
64
- # Get the cached authentication object and retrieve the presumably valid authentication header.
65
- # @return [Hash<String,String>] hash including a valid authentication header
66
- def headers
67
- auth_object = auth_objects_cache[RequestStore.store[:cache_key]]
68
- # AuthClient, generates the header for us
69
- auth_object.auth_header
70
- end
71
-
72
- # Execute an API call, targeted directly against the vendors API.
73
- # @param [Symbol] method http method to use, one of: [:GET, :POST, :DELETE, :PUT, :PATCH]
74
- # @param [String] path url path to append to the endpoint's URL
75
- # @param [Hash] params body params to use for PATCH, :PUT and :POST requests
76
- # @return [Object] the actual response body of the vendor platform
77
- def endpoint_call(method, path, params)
78
- case method
79
- when :GET
80
- get(path, native_call: true).body
81
- when :POST
82
- post(path, native_call: true, body: params).body
83
- when :DELETE
84
- delete(path, native_call: true).body
85
- when :PUT
86
- put(path, native_call: true, body: params).body
87
- when :PATCH
88
- patch(path, native_call: true, body: params).body
89
- else
90
- fail AdapterRequestError, 'Unsupported adapter call method. Allowed are: GET, POST, PATCH, PUT, DELETE'
91
- end
92
- end
93
-
94
- # Fail with a {Errors::PlatformSpecificSemanticError} error and format the error message to include the values
95
- # that are passed in the params. Requires the adapter to provide a +semantic_error_messages+ method, which shall
96
- # return a Hash with the platform specific semantic errors.
97
- # @param [Symbol] error_name error that shall be returned
98
- # @param [Array<String>] params values that are to be included in the error message template
99
- # @raise [Errors::PlatformSpecificSemanticError]
100
- def fail_with(error_name, params = nil)
101
- unless respond_to?(:semantic_error_messages)
102
- fail StandardError 'Invalid adapter implementation, no :semantic_error_messages method provided'
103
- end
104
- error = semantic_error_messages[error_name]
105
- fail Errors::PlatformSpecificSemanticError.new(error[:message] % params, error[:code])
106
- end
107
- end
108
- end
109
- end
1
+ module Nucleus
2
+ # The {Adapters} module combines all application logic to communicate with the different vendor platforms
3
+ # and created the unified API.
4
+ module Adapters
5
+ # The {BaseAdapter} is an abstract class that shall be extended by all actual Adapters.
6
+ # It provides methods to common functionality:<br>
7
+ # * authentication (+cache)
8
+ # * http client with general error handling
9
+ # * native platform API calls
10
+ # @abstract
11
+ class BaseAdapter
12
+ include HttpClient
13
+ include HttpTailClient
14
+ include Logging
15
+
16
+ attr_reader :endpoint_url
17
+
18
+ def initialize(endpoint_url, endpoint_app_domain = nil, check_certificates = true)
19
+ raise ArgumentError, "'endpoint_url' must be a valid URL" unless endpoint_url =~ /\A#{URI.regexp(['https'])}\z/
20
+ @endpoint_url = endpoint_url
21
+ @endpoint_app_domain = endpoint_app_domain
22
+ @check_certificates = check_certificates
23
+ end
24
+
25
+ # thread-based cache for the api authorization headers
26
+ thread_config_accessor :auth_objects_cache, default: {}
27
+
28
+ # Cache the auth information.
29
+ # @param [String] key cache key
30
+ # @param [Nucleus::Adapters::AuthClient] auth_object authentication client to be cached
31
+ # @return [void]
32
+ def cache(key, auth_object)
33
+ auth_objects_cache[key] = auth_object
34
+ end
35
+
36
+ # Are there cached information for this key?
37
+ # @param [String] key cache key
38
+ # @return [true, false] true if has cached auth info, else false
39
+ def cache?(key)
40
+ auth_objects_cache.key? key
41
+ end
42
+
43
+ # Get the currently cached authentication object.
44
+ # @param [String] key cache key
45
+ # @return [Hash<String,String>, Nucleus::Adapters::AuthClient] cached authentication client
46
+ def cached(key)
47
+ return nil unless cache?(key)
48
+ auth_objects_cache[key]
49
+ end
50
+
51
+ # Create the cache key for the username / password combination and save it in the {::RequestStore} to make it
52
+ # available throughout the current request.
53
+ # @param [String] username the username for the authentication
54
+ # @param [String] password the password for the authentication
55
+ # @return [String] calculated hash key for the input values
56
+ def cache_key(username, password)
57
+ # calculate the cache only once per request
58
+ return RequestStore.store[:cache_key] if RequestStore.exist?(:cache_key)
59
+ key = Digest::SHA256.hexdigest "#{endpoint_url}#{username}:#{password}"
60
+ RequestStore.store[:cache_key] = key
61
+ key
62
+ end
63
+
64
+ # Get the cached authentication object and retrieve the presumably valid authentication header.
65
+ # @return [Hash<String,String>] hash including a valid authentication header
66
+ def headers
67
+ auth_object = auth_objects_cache[RequestStore.store[:cache_key]]
68
+ # AuthClient, generates the header for us
69
+ auth_object.auth_header
70
+ end
71
+
72
+ # Execute an API call, targeted directly against the vendors API.
73
+ # @param [Symbol] method http method to use, one of: [:GET, :POST, :DELETE, :PUT, :PATCH]
74
+ # @param [String] path url path to append to the endpoint's URL
75
+ # @param [Hash] params body params to use for PATCH, :PUT and :POST requests
76
+ # @return [Object] the actual response body of the vendor platform
77
+ def endpoint_call(method, path, params)
78
+ case method
79
+ when :GET
80
+ get(path, native_call: true).body
81
+ when :POST
82
+ post(path, native_call: true, body: params).body
83
+ when :DELETE
84
+ delete(path, native_call: true).body
85
+ when :PUT
86
+ put(path, native_call: true, body: params).body
87
+ when :PATCH
88
+ patch(path, native_call: true, body: params).body
89
+ else
90
+ raise AdapterRequestError, 'Unsupported adapter call method. Allowed are: GET, POST, PATCH, PUT, DELETE'
91
+ end
92
+ end
93
+
94
+ # Fail with a {Errors::PlatformSpecificSemanticError} error and format the error message to include the values
95
+ # that are passed in the params. Requires the adapter to provide a +semantic_error_messages+ method, which shall
96
+ # return a Hash with the platform specific semantic errors.
97
+ # @param [Symbol] error_name error that shall be returned
98
+ # @param [Array<String>] params values that are to be included in the error message template
99
+ # @raise [Errors::PlatformSpecificSemanticError]
100
+ def fail_with(error_name, params = nil)
101
+ unless respond_to?(:semantic_error_messages)
102
+ raise StandardError 'Invalid adapter implementation, no :semantic_error_messages method provided'
103
+ end
104
+ error = semantic_error_messages[error_name]
105
+ raise Errors::PlatformSpecificSemanticError.new(error[:message] % params, error[:code])
106
+ end
107
+ end
108
+ end
109
+ end