File: update_stack.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (105 lines) | stat: -rw-r--r-- 4,346 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module Fog
  module AWS
    class CloudFormation
      class Real
        require 'fog/aws/parsers/cloud_formation/update_stack'

        # Update a stack.
        #
        # @param [String] stack_name Name of the stack to update.
        # @param [Hash] options
        #   * 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.
        #   * NotificationARNs [Array] List of SNS topics to publish events to.
        #   * ResourceTypes [Array] The template resource types that you have permissions to work.
        #   * StackPolicyBody [String] Structure containing the stack policy body.
        #   * StackPolicyURL [String] URL of file containing the stack policy.
        #   * StackPolicyDuringUpdateBody [String] Structure containing the stack policy body to use during update.
        #   * StackPolicyDuringUpdateURL [String] URL of file containing the stack policy to use during update.
        #   * Tags [Array] Key-value pairs to associate with this stack.
        #   * UsePreviousTemplate [Boolean] Reuse the existing template that is associated with the stack that you are updating.
        #
        # @return [Excon::Response]
        #   * body [Hash]:
        #     * StackId [String] - Id of the stack being updated
        #
        # @see http://docs.amazonwebservices.com/AWSCloudFormation/latest/APIReference/API_UpdateStack.html
        #
        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['StackPolicyBody']
            params['StackPolicyBody'] = options['StackPolicyBody']
          elsif options['StackPolicyURL']
            params['StackPolicyURL'] = options['StackPolicyURL']
          end

          if options['StackPolicyDuringUpdateBody']
            params['StackPolicyDuringUpdateBody'] = options['StackPolicyDuringUpdateBody']
          elsif options['StackPolicyDuringUpdateURL']
            params['StackPolicyDuringUpdateURL'] = options['StackPolicyDuringUpdateURL']
          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['Capabilities']
            params.merge!(Fog::AWS.indexed_param("Capabilities.member", [*options['Capabilities']]))
          end

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

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

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

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