MovableInkAWS 2.7.4 → 2.7.6

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: 4fe63a733a1146744fab177c92681cab506b4b4b7734c4cd3a7c91d65de0fd84
4
- data.tar.gz: a6eab91fdcf488895c874d5cedd53e9ef4ccabba9f679461cfcb9148cefd612b
3
+ metadata.gz: 6f6dce584d3446fcb01d1b4ae891bd1d639f4319662c0ffebe0ab80509a04974
4
+ data.tar.gz: b6535994401231a30f9500393deb8d108271ae37e7224e9a47e608c92bfbe5a1
5
5
  SHA512:
6
- metadata.gz: 82769e75ee09f4c0a8dc5c8a4d75c8aba427c3affd5c56270fdffd3bc5124cd20f1b1459dead8adc458f8cb9da3855d5ad4526adf391e35fe4b36ab5a9569697
7
- data.tar.gz: 3a07058264a557f11c2a80c9e2eaddcb6189514d2cd449c263ca7ce148407e8ddffb5c66acc351c81f2d9edaa69edd1d2686f02308640d951d232c042136d80f
6
+ metadata.gz: d679722fe0abdb344440a2e1dcabb913be3ba9ff279be3d8523c4e98e0d067e6d360362bc8dce7f71d0afd510eead66ab329702792426d5df9336bb3ca8dca04
7
+ data.tar.gz: '0238e618656e228fb021ce475303791d99c09c814e5b55f7948478dc78c3657b2a0d355acee87b8dbd5e0ed61997c82c00888970daae64e13c9927bc87a2b795'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- MovableInkAWS (2.7.4)
4
+ MovableInkAWS (2.7.6)
5
5
  aws-sdk-athena (~> 1)
6
6
  aws-sdk-autoscaling (~> 1)
7
7
  aws-sdk-cloudwatch (~> 1)
@@ -4,9 +4,9 @@ require 'diplomat'
4
4
  module MovableInk
5
5
  class AWS
6
6
  module EC2
7
- def ec2(region: my_region)
7
+ def ec2(region: my_region, client: nil)
8
8
  @ec2_client ||= {}
9
- @ec2_client[region] ||= Aws::EC2::Client.new(region: region)
9
+ @ec2_client[region] ||= (client) ? client : Aws::EC2::Client.new(region: region)
10
10
  end
11
11
 
12
12
  def mi_env_cache_file_path
@@ -38,9 +38,9 @@ module MovableInk
38
38
  end
39
39
  end
40
40
 
41
- def all_instances(region: my_region, no_filter: false)
41
+ def all_instances(region: my_region, no_filter: false, client: nil)
42
42
  @all_instances ||= {}
43
- @all_instances[region] ||= load_all_instances(region, no_filter: no_filter)
43
+ @all_instances[region] ||= load_all_instances(region, no_filter: no_filter, client: client)
44
44
  end
45
45
 
46
46
  def default_filter
@@ -54,11 +54,11 @@ module MovableInk
54
54
  }]
55
55
  end
56
56
 
57
- def load_all_instances(region, no_filter: false, filter: nil)
57
+ def load_all_instances(region, no_filter: false, filter: nil, client: nil)
58
58
  filters = no_filter ? nil : (filter || default_filter)
59
59
 
60
60
  run_with_backoff do
61
- ec2(region: region).describe_instances(filters: filters).flat_map do |resp|
61
+ ec2(region: region, client: client).describe_instances(filters: filters).flat_map do |resp|
62
62
  resp.reservations.flat_map(&:instances)
63
63
  end
64
64
  end
@@ -211,12 +211,26 @@ module MovableInk
211
211
  private_ip_addresses(ordered_instances)
212
212
  end
213
213
 
