File: stack.rb

package info (click to toggle)
ruby-aws-sdk 1.67.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,840 kB
  • sloc: ruby: 28,436; makefile: 7
file content (266 lines) | stat: -rw-r--r-- 7,952 bytes parent folder | download | duplicates (4)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.


module AWS
  class CloudFormation

    # @attr_reader [String] template Returns the stack's template as a JSON
    #   string.
    #
    # @attr_reader [Time] creation_time The time the stack was created.
    #
    # @attr_reader [Time,nil] last_updated_time The time the stack was
    #   last updated.
    #
    # @attr_reader [String] stack_id Unique stack identifier.
    #
    # @attr_reader [String] status The status of the stack.
    #
    # @attr_reader [String] status_reason Success/Failure message
    #   associated with the `status`.
    #
    # @attr_reader [Array<String>] capabilities The capabilities
    #   allowed in the stack.
    #
    # @attr_reader [String] description User defined description
    #   associated with the stack.
    #
    # @attr_reader [Boolean] disable_rollback Specifies if the stack
    #   is rolled back due to stack creation errors.
    #
    # @attr_reader [Array<String>] notification_arns
    #   SNS topic ARNs to which stack related events are published.
    #
    # @attr_reader [Hash] parameters Returns a hash of stack parameters.
    #
    # @attr_reader [Integer] timeout
    #   The number of minutes within the stack creation should complete.
    #
    class Stack < Core::Resource

      include StackOptions

      # @api private
      def initialize name, options = {}
        @name = name
        super
      end

      # @return [String] Returns the stack name.
      attr_reader :name

      define_attribute_type :template

      define_attribute_type :list

      define_attribute_type :describe

      # returned by GetTemplate

      template_attribute :template, :from => :template_body

      alias_method :template_body, :template

      # returned by ListStacks & DescribeStacks

      list_attribute :creation_time, :static => true

      list_attribute :last_updated_time

      list_attribute :stack_id, :static => true

      list_attribute :status, :from => :stack_status

      list_attribute :status_reason, :from => :stack_status_reason

      # returned by DescribeStacks

      describe_attribute :capabilities

      describe_attribute :description

      describe_attribute :disable_rollback, :from => :disable_rollback?

      alias_method :disable_rollback?, :disable_rollback

      describe_attribute :notification_arns

      describe_attribute :output_details, :from => :outputs

      protected :output_details

      describe_attribute :parameters do
        translates_output do |params|
          params.inject({}) do |hash,param|
            hash.merge(param[:parameter_key] => param[:parameter_value])
          end
        end
      end

      describe_attribute :timeout, :from => :timeout_in_minutes

      alias_method :timeout_in_minutes, :timeout

      # attribute providers

      provider(:describe_stacks) do |provider|
        provider.find do |resp|
          resp.data[:stacks].find{|stack| stack[:stack_name] == name }
        end
        provider.provides(*(list_attributes.keys + describe_attributes.keys))
      end

      provider(:list_stacks) do |provider|
        provider.find do |resp|
          resp.data[:stack_summaries].find{|stack| stack[:stack_name] == name }
        end
        provider.provides *list_attributes.keys
      end

      provider(:get_template) do |provider|
        provider.find do |resp|
          resp if resp.request_options[:stack_name] == name
        end
        provider.provides *template_attributes.keys
      end

      # @return [Array<StackOutput>]
      def outputs
        output_details.collect do |o|
          key, value, desc = o.values_at(:output_key, :output_value, :description)
          StackOutput.new(self, key, value, desc)
        end
      end

      # @return [StackEventCollection] Returns a collection that represents
      #   all events for this stack.
      def events
        StackEventCollection.new(self)
      end

      # Returns a stack resource collection that enumerates all resources
      # for this stack.
      #
      #     stack.resources.each do |resource|
      #       puts "#{resource.resource_type}: #{resource.physical_resource_id}"
      #     end
      #
      # If you want a specific resource and you know its logical resource
      # id, you can use this collection to return a reference to it.
      #
      #     resource = stack.resources['logical-resource-id']
      #
      # @return [StackResourceCollection]
      #
      def resources
        StackResourceCollection.new(self)
      end

      # Returns a stack resource summary collection, that when enumerated
      # yields summary hashes.  Each hash has the following keys:
      #
      # * `:last_updated_timestamp`
      # * `:logical_resource_id`
      # * `:physical_resource_id`
      # * `:resource_status`
      # * `:resource_status_reason`
      # * `:resource_type`
      #
      # @return [StackResourceSummaryCollection]
      #
      def resource_summaries
        StackResourceSummaryCollection.new(self)
      end

      # @param [Hash] options
      #
      # @option options [String,URI,S3::S3Object,Object] :template
      #   A new stack template.  This may be provided in a number of formats
      #   including:
      #
      #     * a String, containing the template as a JSON document.
      #     * a URL String pointing to the document in S3.
      #     * a URI object pointing to the document in S3.
      #     * an {S3::S3Object} which contains the template.
      #     * an Object which responds to #to_json and returns the template.
      #
      # @option options [Hash] :parameters A hash that specifies the
      #   input parameters of the new stack.
      #
      # @option options[Array<String>] :capabilities The list of capabilities
      #   that you want to allow in the stack. If your stack contains IAM
      #   resources, you must specify the CAPABILITY_IAM value for this
      #   parameter; otherwise, this action returns an
      #   InsufficientCapabilities error. IAM resources are the following:
      #
      #     * AWS::IAM::AccessKey
      #     * AWS::IAM::Group
      #     * AWS::IAM::Policy
      #     * AWS::IAM::User
      #     * AWS::IAM::UserToGroupAddition
      #
      # @return [nil]
      #
      def update options = {}
        client_opts = options.dup

        apply_stack_name(name, client_opts)
        apply_template(client_opts)
        apply_parameters(client_opts)

        client.update_stack(client_opts)

        nil
      end

      # @return (see CloudFormation#estimate_template_cost)
      def estimate_template_cost
        cloud_formation = CloudFormation.new(:config => config)
        cloud_formation.estimate_template_cost(template, parameters)
      end

      # Deletes the current stack.
      # @return [nil]
      def delete
        client.delete_stack(:stack_name => name)
        nil
      end

      # @return [Boolean]
      def exists?
        begin
          client.describe_stacks(resource_options)
          true
        rescue Errors::ValidationError
          false
        end
      end

      protected

      def resource_identifiers
        [[:stack_name, name]]
      end

      def get_resource attribute
        if attribute.name == :template
          client.get_template(resource_options)
        else
          client.describe_stacks(resource_options)
        end
      end

    end
  end
end