2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/aws'
|
2011-08-31 16:52:53 -04:00
|
|
|
|
2011-02-26 15:18:42 -05:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class CloudFormation < Fog::Service
|
2012-06-18 04:44:51 -04:00
|
|
|
extend Fog::AWS::CredentialFetcher::ServiceMethods
|
2011-02-26 15:18:42 -05:00
|
|
|
|
|
|
|
requires :aws_access_key_id, :aws_secret_access_key
|
2012-06-18 04:44:51 -04:00
|
|
|
recognizes :host, :path, :port, :scheme, :persistent, :region, :use_iam_profile, :aws_session_token, :aws_credentials_expire_at
|
2011-02-26 15:18:42 -05:00
|
|
|
|
|
|
|
request_path 'fog/aws/requests/cloud_formation'
|
|
|
|
request :create_stack
|
2011-10-18 10:06:35 -04:00
|
|
|
request :update_stack
|
2011-02-26 15:18:42 -05:00
|
|
|
request :delete_stack
|
2011-02-26 15:24:59 -05:00
|
|
|
request :describe_stack_events
|
|
|
|
request :describe_stack_resources
|
|
|
|
request :describe_stacks
|
2011-02-26 15:18:42 -05:00
|
|
|
request :get_template
|
|
|
|
request :validate_template
|
2013-05-04 21:32:57 -04:00
|
|
|
request :list_stacks
|
|
|
|
request :list_stack_resources
|
2011-02-26 15:18:42 -05:00
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def initialize(options={})
|
2011-03-14 21:19:20 -04:00
|
|
|
Fog::Mock.not_implemented
|
2011-02-26 15:18:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2012-06-18 04:44:51 -04:00
|
|
|
include Fog::AWS::CredentialFetcher::ConnectionMethods
|
2011-02-26 15:18:42 -05:00
|
|
|
# Initialize connection to CloudFormation
|
|
|
|
#
|
|
|
|
# ==== Notes
|
|
|
|
# options parameter must include values for :aws_access_key_id and
|
|
|
|
# :aws_secret_access_key in order to create a connection
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
# cf = CloudFormation.new(
|
|
|
|
# :aws_access_key_id => your_aws_access_key_id,
|
|
|
|
# :aws_secret_access_key => your_aws_secret_access_key
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * options<~Hash> - config arguments for connection. Defaults to {}.
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# * CloudFormation object with connection to AWS.
|
|
|
|
def initialize(options={})
|
|
|
|
require 'fog/core/parser'
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
@use_iam_profile = options[:use_iam_profile]
|
|
|
|
setup_credentials(options)
|
2011-03-02 13:57:52 -05:00
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2011-03-02 13:57:52 -05:00
|
|
|
options[:region] ||= 'us-east-1'
|
2012-02-02 15:45:32 -05:00
|
|
|
@host = options[:host] || "cloudformation.#{options[:region]}.amazonaws.com"
|
2011-09-12 11:01:48 -04:00
|
|
|
@path = options[:path] || '/'
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@scheme = options[:scheme] || 'https'
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
|
2011-02-26 15:18:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
def setup_credentials(options)
|
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
@aws_secret_access_key = options[:aws_secret_access_key]
|
|
|
|
@aws_session_token = options[:aws_session_token]
|
|
|
|
@aws_credentials_expire_at = options[:aws_credentials_expire_at]
|
|
|
|
|
|
|
|
@hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)
|
|
|
|
end
|
|
|
|
|
2011-02-26 15:18:42 -05:00
|
|
|
def request(params)
|
2012-06-18 04:44:51 -04:00
|
|
|
refresh_credentials_if_expired
|
|
|
|
|
2011-02-26 15:18:42 -05:00
|
|
|
idempotent = params.delete(:idempotent)
|
|
|
|
parser = params.delete(:parser)
|
|
|
|
|
2011-06-20 16:49:37 -04:00
|
|
|
body = Fog::AWS.signed_params(
|
2011-02-26 15:18:42 -05:00
|
|
|
params,
|
|
|
|
{
|
|
|
|
:aws_access_key_id => @aws_access_key_id,
|
[AWS] make beanstalk, cdn, cloudformation, cloudwatch, elasticache, elb, storage, rds, ses, sns, route53 temporary credential friendly
2012-06-20 18:16:34 -04:00
|
|
|
:aws_session_token => @aws_session_token,
|
2011-02-26 15:18:42 -05:00
|
|
|
:hmac => @hmac,
|
|
|
|
:host => @host,
|
|
|
|
:path => @path,
|
|
|
|
:port => @port,
|
|
|
|
:version => '2010-05-15'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
begin
|
|
|
|
response = @connection.request({
|
|
|
|
:body => body,
|
|
|
|
:expects => 200,
|
|
|
|
:idempotent => idempotent,
|
|
|
|
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
|
|
:host => @host,
|
|
|
|
:method => 'POST',
|
|
|
|
:parser => parser
|
|
|
|
})
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2013-03-22 05:45:05 -04:00
|
|
|
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m)
|
2011-02-26 15:18:42 -05:00
|
|
|
raise case match[1].split('.').last
|
2013-05-05 08:20:05 -04:00
|
|
|
when 'NotFound', 'ValidationError'
|
|
|
|
Fog::AWS::CloudFormation::NotFound.slurp(error, match[2])
|
2011-02-26 15:18:42 -05:00
|
|
|
else
|
2013-05-05 08:20:05 -04:00
|
|
|
Fog::AWS::CloudFormation::Error.slurp(error, "#{match[1]} => #{match[2]}")
|
2011-02-26 15:18:42 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|