Skip to content

Commit

Permalink
Merge pull request #24 from burtcorp/cuffdown-specs
Browse files Browse the repository at this point in the history
Repair cuffdown with --region option
  • Loading branch information
bittrance authored Sep 23, 2018
2 parents 826c243 + 4514100 commit ed133f1
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 56 deletions.
54 changes: 2 additions & 52 deletions bin/cuffdown
Original file line number Diff line number Diff line change
@@ -1,55 +1,5 @@
#!/usr/bin/env ruby

require 'cuffsert/metadata'
require 'cuffsert/rxcfclient'
require 'yaml'
require 'cuffdown/main'

module CuffDown
def self.parameters(stack)
(stack[:parameters] || []).map do |param|
{
'Name' => param[:parameter_key],
'Value' => param[:parameter_value],
}
end
end

def self.tags(stack)
(stack[:tags] || []).map do |param|
{
'Name' => param[:key],
'Value' => param[:value],
}
end
end

def self.dump(name, params, tags, output)
result = {
'Format' => 'v1',
'Suffix' => name,
'Parameters' => params,
'Tags' => tags,
}
YAML.dump(result, output)
end

def self.run(args)
meta = CuffSert::StackConfig.new
meta.stackname = args[0]
client = CuffSert::RxCFClient.new
stack = client.find_stack_blocking(meta)
unless stack
STDERR.puts "No such stack #{meta.stackname}"
exit(1)
end
stack = stack.to_h
self.dump(
stack[:stack_name],
self.parameters(stack),
self.tags(stack),
STDOUT
)
end
end

CuffDown.run(ARGV)
CuffDown.run(ARGV, STDOUT)
6 changes: 6 additions & 0 deletions lib/cuffbase.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require 'yaml'

module CuffBase
def self.shared_cli_args(opts, args)
opts.on('--region=aws_region', 'AWS region, overrides env variable AWS_REGION') do |region|
args[:aws_region] = region
end
end

def self.empty_from_template(io)
self.template_parameters(io) {|_| nil }
end
Expand Down
67 changes: 67 additions & 0 deletions lib/cuffdown/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'cuffbase'
require 'cuffsert/metadata'
require 'cuffsert/rxcfclient'
require 'optparse'
require 'yaml'

module CuffDown
def self.parse_cli_args(argv)
args = {}
parser = OptionParser.new do |opts|
opts.banner = 'Output CuffSert-formatted metadata from an existing stack.'
opts.separator('')
opts.separator('Usage: cuffdown stack-name')
CuffBase.shared_cli_args(opts, args)
end
stackname, _ = parser.parse(argv)
args[:stackname] = stackname
args
end

def self.parameters(stack)
(stack[:parameters] || []).map do |param|
{
'Name' => param[:parameter_key],
'Value' => param[:parameter_value],
}
end
end

def self.tags(stack)
(stack[:tags] || []).map do |param|
{
'Name' => param[:key],
'Value' => param[:value],
}
end
end

def self.dump(name, params, tags, output)
result = {
'Format' => 'v1',
'Suffix' => name,
'Parameters' => params,
'Tags' => tags,
}
YAML.dump(result, output)
end

def self.run(argv, output)
cli_args = self.parse_cli_args(argv)
meta = CuffSert::StackConfig.new
meta.stackname = cli_args[:stackname]
client = CuffSert::RxCFClient.new(cli_args[:aws_region])
stack = client.find_stack_blocking(meta)
unless stack
STDERR.puts "No such stack #{meta.stackname}"
exit(1)
end
stack = stack.to_h
self.dump(
stack[:stack_name],
self.parameters(stack),
self.tags(stack),
output
)
end
end
8 changes: 4 additions & 4 deletions lib/cuffsert/cli_args.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'optparse'
require 'cuffbase'
require 'cuffsert/version'

module CuffSert
Expand All @@ -23,6 +24,9 @@ def self.parse_cli_args(argv)
opts.separator(' cuffsert --name <stackname> {--parameter Name=Value | --tag Name=Value}... [stack.json]')
opts.separator(' cuffsert --metadata <metadata.json> --selector <path/in/metadata> stack.json')
opts.separator(' cuffsert --metadata <metadata.json> --selector <path/in/metadata> {--parameter Name=Value | --tag Name=Value}... [stack.json]')

CuffBase.shared_cli_args(opts, args)

opts.on('--metadata path', '-m path', 'Yaml file to read stack metadata from') do |path|
path = '/dev/stdin' if path == '-'
unless File.exist?(path)
Expand Down Expand Up @@ -64,10 +68,6 @@ def self.parse_cli_args(argv)
args[:overrides][:tags][key] = val
end

opts.on('--region=aws_region', 'AWS region, overrides env variable AWS_REGION') do |region|
args[:aws_region] = region
end

opts.on('--s3-upload-prefix=prefix', 'Templates > 51200 bytes are uploaded here. Format: s3://bucket-name/[pre/fix]') do |prefix|
unless prefix.start_with?('s3://')
raise "Upload prefix #{prefix} must start with s3://"
Expand Down
38 changes: 38 additions & 0 deletions spec/cuffdown/main_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'cuffdown/main'
require 'spec_helpers'

describe CuffDown do
include_context 'stack states'

describe '.run' do
let(:cli_args) { ['some-stack'] }
let(:output) { StringIO.new }
let :stack_complete do
super().merge(
:parameters => [
{:parameter_key => 'p1', :parameter_value => 'v1'}
]
)
end

let(:cfmock) { double(:cfmock) }

before do
allow(Aws::CloudFormation::Client).to receive(:new).and_return(cfmock)
allow(cfmock).to receive(:describe_stacks).and_return(stack_complete_describe)
CuffDown.run(cli_args, output)
end

it 'outputs parameter values for the stack' do
expect(output.string).to match(/Name: p1.*Value: v1/m)
end

context 'when invoked with an explicit region' do
let(:cli_args) { ['--region', 'rp-north-1', 'stack-name'] }

it 'passes region to the ClodFormation client' do
expect(Aws::CloudFormation::Client).to have_received(:new).with(hash_including(:region => 'rp-north-1'))
end
end
end
end

0 comments on commit ed133f1

Please sign in to comment.