Skip to content

Commit

Permalink
Move params to payload (#47)
Browse files Browse the repository at this point in the history
* move params to payload

* fix tests

* Bump minitest dep to ~> 5.9

assert_mock added there

* Fix sdk payload test
  • Loading branch information
kddnewton committed Jul 19, 2016
1 parent 6df73a6 commit a9c3871
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 36 deletions.
2 changes: 1 addition & 1 deletion humidifier.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'bundler', '~> 1.11'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'minitest', '~> 5.9'
spec.add_development_dependency 'nokogiri', '~> 1.6'
spec.add_development_dependency 'simplecov', '~> 0.11'
spec.add_development_dependency 'coveralls', '~> 0.8'
Expand Down
12 changes: 4 additions & 8 deletions lib/humidifier/aws_adapters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ class Base
# Create a CFN stack
def create(payload)
try_valid do
params = { stack_name: payload.name, template_body: payload.to_cf }.merge(payload.options)
response = client.create_stack(params)
response = client.create_stack(payload.create_params)
payload.id = response.stack_id
response
end
end

# Delete a CFN stack
def delete(payload)
client.delete_stack({ stack_name: payload.identifier }.merge(payload.options))
client.delete_stack(payload.delete_params)
true
end

Expand All @@ -29,15 +28,12 @@ def deploy(payload)

# Update a CFN stack
def update(payload)
try_valid do
params = { stack_name: payload.identifier, template_body: payload.to_cf }.merge(payload.options)
client.update_stack(params)
end
try_valid { client.update_stack(payload.update_params) }
end

# Validate a template in CFN
def valid?(payload)
try_valid { client.validate_template({ template_body: payload.to_cf }.merge(payload.options)) }
try_valid { client.validate_template(payload.validate_params) }
end

%i[create delete deploy update].each do |method|
Expand Down
3 changes: 1 addition & 2 deletions lib/humidifier/aws_adapters/sdkv2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class SDKV2 < Base
# Create a change set in CFN
def create_change_set(payload)
payload.merge(change_set_name: "changeset-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}")
params = { stack_name: payload.identifier, template_body: payload.to_cf }.merge(payload.options)
try_valid { client.create_change_set(params) }
try_valid { client.create_change_set(payload.create_change_set_params) }
end

# Create a change set if the stack exists, otherwise create the stack
Expand Down
31 changes: 30 additions & 1 deletion lib/humidifier/sdk_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SdkPayload
attr_accessor :stack, :options, :max_wait

extend Forwardable
def_delegators :stack, :id=, :identifier, :name, :to_cf
def_delegators :stack, :id=, :identifier, :name

def initialize(stack, options)
self.stack = stack
Expand All @@ -26,5 +26,34 @@ def ==(other)
def merge(new_options)
self.options = new_options.merge(options)
end

###
# Param sets
###

# Param set for the #create_change_set SDK method
def create_change_set_params
{ stack_name: stack.identifier, template_body: stack.to_cf }.merge(options)
end

# Param set for the #create_stack SDK method
def create_params
{ stack_name: stack.name, template_body: stack.to_cf }.merge(options)
end

# Param set for the #delete_stack SDK method
def delete_params
{ stack_name: stack.identifier }.merge(options)
end

# Param set for the #update_stack SDK method
def update_params
{ stack_name: stack.identifier, template_body: stack.to_cf }.merge(options)
end

# Param set for the #validate_template SDK method
def validate_params
{ template_body: stack.to_cf }.merge(options)
end
end
end
6 changes: 3 additions & 3 deletions test/aws_adapters/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class BaseTest < Minitest::Test

def test_validation_error
create_payload = Object.new
def create_payload.name
def create_payload.create_params
raise SdkSupport::AwsDouble::CloudFormation::Errors::ValidationError, 'test-error'
end

Expand All @@ -20,7 +20,7 @@ def test_create
SdkSupport.expect(:create_stack, [{ stack_name: 'name', template_body: 'body' }], stub(stack_id: 'test-id'))

sdk.create(create_payload)
assert_equal 'test-id', create_payload.id
assert_equal 'test-id', create_payload.stack.id
SdkSupport.verify
end
end
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_deploy_does_not_exist
SdkSupport.expect(:create_stack, [{ stack_name: 'name', template_body: 'body' }], stub(stack_id: 'test-id'))

sdk.deploy(deploy_payload)
assert_equal 'test-id', deploy_payload.id
assert_equal 'test-id', deploy_payload.stack.id
SdkSupport.verify
end
end
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 @@ -56,7 +56,7 @@ def test_create_and_wait

SdkSupport.expect(:create_stack, [{ stack_name: 'name', template_body: 'body' }], stub(stack_id: 'test-id'))
SdkSupport.expect(:wait_until, [:stack_create_complete, stack_name: 'test-id'])
SdkSupport.expect(:max_attempts=, [2])
SdkSupport.expect(:max_attempts=, [Humidifier::SdkPayload::MAX_WAIT / 5])
SdkSupport.expect(:delay=, [5])

sdk.create_and_wait(create_payload)
Expand Down
13 changes: 12 additions & 1 deletion test/sdk_payload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class SdkPayloadTest < Minitest::Test
def test_forwarding
assert_equal 'identifier', build.identifier
assert_equal 'name', build.name
assert_equal 'to_cf', build.to_cf
end

def test_options
Expand All @@ -27,6 +26,18 @@ def test_merge_no_overwrite
assert_equal 'bar', payload.options[:foo]
end

def test_param_sets
{
create_change_set_params: { stack_name: 'identifier', template_body: 'to_cf' },
create_params: { stack_name: 'name', template_body: 'to_cf' },
delete_params: { stack_name: 'identifier' },
update_params: { stack_name: 'identifier', template_body: 'to_cf' },
validate_params: { template_body: 'to_cf' }
}.each do |method, expected|
assert_equal expected.merge(foo: 'bar'), build.public_send(method), "Not equal for method: #{method}"
end
end

private

def build
Expand Down
9 changes: 8 additions & 1 deletion test/sdk_support/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
module SdkSupport
module Helpers
class PayloadStack
attr_accessor :id, :identifier, :name, :to_cf

def initialize(opts = {})
opts.each { |key, value| instance_variable_set(:"@#{key}", value) }
end
end

private

def payload(opts = {})
Payload.new(opts)
Humidifier::SdkPayload.new(PayloadStack.new(opts), {})
end

def stub(value)
Expand Down
18 changes: 0 additions & 18 deletions test/sdk_support/payload.rb

This file was deleted.

0 comments on commit a9c3871

Please sign in to comment.