Skip to content

Commit

Permalink
Added validation to ec2 region names.
Browse files Browse the repository at this point in the history
An argument error is raised if the region name is an availability
zone. This should eliminate the unhelpful networking errors raised
when failing to resolve the endpoint.

Fixes #966
  • Loading branch information
trevorrowe committed Oct 19, 2015
1 parent 42f772d commit b84bfa1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Unreleased Changes
------------------

* Feature - Aws::EC2 - Added helpful error messages when configuring an
`Aws::EC2::Client` with region which is actually an availability zone.
This will prevent users from getting unhelpful networking errors.

```ruby
Aws::EC2::Client.new(region: 'us-east-1a')
#=> raises ArgumentError: :region should be a region name, not an availability zone name; try `us-west-2' instead of `us-west-2a'
```

See [related GitHub issue #966](https://github.com/aws/aws-sdk-ruby/issues/966).

* Issue - Aws::InstanceProfileCredentials - Now retries errors
raised while attempting to parse the expiration time from
instance metadata credentials.
Expand Down
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 @@ -146,6 +146,7 @@ module Plugins
autoload :DynamoDBSimpleAttributes, 'aws-sdk-core/plugins/dynamodb_simple_attributes'
autoload :DynamoDBCRC32Validation, 'aws-sdk-core/plugins/dynamodb_crc32_validation'
autoload :EC2CopyEncryptedSnapshot, 'aws-sdk-core/plugins/ec2_copy_encrypted_snapshot'
autoload :EC2RegionValidation, 'aws-sdk-core/plugins/ec2_region_validation'
autoload :GlacierAccountId, 'aws-sdk-core/plugins/glacier_account_id'
autoload :GlacierApiVersion, 'aws-sdk-core/plugins/glacier_api_version'
autoload :GlacierChecksums, 'aws-sdk-core/plugins/glacier_checksums'
Expand Down
1 change: 1 addition & 0 deletions aws-sdk-core/lib/aws-sdk-core/api/customizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def apply_plugins(client_class)

plugins('ec2', add: %w(
Aws::Plugins::EC2CopyEncryptedSnapshot
Aws::Plugins::EC2RegionValidation
))

api('glacier') do |api|
Expand Down
17 changes: 17 additions & 0 deletions aws-sdk-core/lib/aws-sdk-core/plugins/ec2_region_validation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Aws
module Plugins
# @api private
class EC2RegionValidation < Seahorse::Client::Plugin

def after_initialize(client)
if region = client.config.region
if matches = region.match(/^(\w+-\w+-\d+)[a-z]$/)
msg = ":region option must a region name, not an availability "
msg << "zone name; try `#{matches[1]}' instead of `#{matches[0]}'"
raise ArgumentError, msg
end
end
end
end
end
end
10 changes: 10 additions & 0 deletions aws-sdk-core/spec/aws/ec2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ module EC2

end

describe ':region' do

it 'should not be an availability zone' do
expect {
Client.new(region: 'us-east-1a')
}.to raise_error(ArgumentError)
end

end

describe '#copy_snapshot' do

it 'requires a source shapshot region' do
Expand Down

0 comments on commit b84bfa1

Please sign in to comment.