terrafying 1.7.4 → 1.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eefaff1f899eebaafed67ea46a5ea7c8b7f1b764a526b2f4ad6d3c9ea5bddadd
4
- data.tar.gz: c5f021cf506ee877ee588d28ef8c1fe9a3e6a0bc710b0c9f8289ce026cd19f2f
3
+ metadata.gz: 72da5ae85bf0ba5022d413cb84c630beb572baed66c81713cb3c7d1d8d7cf146
4
+ data.tar.gz: d2a92ce8fc0eb4724df5436aef1ce09880881cca0db4f6eb56556386b8b2fed1
5
5
  SHA512:
6
- metadata.gz: 983266176f2d6ac0b71734ffcdc2bb1fc504e831cba232525a60e5732817b4f8bf15bf5e5cd13391a7be05fb9050d8c884d49fda77c8d1ba2027f17fbe205e4c
7
- data.tar.gz: b529fcaa92002534b110d5e557962fade9d240f4dde02a250742cda3927e5323c91374af62bd2ada138c4aea7b2ec06c55be9bb06d163cc7caa2e55681b8fd68
6
+ metadata.gz: 8983f67957a2b05e62231afc959f9e5c8e9acecf73551b4b97d7fcbef0764e2db5200d0a25fef69835106982836ea58ac21a20a324b2757bc55e3e4cb24bfe67
7
+ data.tar.gz: 0ea60981c01d8f9c71017b64a39990b19e915b77a96297738ce667407ce4e66ddc8db266a5472847f2f6c6454cac2ddb49af5a46467b5cd74fb7948e7abe5a19
data/bin/terrafying CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'terrafying'
4
5
 
@@ -1,16 +1,19 @@
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 'json'
7
11
 
8
12
  Aws.use_bundled_cert!
9
13
 
10
14
  module Terrafying
11
15
  module Aws
12
16
  class Ops
13
-
14
17
  attr_reader :region
15
18
 
16
19
  def initialize(region)
@@ -19,11 +22,11 @@ module Terrafying
19
22
  Kernel.sleep(Kernel.rand((sleep_time / 2)..sleep_time))
20
23
  }
21
24
 
22
- ::Aws.config.update({
25
+ ::Aws.config.update(
23
26
  region: region,
24
27
  retry_limit: 7,
25
28
  retry_backoff: half_jitter
26
- })
29
+ )
27
30
 
28
31
  @autoscaling_client = ::Aws::AutoScaling::Client.new
29
32
  @ec2_resource = ::Aws::EC2::Resource.new
@@ -32,6 +35,7 @@ module Terrafying
32
35
  @route53_client = ::Aws::Route53::Client.new
33
36
  @s3_client = ::Aws::S3::Client.new
34
37
  @sts_client = ::Aws::STS::Client.new
38
+ @pricing_client = ::Aws::Pricing::Client.new(region: 'us-east-1') # no AWS Pricing endpoint in Europe
35
39
 
36
40
  @region = region
37
41
  end
@@ -52,14 +56,13 @@ module Terrafying
52
56
  @security_groups ||= {}
53
57
  @security_groups[name] ||=
54
58
  begin
55
- STDERR.puts "Looking up id of security group '#{name}'"
59
+ warn "Looking up id of security group '#{name}'"
56
60
  groups = all_security_groups.select { |g| g.group_name == name }.take(2)
57
- case
58
- when groups.count == 1
61
+ if groups.count == 1
59
62
  groups.first.id
60
- when groups.count < 1
63
+ elsif groups.count < 1
61
64
  raise "No security group with name '#{name}' was found."
62
- when groups.count > 1
65
+ elsif groups.count > 1
63
66
  raise "More than one security group with name '#{name}' found: " + groups.join(', ')
64
67
  end
65
68
  end
@@ -69,14 +72,13 @@ module Terrafying
69
72
  @security_groups_in_vpc ||= {}
70
73
  @security_groups_in_vpc[vpc_id + name] ||=
71
74
  begin
72
- STDERR.puts "Looking up id of security group '#{name}'"
75
+ warn "Looking up id of security group '#{name}'"
73
76
  groups = all_security_groups.select { |g| g.vpc_id == vpc_id && g.group_name == name }.take(2)
