terrafying 1.7.4 → 1.7.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eefaff1f899eebaafed67ea46a5ea7c8b7f1b764a526b2f4ad6d3c9ea5bddadd
4
- data.tar.gz: c5f021cf506ee877ee588d28ef8c1fe9a3e6a0bc710b0c9f8289ce026cd19f2f
3
+ metadata.gz: 43174bc2a2d3f672dd89dbce04472b9d7ccaf77166f53cf4f337c8b33fc21fe3
4
+ data.tar.gz: 9e59a6a4e385d3077088e869797da449b62db43ba3222000f0cfd6725979bb4a
5
5
  SHA512:
6
- metadata.gz: 983266176f2d6ac0b71734ffcdc2bb1fc504e831cba232525a60e5732817b4f8bf15bf5e5cd13391a7be05fb9050d8c884d49fda77c8d1ba2027f17fbe205e4c
7
- data.tar.gz: b529fcaa92002534b110d5e557962fade9d240f4dde02a250742cda3927e5323c91374af62bd2ada138c4aea7b2ec06c55be9bb06d163cc7caa2e55681b8fd68
6
+ metadata.gz: 5048661bb6d909c3fde40aa6fd30d426025aacd76749c3e8ef18e1e3bf1cda612ab48b9570ddf214880e2ec8984371a59f58643db71e60ed2b889a55e102f0be
7
+ data.tar.gz: 42faf59a56321218c25af7bd94c85c4316dc5de1fffb3f132b6ab1b28ca5d5cbcdb880ed01387bf7c9d639c3dc6c7e1c823facd07e777f2f4a39b0b111c2be8b
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'terrafying'
4
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
  require 'logger'
3
5
  require 'pathname'
@@ -12,9 +14,7 @@ require 'terrafying/version'
12
14
  require 'terrafying/state'
13
15
 
14
16
  module Terrafying
15
-
16
17
  class Config
17
-
18
18
  attr_reader :path, :scope
19
19
 
20
20
  def initialize(path, options)
@@ -22,7 +22,7 @@ module Terrafying
22
22
  @options = options
23
23
  @scope = options[:scope] || scope_for_path(@path)
24
24
 
25
- $stderr.puts "Scope: #{@scope}"
25
+ warn "Scope: #{@scope}"
26
26
 
27
27
  load(path)
28
28
  end
@@ -97,9 +97,7 @@ module Terrafying
97
97
  with_lock do
98
98
  local = State.local(self)
99
99
  state = local.get
100
- if state
101
- State.remote(self).put(state)
102
- end
100
+ State.remote(self).put(state) if state
103
101
  local.delete
104
102
  end
105
103
  end
@@ -108,9 +106,7 @@ module Terrafying
108
106
  with_lock do
109
107
  remote = State.remote(self)
110
108
  state = remote.get
111
- if state
112
- State.local(self).put(state)
113
- end
109
+ State.local(self).put(state) if state
114
110
  end
115
111
  end
116
112
 
@@ -127,6 +123,7 @@ module Terrafying
127
123
  end
128
124
 
129
125
  private
126
+
130
127
  def lock_timeout
131
128
  "-lock-timeout=#{@options[:lock_timeout]}" if @options[:lock_timeout]
132
129
  end
@@ -144,24 +141,22 @@ module Terrafying
144
141
  end
145
142
 
146
143
  def with_config(&block)
147
- abort("***** ERROR: You must have terraform installed to run this gem *****") unless terraform_installed?
144
+ abort('***** ERROR: You must have terraform installed to run this gem *****') unless terraform_installed?
148
145
  check_version
149
- name = File.basename(@path, ".*")
146
+ name = File.basename(@path, '.*')
150
147
  dir = File.join(git_toplevel, 'tmp', SecureRandom.uuid)
151
- terraform_files = File.join(git_toplevel, ".terraform/")
152
- unless Dir.exists?(terraform_files)
148
+ terraform_files = File.join(git_toplevel, '.terraform/')
149
+ unless Dir.exist?(terraform_files)
153
150
  abort("***** ERROR: No .terraform directory found. Please run 'terraform init' to install plugins *****")
154
151
  end
155
152
  FileUtils.mkdir_p(dir)
