File: scheduled_action_collection.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 (202 lines) | stat: -rw-r--r-- 6,012 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
# 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.

require 'time'

module AWS
  class AutoScaling

    class ScheduledActionCollection

      include Core::Collection::WithLimitAndNextToken

      # @api private
      def initialize options = {}
        @filters = options[:filters] || {}
        super
      end

      # Creates a scheduled scaling action for an Auto Scaling group.
      # If you leave a parameter unspecified, the corresponding attribute
      # remains unchanged in the group.
      #
      # You must specify an Auto Scaling group.  This can be implicit
      # or explicit:
      #
      #     # given explicitly
      #     auto_scaling.scheduled_actions.create('action-name', :group => 'group-name')
      #
      #     # implied by the group
      #     group = auto_scaling.groups.first
      #     group.scheduled_actions.create('action-name')
      #
      # @param [String] name
      #
      # @param [Hash] options
      #
      # @option options [Group,String] :group
      #
      # @option options [Integer] :desired_capacity
      #
      # @option options [Integer] :max_size
      #
      # @option options [Integer] :min_size
      #
      # @option options [String] :recurrence
      #
      # @option options [Time] :start_time
      #
      # @option options [Time] :end_time
      #
      # @return [ScheduledAction]
      #
      def create name, options = {}

        group = auto_scaling_group(options)

        scheduled_action = ScheduledAction.new(group, name,
          :auto_scaling_group_name => group.name,
          :config => config)

        scheduled_action.update(options)
        scheduled_action

      end
      alias_method :put, :create

      # @param [String] name The name of the scheduled action.
      # @return [ScheduledAction]
      def [] name
        if group_name = @filters[:auto_scaling_group_name]
          group = Group.new(group_name, :config => config)
          ScheduledAction.new(group, name)
        else
          msg = 'uou must filter this collection by a group to get a ' +
            'scheduled action by name'
          raise msg
        end
      end

      # Returns a new {ScheduledActionCollection} filtered
      # by the given options.
      #
      #     auto_scaling.scheduled_actions.filter(:end_time => Time.now).each do |a|
      #        # ...
      #     end
      #
      # You can chain filter calls:
      #
      #     actions = auto_scaling.scheduled_actions.
      #        filter(:group => 'auto-scaling-group-name').
      #        filter(:start_time => Time.now - 3600).
      #        filter(:end_time => Time.now)
      #
      #     actions.each {|scheduled_action| ... }
      #
      # @param [Hash] filters
      #
      # @option filters [Group,String] :group
      #
      # @option filters [Array<String>] :scheduled_actions
      #    A list of scheduled actions to be described. If this list is
      #    omitted, all scheduled actions are described. The list of
      #    requested scheduled actions cannot contain more than 50 items.
      #    If an Auto Scaling group name is provided,
      #    the results are limited to that group. If unknown scheduled
      #    actions are requested, they are ignored with no error.
      #
      # @option options [Time,String] :start_time The earliest scheduled
      #   start time to return. If `:scheduled_actions` is provided,
      #   this field will be ignored.  Should be a Time object or
      #   an iso8601 string.
      #
      # @option filters [Time,String] :end_time
      #
      # @return [ScheduledActionCollection] Returns a scheduled action
      #   collection that will filter the actions returned by the
      #   given criteria.
      #
      def filter filters = {}
        init_opts = {}
        init_opts[:config] = config
        init_opts[:filters] = @filters
        init_opts[:filters].merge!(filter_opts(filters))
        ScheduledActionCollection.new(init_opts)
      end

      protected

      def auto_scaling_group(options)

        group = options.delete(:group)
        group ||= @filters[:auto_scaling_group_name]
        group = Group.new(group, :config => config) if group.is_a?(String)

        unless group
          raise ArgumentError, 'missing required option :group'
        end

        group

      end

      def filter_opts options

        opts = {}

        if g = options[:group]
          opts[:auto_scaling_group_name] = g.is_a?(Group) ? g.name : g
        end

        if actions = options[:scheduled_actions]
          opts[:scheduled_action_names] = actions
        end

        [:end_time, :start_time].each do |opt|
          if options[opt].is_a?(Time)
            opts[opt] = options[opt].iso8601
          end
        end

        opts

      end

      def _each_item next_token, limit, options = {}, &block

        options[:next_token] = next_token if next_token
        options[:max_records] = limit if limit

        resp = client.describe_scheduled_actions(options.merge(@filters))
        resp.scheduled_update_group_actions.each do |details|

          group = Group.new(details[:auto_scaling_group_name], :config => config)

          scheduled_action = ScheduledAction.new_from(
            :describe_scheduled_actions,
            details,
            group,
            details.scheduled_action_name,
            :config => config)

          yield(scheduled_action)

        end

        resp.data[:next_token]

      end

    end
  end
end