Class: Fog::AWS::CloudFormation::Real

Inherits:
Object
  • Object
show all
Includes:
Fog::AWS::CredentialFetcher::ConnectionMethods
Defined in:
lib/fog/aws/cloud_formation.rb,
lib/fog/aws/requests/cloud_formation/list_stacks.rb,
lib/fog/aws/requests/cloud_formation/create_stack.rb,
lib/fog/aws/requests/cloud_formation/delete_stack.rb,
lib/fog/aws/requests/cloud_formation/get_template.rb,
lib/fog/aws/requests/cloud_formation/update_stack.rb,
lib/fog/aws/requests/cloud_formation/describe_stacks.rb,
lib/fog/aws/requests/cloud_formation/validate_template.rb,
lib/fog/aws/requests/cloud_formation/list_stack_resources.rb,
lib/fog/aws/requests/cloud_formation/describe_stack_events.rb,
lib/fog/aws/requests/cloud_formation/describe_stack_resources.rb

Instance Method Summary collapse

Methods included from Fog::AWS::CredentialFetcher::ConnectionMethods

#refresh_credentials_if_expired

Constructor Details

#initialize(options = {}) ⇒ Real

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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fog/aws/cloud_formation.rb', line 46

def initialize(options={})

  @use_iam_profile = options[:use_iam_profile]

  @instrumentor       = options[:instrumentor]
  @instrumentor_name  = options[:instrumentor_name] || 'fog.aws.cloud_formation'
  @connection_options = options[:connection_options] || {}
  options[:region] ||= 'us-east-1'
  @region = options[:region]
  @host = options[:host] || "cloudformation.#{options[:region]}.amazonaws.com"
  @path       = options[:path]        || '/'
  @persistent = options[:persistent]  || false
  @port       = options[:port]        || 443
  @scheme     = options[:scheme]      || 'https'
  @connection = Fog::XML::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)

  setup_credentials(options)
end

Instance Method Details

#create_stack(stack_name, options = {}) ⇒ Excon::Response

Create a stack.

  • stack_name [String] Name of the stack to create.

  • options [Hash]:

    • TemplateBody [String] Structure containing the template body.

    or (one of the two Template parameters is required)

    • TemplateURL [String] URL of file containing the template body.

    • DisableRollback [Boolean] Controls rollback on stack creation failure, defaults to false.

    • NotificationARNs [Array] List of SNS topics to publish events to.

    • Parameters [Hash] Hash of providers to supply to template

    • TimeoutInMinutes [Integer] Minutes to wait before status is set to CREATE_FAILED

    • Capabilities [Array] List of capabilties the stack is granted. Currently CAPABILITY_IAM for allowing the creation of IAM resources

Returns:

  • (Excon::Response)

    :

    • body [Hash:

      • StackId [String] - Id of the new stack

See Also:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fog/aws/requests/cloud_formation/create_stack.rb', line 26

def create_stack(stack_name, options = {})
  params = {
    'StackName' => stack_name,
  }

  if options['DisableRollback']
    params['DisableRollback'] = options['DisableRollback']
  end

  if options['NotificationARNs']
    params.merge!(Fog::AWS.indexed_param("NotificationARNs.member", [*options['NotificationARNs']]))
  end

  if options['Parameters']
    options['Parameters'].keys.each_with_index do |key, index|
      index += 1 # params are 1-indexed
      params.merge!({
        "Parameters.member.#{index}.ParameterKey"   => key,
        "Parameters.member.#{index}.ParameterValue" => options['Parameters'][key]
      })
    end
  end

  num_tags = 0
  if options['Tags']
    options['Tags'].keys.each_with_index do |key, index|
      index += 1 # tags are 1-indexed
      num_tags += 1 # 10 tag max

      params.merge!({
        "Tags.member.#{index}.Key"   => key,
        "Tags.member.#{index}.Value" => options['Tags'][key]
      })
    end
  end

  if num_tags > 10
    raise ArgumentError.new("a maximum of 10 tags can be specified <#{num_tags}>")
  end

  if options['TemplateBody']
    params['TemplateBody'] = options['TemplateBody']
  elsif options['TemplateURL']
    params['TemplateURL'] = options['TemplateURL']
  end

  if options['TimeoutInMinutes']
    params['TimeoutInMinutes'] = options['TimeoutInMinutes']
  end

  if options['Capabilities']
    params.merge!(Fog::AWS.indexed_param("Capabilities.member", [*options['Capabilities']]))
  end

  request({
    'Action'    => 'CreateStack',
    :parser     => Fog::Parsers::AWS::CloudFormation::CreateStack.new
  }.merge!(params))
end

#delete_stack(stack_name) ⇒ Excon::Response

Delete a stack.

Parameters:

  • stack_name

    String] Name of the stack to create.