156
- output_path = File.join(dir, name + ".tf.json")
153
+ output_path = File.join(dir, name + '.tf.json')
157
154
  FileUtils.cp_r(terraform_files, dir)
158
155
  Dir.chdir(dir) do
159
- begin
160
- File.write(output_path, Terrafying::Generator.pretty_generate)
161
- yield block
162
- ensure
163
- FileUtils.rm_rf(dir) unless @options[:keep]
164
- end
156
+ File.write(output_path, Terrafying::Generator.pretty_generate)
157
+ yield block
158
+ ensure
159
+ FileUtils.rm_rf(dir) unless @options[:keep]
165
160
  end
166
161
  end
167
162
 
@@ -191,31 +186,27 @@ module Terrafying
191
186
  end
192
187
 
193
188
  def with_state(opts, &block)
194
- if !@options[:dynamodb]
195
- return yield(block)
196
- end
189
+ return yield(block) unless @options[:dynamodb]
197
190
 
198
191
  store = State.store(self)
199
192
 
200
193
  begin
201
194
  state = store.get
202
195
  File.write(State::STATE_FILENAME, state) if state
203
- rescue => e
196
+ rescue StandardError => e
204
197
  raise "Error retrieving state for config #{self}: #{e}"
205
198
  end
206
199
 
207
200
  yield block
208
201
 
209
202
  begin
210
- if opts[:mode] == :update
211
- store.put(IO.read(State::STATE_FILENAME))
212
- end
213
- rescue => e
203
+ store.put(IO.read(State::STATE_FILENAME)) if opts[:mode] == :update
204
+ rescue StandardError => e
214
205
  raise "Error updating state for config #{self}: #{e}"
215
206
  end
216
207
  end
217
208
 
218
- def scope_for_path(path)
209
+ def scope_for_path(_path)
219
210
  top_level_path = Pathname.new(git_toplevel)
220
211
  Pathname.new(@path).relative_path_from(top_level_path).to_s
221
212
  end
@@ -224,6 +215,7 @@ module Terrafying
224
215
  @top_level ||= begin
225
216
  top_level = `git rev-parse --show-toplevel`
226
217
  raise "Unable to find .git directory top level for '#{@path}'" if top_level.empty?
218
+
227
219
  File.expand_path(top_level.chomp)
228
220
  end
229
221
  end
@@ -239,16 +231,16 @@ module Terrafying
239
231
  end
240
232
 
241
233
  def terraform_version
242
- `terraform -v`.split("\n").first.split("v").last
234
+ `terraform -v`.split("\n").first.split('v').last
243
235
  end
244
236
 
245
237
  def stream_command(cmd)
246
238
  IO.popen(cmd) do |io|
247
- while (line = io.gets) do
248
- puts line.gsub('\n', "\n").gsub('\\"', "\"")
239
+ while (line = io.gets)
240
+ puts line.gsub('\n', "\n").gsub('\\"', '"')
249
241
  end
250
242
  end
251
- return $?.exitstatus
243
+ $CHILD_STATUS.exitstatus
252
244
  end
253
245
 
254
246
  # Cross-platform way of finding an executable in the $PATH.
@@ -257,12 +249,12 @@ module Terrafying
257
249
  def which(cmd)
258
250
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
259
251
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
260
- exts.each { |ext|
252
+ exts.each do |ext|
261
253
  exe = File.join(path, "#{cmd}#{ext}")
262
254
  return exe if File.executable?(exe) && !File.directory?(exe)
263
- }
255
+ end
264
256
  end
265
- return nil
257
+ nil
266
258
  end
267
259
  end
268
260
  end
@@ -1,16 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'aws-sdk-autoscaling'
2
4
  require 'aws-sdk-ec2'
3
5
  require 'aws-sdk-elasticloadbalancingv2'
4
6
  require 'aws-sdk-route53'
5
7
  require 'aws-sdk-s3'
6
8
  require 'aws-sdk-sts'
9
+ require 'aws-sdk-pricing'
10
+ require 'aws-sdk-kafka'
11
+ require 'json'
7
12
 
8
13
  Aws.use_bundled_cert!
9
14
 
10
15
  module Terrafying
11
16
  module Aws
12
17
  class Ops
13
-
14
18
  attr_reader :region
15
19
 
16
20
  def initialize(region)