74
- case
75
- when groups.count == 1
77
+ if groups.count == 1
76
78
  groups.first.id
77
- when groups.count < 1
79
+ elsif groups.count < 1
78
80
  raise "No security group with name '#{name}' was found."
79
- when groups.count > 1
81
+ elsif groups.count > 1
80
82
  raise "More than one security group with name '#{name}' found: " + groups.join(', ')
81
83
  end
82
84
  end
@@ -87,12 +89,11 @@ module Terrafying
87
89
  @security_groups_by_tags[tags] ||=
88
90
  begin
89
91
  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
92
+ if groups.count == 1
92
93
  groups.first.id
93
- when groups.count < 1
94
+ elsif groups.count < 1
94
95
  raise "No security group with tags '#{tags}' was found."
95
- when groups.count > 1
96
+ elsif groups.count > 1
96
97
  raise "More than one security group with tags '#{tags}' found: " + groups.join(', ')
97
98
  end
98
99
  end
@@ -103,19 +104,18 @@ module Terrafying
103
104
  @instance_profiles[name] ||=
104
105
  begin
105
106
  resource = ::Aws::IAM::Resource.new
106
- STDERR.puts "Looking up id of instance profile '#{name}'"
107
+ warn "Looking up id of instance profile '#{name}'"
107
108
  # unfortunately amazon don't let us filter for profiles using
108
109
  # a name filter, for now we have enumerate and filter manually
109
110
  coll = resource.instance_profiles
110
111
  profiles = []
111
- profiles = coll.select {|p| p.instance_profile_name =~ /#{name}/}
112
+ profiles = coll.select { |p| p.instance_profile_name =~ /#{name}/ }
112
113
 
113
- case
114
- when profiles.count == 1
114
+ if profiles.count == 1
115
115
  profiles.first.instance_profile_id
116
- when profiles.count < 1
116
+ elsif profiles.count < 1
117
117
  raise "No instance profile with name '#{name}' was found."
118
- when profiles.count > 1
118
+ elsif profiles.count > 1
119
119
  raise "More than one instance profile with name '#{name}' found: " + profiles.join(', ')
120
120
  end
121
121
  end
@@ -126,20 +126,18 @@ module Terrafying
126
126
  @route_table_for_subnet[subnet_id] ||=
127
127
  begin
128
128
  resp = @ec2_client.describe_route_tables(
129
- {
130
- filters: [
131
- { name: "association.subnet-id", values: [ subnet_id ] },
132
- ],
133
- })
129
+ filters: [
130
+ { name: 'association.subnet-id', values: [subnet_id] }
131
+ ]
132
+ )
134
133
 
135
134
  route_tables = resp.route_tables
136
135
 
137
- case
138
- when route_tables.count == 1
136
+ if route_tables.count == 1
139
137
  route_tables.first
140
- when route_tables.count < 1
138
+ elsif route_tables.count < 1
141
139
  raise "No route table for subnet '#{subnet_id}' was found."
142
- when profiles.count > 1
140
+ elsif profiles.count > 1
143
141
  raise "More than route table for subnet '#{subnet_id}' found: " + route_tables.join(', ')
144
142
  end
145
143
  end
@@ -150,54 +148,50 @@ module Terrafying
150
148
  @route_table_for_vpc[vpc_id] ||=
151
149
  begin
152
150
  resp = @ec2_client.describe_route_tables(
153
- {
154
- filters: [
155
- { name: "association.main", values: [ "true" ] },
156
- { name: "vpc-id", values: [ vpc_id ] },
157
- ],
158
- })
151
+ filters: [
152
+ { name: 'association.main', values: ['true'] },
153
+ { name: 'vpc-id', values: [vpc_id] }
154
+ ]
155
+ )
159
156
 
160
157
  route_tables = resp.route_tables
161
158
 
162
- case
163
- when route_tables.count == 1
159
+ if route_tables.count == 1
164
160
  route_tables.first
165
- when route_tables.count < 1
161
+ elsif route_tables.count < 1
166
162
  raise "No route table for vpc '#{vpc_id}' was found."
167
- when profiles.count > 1
163
+ elsif profiles.count > 1
168
164
  raise "More than route table for vpc '#{vpc_id}' found: " + route_tables.join(', ')
