lib/miasma/contrib/aws/orchestration.rb in miasma-aws-0.1.6 vs lib/miasma/contrib/aws/orchestration.rb in miasma-aws-0.1.8

- old
+ new

@@ -3,10 +3,16 @@ module Miasma module Models class Orchestration class Aws < Orchestration + # Extended stack model to provide AWS specific stack options + class Stack < Orchestration::Stack + attribute :stack_policy_body, Hash, :coerce => lambda{|v| MultiJson.load(v).to_smash} + attribute :stack_policy_url, String + end + # Service name of the API API_SERVICE = 'cloudformation' # Supported version of the AutoScaling API API_VERSION = '2010-05-15' @@ -33,10 +39,14 @@ :collection => :balancers ), 'AWS::AutoScaling::AutoScalingGroup' => Smash.new( :api => :auto_scale, :collection => :groups + ), + 'AWS::CloudFormation::Stack' => Smash.new( + :api => :orchestration, + :collection => :stacks ) ) # Fetch stacks or update provided stack data # @@ -51,20 +61,33 @@ if(stack) d_params['StackName'] = stack.id descriptions = all_result_pages(nil, :body, 'DescribeStacksResponse', 'DescribeStacksResult', 'Stacks', 'member') do |options| request( :path => '/', - :params => options.merge(d_params) + :form => options.merge(d_params), + :method => :post ) end + policy = request( + :path => '/', + :method => :get, + :params => Smash.new( + 'Action' => 'GetStackPolicy', + 'StackName' => stack.id + ) + ).get(:body, 'GetStackPolicyResult', 'StackPolicyBody') + if(policy) + descriptions.first[:stack_policy] = MultiJson.load(policy).to_smash + end else descriptions = [] end lists = all_result_pages(nil, :body, 'ListStacksResponse', 'ListStacksResult', 'StackSummaries', 'member') do |options| request( :path => '/', - :params => options.merge(l_params) + :form => options.merge(l_params), + :method => :post ) end.map do |stk| desc = descriptions.detect do |d_stk| d_stk['StackId'] == stk['StackId'] end || Smash.new @@ -100,15 +123,23 @@ :key => o['OutputKey'], :value => o['OutputValue'], :description => o['Description'] ) }, + :tags => Smash[ + [stk.fetch('Tags', 'member', [])].flatten(1).map{|param| + [param['Key'], param['Value']] + } + ], :parameters => Smash[ [stk.fetch('Parameters', 'member', [])].flatten(1).map{|param| [param['ParameterKey'], param['ParameterValue']] } - ] + ], + :custom => Smash.new( + :stack_policy_body => policy + ) ).valid_state end end # Save the stack @@ -125,10 +156,26 @@ params["Capabilities.member.#{idx + 1}"] = cap end (stack.notification_topics || []).each_with_index do |topic, idx| params["NotificationARNs.member.#{idx + 1}"] = topic end + (stack.tags || {}).each_with_index do |tag, idx| + params["Tags.member.#{idx + 1}.Key"] = tag.first + params["Tags.member.#{idx + 1}.Value"] = tag.last + end + if(stack.custom[:stack_policy_body]) + params['StackPolicyBody'] = MultiJson.dump(stack.custom[:stack_policy_body]) + end + if(stack.custom[:stack_policy_url]) + params['StackPolicyURL'] = stack.custom[:stack_policy_url] + end + unless(stack.disable_rollback.nil?) + params['OnFailure'] = stack.disable_rollback ? 'nothing' : 'delete' + end + if(stack.on_failure) + params['OnFailure'] = stack.on_failure == 'nothing' ? 'DO_NOTHING' : stack.on_failure.upcase + end if(stack.template.empty?) params['UsePreviousTemplate'] = true else params['TemplateBody'] = MultiJson.dump(stack.template) end @@ -147,12 +194,11 @@ end result = request( :path => '/', :method => :post, :form => Smash.new( - 'Action' => 'CreateStack', - 'DisableRollback' => !!stack.disable_rollback + 'Action' => 'CreateStack' ).merge(params) ) stack.id = result.get(:body, 'CreateStackResponse', 'CreateStackResult', 'StackId') stack.valid_state end @@ -266,11 +312,12 @@ # @return [Array<Models::Orchestration::Stack::Resource>] def resource_all(stack) results = all_result_pages(nil, :body, 'DescribeStackResourcesResponse', 'DescribeStackResourcesResult', 'StackResources', 'member') do |options| request( :path => '/', - :params => options.merge( + :method => :post, + :form => options.merge( Smash.new( 'Action' => 'DescribeStackResources', 'StackName' => stack.id ) ) @@ -305,10 +352,11 @@ # @return [Array<Models::Orchestration::Stack::Event>] def event_all(stack, evt_id=nil) results = all_result_pages(nil, :body, 'DescribeStackEventsResponse', 'DescribeStackEventsResult', 'StackEvents', 'member') do |options| request( :path => '/', - :params => options.merge( + :method => :post, + :form => options.merge( 'Action' => 'DescribeStackEvents', 'StackName' => stack.id ) ) end