@@ -19,11 +23,11 @@ module Terrafying
19
23
  Kernel.sleep(Kernel.rand((sleep_time / 2)..sleep_time))
20
24
  }
21
25
 
22
- ::Aws.config.update({
26
+ ::Aws.config.update(
23
27
  region: region,
24
28
  retry_limit: 7,
25
29
  retry_backoff: half_jitter
26
- })
30
+ )
27
31
 
28
32
  @autoscaling_client = ::Aws::AutoScaling::Client.new
29
33
  @ec2_resource = ::Aws::EC2::Resource.new
@@ -32,7 +36,8 @@ module Terrafying
32
36
  @route53_client = ::Aws::Route53::Client.new
33
37
  @s3_client = ::Aws::S3::Client.new
34
38
  @sts_client = ::Aws::STS::Client.new
35
-
39
+ @pricing_client = ::Aws::Pricing::Client.new(region: 'us-east-1') # no AWS Pricing endpoint in Europe
40
+ @msk_client = ::Aws::Kafka::Client.new
36
41
  @region = region
37
42
  end
38
43
 
@@ -52,14 +57,13 @@ module Terrafying
52
57
  @security_groups ||= {}
53
58
  @security_groups[name] ||=
54
59
  begin
55
- STDERR.puts "Looking up id of security group '#{name}'"
60
+ warn "Looking up id of security group '#{name}'"
56
61
  groups = all_security_groups.select { |g| g.group_name == name }.take(2)
57
- case
58
- when groups.count == 1
62
+ if groups.count == 1
59
63
  groups.first.id
60
- when groups.count < 1
64
+ elsif groups.count < 1
61
65
  raise "No security group with name '#{name}' was found."
62
- when groups.count > 1
66
+ elsif groups.count > 1
63
67
  raise "More than one security group with name '#{name}' found: " + groups.join(', ')
64
68
  end
65
69
  end
@@ -69,14 +73,13 @@ module Terrafying
69
73
  @security_groups_in_vpc ||= {}
70
74
  @security_groups_in_vpc[vpc_id + name] ||=
71
75
  begin
72
- STDERR.puts "Looking up id of security group '#{name}'"
76
+ warn "Looking up id of security group '#{name}'"
73
77
  groups = all_security_groups.select { |g| g.vpc_id == vpc_id && g.group_name == name }.take(2)
74
- case
75
- when groups.count == 1
78
+ if groups.count == 1
76
79
  groups.first.id
77
- when groups.count < 1
80
+ elsif groups.count < 1
78
81
  raise "No security group with name '#{name}' was found."
79
- when groups.count > 1
82
+ elsif groups.count > 1
80
83
  raise "More than one security group with name '#{name}' found: " + groups.join(', ')
81
84
  end
82
85
  end
@@ -87,12 +90,11 @@ module Terrafying
87
90
  @security_groups_by_tags[tags] ||=
88
91
  begin
89
92
  groups = all_security_groups.select { |g| g.tags.any? { |t| t.key == tags.keys && t.value == tags.values } }.take(2)
90
- case
91
- when groups.count == 1
93
+ if groups.count == 1
92
94
  groups.first.id
93
- when groups.count < 1
95
+ elsif groups.count < 1
94
96
  raise "No security group with tags '#{tags}' was found."
95
- when groups.count > 1
97
+ elsif groups.count > 1
96
98
  raise "More than one security group with tags '#{tags}' found: " + groups.join(', ')
97
99
  end
98
100
  end
@@ -103,19 +105,18 @@ module Terrafying
103
105
  @instance_profiles[name] ||=
104
106
  begin
105
107
  resource = ::Aws::IAM::Resource.new
106
- STDERR.puts "Looking up id of instance profile '#{name}'"
108
+ warn "Looking up id of instance profile '#{name}'"
107
109
  # unfortunately amazon don't let us filter for profiles using
108
110
  # a name filter, for now we have enumerate and filter manually
109
111
  coll = resource.instance_profiles
110
112
  profiles = []
