Class: Cumulus::ELB::LoadBalancerConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/elb/models/LoadBalancerConfig.rb

Overview

Public: An object representing configuration for a load balancer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, json = nil) ⇒ LoadBalancerConfig

Public: Constructor

json - a hash containing the JSON configuration for the load balancer



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
# File 'lib/elb/models/LoadBalancerConfig.rb', line 40

def initialize(name, json = nil)
  @name = name
  if !json.nil?

    # load the included listeners and templates
    @listeners = []
    if json["listeners"]
      if json["listeners"]["includes"]
        json["listeners"]["includes"].each do |template_json|
          @listeners << Loader.listener(template_json["template"], template_json["vars"])
        end
      end
      if json["listeners"]["inlines"]
        json["listeners"]["inlines"].each do |inline|
          @listeners << ListenerConfig.new(inline)
        end
      end
    end

    # Map subnets to their actual subnet description from aws
    @subnets = (json["subnets"] || []).map do |subnet|
      full_subnet = EC2::named_subnets[subnet]

      if full_subnet.nil?
        raise "#{subnet} is not a valid subnet name or id"
      else
        full_subnet
      end
    end

    # Map backend policies to the Aws::ElasticLoadBalancing::Types::BackendServerDescription
    # reject any local polices with empty policies array which means it should be deleted from aws
    @backend_policies = (json["backend-policies"] || []).map do |backend|
      Aws::ElasticLoadBalancing::Types::BackendServerDescription.new({
        instance_port: backend["port"],
        policy_names: backend["policies"]
      })
    end.reject { |backend| backend.policy_names.empty? }

    @security_groups = json["security-groups"] || []
    @internal = json["internal"] || false
    @tags = json["tags"] || {}
    @manage_instances = json["manage-instances"] || false
    @health_check = if json["health-check"] then HealthCheckConfig.new(json["health-check"]) end
    @cross_zone = json["cross-zone"] || false
    @access_log = if json["access-log"] then AccessLogConfig.new(json["access-log"]) else false end
    @connection_draining = json["connection-draining"] || false
    @idle_timeout = json["idle-timeout"]
  end
end

Instance Attribute Details

#access_logObject (readonly)

Returns the value of attribute access_log.



29
30
31
# File 'lib/elb/models/LoadBalancerConfig.rb', line 29

def access_log
  @access_log
end

#availability_zonesObject (readonly)

Returns the value of attribute availability_zones.



21
22
23
# File 'lib/elb/models/LoadBalancerConfig.rb', line 21

def availability_zones
  @availability_zones
end

#backend_policiesObject (readonly)

Returns the value of attribute backend_policies.



32
33
34
# File 'lib/elb/models/LoadBalancerConfig.rb', line 32

def backend_policies
  @backend_policies
end

#connection_drainingObject (readonly)

Returns the value of attribute connection_draining.



30
31
32
# File 'lib/elb/models/LoadBalancerConfig.rb', line 30

def connection_draining
  @connection_draining
end

#cross_zoneObject (readonly)

Returns the value of attribute cross_zone.



28
29
30
# File 'lib/elb/models/LoadBalancerConfig.rb', line 28

def cross_zone
  @cross_zone
end

#health_checkObject (readonly)

Returns the value of attribute health_check.



27
28
29
# File 'lib/elb/models/LoadBalancerConfig.rb', line 27

def health_check
  @health_check
end

#idle_timeoutObject (readonly)

Returns the value of attribute idle_timeout.



31
32
33
# File 'lib/elb/models/LoadBalancerConfig.rb', line 31

def idle_timeout
  @idle_timeout
end

#internalObject (readonly)

Returns the value of attribute internal.



24
25
26
# File 'lib/elb/models/LoadBalancerConfig.rb', line 24

def internal
  @internal
end

#listenersObject (readonly)

Returns the value of attribute listeners.



20
21
22
# File 'lib/elb/models/LoadBalancerConfig.rb', line 20

def listeners
  @listeners
end

#manage_instancesObject (readonly)

Returns the value of attribute manage_instances.



26
27
28
# File 'lib/elb/models/LoadBalancerConfig.rb', line 26

def manage_instances
  @manage_instances
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/elb/models/LoadBalancerConfig.rb', line 19

def name
  @name
end

#security_groupsObject (readonly)

Returns the value of attribute security_groups.



23
24
25
# File 'lib/elb/models/LoadBalancerConfig.rb', line 23

def security_groups
  @security_groups
end

#subnetsObject (readonly)

Returns the value of attribute subnets.



22
23
24
# File 'lib/elb/models/LoadBalancerConfig.rb', line 22

def subnets
  @subnets
end

#tagsObject (readonly)

Returns the value of attribute tags.



25
26
27
# File 'lib/elb/models/LoadBalancerConfig.rb', line 25

def tags
  @tags
end

Instance Method Details

#diff(aws) ⇒ Object

Public: Produce an array of differences between this local configuration and the configuration in AWS

aws - the AWS resource

Returns an array of the LoadBalancerDiffs that were found



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
# File 'lib/elb/models/LoadBalancerConfig.rb', line 159

