Skip to content

Commit

Permalink
DEMO ALTERNATIVE: use cfg.endpoint_resolver to resolve config.endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods committed Oct 9, 2024
1 parent b9a04ed commit 13f2c57
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
33 changes: 24 additions & 9 deletions gems/aws-sdk-core/lib/aws-sdk-core/plugins/regional_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ class RegionalEndpoint < Seahorse::Client::Plugin
resolve_endpoint(cfg)
end

DefaultEndpointParameters = Struct.new(
:region,
:use_dual_stack,
:use_fips,
:endpoint
) do
def method_missing(method_name, *_args)
nil
end
end

def after_initialize(client)
region = client.config.region
raise Errors::MissingRegionError if region.nil? || region == ''
Expand Down Expand Up @@ -150,16 +161,20 @@ def resolve_endpoint(cfg)
# that a custom endpoint has NOT been configured by the user
cfg.override_config(:regional_endpoint, true)

# preserve legacy (pre EP2) client.config.endpoint still resolves a value
# when needed.
struct = cfg.instance_variable_get(:@struct)
if struct
# need a separate proc to get namespace/private access to resolve_legacy_endpoint
b = proc { resolve_legacy_endpoint(struct) }
struct.define_singleton_method(:endpoint) { @endpoint ||= b.call }
nil
# preserve legacy (pre EP2) client.config.endpoint
if cfg.respond_to?(:endpoint_provider) && (endpoint_provider = cfg.endpoint_provider)
params = DefaultEndpointParameters.new
params.region = cfg.region
params.use_dual_stack = cfg.use_dualstack_endpoint
params.use_fips = cfg.use_fips_endpoint
begin
endpoint = endpoint_provider.resolve_endpoint(params)
endpoint.url
rescue ArgumentError
# fallback to legacy
resolve_legacy_endpoint(cfg)
end
else
# backup in case internal details of config resolver change
resolve_legacy_endpoint(cfg)
end
end
Expand Down
14 changes: 6 additions & 8 deletions gems/aws-sdk-core/lib/seahorse/client/plugins/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def add_handlers(handlers, config)
end

def after_initialize(client)
# validate endpoint only if user set a custom endpoint
return if client.config.respond_to?(:regional_endpoint) && client.config.regional_endpoint

endpoint = client.config.endpoint
if endpoint.nil?
msg = "missing required option `:endpoint'"
raise ArgumentError, msg
end

endpoint = URI.parse(endpoint.to_s)
if URI::HTTPS === endpoint or URI::HTTP === endpoint
client.config.endpoint = endpoint
Expand All @@ -40,11 +42,7 @@ def after_initialize(client)
class Handler < Client::Handler

def call(context)
if !context.config.respond_to?(:regional_endpoint) || # generic
!context.config.regional_endpoint || # custom endpoint
!context.config.respond_to?(:endpoint_provider) # legacy/pre-ep2/no rules
context.http_request.endpoint = URI.parse(context.config.endpoint.to_s)
end
context.http_request.endpoint = URI.parse(context.config.endpoint.to_s)
@handler.call(context)
end

Expand Down
3 changes: 3 additions & 0 deletions gems/aws-sdk-s3/lib/aws-sdk-s3/endpoint_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def resolve_endpoint(parameters)
use_arn_region = parameters.use_arn_region
use_s3_express_control_endpoint = parameters.use_s3_express_control_endpoint
disable_s3_express_session_auth = parameters.disable_s3_express_session_auth
require 'byebug'
byebug

if Aws::Endpoints::Matchers.set?(region)
if Aws::Endpoints::Matchers.boolean_equals?(accelerate, true) && Aws::Endpoints::Matchers.boolean_equals?(use_fips, true)
raise ArgumentError, "Accelerate cannot be used with FIPS"
Expand Down

0 comments on commit 13f2c57

Please sign in to comment.