111
- profiles = coll.select {|p| p.instance_profile_name =~ /#{name}/}
113
+ profiles = coll.select { |p| p.instance_profile_name =~ /#{name}/ }
112
114
 
113
- case
114
- when profiles.count == 1
115
+ if profiles.count == 1
115
116
  profiles.first.instance_profile_id
116
- when profiles.count < 1
117
+ elsif profiles.count < 1
117
118
  raise "No instance profile with name '#{name}' was found."
118
- when profiles.count > 1
119
+ elsif profiles.count > 1
119
120
  raise "More than one instance profile with name '#{name}' found: " + profiles.join(', ')
120
121
  end
121
122
  end
@@ -126,20 +127,18 @@ module Terrafying
126
127
  @route_table_for_subnet[subnet_id] ||=
127
128
  begin
128
129
  resp = @ec2_client.describe_route_tables(
129
- {
130
- filters: [
131
- { name: "association.subnet-id", values: [ subnet_id ] },
132
- ],
133
- })
130
+ filters: [
131
+ { name: 'association.subnet-id', values: [subnet_id] }
132
+ ]
133
+ )
134
134
 
135
135
  route_tables = resp.route_tables
136
136
 
137
- case
138
- when route_tables.count == 1
137
+ if route_tables.count == 1
139
138
  route_tables.first
140
- when route_tables.count < 1
139
+ elsif route_tables.count < 1
141
140
  raise "No route table for subnet '#{subnet_id}' was found."
142
- when profiles.count > 1
141
+ elsif profiles.count > 1
143
142
  raise "More than route table for subnet '#{subnet_id}' found: " + route_tables.join(', ')
144
143
  end
145
144
  end
@@ -150,54 +149,70 @@ module Terrafying
150
149
  @route_table_for_vpc[vpc_id] ||=
151
150
  begin
152
151
  resp = @ec2_client.describe_route_tables(
153
- {
154
- filters: [
155
- { name: "association.main", values: [ "true" ] },
156
- { name: "vpc-id", values: [ vpc_id ] },
157
- ],
158
- })
152
+ filters: [
153
+ { name: 'association.main', values: ['true'] },
154
+ { name: 'vpc-id', values: [vpc_id] }
155
+ ]
156
+ )
159
157
 
160
158
  route_tables = resp.route_tables
161
159
 
162
- case
163
- when route_tables.count == 1
160
+ if route_tables.count == 1
164
161
  route_tables.first
165
- when route_tables.count < 1
162
+ elsif route_tables.count < 1
166
163
  raise "No route table for vpc '#{vpc_id}' was found."
167
- when profiles.count > 1
164
+ elsif profiles.count > 1
168
165
  raise "More than route table for vpc '#{vpc_id}' found: " + route_tables.join(', ')
169
166
  end
170
167
  end
171
168
  end
172
169
 
170
+ def nat_gateways_for_vpc(vpc_id)
171
+ @nat_gateways_for_vpc ||= {}
172
+ @nat_gateways_for_vpc[vpc_id] ||=
173
+ begin
174
+ resp = @ec2_client.describe_nat_gateways(
175
+ filter: [
176
+ { name: 'vpc-id', values: [vpc_id] }
177
+ ]
178
+ )
179
+
180
+ nat_gateways = resp.nat_gateways
181
+
182
+ if nat_gateways.count >= 1
183
+ nat_gateways
184
+ elsif nat_gateways.count < 1
185
+ raise "No nat-gateways for vpc #{vpc_id} were found"
186
+ end
187
+ end
188
+ end
189
+
173
190
  def security_groups(*names)
174
- names.map{|n| security_group(n)}
191
+ names.map { |n| security_group(n) }
175
192
  end
176
193
 
177
194
  def security_groups_in_vpc(vpc_id, *names)
178
- names.map{|n| security_group_in_vpc(vpc_id, n)}
195
+ names.map { |n| security_group_in_vpc(vpc_id, n) }
179
196
  end
180
197
 
181
198
  def subnet(name)
182
199
  @subnets ||= {}
183
200
  @subnets[name] ||=
184
201
  begin
185
- STDERR.puts "Looking up id of subnet '#{name}'"
202
+ warn "Looking up id of subnet '#{name}'"
186
203
  subnets = @ec2_resource.subnets(
187
- {
188
- filters: [
189
- {
190
- name: "tag:Name",
191
- values: [name],
192
- },
193
- ],
194
- }).limit(2)
195
- case
196
- when subnets.count == 1
204
+ filters: [
205
+ {
206
+ name: 'tag:Name',
207
+ values: [name]
208
+ }
209
+ ]
210
+ ).limit(2)
211
+ if subnets.count == 1
197
212
  subnets.first.id
