Skip to content

Commit

Permalink
Allow passing a region to cuffdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
bittrance authored and Bittrance committed Sep 23, 2018
1 parent 8c725d8 commit 4514100
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
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
22 changes: 19 additions & 3 deletions lib/cuffdown/main.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
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|
{
Expand Down Expand Up @@ -31,10 +46,11 @@ def self.dump(name, params, tags, output)
YAML.dump(result, output)
end

def self.run(args, output)
def self.run(argv, output)
cli_args = self.parse_cli_args(argv)
meta = CuffSert::StackConfig.new
meta.stackname = args[0]
client = CuffSert::RxCFClient.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}"
Expand Down
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
10 changes: 9 additions & 1 deletion spec/cuffdown/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@
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
end

0 comments on commit 4514100

Please sign in to comment.