File: load_balancer.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 (280 lines) | stat: -rw-r--r-- 10,109 bytes parent folder | download | duplicates (3)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# 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

    # @attr_reader [String] name The name of the load balancer.
    #
    # @attr_reader [Array<String>] availability_zone_names Return the names of
    #   the availability zones this load balancer routes traffic to.
    #
    # @attr_reader [String] canonical_hosted_zone_name Provides the name of
    #   the Amazon Route 53 hosted zone that is associated with the load
    #   balancer.  For more information: [using-domain-names-with-elb.html](http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/index.html?using-domain-names-with-elb.html).
    #
    # @attr_reader [String] canonical_hosted_zone_name_id Provides the ID of
    #   the Amazon Route 53 hosted zone name that is associated with the
    #   load balancer.  For more information: [using-domain-names-with-elb.html](http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/index.html?using-domain-names-with-elb.html).
    #
    # @attr_reader [String] dns_name Specifies the external DNS name
    #   associated with this load balancer.
    #
    # @attr_reader [Hash] policy_descriptions Returns a hash of
    #   `:app_cookie_stickiness_policies`, `:lb_cookie_stickiness_policies`
    #   and `:other_policies`.  See also {#policies}.
    #
    # @attr_reader [String,nil] scheme Specifies the type of LoadBalancer.
    #   This attribute it set only for LoadBalancers attached to an Amazon VPC.
    #   If the Scheme is 'internet-facing', the LoadBalancer has a publicly
    #   resolvable DNS name that resolves to public IP addresses.
    #   If the Scheme is 'internal', the LoadBalancer has a publicly
    #   resolvable DNS name that resolves to private IP addresses.
    #
    # @attr_reader [Array<String>] subnet_ids Provides a list of VPC subnet IDs
    #   for the LoadBalancer.
    #
    # @attr_reader [Hash] health_check
    #   Returns a hash of the various health probes conducted on the
    #   load balancer instances.  The following entries are returned:
    #   * `:healthy_threshold`
    #   * `:unhealthy_threshold`
    #   * `:interval`
    #   * `:target`
    #   * `:timeout`
    #   See {#configure_health_check} for more details on what each of the
    #   configuration values mean.
    #
    # @return [Hash]
    #
    #
    class LoadBalancer < Core::Resource

      def initialize name, options = {}
        super(options.merge(:name => name.to_s))
      end

      attribute :name, :from => :load_balancer_name, :static => true

      # see #availability_zones
      attribute :availability_zone_names, :from => :availability_zones

      # see #backend_server_policies
      attribute :backend_server_descriptions

      attribute :canonical_hosted_zone_name, :static => true

      attribute :canonical_hosted_zone_name_id, :static => true

      attribute :created_time, :static => true

      attribute :dns_name, :static => true

      attribute :health_check, :alias => :health_check_configuration

      # see #instances
      attribute :instance_descriptions, :from => :instances

      # see #listeners
      attribute :listener_descriptions

      attribute :policy_descriptions, :from => :policies

      attribute :scheme, :static => true

      attribute :subnet_ids, :from => :subnets, :static => true

      attribute :security_group_ids, :from => :security_groups, :static => true

      attribute :source_security_group_owner_alias,
        :from => [:source_security_group, :owner_alias],
        :static => true

      attribute :source_security_group_name,
        :from => [:source_security_group, :group_name],
        :static => true

      populates_from(:describe_load_balancers) do |resp|
        resp.data[:load_balancer_descriptions].find do |lb|
          lb[:load_balancer_name] == name
        end
      end

      # A collection that help maanage the availability zones for
      # this load balancer.
      #
      # @example enable an availability zone
      #
      #   load_balancer.availability_zones.enable('us-west-2b')
      #
      # @example disable an availability zone
      #
      #   load_balancer.availability_zones.disable('us-west-2b')
      #
      # @example list enabled availability zoens
      #
      #   load_balancer.availability_zones.each do |zone|
      #     puts zone.name
      #   end
      #
      # @return [AvailabilityZoneCollection] Returns a collection that
      #   represents this load balancer's availability zones.  You can
      #   use this collection to enable and disable availability zones.
      def availability_zones
        AvailabilityZoneCollection.new(self)
      end

      # @return [ListenerCollection]
      def listeners
        ListenerCollection.new(self)
      end

      # @return [PolicyCollection]
      def policies
        LoadBalancerPolicyCollection.new(self)
      end

      # @return [InstanceCollection]
      def instances
        InstanceCollection.new(self)
      end

      # @return [BackendServerPolicyCollection]
      def backend_server_policies
        BackendServerPolicyCollection.new(self)
      end

      # Updates the configuration that drives the instance health checks.
      #
      # You only need to pass the options you want to change.  You can
      # call {#health_check} if you want to see what the
      # current configuration values are.
      #
      # @param [Hash] options
      #
      # @option options [Integer] :healthy_threshold Specifies the number of
      #   consecutive health probe successes required before moving the
      #   instance to the Healthy state.
      #
      # @option options [Integer] :unhealthy_threshold Specifies the number
      #   of consecutive health probe failures required before moving the
      #   instance to the Unhealthy state.
      #
      # @option options [Integer] :interval Specifies the approximate
      #   interval, in seconds, between health checks of an individual
      #   instance.
      #
      # @option options [Integer] :timeout Specifies the amount of time, in
      #   seconds, during which no response means a failed health probe.
      #   This value must be less than the `:interval` value.
      #
      # @option options [String] :target Specifies the instance being checked.
      #
      #   This option should be formatted like: "TCP:80"
      #
      #   * The protocol is either TCP, HTTP, HTTPS, or SSL.
      #   * The range of valid ports is one (1) through 65535.
      #
      #   TCP is the default, specified as a TCP: port pair, for example
      #   "TCP:5000". In this case a healthcheck simply attempts to open a
      #   TCP connection to the instance on the specified port. Failure to
      #   connect within the configured timeout is considered unhealthy.
      #
      #   SSL is also specified as SSL: port pair, for example, SSL:5000.
      #   For HTTP or HTTPS protocol, the situation is different. You have
      #   to include a ping path in the string. HTTP is specified as a
      #   HTTP:port;/;PathToPing; grouping, for example
      #   "HTTP:80/weather/us/wa/seattle". In this case, a HTTP GET request
      #   is issued to the instance on the given port and path. Any answer
      #   other than "200 OK" within the timeout period is considered
      #   unhealthy.
      #
      #   The total length of the HTTP ping target needs to be 1024 16-bit
      #   Unicode characters or less.
      #
      def configure_health_check options = {}

        new_config = health_check.merge(options)

        response = client.configure_health_check(
          :load_balancer_name => name,
          :health_check => new_config)

        new_config

      end

      # @note VPC only
      # @return [Array<EC2::Subnet>] Returns an array of VPC subnets
      #   for this load balancer.
      def subnets
        subnet_ids.map{|id| EC2::Subnet.new(id, :config => config) }
      end

      # @note VPC only
      # Returns the VPC security groups assigned to this load balancer.
      # @return [Array<EC2::SecurityGroup>]
      def security_groups
        security_group_ids.collect do |id|
          EC2::SecurityGroup.new(id, :config => config)
        end
      end

      # Generally you don't need to call this method, rather you can
      # just pass the load balancer as a source to the various
      # authorize and revoke methods of {EC2::SecurityGroup}:
      #
      #     security_group.authorize_ingress(load_balancer)
      #     security_group.revoke_ingress(load_balancer)
      #
      # @return [Hash] Returns a hash that can be passed to the following
      #   {EC2::SecurityGroup} methods as a source:
      #
      #   * {EC2::SecurityGroup#authorize_ingress}
      #   * {EC2::SecurityGroup#authorize_egress}
      #
      def source_security_group
        {
          :group_name => source_security_group_name,
          :user_id => source_security_group_owner_alias,
        }
      end

      # @return [Boolean] Returns true if the load balancer exists.
      def exists?
        client.describe_load_balancers(:load_balancer_names => [name])
        true
      rescue Errors::LoadBalancerNotFound
        false
      end

      # Deletes the load balancer.
      # @return [nil]
      def delete
        client.delete_load_balancer(:load_balancer_name => name)
        nil
      end

      protected

      def resource_identifiers
        [[:load_balancer_name, name]]
      end

      def get_resource attr_name
        client.describe_load_balancers(:load_balancer_names => [name])
      end

    end
  end
end