Class: Terraforming::Resource::ELB

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/terraforming/resource/elb.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#apply_template, #name_from_tag, #normalize_module_name, #prettify_policy, #template_path

Constructor Details

#initialize(client) ⇒ ELB

Returns a new instance of ELB.



14
15
16
# File 'lib/terraforming/resource/elb.rb', line 14

def initialize(client)
  @client = client
end

Class Method Details

.tf(client: Aws::ElasticLoadBalancing::Client.new) ⇒ Object



6
7
8
# File 'lib/terraforming/resource/elb.rb', line 6

def self.tf(client: Aws::ElasticLoadBalancing::Client.new)
  self.new(client).tf
end

.tfstate(client: Aws::ElasticLoadBalancing::Client.new) ⇒ Object



10
11
12
# File 'lib/terraforming/resource/elb.rb', line 10

def self.tfstate(client: Aws::ElasticLoadBalancing::Client.new)
  self.new(client).tfstate
end

Instance Method Details

#access_logs_attributes_of(load_balancer_attributes) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/terraforming/resource/elb.rb', line 63

def access_logs_attributes_of(load_balancer_attributes)
  access_log = load_balancer_attributes.access_log

  if access_log.enabled
    {
      "access_logs.#" => "1",
      "access_logs.0.bucket" => access_log.s3_bucket_name,
      "access_logs.0.bucket_prefix" => access_log.s3_bucket_prefix,
      "access_logs.0.interval" => access_log.emit_interval.to_s,
    }
  else
    {
      "access_logs.#" => "0",
    }
  end
end

#healthcheck_attributes_of(elb) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/terraforming/resource/elb.rb', line 80

def healthcheck_attributes_of(elb)
  hashcode = healthcheck_hashcode_of(elb.health_check)
  attributes = {
    # Now each ELB supports one heatlhcheck
    "health_check.#" => "1",
    "health_check.#{hashcode}.healthy_threshold" => elb.health_check.healthy_threshold.to_s,
    "health_check.#{hashcode}.interval" => elb.health_check.interval.to_s,
    "health_check.#{hashcode}.target" => elb.health_check.target,
    "health_check.#{hashcode}.timeout" => elb.health_check.timeout.to_s,
    "health_check.#{hashcode}.unhealthy_threshold" => elb.health_check.unhealthy_threshold.to_s
  }

  attributes
end

#healthcheck_hashcode_of(health_check) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/terraforming/resource/elb.rb', line 95

def healthcheck_hashcode_of(health_check)
  string =
    "#{health_check.healthy_threshold}-" <<
    "#{health_check.unhealthy_threshold}-" <<
    "#{health_check.target}-" <<
    "#{health_check.interval}-" <<
    "#{health_check.timeout}-"

  Zlib.crc32(string)
end

#instances_attributes_of(elb) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/terraforming/resource/elb.rb', line 117

def instances_attributes_of(elb)
  attributes = { "instances.#" => elb.instances.length.to_s }

  elb.instances.each do |instance|
    attributes["instances.#{Zlib.crc32(instance.instance_id)}"] = instance.instance_id
  end

  attributes
end

#internal?(load_balancer) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/terraforming/resource/elb.rb', line 198

def internal?(load_balancer)
  load_balancer.scheme == "internal"
end

#listener_attributes_of(listener) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/terraforming/resource/elb.rb', line 157

def listener_attributes_of(listener)
  hashcode = listener_hashcode_of(listener)

  attributes = {
    "listener.#{hashcode}.instance_port" => listener.instance_port.to_s,
    "listener.#{hashcode}.instance_protocol" => listener.instance_protocol.downcase,
    "listener.#{hashcode}.lb_port" => listener.load_balancer_port.to_s,
    "listener.#{hashcode}.lb_protocol" => listener.protocol.downcase,
    "listener.#{hashcode}.ssl_certificate_id" => listener.ssl_certificate_id
  }

  attributes
end

#listener_hashcode_of(listener) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/terraforming/resource/elb.rb', line 171

def listener_hashcode_of(listener)
  string =
    "#{listener.instance_port}-" <<
    "#{listener.instance_protocol.downcase}-" <<
    "#{listener.load_balancer_port}-" <<
    "#{listener.protocol.downcase}-" <<
    "#{listener.ssl_certificate_id}-"

  Zlib.crc32(string)
end

