Skip to content

Commit

Permalink
Favor sdkv2 (#57)
Browse files Browse the repository at this point in the history
* Favor SDK v2

This PR does two things. The first is that in AwsShim, it changes the behavior to favor AWS SDK v2 if it's in the load path over v1. Presumably if you have both in your load path you want the most recent. The other thing that this PR does is allow you to manually specify which version of the SDK to use through the config object. That would look like:

    Humidifier.configure { |config| config.sdk_version = 1 }

* Add docs on specifying the SDK version
  • Loading branch information
kddnewton committed Nov 3, 2016
1 parent cbeebf4 commit e3a4a31
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 30 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ Once stacks have the appropriate resources, you can query AWS to handle all stac

Humidifier assumes you have an `aws-sdk` gem installed when you call these operations. It detects the version of the gem you have installed and uses the appropriate API depending on what is available. If Humidifier cannot find any way to use the AWS SDK, it will warn you on every API call and simply return false.

You can also manually specify the version of the SDK that you want to use, in the case that you have both gems in your load path. In that case, you would specify it on the Humidifier configuration object:

```ruby
Humidifier.configure do |config|
config.sdk_version = 1
end
```

#### CloudFormation functions

You can use CFN intrinsic functions and references using `Humidifier.fn.[name]` and `Humidifier.ref`. Those will build appropriate structures that know how to be dumped to CFN syntax appropriately.
Expand Down
26 changes: 19 additions & 7 deletions lib/humidifier/aws_shim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ class AwsShim

attr_accessor :shim

# Attempt to require both aws-sdk-v1 and aws-sdk, then set the shim based on what successfully loaded
# Either set the SDK based on the configured option or guess the SDK
# version by attempting to require both aws-sdk-v1 and aws-sdk, then setting
# the shim based on what successfully loaded
def initialize
try_require_sdk('aws-sdk-v1')
try_require_sdk('aws-sdk')

self.shim =
if Object.const_defined?(:AWS)
if Humidifier.config.sdk_version_1?
AwsAdapters::SDKV1.new
elsif Object.const_defined?(:Aws)
elsif Humidifier.config.sdk_version_2?
AwsAdapters::SDKV2.new
else
AwsAdapters::Noop.new
guess_sdk
end
end

Expand All @@ -52,6 +51,19 @@ def shim

private

def guess_sdk
try_require_sdk('aws-sdk-v1')
try_require_sdk('aws-sdk')

if Object.const_defined?(:Aws)
AwsAdapters::SDKV2.new
elsif Object.const_defined?(:AWS)
AwsAdapters::SDKV1.new
else
AwsAdapters::Noop.new
end
end

def try_require_sdk(name)
require name
rescue LoadError # rubocop:disable Lint/HandleExceptions
Expand Down
17 changes: 14 additions & 3 deletions lib/humidifier/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@ class Configuration
end
MSG

attr_accessor :s3_bucket, :s3_prefix
attr_accessor :s3_bucket, :s3_prefix, :sdk_version

def initialize(opts = {})
self.s3_bucket = opts[:s3_bucket]
self.s3_prefix = opts[:s3_prefix]
self.s3_bucket = opts[:s3_bucket]
self.s3_prefix = opts[:s3_prefix]
self.sdk_version = opts[:sdk_version]
end

# raise an error unless the s3_bucket field is set
def ensure_upload_configured!(payload)
raise UPLOAD_MESSAGE.gsub('%{identifier}', payload.identifier) if s3_bucket.nil?
end

# true if the sdk_version option is set to 1 or '1'
def sdk_version_1?
sdk_version.to_s == '1'
end

# true if the sdk_version option is set to 2 or '2'
def sdk_version_2?
sdk_version.to_s == '2'
end
end
end
2 changes: 1 addition & 1 deletion lib/humidifier/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Humidifier
# Gem version
VERSION = '0.6.1'.freeze
VERSION = '0.7.0'.freeze
end
2 changes: 1 addition & 1 deletion test/aws_adapters/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def upload_object(_, key)
end
end

with_config('test.s3.bucket', 'prefix/') do
with_config(s3_bucket: 'test.s3.bucket', s3_prefix: 'prefix/') do
assert_equal 'prefix/name.json', fake_sdk.new.upload(payload(identifier: 'name'))
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/aws_adapters/sdkv1_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_create_and_wait_failure
end

def test_upload
with_config('test.s3.bucket') do
with_config(s3_bucket: 'test.s3.bucket') do
with_sdk_v1_loaded do |sdk|
upload_expectations
sdk.upload(payload(identifier: 'identifier', to_cf: 'body'))
Expand Down
2 changes: 1 addition & 1 deletion test/aws_adapters/sdkv2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_create_and_wait
end

def test_upload
with_config('test.s3.bucket') do
with_config(s3_bucket: 'test.s3.bucket') do
with_sdk_v2_loaded do |sdk|
SdkSupport.expect(:config, [], SdkSupport.double)
SdkSupport.expect(:update, [region: Humidifier::AwsShim::REGION])
Expand Down
31 changes: 21 additions & 10 deletions test/aws_shim_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,30 @@ def test_forwarding
mock.verify
end

def test_initialize_noop
assert_kind_of Humidifier::AwsAdapters::Noop, Humidifier::AwsShim.new.shim
def test_initialize_sdk_version_1?
with_config(sdk_version: 1) { assert_shim(:SDKV1) }
end

def test_initialize_sdk_v1
with_sdk_v1_loaded do
assert_kind_of Humidifier::AwsAdapters::SDKV1, Humidifier::AwsShim.new.shim
end
def test_initialize_sdk_version_2?
with_config(sdk_version: 2) { assert_shim(:SDKV2) }
end

def test_initialize_sdk_v2
with_sdk_v2_loaded do
assert_kind_of Humidifier::AwsAdapters::SDKV2, Humidifier::AwsShim.new.shim
end
def test_guess_sdk_noop
assert_shim(:Noop)
end

def test_guess_sdk_sdk_v1
with_sdk_v1_loaded { assert_shim(:SDKV1) }
end

def test_guess_sdk_sdk_v2
with_sdk_v2_loaded { assert_shim(:SDKV2) }
end

private

def assert_shim(kind)
expected = Humidifier::AwsAdapters.const_get(kind)
assert_kind_of expected, Humidifier::AwsShim.new.shim
end
end
24 changes: 20 additions & 4 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@
class ConfigurationTest < Minitest::Test

def test_ensure_upload_configured!
config = Humidifier::Configuration.new
error =
assert_raises RuntimeError do
config.ensure_upload_configured!(payload(identifier: 'foobar'))
build.ensure_upload_configured!(payload(identifier: 'foobar'))
end
assert_match(/foobar/, error.message)
end

def test_ensure_upload_configured_passes
config = Humidifier::Configuration.new
config.s3_bucket = Object.new
config = build(s3_bucket: Object.new)
config.ensure_upload_configured!(payload(identifier: 'foobar'))
end

def test_sdk_version_1?
assert build(sdk_version: 1).sdk_version_1?
assert build(sdk_version: '1').sdk_version_1?
refute build(sdk_version: 'foobar').sdk_version_1?
end

def test_sdk_version_2?
assert build(sdk_version: 2).sdk_version_2?
assert build(sdk_version: '2').sdk_version_2?
refute build(sdk_version: 'foobar').sdk_version_2?
end

private

def build(options = {})
Humidifier::Configuration.new(options)
end
end
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def tracker

# extra methods for testing config and serializers
Minitest::Test.send(:include, Module.new do
def with_config(s3_bucket, s3_prefix = nil, &block)
config = Humidifier::Configuration.new(s3_bucket: s3_bucket, s3_prefix: s3_prefix)
def with_config(options = {}, &block)
config = Humidifier::Configuration.new(options)
Humidifier.stub(:config, config, &block)
end

Expand Down

0 comments on commit e3a4a31

Please sign in to comment.