214
- def redis_by_role(role, port)
215
- instance_ip_addresses_by_role(role: role)
216
- .shuffle
217
- .inject([]) { |redii, instance|
218
- redii.push({"host" => instance, "port" => port})
219
- }
214
+ def redis_by_role(role, port, availability_zones = [])
215
+ if availability_zones.class != Array
216
+ raise MovableInk::AWS::Errors::AvailabilityZonesListInvalidError
217
+ end
218
+
219
+ redis_instances = []
220
+
221
+ def redis(host, port)
222
+ {"host" => host, "port" => port}
223
+ end
224
+
225
+ if availability_zones.length > 0
226
+ availability_zones.each do |az|
227
+ redis_instances << instance_ip_addresses_by_role(role: role, availability_zone: az).inject([]) { |redii, instance| redii.push(redis(instance, port)) }
228
+ end
229
+ else
230
+ redis_instances << instance_ip_addresses_by_role(role: role).inject([]) { |redii, instance| redii.push(redis(instance, port)) }
231
+ end
232
+
233
+ redis_instances.flatten.shuffle
220
234
  end
221
235
 
222
236
  def elastic_ips
@@ -8,6 +8,7 @@ module MovableInk
8
8
  class InvalidDiscoveryTypeError < StandardError; end
9
9
  class RoleNameRequiredError < StandardError; end
10
10
  class RoleNameInvalidError < StandardError; end
11
+ class AvailabilityZonesListInvalidError < StandardError; end
11
12
 
12
13
  class ExpectedError
13
14
  def initialize(error_class, message_patterns = [])
@@ -1,5 +1,5 @@
1
1
  module MovableInk
2
2
  class AWS
3
- VERSION = '2.7.4'
3
+ VERSION = '2.7.6'
4
4
  end
5
5
  end
data/spec/ec2_spec.rb CHANGED
@@ -589,6 +589,34 @@ describe MovableInk::AWS::EC2 do
589
589
  }
590
590
  ]])
591
591
  }
592
+ let(:redis_data_cross_azs) { ec2.stub_data(:describe_instances, reservations: [
593
+ instances: [
594
+ {
595
+ tags: [
596
+ {
597
+ key: 'mi:roles',
598
+ value: 'visitor_redis'
599
+ }
600
+ ],
601
+ private_ip_address: '10.0.0.1',
602
+ placement: {
603
+ availability_zone: availability_zone
604
+ }
605
+ },
606
+ {
607
+ tags: [
608
+ {
609
+ key: 'mi:roles',
610
+ value: 'visitor_redis'
611
+ }
612
+ ],
613
+ private_ip_address: '10.0.0.2',
614
+ placement: {
615
+ availability_zone: 'us-east-1z'
616
+ }
617
+ }
618
+ ]])
619
+ }
592
620
  let(:redii) { [{"host" => '10.0.0.1', "port" => 6379},{"host" => '10.0.0.2', "port" => 6379}] }
593
621
 
594
622
  it "should return redis IPs and ports" do
@@ -600,6 +628,20 @@ describe MovableInk::AWS::EC2 do
600
628
 
601
629
  expect(aws.redis_by_role('visitor_redis', port)).to match_array(redii)
602
630
  end
631
+
632
+ it "should return redis IPs and ports only from specified AZs" do
633
+ ec2.stub_responses(:describe_instances, redis_data_cross_azs)
634
+ allow(aws).to receive(:mi_env).and_return('test')
635
+ allow(aws).to receive(:availability_zone).and_return(availability_zone)
636
+ allow(aws).to receive(:my_region).and_return('us-east-1')
637
+ allow(aws).to receive(:ec2).and_return(ec2)
638
+
639
+ expect(aws.redis_by_role('visitor_redis', port, ['us-east-1z'])).to match_array([{"host" => '10.0.0.2', "port" => 6379}])
640
+ end
641
+
642
+ it "should throw exception if specified AZs list isnt array" do
643
+ expect { aws.redis_by_role('visitor_redis', port, 'us-east-1z') }.to raise_error(MovableInk::AWS::Errors::AvailabilityZonesListInvalidError)
644
+ end
603
645
  end
604
646
 
605
647
  context "elastic IPs" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: MovableInkAWS
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.4
4
+ version: 2.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - MI SRE
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-29 00:00:00.000000000 Z
11
+ date: 2022-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core