#listeners_attributes_of(elb) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/terraforming/resource/elb.rb', line 147

def listeners_attributes_of(elb)
  attributes = { "listener.#" => elb.listener_descriptions.length.to_s }

  elb.listener_descriptions.each do |listener_description|
    attributes.merge!(listener_attributes_of(listener_description.listener))
  end

  attributes
end

#load_balancer_attributes_of(load_balancer) ⇒ Object



186
187
188
# File 'lib/terraforming/resource/elb.rb', line 186

def load_balancer_attributes_of(load_balancer)
  @client.describe_load_balancer_attributes(load_balancer_name: load_balancer.load_balancer_name).load_balancer_attributes
end

#load_balancersObject



182
183
184
# File 'lib/terraforming/resource/elb.rb', line 182

def load_balancers
  @client.describe_load_balancers.map(&:load_balancer_descriptions).flatten
end

#module_name_of(load_balancer) ⇒ Object



190
191
192
# File 'lib/terraforming/resource/elb.rb', line 190

def module_name_of(load_balancer)
  normalize_module_name(load_balancer.load_balancer_name)
end

#sg_attributes_of(elb) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/terraforming/resource/elb.rb', line 137

def sg_attributes_of(elb)
  attributes = { "security_groups.#" => elb.security_groups.length.to_s }

  elb.security_groups.each do |sg_id|
    attributes["security_groups.#{Zlib.crc32(sg_id)}"] = sg_id
  end

  attributes
end

#subnets_attributes_of(elb) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/terraforming/resource/elb.rb', line 127

def subnets_attributes_of(elb)
  attributes = { "subnets.#" => elb.subnets.length.to_s }

  elb.subnets.each do |subnet_id|
    attributes["subnets.#{Zlib.crc32(subnet_id)}"] = subnet_id
  end

  attributes
end

#tags_attributes_of(elb) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/terraforming/resource/elb.rb', line 106

def tags_attributes_of(elb)
  tags = @client.describe_tags(load_balancer_names: [elb.load_balancer_name]).tag_descriptions.first.tags
  attributes = { "tags.#" => tags.length.to_s }

  tags.each do |tag|
    attributes["tags.#{tag.key}"] = tag.value
  end

  attributes
end

#tfObject



18
19
20
# File 'lib/terraforming/resource/elb.rb', line 18

def tf
  apply_template(@client, "tf/elb")
end

#tfstateObject



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
# File 'lib/terraforming/resource/elb.rb', line 22

def tfstate
  load_balancers.inject({}) do |resources, load_balancer|
    load_balancer_attributes = load_balancer_attributes_of(load_balancer)
    attributes = {
      "availability_zones.#" => load_balancer.availability_zones.length.to_s,
      "connection_draining" => load_balancer_attributes.connection_draining.enabled.to_s,
      "connection_draining_timeout" => load_balancer_attributes.connection_draining.timeout.to_s,
      "cross_zone_load_balancing" => load_balancer_attributes.cross_zone_load_balancing.enabled.to_s,
      "dns_name" => load_balancer.dns_name,
      "id" => load_balancer.load_balancer_name,
      "idle_timeout" => load_balancer_attributes.connection_settings.idle_timeout.to_s,
      "instances.#" => load_balancer.instances.length.to_s,
      "internal" => internal?(load_balancer).to_s,
      "name" => load_balancer.load_balancer_name,
      "source_security_group" => load_balancer.source_security_group.group_name,
    }

    if load_balancer_attributes.access_log.enabled

    end

    attributes.merge!(access_logs_attributes_of(load_balancer_attributes))
    attributes.merge!(healthcheck_attributes_of(load_balancer))
    attributes.merge!(listeners_attributes_of(load_balancer))
    attributes.merge!(sg_attributes_of(load_balancer))
    attributes.merge!(subnets_attributes_of(load_balancer))
    attributes.merge!(instances_attributes_of(load_balancer))
    attributes.merge!(tags_attributes_of(load_balancer))

    resources["aws_elb.#{module_name_of(load_balancer)}"] = {
      "type" => "aws_elb",
      "primary" => {
        "id" => load_balancer.load_balancer_name,
        "attributes" => attributes
      }
    }

    resources
  end
end

#vpc_elb?(load_balancer) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/terraforming/resource/elb.rb', line 194

def vpc_elb?(load_balancer)
  load_balancer.vpc_id != ""
end