-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ktheory/stack-outputs
Support stack outputs as parameters in cfn-flow.yml
- Loading branch information
Showing
8 changed files
with
331 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module CfnFlow | ||
class CachedStack | ||
|
||
class MissingOutput < StandardError; end | ||
|
||
def self.stack_cache | ||
@stack_cache ||= {} | ||
end | ||
|
||
def self.get_output(stack:, output:) | ||
new(stack).output(output) | ||
end | ||
|
||
attr_reader :stack_name | ||
|
||
def initialize(stack_name) | ||
@stack_name = stack_name | ||
end | ||
|
||
def output(name) | ||
output = stack_cache.outputs.detect{|out| out.output_key == name } | ||
unless output | ||
raise MissingOutput.new("Can't find outpout #{name} for stack #{stack_name}") | ||
end | ||
output.output_value | ||
end | ||
|
||
def stack_cache | ||
self.class.stack_cache[stack_name] ||= CfnFlow.cfn_resource.stack(stack_name).load | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module CfnFlow | ||
# Extend hash with some special behavior to generate the | ||
# style of hash aws-sdk expects | ||
class StackParams < Hash | ||
|
||
def self.expanded(hash) | ||
self[hash]. | ||
with_symbolized_keys. | ||
with_expanded_parameters. | ||
with_expanded_tags. | ||
with_expanded_template_body | ||
end | ||
|
||
def with_symbolized_keys | ||
self.inject(StackParams.new) do |accum, pair| | ||
key, value = pair | ||
accum.merge(key.to_sym => value) | ||
end | ||
end | ||
|
||
def with_expanded_parameters | ||
return self unless self[:parameters].is_a? Hash | ||
|
||
expanded_params = self[:parameters].map do |key,value| | ||
{ parameter_key: key, parameter_value: fetch_value(key, value) } | ||
end | ||
|
||
self.merge(parameters: expanded_params) | ||
end | ||
|
||
def with_expanded_tags | ||
return self unless self[:tags].is_a? Hash | ||
|
||
tags = self[:tags].map do |key, value| | ||
{key: key, value: value} | ||
end | ||
|
||
self.merge(tags: tags) | ||
end | ||
|
||
def add_tag(hash) | ||
new_tags = hash.map do |k,v| | ||
{key: k, value: v } | ||
end | ||
tags = (self[:tags] || []) + new_tags | ||
self.merge(tags: tags) | ||
end | ||
|
||
def with_expanded_template_body | ||
return self unless self[:template_body].is_a? String | ||
body = CfnFlow::Template.new(self[:template_body]).to_json | ||
self.merge(template_body: body) | ||
rescue CfnFlow::Template::Error | ||
# Do nothing | ||
self | ||
end | ||
|
||
def fetch_value(key, value) | ||
# Dereference stack output params | ||
if value.is_a?(Hash) && value.key?('stack') | ||
stack_name = value['stack'] | ||
stack_output_name = value['output'] || key | ||
|
||
value = CachedStack.get_output(stack: stack_name, output: stack_output_name) | ||
else | ||
value | ||
end | ||
end | ||
private :fetch_value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module CfnFlow | ||
VERSION = '0.8.0' | ||
VERSION = '0.9.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require_relative '../helper' | ||
|
||
describe 'CfnFlow::CachedStack' do | ||
subject { CfnFlow::CachedStack } | ||
|
||
describe '.stack_cache' do | ||
it 'defaults to a hash' do | ||
subject.stack_cache.must_equal({}) | ||
end | ||
end | ||
|
||
describe '.get_output' do | ||
let(:output_value) { 'myvalue' } | ||
|
||
before do | ||
Aws.config[:cloudformation]= { | ||
stub_responses: { | ||
describe_stacks: { stacks: [ stub_stack_data.merge(outputs: [{ output_key: "myoutput", output_value: output_value } ]) ] } | ||
} | ||
} | ||
end | ||
|
||
it 'returns the output' do | ||
subject.get_output(stack: 'mystack', output: 'myoutput').must_equal output_value | ||
end | ||
|
||
it 'has required kwargs' do | ||
-> { subject.get_output }.must_raise(ArgumentError) | ||
end | ||
end | ||
|
||
describe 'an instance' do | ||
subject { CfnFlow::CachedStack.new('mystack') } | ||
let(:output_value) { 'myvalue' } | ||
|
||
before do | ||
Aws.config[:cloudformation]= { | ||
stub_responses: { | ||
describe_stacks: { stacks: [ stub_stack_data.merge(outputs: [{ output_key: "myoutput", output_value: output_value } ]) ] } | ||
} | ||
} | ||
end | ||
|
||
it "should return the output value" do | ||
subject.output('myoutput').must_equal output_value | ||
end | ||
|
||
describe "with a missing output" do | ||
it "should raise an error" do | ||
-> { subject.output("no-such-output") }.must_raise(CfnFlow::CachedStack::MissingOutput) | ||
end | ||
end | ||
|
||
describe "with a missing stack" do | ||
|
||
subject { CfnFlow::CachedStack.new('no-such-stack') } | ||
before do | ||
Aws.config[:cloudformation]= { | ||
stub_responses: { | ||
describe_stacks: 'ValidationError' | ||
} | ||
} | ||
end | ||
|
||
it "should raise an error" do | ||
-> { subject.output('blah') }.must_raise(Aws::CloudFormation::Errors::ValidationError) | ||
end | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.