198
- when subnets.count < 1
213
+ elsif subnets.count < 1
199
214
  raise "No subnet with name '#{name}' was found."
200
- when subnets.count > 1
215
+ elsif subnets.count > 1
201
216
  raise "More than one subnet with this name '#{name}' found : " + subnets.join(', ')
202
217
  end
203
218
  end
@@ -208,23 +223,21 @@ module Terrafying
208
223
  @subnets_by_id[id] ||=
209
224
  begin
210
225
  resp = @ec2_client.describe_subnets(
211
- {
212
- subnet_ids: [id],
213
- })
226
+ subnet_ids: [id]
227
+ )
214
228
  subnets = resp.subnets
215
- case
216
- when subnets.count == 1
229
+ if subnets.count == 1
217
230
  subnets.first
218
- when subnets.count < 1
231
+ elsif subnets.count < 1
219
232
  raise "No subnet with id '#{id}' was found."
220
- when subnets.count > 1
233
+ elsif subnets.count > 1
221
234
  raise "More than one subnet with this id '#{id}' found : " + subnets.join(', ')
222
235
  end
223
236
  end
224
237
  end
225
238
 
226
239
  def subnets(*names)
227
- names.map{|n| subnet(n)}
240
+ names.map { |n| subnet(n) }
228
241
  end
229
242
 
230
243
  def subnets_for_vpc(vpc_id)
@@ -232,48 +245,42 @@ module Terrafying
232
245
  @subnets_for_vpc[vpc_id] ||=
233
246
  begin
234
247
  resp = @ec2_client.describe_subnets(
235
- {
236
- filters: [
237
- { name: "vpc-id", values: [ vpc_id ] },
238
- ],
239
- })
248
+ filters: [
249
+ { name: 'vpc-id', values: [vpc_id] }
250
+ ]
251
+ )
240
252
 
241
253
  subnets = resp.subnets
242
254
 
243
- case
244
- when subnets.count >= 1
255
+ if subnets.count >= 1
245
256
  subnets
246
- when subnets.count < 1
257
+ elsif subnets.count < 1
247
258
  raise "No subnets found for '#{vpc_id}'."
248
259
  end
249
260
  end
250
261
  end
251
262
 
252
- def ami(name, owners=["self"])
263
+ def ami(name, owners = ['self'])
253
264
  @ami ||= {}
254
265
  @ami[name] ||=
255
266
  begin