def diff(aws)
  diffs = []

  listener_diff = LoadBalancerDiff.listeners(aws.listener_descriptions, @listeners)
  if listener_diff
    diffs << listener_diff
  end

  aws_subnets = aws.subnets.map { |subnet_id| EC2::id_subnets[subnet_id] }
  if @subnets.sort != aws_subnets.sort
    diffs << LoadBalancerDiff.subnets(aws_subnets, @subnets)
  end

  named_aws_security_groups = aws.security_groups.map do |sg_id|
    SecurityGroups::id_security_groups[sg_id].group_name
  end
  if @security_groups.sort != named_aws_security_groups.sort
    diffs << LoadBalancerDiff.security_groups(named_aws_security_groups, @security_groups)
  end

  aws_internal = (aws.scheme == "internal")
  if @internal != aws_internal
    diffs << LoadBalancerDiff.internal(aws_internal, @internal)
  end

  aws_tags = Hash[ELB::elb_tags(@name).map { |tag| [tag.key, tag.value] }]
  if @tags != aws_tags
    diffs << LoadBalancerDiff.tags(aws_tags, @tags)
  end

  aws_instances = (aws.instances || []).map { |i| i.instance_id }
  if (@manage_instances != false) && @manage_instances.sort != aws_instances.sort
    diffs << LoadBalancerDiff.instances(aws_instances, @manage_instances)
  end

  aws_health_check = aws.health_check
  health_diffs = @health_check.diff(aws_health_check)
  if !health_diffs.empty?
    diffs << LoadBalancerDiff.health_check(health_diffs)
  end

  aws_backend_policies = aws.backend_server_descriptions
  if @backend_policies.sort != aws_backend_policies.sort
    diffs << LoadBalancerDiff.backend_policies(aws_backend_policies, @backend_policies)
  end

  aws_attributes = ELB::elb_attributes(@name)

  aws_cross_zone = (aws_attributes.cross_zone_load_balancing.enabled) rescue false
  if @cross_zone != aws_cross_zone
    diffs << LoadBalancerDiff.new(LoadBalancerChange::CROSS, aws_cross_zone, @cross_zone)
  end

  aws_access_log = aws_attributes.access_log
  if @access_log == false
    if aws_access_log.enabled == true
      log_diffs = (AccessLogConfig.new).diff(aws_access_log)
      diffs << LoadBalancerDiff.access_log(log_diffs)
    end
  else
    log_diffs = @access_log.diff(aws_access_log)
    if !log_diffs.empty?
      diffs << LoadBalancerDiff.access_log(log_diffs)
    end
  end

  aws_connection_draining = if aws_attributes.connection_draining.enabled then aws_attributes.connection_draining.timeout else false end
  if @connection_draining != aws_connection_draining
    diffs << LoadBalancerDiff.new(LoadBalancerChange::DRAINING, aws_connection_draining, @connection_draining)
  end

  aws_idle_timeout = aws_attributes.connection_settings.idle_timeout
  if @idle_timeout != aws_idle_timeout
    diffs << LoadBalancerDiff.new(LoadBalancerChange::IDLE, aws_idle_timeout, @idle_timeout)
  end

  diffs.flatten
end

#populate!(aws_elb, aws_tags, aws_attributes) ⇒ Object

Public: populates the fields of a LoadBalancerConfig from AWS config

aws_elb - the elb



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/elb/models/LoadBalancerConfig.rb', line 128

def populate!(aws_elb, aws_tags, aws_attributes)
  @listeners = aws_elb.listener_descriptions.map do |l|
    config = ListenerConfig.new
    config.populate!(l)
    config
  end
  @subnets = aws_elb.subnets.map { |subnet_id| EC2::id_subnets[subnet_id] }
  @security_groups = aws_elb.security_groups.map do |sg_id|
      SecurityGroups::id_security_groups[sg_id].group_name
  end
  @internal = aws_elb.scheme == "internal"
  @tags = Hash[aws_tags.map do |tag|
    [tag.key, tag.value]
  end]
  @manage_instances = aws_elb.instances.map { |i| i.instance_id }
  @health_check = HealthCheckConfig.new
  @health_check.populate!(aws_elb.health_check)
  @backend_policies = aws_elb.backend_server_descriptions
  @cross_zone = aws_attributes.cross_zone_load_balancing.enabled
  @access_log = AccessLogConfig.new
  @access_log.populate!(aws_attributes.access_log)
  @connection_draining = aws_attributes.connection_draining.enabled && aws_attributes.connection_draining.timeout
  @idle_timeout = aws_attributes.connection_settings.idle_timeout
end

#pretty_jsonObject

Public: Get the config as a prettified JSON string.

Returns the JSON string



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/elb/models/LoadBalancerConfig.rb', line 100

def pretty_json
  JSON.pretty_generate({
    "listeners" => {
      "includes" => [],
      "inlines" => @listeners.map(&:to_hash)
    },
    "subnets" => @subnets.map { |s| s.name || s.subnet_id },
    "security-groups" => @security_groups,
    "internal" => @internal,
    "tags" => @tags,
    "manage-instances" => @manage_instances,
    "health-check" => @health_check.to_hash,
    "backend-policies" => @backend_policies.map do |backend_policy|
      {
        "port" => backend_policy.instance_port,
        "policies" => backend_policy.policy_names
      }
    end,
    "cross-zone" => @cross_zone,
    "access-log" => if @access_log then @access_log.to_hash else @access_log end,
    "connection-draining" => @connection_draining,
    "idle-timeout" => @idle_timeout,
  })
end

#vpc_idObject

Public: Returns the vpc id for the load balancer config using the first subnet



92
93
94
95
# File 'lib/elb/models/LoadBalancerConfig.rb', line 92

def vpc_id
  first_subnet = @subnets.first
  first_subnet.vpc_id if first_subnet
end