169
165
  end
170
166
  end
171
167
  end
172
168
 
173
169
  def security_groups(*names)
174
- names.map{|n| security_group(n)}
170
+ names.map { |n| security_group(n) }
175
171
  end
176
172
 
177
173
  def security_groups_in_vpc(vpc_id, *names)
178
- names.map{|n| security_group_in_vpc(vpc_id, n)}
174
+ names.map { |n| security_group_in_vpc(vpc_id, n) }
179
175
  end
180
176
 
181
177
  def subnet(name)
182
178
  @subnets ||= {}
183
179
  @subnets[name] ||=
184
180
  begin
185
- STDERR.puts "Looking up id of subnet '#{name}'"
181
+ warn "Looking up id of subnet '#{name}'"
186
182
  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
183
+ filters: [
184
+ {
185
+ name: 'tag:Name',
186
+ values: [name]
187
+ }
188
+ ]
189
+ ).limit(2)
190
+ if subnets.count == 1
197
191
  subnets.first.id
198
- when subnets.count < 1
192
+ elsif subnets.count < 1
199
193
  raise "No subnet with name '#{name}' was found."
200
- when subnets.count > 1
194
+ elsif subnets.count > 1
201
195
  raise "More than one subnet with this name '#{name}' found : " + subnets.join(', ')
202
196
  end
203
197
  end
@@ -208,23 +202,21 @@ module Terrafying
208
202
  @subnets_by_id[id] ||=
209
203
  begin
210
204
  resp = @ec2_client.describe_subnets(
211
- {
212
- subnet_ids: [id],
213
- })
205
+ subnet_ids: [id]
206
+ )
214
207
  subnets = resp.subnets
215
- case
216
- when subnets.count == 1
208
+ if subnets.count == 1
217
209
  subnets.first
218
- when subnets.count < 1
210
+ elsif subnets.count < 1
219
211
  raise "No subnet with id '#{id}' was found."
220
- when subnets.count > 1
212
+ elsif subnets.count > 1
221
213
  raise "More than one subnet with this id '#{id}' found : " + subnets.join(', ')
222
214
  end
223
215
  end
224
216
  end
225
217
 
226
218
  def subnets(*names)
227
- names.map{|n| subnet(n)}
219
+ names.map { |n| subnet(n) }
228
220
  end
229
221
 
230
222
  def subnets_for_vpc(vpc_id)
@@ -232,48 +224,42 @@ module Terrafying
232
224
  @subnets_for_vpc[vpc_id] ||=
233
225
  begin
234
226
  resp = @ec2_client.describe_subnets(
235
- {
236
- filters: [
237
- { name: "vpc-id", values: [ vpc_id ] },
238
- ],
239
- })
227
+ filters: [
228
+ { name: 'vpc-id', values: [vpc_id] }
229
+ ]
230
+ )
240
231
 
241
232
  subnets = resp.subnets
242
233
 
243
- case
244
- when subnets.count >= 1
234
+ if subnets.count >= 1
245
235
  subnets
246
- when subnets.count < 1
236
+ elsif subnets.count < 1
247
237
  raise "No subnets found for '#{vpc_id}'."
248
238
  end
249
239
  end
250
240
  end
251
241
 
252
- def ami(name, owners=["self"])
242
+ def ami(name, owners = ['self'])
253
243
  @ami ||= {}
254
244
  @ami[name] ||=
255
245
  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
246
+ warn "looking for an image with prefix '#{name}'"
247
+ resp = @ec2_client.describe_images(owners: owners)
248
+ raise 'no images were found' if resp.images.count < 1
249
+
261
250
  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
251
+ raise "no image with name '#{name}' was found" if m.count == 0
252
+
253
+ m.sort { |x, y| y.creation_date <=> x.creation_date }.shift.image_id
266
254
  end
267
255
  end
268
256
 
269
257
  def availability_zones
270
258
  @availability_zones ||=
271
259
  begin
272
- STDERR.puts "looking for AZs in the current region"
260
+ warn 'looking for AZs in the current region'
273
261
  resp = @ec2_client.describe_availability_zones({})
274
- resp.availability_zones.map { |zone|
275
- zone.zone_name
276
- }
262
+ resp.availability_zones.map(&:zone_name)
277
263
  end
