Skip to content

Commit ab1503e

Browse files
authored
Instance plugins (#3051)
1 parent e10c7ee commit ab1503e

File tree

12 files changed

+206
-99
lines changed

12 files changed

+206
-99
lines changed

build_tools/aws-sdk-code-generator/spec/interfaces/resources_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
describe 'Interfaces' do
66

77
before(:all) do
8-
# TODO : support Aws.config[:sample] = { ... }
98
@tmpdir = SpecHelper.generate_service(['Sample'], multiple_files: true)
109
end
1110

build_tools/aws-sdk-code-generator/templates/client_class.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ module {{module_name}}
3737
{{#client_constructor}}
3838
# @overload initialize(options)
3939
# @param [Hash] options
40+
#
41+
# @option options [Array<Seahorse::Client::Plugin>] :plugins ([]])
42+
# A list of plugins to apply to the client. Each plugin is either a
43+
# class name or an instance of a plugin class.
44+
#
4045
{{>documentation}}
4146
{{/client_constructor}}
4247
def initialize(*args)

build_tools/services.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class ServiceEnumerator
1010
MANIFEST_PATH = File.expand_path('../../services.json', __FILE__)
1111

1212
# Minimum `aws-sdk-core` version for new gem builds
13-
MINIMUM_CORE_VERSION = "3.197.0"
13+
MINIMUM_CORE_VERSION = "3.198.0"
1414

1515
# Minimum `aws-sdk-core` version for new S3 gem builds
16-
MINIMUM_CORE_VERSION_S3 = "3.197.0"
16+
MINIMUM_CORE_VERSION_S3 = "3.198.0"
1717

1818
EVENTSTREAM_PLUGIN = "Aws::Plugins::EventStreamConfiguration"
1919

gems/aws-sdk-core/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Feature - Support `:plugins` option on all Clients or using `Aws.config[:plugins]`.
5+
46
3.197.2 (2024-06-20)
57
------------------
68

gems/aws-sdk-core/lib/aws-sdk-core/plugins/global_configuration.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module Plugins
4343
# @api private
4444
class GlobalConfiguration < Seahorse::Client::Plugin
4545

46-
@identifiers = Set.new()
46+
@identifiers = Set.new
4747

4848
# @api private
4949
def before_initialize(client_class, options)
@@ -55,17 +55,18 @@ def before_initialize(client_class, options)
5555
private
5656

5757
def apply_service_defaults(client_class, options)
58-
if defaults = Aws.config[client_class.identifier]
59-
defaults.each do |option_name, default|
60-
options[option_name] = default unless options.key?(option_name)
61-
end
58+
return unless (defaults = Aws.config[client_class.identifier])
59+
60+
defaults.each do |option_name, default|
61+
options[option_name] = default unless options.key?(option_name)
6262
end
6363
end
6464

65-
def apply_aws_defaults(client_class, options)
65+
def apply_aws_defaults(_client_class, options)
6666
Aws.config.each do |option_name, default|
6767
next if self.class.identifiers.include?(option_name)
6868
next if options.key?(option_name)
69+
6970
options[option_name] = default
7071
end
7172
end
@@ -80,9 +81,7 @@ def add_identifier(identifier)
8081

8182
# @return [Set<String>]
8283
# @api private
83-
def identifiers
84-
@identifiers
85-
end
84+
attr_reader :identifiers
8685

8786
end
8887
end

gems/aws-sdk-core/lib/aws-sdk-core/plugins/retry_errors.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ class RetryErrors < Seahorse::Client::Plugin
113113
functionality of `standard` mode along with automatic client side
114114
throttling. This is a provisional mode that may change behavior
115115
in the future.
116-
117116
DOCS
118117
resolve_retry_mode(cfg)
119118
end

gems/aws-sdk-core/lib/seahorse/client/base.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'thread'
4-
53
module Seahorse
64
module Client
75
class Base
@@ -60,6 +58,7 @@ def operation_names
6058
def build_config(plugins, options)
6159
config = Configuration.new
6260
config.add_option(:api)
61+
config.add_option(:plugins)
6362
plugins.each do |plugin|
6463
plugin.add_options(config) if plugin.respond_to?(:add_options)
6564
end
@@ -96,9 +95,9 @@ def context_for(operation_name, params)
9695
class << self
9796

9897
def new(options = {})
99-
plugins = build_plugins
10098
options = options.dup
101-
before_initialize(plugins, options)
99+
plugins = build_plugins(self.plugins + options.fetch(:plugins, []))
100+
plugins = before_initialize(plugins, options)
102101
client = allocate
103102
client.send(:initialize, plugins, options)
104103
client
@@ -209,17 +208,28 @@ def define_operation_methods
209208
include(operations_module)
210209
end
211210

212-
def build_plugins
211+
def build_plugins(plugins)
213212
plugins.map { |plugin| plugin.is_a?(Class) ? plugin.new : plugin }
214213
end
215214

216215
def before_initialize(plugins, options)
217-
plugins.each do |plugin|
218-
plugin.before_initialize(self, options) if plugin.respond_to?(:before_initialize)
216+
queue = Queue.new
217+
plugins.each { |plugin| queue.push(plugin) }
218+
until queue.empty?
219+
plugin = queue.pop
220+
next unless plugin.respond_to?(:before_initialize)
221+
222+
plugins_before = options.fetch(:plugins, [])
223+
plugin.before_initialize(self, options)
224+
plugins_after = build_plugins(options.fetch(:plugins, []) - plugins_before)
225+
# Plugins with before_initialize can add other plugins
226+
plugins_after.each { |p| queue.push(p); plugins << p }
219227
end
228+
plugins
220229
end
221230

222231
def inherited(subclass)
232+
super
223233
subclass.instance_variable_set('@plugins', PluginList.new(@plugins))
224234
end
225235

gems/aws-sdk-core/lib/seahorse/client/plugins/endpoint.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Endpoint < Plugin
1717
'http://example.com'
1818
'https://example.com'
1919
'http://example.com:123'
20-
2120
DOCS
2221

2322
def add_handlers(handlers, config)

gems/aws-sdk-core/spec/aws/plugins/global_configuration_spec.rb

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,23 @@ module Plugins
1313
option(:property, 'plugin-default')
1414
end)
1515

16+
let(:plugin) do
17+
p = double('plugin')
18+
allow(p).to receive(:is_a?).with(kind_of(Class)).and_return(false)
19+
p
20+
end
21+
22+
let(:options) do
23+
{
24+
region: 'us-east-1',
25+
credentials: Credentials.new('akid', 'secret'),
26+
plugins: [plugin]
27+
}
28+
end
29+
1630
before(:each) do
1731
Aws.config.clear
18-
Aws.config[:region] = 'us-east-1'
19-
Aws.config[:credentials] = Credentials.new('akid', 'secret')
32+
Aws.config = options
2033
end
2134

2235
before(:all) do
@@ -45,6 +58,43 @@ module Plugins
4558
expect(GlobalConfigClient.new(property: 'arg').config.property).to eq('arg')
4659
end
4760

61+
context 'plugins' do
62+
it 'instructs plugins to #before_initialize' do
63+
expect(plugin).to receive(:before_initialize).with(GlobalConfigClient, options)
64+
GlobalConfigClient.new
65+
end
66+
67+
it 'instructs plugins to #add_options' do
68+
expect(plugin).to receive(:add_options)
69+
GlobalConfigClient.new
70+
end
71+
72+
it 'instructs plugins to #add_handlers' do
73+
expect(plugin).to receive(:add_handlers).
74+
with(kind_of(Seahorse::Client::HandlerList), kind_of(Struct))
75+
GlobalConfigClient.new
76+
end
77+
78+
it 'instructs plugins to #after_initialize' do
79+
expect(plugin).to receive(:after_initialize).with(kind_of(Seahorse::Client::Base))
80+
GlobalConfigClient.new
81+
end
82+
83+
it 'does not call methods that plugin does not respond to' do
84+
plugin = Object.new
85+
allow(plugin).to receive(:respond_to?).with(:before_initialize).and_return(false)
86+
allow(plugin).to receive(:respond_to?).with(:add_options).and_return(false)
87+
allow(plugin).to receive(:respond_to?).with(:add_handlers).and_return(false)
88+
allow(plugin).to receive(:respond_to?).with(:after_initialize).and_return(false)
89+
expect(plugin).not_to receive(:before_initialize)
90+
expect(plugin).not_to receive(:add_options)
91+
expect(plugin).not_to receive(:add_handlers)
92+
expect(plugin).not_to receive(:after_initialize)
93+
Aws.config[:plugins] = [plugin]
94+
GlobalConfigClient.new
95+
end
96+
end
97+
4898
end
4999
end
50100
end

0 commit comments

Comments
 (0)