From 35b25fd4fab22a12d8271b3987554472a634a17c Mon Sep 17 00:00:00 2001 From: Joe Kinsella Date: Sat, 4 May 2013 21:32:57 -0400 Subject: [PATCH] Adding support for AWS CloudFormation list_stacks and list_stack_resources API calls --- lib/fog/aws/cloud_formation.rb | 2 + .../cloud_formation/list_stack_resources.rb | 32 ++++++++++++++++ .../parsers/cloud_formation/list_stacks.rb | 34 +++++++++++++++++ .../cloud_formation/list_stack_resources.rb | 36 ++++++++++++++++++ .../requests/cloud_formation/list_stacks.rb | 37 +++++++++++++++++++ .../requests/cloud_formation/stack_tests.rb | 10 +++++ 6 files changed, 151 insertions(+) create mode 100644 lib/fog/aws/parsers/cloud_formation/list_stack_resources.rb create mode 100644 lib/fog/aws/parsers/cloud_formation/list_stacks.rb create mode 100644 lib/fog/aws/requests/cloud_formation/list_stack_resources.rb create mode 100644 lib/fog/aws/requests/cloud_formation/list_stacks.rb diff --git a/lib/fog/aws/cloud_formation.rb b/lib/fog/aws/cloud_formation.rb index c33e2ef08..42484a432 100644 --- a/lib/fog/aws/cloud_formation.rb +++ b/lib/fog/aws/cloud_formation.rb @@ -17,6 +17,8 @@ module Fog request :describe_stacks request :get_template request :validate_template + request :list_stacks + request :list_stack_resources class Mock diff --git a/lib/fog/aws/parsers/cloud_formation/list_stack_resources.rb b/lib/fog/aws/parsers/cloud_formation/list_stack_resources.rb new file mode 100644 index 000000000..4f72c73bd --- /dev/null +++ b/lib/fog/aws/parsers/cloud_formation/list_stack_resources.rb @@ -0,0 +1,32 @@ +module Fog + module Parsers + module AWS + module CloudFormation + + class ListStackResources < Fog::Parsers::Base + + def reset + @resource = {} + @response = { 'StackResourceSummaries' => [] } + end + + def end_element(name) + case name + when 'ResourceStatus', 'LogicalResourceId', 'PhysicalResourceId', 'ResourceType' + @resource[name] = value + when 'member' + @response['StackResourceSummaries'] << @resource + @resource = {} + when 'LastUpdatedTimestamp' + @resource[name] = Time.parse(value) + when 'RequestId' + @response[name] = value + when 'NextToken' + @response[name] = value + end + end + end + end + end + end +end diff --git a/lib/fog/aws/parsers/cloud_formation/list_stacks.rb b/lib/fog/aws/parsers/cloud_formation/list_stacks.rb new file mode 100644 index 000000000..5e41b4c7f --- /dev/null +++ b/lib/fog/aws/parsers/cloud_formation/list_stacks.rb @@ -0,0 +1,34 @@ +module Fog + module Parsers + module AWS + module CloudFormation + + class ListStacks < Fog::Parsers::Base + + def reset + @stack = {} + @response = { 'StackSummaries' => [] } + end + + def end_element(name) + case name + when 'StackId', 'StackStatus', 'StackName', 'TemplateDescription' + @stack[name] = value + when 'member' + @response['StackSummaries'] << @stack + @stack = {} + when 'RequestId' + @response[name] = value + when 'CreationTime' + @stack[name] = Time.parse(value) + when 'DeletionTime' + @stack[name] = Time.parse(value) + when 'NextToken' + @response[name] = value + end + end + end + end + end + end +end diff --git a/lib/fog/aws/requests/cloud_formation/list_stack_resources.rb b/lib/fog/aws/requests/cloud_formation/list_stack_resources.rb new file mode 100644 index 000000000..246f96b35 --- /dev/null +++ b/lib/fog/aws/requests/cloud_formation/list_stack_resources.rb @@ -0,0 +1,36 @@ +module Fog + module AWS + class CloudFormation + class Real + + require 'fog/aws/parsers/cloud_formation/list_stack_resources' + + # List stack resources. + # + # @param options [Hash] + # @option options StackName [String] Name of the stack to describe. + # + # @return [Excon::Response] + # * body [Hash]: + # * StackResourceSummaries [Array] - Matching stacks + # * resources [Hash]: + # * ResourceStatus [String] - + # * LogicalResourceId [String] - + # * PhysicalResourceId [String] - + # * ResourceType [String] - + # * LastUpdatedTimestamp [Time] - + # + # + # @see http://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_ListStacks.html + + def list_stack_resources(options = {}) + request({ + 'Action' => 'ListStackResources', + :parser => Fog::Parsers::AWS::CloudFormation::ListStackResources.new + }.merge!(options)) + end + + end + end + end +end diff --git a/lib/fog/aws/requests/cloud_formation/list_stacks.rb b/lib/fog/aws/requests/cloud_formation/list_stacks.rb new file mode 100644 index 000000000..4bfbf9268 --- /dev/null +++ b/lib/fog/aws/requests/cloud_formation/list_stacks.rb @@ -0,0 +1,37 @@ +module Fog + module AWS + class CloudFormation + class Real + + require 'fog/aws/parsers/cloud_formation/list_stacks' + + # List stacks. + # + # @param options [Hash] + # + # @return [Excon::Response] + # * body [Hash]: + # * StackSummaries [Array] - Matching stacks + # * stack [Hash]: + # * StackId [String] - + # * StackName [String] - + # * TemplateDescription [String] - + # * CreationTime [Time] - + # * DeletionTime [Time] - + # * StackStatus [String] - + # * DeletionTime [String] - + # + # + # @see http://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_ListStacks.html + + def list_stacks(options = {}) + request({ + 'Action' => 'ListStacks', + :parser => Fog::Parsers::AWS::CloudFormation::ListStacks.new + }.merge!(options)) + end + + end + end + end +end diff --git a/tests/aws/requests/cloud_formation/stack_tests.rb b/tests/aws/requests/cloud_formation/stack_tests.rb index 8ced99e75..3fb72aeb8 100644 --- a/tests/aws/requests/cloud_formation/stack_tests.rb +++ b/tests/aws/requests/cloud_formation/stack_tests.rb @@ -144,6 +144,16 @@ Shindo.tests('AWS::CloudFormation | stack requests', ['aws', 'cloudformation']) Fog::AWS[:cloud_formation].delete_stack(@stack_name) end + tests("list_stacks").succeeds do + pending if Fog.mocking? + Fog::AWS[:cloud_formation].list_stacks.body + end + + tests("list_stack_resources").succeeds do + pending if Fog.mocking? + Fog::AWS[:cloud_formation].list_stack_resources("StackName"=>@stack_name).body + end + unless Fog.mocking? @keypair.destroy end