278
264
  end
279
265
 
@@ -281,18 +267,17 @@ module Terrafying
281
267
  @vpcs ||= {}
282
268
  @vpcs[name] ||=
283
269
  begin
284
- STDERR.puts "looking for a VPC with name '#{name}'"
270
+ warn "looking for a VPC with name '#{name}'"
285
271
  resp = @ec2_client.describe_vpcs({})
286
- matching_vpcs = resp.vpcs.select { |vpc|
287
- name_tag = vpc.tags.select { |tag| tag.key == "Name" }.first
272
+ matching_vpcs = resp.vpcs.select do |vpc|
273
+ name_tag = vpc.tags.select { |tag| tag.key == 'Name' }.first
288
274
  name_tag && name_tag.value == name
289
- }
290
- case
291
- when matching_vpcs.count == 1
275
+ end
276
+ if matching_vpcs.count == 1
292
277
  matching_vpcs.first
293
- when matching_vpcs.count < 1
278
+ elsif matching_vpcs.count < 1
294
279
  raise "No VPC with name '#{name}' was found."
295
- when matching_vpcs.count > 1
280
+ elsif matching_vpcs.count > 1
296
281
  raise "More than one VPC with name '#{name}' was found: " + matching_vpcs.join(', ')
297
282
  end
298
283
  end
@@ -302,23 +287,20 @@ module Terrafying
302
287
  @route_tables ||= {}
303
288
  @route_tables[name] ||=
304
289
  begin
305
- STDERR.puts "looking for a route table with name '#{name}'"
290
+ warn "looking for a route table with name '#{name}'"
306
291
  route_tables = @ec2_client.describe_route_tables(
307
- {
308
- filters: [
309
- {
310
- name: "tag:Name",
311
- values: [name],
312
- },
313
- ],
314
- }
292
+ filters: [
293
+ {
294
+ name: 'tag:Name',
295
+ values: [name]
296
+ }
297
+ ]
315
298
  ).route_tables
316
- case
317
- when route_tables.count == 1
299
+ if route_tables.count == 1
318
300
  route_tables.first.route_table_id
319
- when route_tables.count < 1
301
+ elsif route_tables.count < 1
320
302
  raise "No route table with name '#{name}' was found."
321
- when route_tables.count > 1
303
+ elsif route_tables.count > 1
322
304
  raise "More than one route table with name '#{name}' was found: " + route_tables.join(', ')
323
305
  end
324
306
  end
@@ -328,23 +310,20 @@ module Terrafying
328
310
  @ips ||= {}
329
311
  @ips[alloc_id] ||=
330
312
  begin
331
- STDERR.puts "looking for an elastic ip with allocation_id '#{alloc_id}'"
313
+ warn "looking for an elastic ip with allocation_id '#{alloc_id}'"
332
314
  ips = @ec2_client.describe_addresses(
333
- {
334
- filters: [
335
- {
336
- name: "allocation-id",
337
- values: [alloc_id],
338
- },
339
- ],
340
- }
315
+ filters: [
316
+ {
317
+ name: 'allocation-id',
318
+ values: [alloc_id]
319
+ }
320
+ ]
341
321
  ).addresses
342
- case
343
- when ips.count == 1
322
+ if ips.count == 1
344
323
  ips.first
345
- when ips.count < 1
324
+ elsif ips.count < 1
346
325
  raise "No elastic ip with allocation_id '#{alloc_id}' was found."
347
- when ips.count > 1
326
+ elsif ips.count > 1
348
327
  raise "More than one elastic ip with allocation_id '#{alloc_id}' was found: " + ips.join(', ')
349
328
  end
350
329
  end
@@ -354,16 +333,15 @@ module Terrafying
354
333
  @hosted_zones ||= {}
355
334
  @hosted_zones[fqdn] ||=
356
335
  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|
336
+ warn "looking for a hosted zone with fqdn '#{fqdn}'"
337
+ hosted_zones = @route53_client.list_hosted_zones_by_name(dns_name: fqdn).hosted_zones.select do |zone|
359
338
  zone.name == "#{fqdn}." && !zone.config.private_zone