Returns:

  • (Excon::Response)

See Also:



15
16
17
18
19
20
21
# File 'lib/fog/aws/requests/cloud_formation/delete_stack.rb', line 15

def delete_stack(stack_name)
  request(
    'Action'    => 'DeleteStack',
    'StackName' => stack_name,
    :parser     => Fog::Parsers::AWS::CloudFormation::Basic.new
  )
end

#describe_stack_events(stack_name, options = {}) ⇒ Excon::Response

Describe stack events.

Parameters:

  • stack_name (String)

    stack name to return events for.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • NextToken (String)

    Identifies the start of the next list of events, if there is one.

Returns:

  • (Excon::Response)
    • body [Hash]:

      • StackEvents [Array] - Matching resources

        • event [Hash]:

          • EventId [String] -

          • StackId [String] -

          • StackName [String] -

          • LogicalResourceId [String] -

          • PhysicalResourceId [String] -

          • ResourceType [String] -

          • Timestamp [Time] -

          • ResourceStatus [String] -

          • ResourceStatusReason [String] -

See Also:



29
30
31
32
33
34
35
# File 'lib/fog/aws/requests/cloud_formation/describe_stack_events.rb', line 29

def describe_stack_events(stack_name, options = {})
  request({
    'Action'    => 'DescribeStackEvents',
    'StackName' => stack_name,
    :parser     => Fog::Parsers::AWS::CloudFormation::DescribeStackEvents.new
  }.merge!(options))
end

#describe_stack_resources(options = {}) ⇒ Excon::Response

Describe stack resources.

Parameters:

  • options (defaults to: {})

    Hash]:

    • PhysicalResourceId [String] name or unique identifier that corresponds to a physical instance ID

    or (one of PhysicalResourceId and StackName is required)

    • StackName [String] Only return events related to this stack name

    • LogicalResourceId [String] Logical name of the resource as specified in the template

Returns:

  • (Excon::Response)
    • body [Hash]:

      • StackResources [Array] - Matching resources

        • resource [Hash]:

          • StackId [String] -

          • StackName [String] -

          • LogicalResourceId [String] -

          • PhysicalResourceId [String] -

          • ResourceType [String] -

          • Timestamp [Time] -

          • ResourceStatus [String] -

See Also:



29
30
31
32
33
34
# File 'lib/fog/aws/requests/cloud_formation/describe_stack_resources.rb', line 29

def describe_stack_resources(options = {})
  request({
    'Action'    => 'DescribeStackResources',
    :parser     => Fog::Parsers::AWS::CloudFormation::DescribeStackResources.new
  }.merge!(options))
end

#describe_stacks(options = {}) ⇒ Excon::Response

Describe stacks.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • StackName (String)

    Name of the stack to describe.

Returns:

  • (Excon::Response)
    • body [Hash]:

      • Stacks [Array] - Matching stacks

        • stack [Hash]:

          • StackName [String] -

          • StackId [String] -

          • CreationTime [String] -

          • StackStatus [String] -

          • DisableRollback [String] -

          • Outputs [Array] -

            • output [Hash]:

              • OutputKey [String] -

              • OutputValue [String] -

See Also:



28
29
30
31
32
33
# File 'lib/fog/aws/requests/cloud_formation/describe_stacks.rb', line 28

def describe_stacks(options = {})
  request({
    'Action'    => 'DescribeStacks',
    :parser     => Fog::Parsers::AWS::CloudFormation::DescribeStacks.new
  }.merge!(options))
end

#get_template(stack_name) ⇒ Excon::Response

Describe stacks.

