Skip to content

Commit

Permalink
Support all options to create orchestration stacks
Browse files Browse the repository at this point in the history
Now accept all options that Cloudformation recognizes while creating a stack

https://bugzilla.redhat.com/show_bug.cgi?id=1385712
  • Loading branch information
bzwei committed Nov 28, 2016
1 parent 865ea3e commit 2a4ae0f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
class ManageIQ::Providers::Amazon::CloudManager::OrchestrationServiceOptionConverter < ::ServiceOrchestration::OptionConverter
REGEX_TAGS = /([^(=>), ]+)[(=>) ]+([^(=>), ]+)[, ]*/
REGEX_ARRAY = /[, ]+/
private_constant :REGEX_TAGS, :REGEX_TAGS

def stack_create_options
on_failure = @dialog_options['dialog_stack_onfailure']
timeout = @dialog_options['dialog_stack_timeout']
stack_options = {:parameters => stack_parameters, :disable_rollback => on_failure != 'ROLLBACK'}
stack_options[:timeout_in_minutes] = timeout.to_i unless timeout.blank?
policy_body, policy_url = parse_policy(@dialog_options['dialog_stack_policy'])

stack_options = {
:parameters => stack_parameters,
:on_failure => @dialog_options['dialog_stack_onfailure'],
:timeout_in_minutes => timeout.blank? ? nil : timeout.to_i,
:notification_arns => @dialog_options['dialog_stack_notifications'].try(:split, REGEX_ARRAY),
:capabilities => @dialog_options['dialog_stack_capabilities'], #multi-selected dropdown, as an array
:resource_types => @dialog_options['dialog_stack_resource_types'].try(:split, REGEX_ARRAY),
:role_arn => @dialog_options['dialog_stack_role'],
:stack_policy_body => policy_body,
:stack_policy_url => policy_url,
:tags => parse_tags(@dialog_options['dialog_stack_tags'])
}

stack_options.select { |_k, v| v.present? }
end

private

def parse_tags(input)
return unless input

# input example: "tag_key1 => tag_val1, tag_key2 => tag_val2"
input.scan(REGEX_TAGS).each_with_object([]) { |tag, arr| arr.push({:key => tag.first, :value => tag.last}) }
end

def parse_policy(input)
return unless input

stack_options
begin
JSON.parse(input)
policy_body = input
rescue JSON::ParserError
policy_url = input
end
[policy_body, policy_url]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
describe ManageIQ::Providers::Amazon::CloudManager::OrchestrationServiceOptionConverter do
subject { described_class.new(options) }

describe '#stack_create_options' do
context 'no option is set' do
let(:options) { {} }

it 'returns an empty option hash for stack creation' do
expect(subject.stack_create_options).to be_empty
end
end

context 'all options are empty' do
let(:options) do
{
"dialog_stack_timeout" => "",
"dialog_stack_onfailure" => "",
"dialog_stack_notifications" => "",
"dialog_stack_capabilities" => "",
"dialog_stack_resource_types" => "",
"dialog_stack_policy" => "",
"dialog_stack_role" => "",
"dialog_stack_tags" => ""
}
end

it 'returns an empty option hash for stack creation' do
expect(subject.stack_create_options).to be_empty
end
end
end

context 'timeout option' do
let(:options) { {'dialog_stack_timeout' => '30'} }

it { expect(subject.stack_create_options[:timeout_in_minutes]).to eq(30) }
end

context 'on_failure option' do
let(:options) { {'dialog_stack_onfailure' => 'ROLLBACK'} }

it { expect(subject.stack_create_options[:on_failure]).to eq('ROLLBACK') }
end

context 'notification arn option' do
let(:options) { {'dialog_stack_notifications' => 'ARN1, ARN2'} }

it { expect(subject.stack_create_options[:notification_arns]).to eq(%w(ARN1 ARN2)) }
end

context 'capabilities option' do
let(:options) { {'dialog_stack_capabilities' => %w(CAPABILITY_IAM CAPABILITY_NAMED_IAM)} }

it { expect(subject.stack_create_options[:capabilities]).to eq(%w(CAPABILITY_IAM CAPABILITY_NAMED_IAM)) }
end

context 'policy body option' do
let(:options) { {'dialog_stack_policy' => '{"valid":"JSON string"}'} }

it { expect(subject.stack_create_options[:stack_policy_body]).to eq('{"valid":"JSON string"}') }
end

context 'policy url option' do
let(:options) { {'dialog_stack_policy' => 'http://url'} }

it { expect(subject.stack_create_options[:stack_policy_url]).to eq('http://url') }
end

context 'tags option' do
let(:options) { {'dialog_stack_tags' => 'tag_key1 => tag_val1, tag_key2=>tag_val2'} }

it { expect(subject.stack_create_options[:tags]).to eq(
[{:key => 'tag_key1', :value => 'tag_val1'}, {:key => 'tag_key2', :value => 'tag_val2'}]) }
end
end

0 comments on commit 2a4ae0f

Please sign in to comment.