360
- }
361
- case
362
- when hosted_zones.count == 1
339
+ end
340
+ if hosted_zones.count == 1
363
341
  hosted_zones.first
364
- when hosted_zones.count < 1
342
+ elsif hosted_zones.count < 1
365
343
  raise "No hosted zone with fqdn '#{fqdn}' was found."
366
- when hosted_zones.count > 1
344
+ elsif hosted_zones.count > 1
367
345
  raise "More than one hosted zone with name '#{fqdn}' was found: " + hosted_zones.join(', ')
368
346
  end
369
347
  end
@@ -373,11 +351,11 @@ module Terrafying
373
351
  @hosted_zones ||= {}
374
352
  @hosted_zones[tag] ||=
375
353
  begin
376
- STDERR.puts "looking for a hosted zone with tag '#{tag}'"
354
+ warn "looking for a hosted zone with tag '#{tag}'"
377
355
  @aws_hosted_zones ||= @route53_client.list_hosted_zones.hosted_zones.map do |zone|
378
356
  {
379
357
  zone: zone,
380
- tags: @route53_client.list_tags_for_resource({resource_type: "hostedzone", resource_id: zone.id.split('/')[2]}).resource_tag_set.tags
358
+ tags: @route53_client.list_tags_for_resource(resource_type: 'hostedzone', resource_id: zone.id.split('/')[2]).resource_tag_set.tags
381
359
  }
382
360
  end
383
361
 
@@ -387,12 +365,11 @@ module Terrafying
387
365
  end
388
366
  end
389
367
 
390
- case
391
- when hosted_zones.count == 1
368
+ if hosted_zones.count == 1
392
369
  hosted_zones.first[:zone]
393
- when hosted_zones.count < 1
370
+ elsif hosted_zones.count < 1
394
371
  raise "No hosted zone with tag '#{tag}' was found."
395
- when hosted_zones.count > 1
372
+ elsif hosted_zones.count > 1
396
373
  raise "More than one hosted zone with tag '#{tag}' was found: " + hosted_zones.join(', ')
397
374
  end
398
375
  end
@@ -402,7 +379,7 @@ module Terrafying
402
379
  @s3_objects ||= {}
403
380
  @s3_objects["#{bucket}-#{key}"] ||=
404
381
  begin
405
- resp = @s3_client.get_object({ bucket: bucket, key: key })
382
+ resp = @s3_client.get_object(bucket: bucket, key: key)
406
383
  resp.body.read
407
384
  end
408
385
  end
@@ -411,7 +388,7 @@ module Terrafying
411
388
  @list_objects ||= {}
412
389
  @list_objects[bucket] ||=
413
390
  begin
414
- resp = @s3_client.list_objects_v2({ bucket: bucket })
391
+ resp = @s3_client.list_objects_v2(bucket: bucket)
415
392
  resp.contents
416
393
  end
417
394
  end
@@ -421,23 +398,20 @@ module Terrafying
421
398
  @endpoint_service[service_name] ||=
422
399
  begin
423
400
  resp = @ec2_client.describe_vpc_endpoint_service_configurations(
424
- {
425
- filters: [
426
- {
427
- name: "service-name",
428
- values: [service_name],
429
- },
430
- ],
431
- }
401
+ filters: [
402
+ {
403
+ name: 'service-name',
404
+ values: [service_name]
405
+ }
406
+ ]
432
407
  )
433
408
 
434
409
  endpoint_services = resp.service_configurations
435
- case
436
- when endpoint_services.count == 1
410
+ if endpoint_services.count == 1
437
411
  endpoint_services.first
438
- when endpoint_services.count < 1
412
+ elsif endpoint_services.count < 1
439
413
  raise "No endpoint service with name '#{service_name}' was found."
440
- when endpoint_services.count > 1
414
+ elsif endpoint_services.count > 1
441
415
  raise "More than one endpoint service with name '#{service_name}' was found: " + endpoint_services.join(', ')
442
416
  end
443
417
  end
@@ -449,16 +423,15 @@ module Terrafying
449
423
  begin
450
424
  resp = @ec2_client.describe_vpc_endpoint_service_configurations
451
425
 