256
- STDERR.puts "looking for an image with prefix '#{name}'"
257
- resp = @ec2_client.describe_images({owners: owners})
258
- if resp.images.count < 1
259
- raise "no images were found"
260
- end
267
+ warn "looking for an image with prefix '#{name}'"
268
+ resp = @ec2_client.describe_images(owners: owners)
269
+ raise 'no images were found' if resp.images.count < 1
270
+
261
271
  m = resp.images.select { |a| /^#{name}/.match(a.name) }
262
- if m.count == 0
263
- raise "no image with name '#{name}' was found"
264
- end
265
- m.sort { |x,y| y.creation_date <=> x.creation_date }.shift.image_id
272
+ raise "no image with name '#{name}' was found" if m.count == 0
273
+
274
+ m.sort { |x, y| y.creation_date <=> x.creation_date }.shift.image_id
266
275
  end
267
276
  end
268
277
 
269
278
  def availability_zones
270
279
  @availability_zones ||=
271
280
  begin
272
- STDERR.puts "looking for AZs in the current region"
281
+ warn 'looking for AZs in the current region'
273
282
  resp = @ec2_client.describe_availability_zones({})
274
- resp.availability_zones.map { |zone|
275
- zone.zone_name
276
- }
283
+ resp.availability_zones.map(&:zone_name)
277
284
  end
278
285
  end
279
286
 
@@ -281,18 +288,17 @@ module Terrafying
281
288
  @vpcs ||= {}
282
289
  @vpcs[name] ||=
283
290
  begin
284
- STDERR.puts "looking for a VPC with name '#{name}'"
291
+ warn "looking for a VPC with name '#{name}'"
285
292
  resp = @ec2_client.describe_vpcs({})
286
- matching_vpcs = resp.vpcs.select { |vpc|
287
- name_tag = vpc.tags.select { |tag| tag.key == "Name" }.first
293
+ matching_vpcs = resp.vpcs.select do |vpc|
294
+ name_tag = vpc.tags.select { |tag| tag.key == 'Name' }.first
288
295
  name_tag && name_tag.value == name
289
- }
290
- case
291
- when matching_vpcs.count == 1
296
+ end
297
+ if matching_vpcs.count == 1
292
298
  matching_vpcs.first
293
- when matching_vpcs.count < 1
299
+ elsif matching_vpcs.count < 1
294
300
  raise "No VPC with name '#{name}' was found."
295
- when matching_vpcs.count > 1
301
+ elsif matching_vpcs.count > 1
296
302
  raise "More than one VPC with name '#{name}' was found: " + matching_vpcs.join(', ')
297
303
  end
298
304
  end
@@ -302,23 +308,20 @@ module Terrafying
302
308
  @route_tables ||= {}
303
309
  @route_tables[name] ||=
304
310
  begin
305
- STDERR.puts "looking for a route table with name '#{name}'"
311
+ warn "looking for a route table with name '#{name}'"
306
312
  route_tables = @ec2_client.describe_route_tables(
307
- {
308
- filters: [
309
- {
310
- name: "tag:Name",
311
- values: [name],
312
- },
313
- ],
314
- }
313
+ filters: [
314
+ {
315
+ name: 'tag:Name',
316
+ values: [name]
317
+ }
318
+ ]
315
319
  ).route_tables
316
- case
317
- when route_tables.count == 1
320
+ if route_tables.count == 1
318
321
  route_tables.first.route_table_id
319
- when route_tables.count < 1
322
+ elsif route_tables.count < 1
320
323
  raise "No route table with name '#{name}' was found."
321
- when route_tables.count > 1
324
+ elsif route_tables.count > 1
322
325
  raise "More than one route table with name '#{name}' was found: " + route_tables.join(', ')
323
326
  end
324
327
  end
@@ -328,23 +331,20 @@ module Terrafying
328
331
  @ips ||= {}
329
332
  @ips[alloc_id] ||=
330
333
  begin
331
- STDERR.puts "looking for an elastic ip with allocation_id '#{alloc_id}'"
334
+ warn "looking for an elastic ip with allocation_id '#{alloc_id}'"
332
335
  ips = @ec2_client.describe_addresses(
333
- {
334
- filters: [
335
- {
336
- name: "allocation-id",
337
- values: [alloc_id],
338
- },
339
- ],
340
- }
336
+ filters: [
337
+ {
338
+ name: 'allocation-id',
339
+ values: [alloc_id]
340
+ }
341
+ ]
341
342
  ).addresses
342
- case
343
- when ips.count == 1
343
+ if ips.count == 1
344
344
  ips.first
345
- when ips.count < 1
345
+ elsif ips.count < 1
346
346
  raise "No elastic ip with allocation_id '#{alloc_id}' was found."
347
- when ips.count > 1
347
+ elsif ips.count > 1
348
348
  raise "More than one elastic ip with allocation_id '#{alloc_id}' was found: " + ips.join(', ')
349
349
  end
350
350
  end
@@ -354,16 +354,15 @@ module Terrafying
354
354
  @hosted_zones ||= {}
355
355
  @hosted_zones[fqdn] ||=
356
356
  begin
357
- STDERR.puts "looking for a hosted zone with fqdn '#{fqdn}'"
358
- hosted_zones = @route53_client.list_hosted_zones_by_name({ dns_name: fqdn }).hosted_zones.select { |zone|
357
+ warn "looking for a hosted zone with fqdn '#{fqdn}'"
358
+ hosted_zones = @route53_client.list_hosted_zones_by_name(dns_name: fqdn).hosted_zones.select do |zone|
359
359
  zone.name == "#{fqdn}." && !zone.config.private_zone
360
- }
361
- case
362
- when hosted_zones.count == 1
360
+ end
361
+ if hosted_zones.count == 1
363
362
  hosted_zones.first
364
- when hosted_zones.count < 1
363
+ elsif hosted_zones.count < 1
365
364
  raise "No hosted zone with fqdn '#{fqdn}' was found."
366
- when hosted_zones.count > 1
365
+ elsif hosted_zones.count > 1
367
366
  raise "More than one hosted zone with name '#{fqdn}' was found: " + hosted_zones.join(', ')
368
367
  end
369
368
  end
@@ -373,11 +372,11 @@ module Terrafying
373
372
  @hosted_zones ||= {}
374
373
  @hosted_zones[tag] ||=
375
374
  begin
376
- STDERR.puts "looking for a hosted zone with tag '#{tag}'"
375
+ warn "looking for a hosted zone with tag '#{tag}'"
377
376
  @aws_hosted_zones ||= @route53_client.list_hosted_zones.hosted_zones.map do |zone|
378
377
  {
379
378
  zone: zone,
380
- tags: @route53_client.list_tags_for_resource({resource_type: "hostedzone", resource_id: zone.id.split('/')[2]}).resource_tag_set.tags
379
+ tags: @route53_client.list_tags_for_resource(resource_type: 'hostedzone', resource_id: zone.id.split('/')[2]).resource_tag_set.tags
381
380
  }
382
381
  end
383
382
 
@@ -387,12 +386,11 @@ module Terrafying
387
386
  end
388
387
  end
389
388
 
390
- case
391
- when hosted_zones.count == 1
389
+ if hosted_zones.count == 1
392
390
  hosted_zones.first[:zone]
393
- when hosted_zones.count < 1
391
+ elsif hosted_zones.count < 1
394
392
  raise "No hosted zone with tag '#{tag}' was found."
395
- when hosted_zones.count > 1
393
+ elsif hosted_zones.count > 1
396
394
  raise "More than one hosted zone with tag '#{tag}' was found: " + hosted_zones.join(', ')
397
395
  end
398
396
  end
@@ -402,7 +400,7 @@ module Terrafying
402
400
  @s3_objects ||= {}
403
401
  @s3_objects["#{bucket}-#{key}"] ||=
404
402
  begin
405
- resp = @s3_client.get_object({ bucket: bucket, key: key })
403
+ resp = @s3_client.get_object(bucket: bucket, key: key)
406
404
  resp.body.read
407
405
  end
408
406
  end
@@ -411,7 +409,7 @@ module Terrafying
411
409
  @list_objects ||= {}
412
410
  @list_objects[bucket] ||=
413
411
  begin
414
- resp = @s3_client.list_objects_v2({ bucket: bucket })
412
+ resp = @s3_client.list_objects_v2(bucket: bucket)
415
413
  resp.contents
416
414
  end
417
415
  end
@@ -421,23 +419,20 @@ module Terrafying
421
419
  @endpoint_service[service_name] ||=
422
420
  begin
423
421
  resp = @ec2_client.describe_vpc_endpoint_service_configurations(
424
- {
425
- filters: [
426
- {
427
- name: "service-name",
428
- values: [service_name],
429
- },
430
- ],
431
- }
422
+ filters: [
423
+ {
424
+ name: 'service-name',
425
+ values: [service_name]
426
+ }
427
+ ]
432
428
  )
433
429
 
434
430
  endpoint_services = resp.service_configurations
435
- case
436
- when endpoint_services.count == 1
431
+ if endpoint_services.count == 1
437
432
  endpoint_services.first
438
- when endpoint_services.count < 1
433
+ elsif endpoint_services.count < 1
439
434
  raise "No endpoint service with name '#{service_name}' was found."
440
- when endpoint_services.count > 1
435
+ elsif endpoint_services.count > 1
441
436
  raise "More than one endpoint service with name '#{service_name}' was found: " + endpoint_services.join(', ')
442
437
  end
443
438
  end
@@ -449,16 +444,15 @@ module Terrafying
449
444
  begin
450
445
  resp = @ec2_client.describe_vpc_endpoint_service_configurations
451
446
 
452
- services = resp.service_configurations.select { |service|
447
+ services = resp.service_configurations.select do |service|
453
448
  service.network_load_balancer_arns.include?(arn)
454
- }
449
+ end
455
450
 
456
- case
457
- when services.count == 1
451
+ if services.count == 1
458
452
  services.first
459
- when services.count < 1
453
+ elsif services.count < 1
460
454
  raise "No endpoint service with lb arn '#{arn}' was found."
461
- when services.count > 1
455
+ elsif services.count > 1
462
456
  raise "More than one endpoint service with lb arn '#{arn}' was found: " + services.join(', ')
463
457
  end
464
458
  end
@@ -468,14 +462,13 @@ module Terrafying
468
462
  @lbs ||= {}
469
463
  @lbs[name] ||=
470
464
  begin
471
- load_balancers = @elb_client.describe_load_balancers({ names: [name] }).load_balancers
465
+ load_balancers = @elb_client.describe_load_balancers(names: [name]).load_balancers
472
466
 
473
- case
474
- when load_balancers.count == 1
467
+ if load_balancers.count == 1
475
468
  load_balancers.first
476
- when load_balancers.count < 1
469
+ elsif load_balancers.count < 1
477
470
  raise "No load balancer with name '#{name}' was found."
478
- when load_balancers.count > 1
471
+ elsif load_balancers.count > 1
479
472
  raise "More than one load balancer with name '#{name}' was found: " + load_balancers.join(', ')
480
473
  end
481
474
  end
@@ -486,9 +479,7 @@ module Terrafying
486
479
  @target_groups[arn] ||=
487
480
  begin
488
481
  resp = @elb_client.describe_target_groups(
489
- {
490
- load_balancer_arn: arn,
491
- }
482
+ load_balancer_arn: arn
492
483
  )
493
484
 
494
485
  resp.target_groups
@@ -500,16 +491,16 @@ module Terrafying
500
491
  next_token = nil
501
492
 
502
493
  loop do
503
- resp = @autoscaling_client.describe_auto_scaling_groups({ next_token: next_token })
494
+ resp = @autoscaling_client.describe_auto_scaling_groups(next_token: next_token)
504
495
 
505
- asgs = asgs + resp.auto_scaling_groups.select { |asg|
506
- matches = asg.tags.select { |tag|
496
+ asgs += resp.auto_scaling_groups.select do |asg|
497
+ matches = asg.tags.select do |tag|
507
498
  expectedTags[tag.key.to_sym] == tag.value ||
508
499
  expectedTags[tag.key] == tag.value
509
- }
500
+ end
510
501
 
511
502
  matches.count == expectedTags.count
512
- }
503
+ end
513
504
 
514
505
  if resp.next_token
515
506
  next_token = resp.next_token
@@ -520,7 +511,51 @@ module Terrafying
520
511
 
521
512
  asgs
522
513
  end
523
- end
524
514
 
515
+ def products(products_filter, _region = 'us-east-1')
516
+ next_token = nil
517
+ Enumerator.new do |y|
518
+ loop do
519
+ resp = @pricing_client.get_products(products_filter.merge(next_token: next_token))
520
+ resp.price_list.each do |product|
521
+ y << product
522
+ end
523
+ next_token = resp.next_token
524
+ break if next_token.nil?
525
+ end
526
+ end
527
+ end
528
+
529
+ def instance_type_vcpu_count(instance_type, location = 'EU (Ireland)')
530
+ products_filter = {
531
+ service_code: 'AmazonEC2',
532
+ filters: [
533
+ { field: 'operatingSystem', type: 'TERM_MATCH', value: 'Linux' },
534
+ { field: 'tenancy', type: 'TERM_MATCH', value: 'Shared' },
535
+ { field: 'instanceType', type: 'TERM_MATCH', value: instance_type },
536
+ { field: 'location', type: 'TERM_MATCH', value: location },
537
+ { field: 'preInstalledSw', type: 'TERM_MATCH', value: 'NA' }
538
+ ],
539
+ format_version: 'aws_v1'
540
+ }
541
+
542
+ products(products_filter).each do |product|
543
+ vcpu = JSON.parse(product)['product']['attributes']['vcpu']
544
+ return vcpu.to_i if vcpu
545
+ end
546
+ end
547
+
548
+ def msk_brokers(cluster_arn)
549
+ @brokers ||= {}
550
+ @brokers[cluster_arn] ||= begin
551
+ resp = @msk_client.get_bootstrap_brokers(cluster_arn: cluster_arn)
552
+ brokers = resp.bootstrap_broker_string_tls.split(',')
553
+
554
+ raise "No brokers found for cluster with arn: \"#{cluster_arn}\"'" if brokers.empty?
555
+
556
+ brokers
557
+ end
558
+ end
559
+ end
525
560
  end
526
561
  end