Skip to content

Commit

Permalink
Refactor to Use Hash#fetch
Browse files Browse the repository at this point in the history
Additionally, supports switching the endpoints.json file to another
path.
  • Loading branch information
awood45 committed Apr 7, 2016
1 parent abe39cc commit 91c9876
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 57 deletions.
1 change: 1 addition & 0 deletions aws-sdk-core/lib/aws-sdk-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ module Aws
autoload :ParamConverter, 'aws-sdk-core/param_converter'
autoload :ParamValidator, 'aws-sdk-core/param_validator'
autoload :RefreshingCredentials, 'aws-sdk-core/refreshing_credentials'
autoload :RegionsAndEndpoints, 'aws-sdk-core/regions_and_endpoints'
autoload :Service, 'aws-sdk-core/service'
autoload :SharedCredentials, 'aws-sdk-core/shared_credentials'
autoload :Structure, 'aws-sdk-core/structure'
Expand Down
60 changes: 3 additions & 57 deletions aws-sdk-core/lib/aws-sdk-core/endpoint_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,17 @@ module Aws
# @api private
class EndpointProvider

PATH = File.join(File.dirname(__FILE__), '..', '..', 'endpoints.json')
RULES = Json.load_file(PATH)
RULES = RegionsAndEndpoints.new

class << self

def resolve(region, service)
"https://" + endpoint_for(region, service)
RULES.resolve(region, service)
end

def signing_region(region, service)
partition = get_partition(region)
[
"services", service, "endpoints", region, "credentialScope", "region"
].inject(partition) do |acc, key|
if acc && acc[key]
acc[key]
else
region
end
end
RULES.signing_region(region, service)
end

private
def endpoint_for(region, service)
partition = get_partition(region)
endpoint = default_endpoint(partition, service, region)
if service_cfg = partition["services"][service]
if service_cfg["isRegionalized"] == false
if partition_endpoint = service_cfg["partitionEndpoint"]
region = partition_endpoint
end
end
if service_defaults = service_cfg["defaults"]
if host = service_defaults["hostname"]
endpoint = host
end
end
if endpoint_cfg = service_cfg["endpoints"][region]
if host = endpoint_cfg["hostname"]
endpoint = host
end
end
end
endpoint
end

def default_endpoint(partition, service, region)
hostname_template = partition["defaults"]["hostname"]
hostname_template.sub(
'{region}', region
).sub(
'{service}', service
).sub(
'{dnsSuffix}', partition["dnsSuffix"]
)
end

def get_partition(region)
partition = RULES["partitions"].find do |p|
region.match(p["regionRegex"])
end
partition ||= RULES["partitions"].find { |p| p["partition"] == "aws" }
partition
end

end
end
end
73 changes: 73 additions & 0 deletions aws-sdk-core/lib/aws-sdk-core/regions_and_endpoints.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Aws
# @api private
class RegionsAndEndpoints

attr_reader :rules

PATH = File.join(File.dirname(__FILE__), '..', '..', 'endpoints.json')

def initialize(file = PATH)
@rules = Json.load_file(file)
end

def set_endpoints_file(file)
@rules = Json.load_file(file)
end

def resolve(region, service)
"https://" + endpoint_for(region, service)
end

def signing_region(region, service)
partition = get_partition(region)
config_region = partition.fetch("services", {}).
fetch(service, {}).
fetch("endpoints", {}).
fetch(region, {}).
fetch("credentialScope", {})["region"]
config_region ||= region
config_region
end

private
def endpoint_for(region, service)
partition = get_partition(region)
endpoint = default_endpoint(partition, service, region)
service_cfg = partition.fetch("services", {}).fetch(service, {})

# Check for service-level default endpoint.
endpoint = service_cfg.fetch("defaults", {}).fetch("hostname", endpoint)

# Check for global endpoint.
if service_cfg["isRegionalized"] == false
region = service_cfg.fetch("partitionEndpoint", region)
end

# Check for service/region level endpoint.
endpoint = service_cfg.fetch("endpoints", {}).
fetch(region, {}).fetch("hostname", endpoint)

endpoint
end

def default_endpoint(partition, service, region)
hostname_template = partition["defaults"]["hostname"]
hostname_template.sub(
'{region}', region
).sub(
'{service}', service
).sub(
'{dnsSuffix}', partition["dnsSuffix"]
)
end

def get_partition(region)
partition = @rules["partitions"].find do |p|
region.match(p["regionRegex"])
end
partition ||= @rules["partitions"].find { |p| p["partition"] == "aws" }
partition
end

end
end

0 comments on commit 91c9876

Please sign in to comment.