452
- services = resp.service_configurations.select { |service|
426
+ services = resp.service_configurations.select do |service|
453
427
  service.network_load_balancer_arns.include?(arn)
454
- }
428
+ end
455
429
 
456
- case
457
- when services.count == 1
430
+ if services.count == 1
458
431
  services.first
459
- when services.count < 1
432
+ elsif services.count < 1
460
433
  raise "No endpoint service with lb arn '#{arn}' was found."
461
- when services.count > 1
434
+ elsif services.count > 1
462
435
  raise "More than one endpoint service with lb arn '#{arn}' was found: " + services.join(', ')
463
436
  end
464
437
  end
@@ -468,14 +441,13 @@ module Terrafying
468
441
  @lbs ||= {}
469
442
  @lbs[name] ||=
470
443
  begin
471
- load_balancers = @elb_client.describe_load_balancers({ names: [name] }).load_balancers
444
+ load_balancers = @elb_client.describe_load_balancers(names: [name]).load_balancers
472
445
 
473
- case
474
- when load_balancers.count == 1
446
+ if load_balancers.count == 1
475
447
  load_balancers.first
476
- when load_balancers.count < 1
448
+ elsif load_balancers.count < 1
477
449
  raise "No load balancer with name '#{name}' was found."
478
- when load_balancers.count > 1
450
+ elsif load_balancers.count > 1
479
451
  raise "More than one load balancer with name '#{name}' was found: " + load_balancers.join(', ')
480
452
  end
481
453
  end
@@ -486,9 +458,7 @@ module Terrafying
486
458
  @target_groups[arn] ||=
487
459
  begin
488
460
  resp = @elb_client.describe_target_groups(
489
- {
490
- load_balancer_arn: arn,
491
- }
461
+ load_balancer_arn: arn
492
462
  )
493
463
 
494
464
  resp.target_groups
@@ -500,16 +470,16 @@ module Terrafying
500
470
  next_token = nil
501
471
 
502
472
  loop do
503
- resp = @autoscaling_client.describe_auto_scaling_groups({ next_token: next_token })
473
+ resp = @autoscaling_client.describe_auto_scaling_groups(next_token: next_token)
504
474
 
505
- asgs = asgs + resp.auto_scaling_groups.select { |asg|
506
- matches = asg.tags.select { |tag|
475
+ asgs += resp.auto_scaling_groups.select do |asg|
476
+ matches = asg.tags.select do |tag|
507
477
  expectedTags[tag.key.to_sym] == tag.value ||
508
478
  expectedTags[tag.key] == tag.value
509
- }
479
+ end
510
480
 
511
481
  matches.count == expectedTags.count
512
- }
482
+ end
513
483
 
514
484
  if resp.next_token
515
485
  next_token = resp.next_token
@@ -520,7 +490,39 @@ module Terrafying
520
490
 
521
491
  asgs
522
492
  end
523
- end
524
493
 
494
+ def products(products_filter, _region = 'us-east-1')
495
+ next_token = nil
496
+ Enumerator.new do |y|
497
+ loop do
498
+ resp = @pricing_client.get_products(products_filter.merge(next_token: next_token))
499
+ resp.price_list.each do |product|
500
+ y << product
501
+ end
502
+ next_token = resp.next_token
503
+ break if next_token.nil?
504
+ end
505
+ end
506
+ end
507
+
508
+ def instance_type_vcpu_count(instance_type, location = 'EU (Ireland)')
509
+ products_filter = {
510
+ service_code: 'AmazonEC2',
511
+ filters: [
512
+ { field: 'operatingSystem', type: 'TERM_MATCH', value: 'Linux' },
513
+ { field: 'tenancy', type: 'TERM_MATCH', value: 'Shared' },
514
+ { field: 'instanceType', type: 'TERM_MATCH', value: instance_type },
515
+ { field: 'location', type: 'TERM_MATCH', value: location },
516
+ { field: 'preInstalledSw', type: 'TERM_MATCH', value: 'NA' }
517
+ ],
518
+ format_version: 'aws_v1'
519
+ }
520
+
521
+ products(products_filter).each do |product|
522
+ vcpu = JSON.parse(product)['product']['attributes']['vcpu']
523
+ return vcpu.to_i if vcpu
524
+ end
525
+ end
526
+ end
525
527
  end
526
528
  end