Parameters:

  • stack_name (String)

    stack name to get template from

Returns:

  • (Excon::Response)
    • body [Hash]:

      • TemplateBody [String] - structure containing the template body (json)

See Also:



17
18
19
20
21
22
23
# File 'lib/fog/aws/requests/cloud_formation/get_template.rb', line 17

def get_template(stack_name)
  request(
    'Action'    => 'GetTemplate',
    'StackName' => stack_name,
    :parser     => Fog::Parsers::AWS::CloudFormation::GetTemplate.new
  )
end

#list_stack_resources(options = {}) ⇒ Excon::Response

List stack resources.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • StackName (String)

    Name of the stack to describe.

Returns:

  • (Excon::Response)
    • body [Hash]:

      • StackResourceSummaries [Array] - Matching stacks

        • resources [Hash]:

          • ResourceStatus [String] -

          • LogicalResourceId [String] -

          • PhysicalResourceId [String] -

          • ResourceType [String] -

          • LastUpdatedTimestamp [Time] -

See Also:



25
26
27
28
29
30
# File 'lib/fog/aws/requests/cloud_formation/list_stack_resources.rb', line 25

def list_stack_resources(options = {})
  request({
    'Action'    => 'ListStackResources',
    :parser     => Fog::Parsers::AWS::CloudFormation::ListStackResources.new
  }.merge!(options))
end

#list_stacks(options = {}) ⇒ Excon::Response

List stacks.

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (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 Also:



26
27
28
29
30
31
# File 'lib/fog/aws/requests/cloud_formation/list_stacks.rb', line 26

def list_stacks(options = {})
  request({
    'Action'    => 'ListStacks',
    :parser     => Fog::Parsers::AWS::CloudFormation::ListStacks.new
  }.merge!(options))
end

#reloadObject



65
66
67
# File 'lib/fog/aws/cloud_formation.rb', line 65

def reload
  @connection.reset
end

#update_stack(stack_name, options = {}) ⇒ Excon::Response

Update a stack.

Parameters:

  • stack_name (String)

    Name of the stack to update.

  • options (Hash) (defaults to: {})
    • TemplateBody [String] Structure containing the template body.

    or (one of the two Template parameters is required)

    • TemplateURL [String] URL of file containing the template body.

    • Parameters [Hash] Hash of providers to supply to template.

    • Capabilities [Array] List of capabilties the stack is granted. Currently CAPABILITY_IAM for allowing the creation of IAM resources.

Returns:

  • (Excon::Response)
    • body [Hash]:

      • StackId [String] - Id of the stack being updated

See Also:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/aws/requests/cloud_formation/update_stack.rb', line 23

def update_stack(stack_name, options = {})
  params = {
    'StackName' => stack_name,
  }

  if options['Parameters']
    options['Parameters'].keys.each_with_index do |key, index|
      index += 1 # params are 1-indexed
      params.merge!({
        "Parameters.member.#{index}.ParameterKey"   => key,
        "Parameters.member.#{index}.ParameterValue" => options['Parameters'][key]
      })
    end
  end

  if options['TemplateBody']
    params['TemplateBody'] = options['TemplateBody']
  elsif options['TemplateURL']
    params['TemplateURL'] = options['TemplateURL']
  end

  if options['Capabilities']
    params.merge!(Fog::AWS.indexed_param("Capabilities.member", [*options['Capabilities']]))
  end

  request({
    'Action'    => 'UpdateStack',
    :parser     => Fog::Parsers::AWS::CloudFormation::UpdateStack.new
  }.merge!(params))
end

#validate_template(options = {}) ⇒ Excon::Response

Describe stacks.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • TemplateBody (String)

    template structure

  • TemplateURL (String)

    template url

Returns:

  • (Excon::Response)
    • body [Hash]:

      • Description [String] - description found within the template

      • Parameters [String] - list of template parameter structures

See Also:



20
21
22
23
24
25
# File 'lib/fog/aws/requests/cloud_formation/validate_template.rb', line 20

def validate_template(options = {})
  request({
    'Action'    => 'ValidateTemplate',
    :parser     => Fog::Parsers::AWS::CloudFormation::ValidateTemplate.new
  }.merge!(options))
end