From b84bfa18fe4fca9395582520d55ac75ff374a652 Mon Sep 17 00:00:00 2001 From: Trevor Rowe Date: Mon, 19 Oct 2015 13:45:48 -0700 Subject: [PATCH] Added validation to ec2 region names. 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 --- CHANGELOG.md | 11 +++++++++++ aws-sdk-core/lib/aws-sdk-core.rb | 1 + .../lib/aws-sdk-core/api/customizations.rb | 1 + .../plugins/ec2_region_validation.rb | 17 +++++++++++++++++ aws-sdk-core/spec/aws/ec2/client_spec.rb | 10 ++++++++++ 5 files changed, 40 insertions(+) create mode 100644 aws-sdk-core/lib/aws-sdk-core/plugins/ec2_region_validation.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d325839e21..240521833bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/aws-sdk-core/lib/aws-sdk-core.rb b/aws-sdk-core/lib/aws-sdk-core.rb index 411620076fa..de8641b5f63 100644 --- a/aws-sdk-core/lib/aws-sdk-core.rb +++ b/aws-sdk-core/lib/aws-sdk-core.rb @@ -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' diff --git a/aws-sdk-core/lib/aws-sdk-core/api/customizations.rb b/aws-sdk-core/lib/aws-sdk-core/api/customizations.rb index 8378a2d4233..c65f38725e3 100644 --- a/aws-sdk-core/lib/aws-sdk-core/api/customizations.rb +++ b/aws-sdk-core/lib/aws-sdk-core/api/customizations.rb @@ -80,6 +80,7 @@ def apply_plugins(client_class) plugins('ec2', add: %w( Aws::Plugins::EC2CopyEncryptedSnapshot + Aws::Plugins::EC2RegionValidation )) api('glacier') do |api| diff --git a/aws-sdk-core/lib/aws-sdk-core/plugins/ec2_region_validation.rb b/aws-sdk-core/lib/aws-sdk-core/plugins/ec2_region_validation.rb new file mode 100644 index 00000000000..669a9778a1b --- /dev/null +++ b/aws-sdk-core/lib/aws-sdk-core/plugins/ec2_region_validation.rb @@ -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 diff --git a/aws-sdk-core/spec/aws/ec2/client_spec.rb b/aws-sdk-core/spec/aws/ec2/client_spec.rb index e83bf4f2967..d230280a2a2 100644 --- a/aws-sdk-core/spec/aws/ec2/client_spec.rb +++ b/aws-sdk-core/spec/aws/ec2/client_spec.rb @@ -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