File: availability_zone_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 (138 lines) | stat: -rw-r--r-- 4,712 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
# 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 ELB

    # A collection that help maanage the availability zones for
    # a load balancer.
    #
    #     load_balancer = AWS::ELB.new.load_balancers['my-load-balancer']
    #
    #     zones = load_balancer.availability_zones
    #
    #     # adding zones
    #     zones.enable('us-west-2b', 'us-west-2c')
    #
    #     # removing zones
    #     zones.disable('us-west-2b')
    #
    #     # enumerating enabled zones
    #     zones.each do |zone|
    #       puts zone.name
    #     end
    #
    class AvailabilityZoneCollection

      include Core::Collection::Simple

      # @param [LoadBalancer] load_balancer The load balancer this list of
      #   availability zones belongs to.
      def initialize load_balancer, options = {}
        @load_balancer = load_balancer
        super
      end

      # @return [LoadBalancer] Returns the load balancer this list describes.
      attr_reader :load_balancer

      # Adds one or more EC2 Availability Zones to the load balancer.
      #
      #     load_balancer.availability_zones.enable("us-west-2a", "us-west-2b")
      #
      # You can also pass {EC2::AvailabilityZone} objects:
      #
      #     # enable all availabilty zones for this region
      #     zones = AWS::EC2.new.availability_zones.to_a
      #     load_balancer.availability_zones.enable(zones)
      #
      # The load balancer evenly distributes requests across all its
      # registered availability zones that contain instances. As a result,
      # the client must ensure that its load balancer is appropriately
      # scaled for each registered Availability Zone.
      #
      # @param [String,EC2::AvailabilityZone] availability_zones One or more
      #   availability zone names (strings) or objects {EC2::AvailabilityZone}.
      #
      # @return [nil]
      #
      def enable *availability_zones

        names = availability_zones.flatten.collect do |av|
          av.is_a?(EC2::AvailabilityZone) ? av.name : av.to_s
        end

        client.enable_availability_zones_for_load_balancer(
          :load_balancer_name => load_balancer.name,
          :availability_zones => names)

        nil

      end

      # Removes the specified EC2 availability zones from the set of
      # configured availability zones for the load balancer.
      #
      #     load_balancer.availability_zones.disable("us-west-2a", "us-west-2b")
      #
      # You can also pass {EC2::AvailabilityZone} objects:
      #
      #     # disable all availabilty zones
      #     zones = AWS::EC2.new.availability_zones.to_a
      #     load_balancer.availability_zones.disable(zones)
      #
      # There must be at least one availability zone registered with a
      # load balancer at all times. A client cannot remove all the availability
      # zones from a load balancer. Once an availability zone is removed,
      # all the instances registered with the load balancer that are in the
      # removed availability zone go into the out of service state.
      #
      # Upon availability zone removal, the load balancer attempts to
      # equally balance the traffic among its remaining usable availability
      # zones. Trying to remove an availability zone that was not
      # associated with the load balancer does nothing.
      #
      # @param [String,EC2::AvailabilityZone] availability_zones One or more
      #   availability zone names (strings) or objects {EC2::AvailabilityZone}.
      #
      # @return [nil]
      #
      def disable *availability_zones

        names = availability_zones.flatten.collect do |av|
          av.is_a?(EC2::AvailabilityZone) ? av.name : av.to_s
        end

        client.disable_availability_zones_for_load_balancer(
          :load_balancer_name => load_balancer.name,
          :availability_zones => names)

        nil

      end

      protected
      def _each_item options = {}, &block
        load_balancer.availability_zone_names.each do |az_name|

          availability_zone = EC2::AvailabilityZone.new(
            az_name, :config => config)

          yield(availability_zone)

        end
      end

    end
  end
end