Class: Kelbim::ELBWrapper::LoadBalancerCollection::LoadBalancer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logger::ClientHelper
Defined in:
lib/kelbim/wrapper/policy.rb,
lib/kelbim/wrapper/listener.rb,
lib/kelbim/wrapper/load-balancer.rb,
lib/kelbim/wrapper/policy-collection.rb,
lib/kelbim/wrapper/listener-collection.rb

Defined Under Namespace

Classes: ListenerCollection

Instance Method Summary collapse

Methods included from Logger::ClientHelper

#log

Constructor Details

#initialize(load_balancer, options) ⇒ LoadBalancer

Returns a new instance of LoadBalancer.



12
13
14
15
# File 'lib/kelbim/wrapper/load-balancer.rb', line 12

def initialize(load_balancer, options)
  @load_balancer = load_balancer
  @options = options
end

Instance Method Details

#deleteObject



170
171
172
173
174
175
176
177
# File 'lib/kelbim/wrapper/load-balancer.rb', line 170

def delete
  log(:info, 'Delete LoadBalancer', :red, log_id)

  unless @options.dry_run
    @load_balancer.delete
    @options.updated = true
  end
end

#eql?(dsl) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kelbim/wrapper/load-balancer.rb', line 21

def eql?(dsl)
  compare_scheme(dsl) do
    log(:warn, "`scheme`('internet-facing' or 'internal') cannot be updated", :yellow, "#{vpc_id || :classic} > #{name}")
  end

  compare_health_check(dsl) or return false
  compare_attributes(dsl) or return false

  if self.vpc_id
    compare_subnet_ids(dsl) or return false
    compare_security_groups(dsl) or return false
  else
    compare_availability_zones(dsl) or return false
  end

  compare_instances(dsl)
end

#listenersObject



17
18
19
# File 'lib/kelbim/wrapper/load-balancer.rb', line 17

def listeners
  ListenerCollection.new(@load_balancer.listeners, self, @options)
end

#update(dsl) ⇒ Object



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
# File 'lib/kelbim/wrapper/load-balancer.rb', line 39

def update(dsl)
  log(:info, 'Update LoadBalancer', :green, log_id)

  compare_instances(dsl) do |aws_instance_ids, dsl_instance_ids|
    instance_names = @options.instance_names[self.vpc_id] || {}

    add_ids = (dsl_instance_ids - aws_instance_ids)

    unless add_ids.empty?
      log(:info, '  register instances=' + add_ids.map {|i| instance_names.fetch(i, i) }.join(', '), :green)

      unless @options.dry_run
        @load_balancer.instances.register(*add_ids)
        @options.updated = true
      end
    end

    del_ids = (aws_instance_ids - dsl_instance_ids)

    unless del_ids.empty?
      log(:info, '  deregister instances=' + del_ids.map {|i| instance_names.fetch(i, i) }.join(', '), :green)

      unless @options.dry_run
        @load_balancer.instances.deregister(*del_ids)
        @options.updated = true
      end
    end
  end

  compare_health_check(dsl) do |old_data, new_data|
    log(:info, '  health_check:', :green)
    log(:info, Kelbim::Utils.diff(old_data, new_data, :color => @options[:color], :indent => '    '), false)

    unless @options.dry_run
      @load_balancer.configure_health_check(dsl.health_check)
      @options.updated = true
    end
  end

  compare_attributes(dsl) do |old_data, new_data|
    log(:info, '  attributes:', :green)
    log(:info, Kelbim::Utils.diff(old_data, new_data, :color => @options[:color], :indent => '    '), false)

    unless @options.dry_run
      @load_balancer.attributes = dsl.attributes
      @options.updated = true
    end
  end

  if self.vpc_id
    compare_subnet_ids(dsl) do |aws_subnet_ids, dsl_subnet_ids|
      add_ids = (dsl_subnet_ids - aws_subnet_ids)

      unless add_ids.empty?
        log(:info, '  attach subnets=' + add_ids.join(', '), :green)

        unless @options.dry_run
          @options.elb.client.attach_load_balancer_to_subnets(
            :load_balancer_name => @load_balancer.name,
            :subnets => add_ids,
          )

          @options.updated = true
        end
      end

      del_ids = (aws_subnet_ids - dsl_subnet_ids)

      unless del_ids.empty?
        log(:info, '  detach subnets=' + del_ids.join(', '), :green)

        unless @options.dry_run
          @options.elb.client.detach_load_balancer_from_subnets(
            :load_balancer_name => @load_balancer.name,
            :subnets => del_ids,
          )

          @options.updated = true
        end
      end
    end

    compare_security_groups(dsl) do |aws_sg_ids, dsl_sg_ids|
      sg_names = @options.security_group_names[self.vpc_id] || {}
      old_data = aws_sg_ids.map {|i| sg_names.fetch(i, i) }.sort
      new_data = dsl_sg_ids.map {|i| sg_names.fetch(i, i) }.sort
      log(:info, '  security groups:', :green)
      log(:info, Kelbim::Utils.diff(old_data, new_data, :color => @options[:color], :indent => '    '), false)

      unless @options.dry_run
        @options.elb.client.apply_security_groups_to_load_balancer(
          :load_balancer_name => @load_balancer.name,
          :security_groups => dsl_sg_ids,
        )

        @options.updated = true
      end
    end
  else
    compare_availability_zones(dsl) do |aws_az_names, dsl_az_names|
      add_names = (dsl_az_names - aws_az_names)

      unless add_names.empty?
        log(:info, '  enable availability zones=' + add_names.join(', '), :green)

        unless @options.dry_run
          add_names.each do |az_name|
            @load_balancer.availability_zones.enable(az_name)
          end

          @options.updated = true
        end
      end

      del_names = (aws_az_names - dsl_az_names)

      unless del_names.empty?
        log(:info, '  disable availability zones=' + del_names.join(', '), :green)

        unless @options.dry_run
          del_names.each do |az_name|
            @load_balancer.availability_zones.disable(az_name)
          end

          @options.updated = true
        end
      end
    end
  end
end