Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
MovableInkAWS (2.11.3)
MovableInkAWS (2.11.4)
aws-sdk-athena (~> 1)
aws-sdk-autoscaling (~> 1)
aws-sdk-cloudwatch (~> 1)
Expand Down
31 changes: 26 additions & 5 deletions lib/movable_ink/aws/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ def redis(host, port)
redis_instances.flatten.shuffle
end

def describe_ip(public_ip:)
expected_errors = [
MovableInk::AWS::Errors::ExpectedError.new(Aws::EC2::Errors::InvalidAddressNotFound, [/Address \'#{public_ip}\' not found./])
]
begin
run_with_backoff(expected_errors: expected_errors) do
response = ec2.describe_addresses({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this use ec2_with_retries, so that it automatically retries getting the credentials?

public_ips: [public_ip]
})
raise MovableInk::AWS::Errors::ServiceError if response.length > 1
return response[0]
end
rescue MovableInk::AWS::Errors::ServiceError, Aws::EC2::Errors::InvalidAddressNotFound
return nil
end
end

def elastic_ips
@all_elastic_ips ||= run_with_backoff do
ec2.describe_addresses.addresses
Expand All @@ -258,20 +275,24 @@ def available_elastic_ips(role:)
unassigned_elastic_ips.select { |address| address.tags.detect { |t| t.key == 'mi:roles' && t.value == role } }
end

def assign_ip_address_with_retries(role:)
def assign_ip_address_with_retries(role:, allow_reassociation: false)
response = nil
run_with_backoff do
ec2_with_retries.associate_address({
response = ec2_with_retries.associate_address({
instance_id: instance_id,
allocation_id: available_elastic_ips(role: role).sample.allocation_id
allocation_id: available_elastic_ips(role: role).sample.allocation_id,
allow_reassociation: allow_reassociation
})
end
response
end

def assign_ip_address(role:)
def assign_ip_address(role:, allow_reassociation: false)
run_with_backoff do
ec2.associate_address({
instance_id: instance_id,
allocation_id: available_elastic_ips(role: role).sample.allocation_id
allocation_id: available_elastic_ips(role: role).sample.allocation_id,
allow_reassociation: allow_reassociation
})
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/movable_ink/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module MovableInk
class AWS
VERSION = '2.11.3'
VERSION = '2.11.4'
end
end
34 changes: 34 additions & 0 deletions spec/ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,40 @@

expect(aws.assign_ip_address(role: 'some_role').association_id).to eq('eipassoc-3')
end

it "should assign an elastic IP with allow_reassociation flag" do
ec2.stub_responses(:describe_addresses, elastic_ip_data)
ec2.stub_responses(:associate_address, associate_address_data)
allow(aws).to receive(:my_region).and_return('us-east-1')
allow(aws).to receive(:instance_id).and_return('i-12345')
allow(aws).to receive(:ec2).and_return(ec2)

expect(aws.assign_ip_address(role: 'some_role', allow_reassociation: true).association_id).to eq('eipassoc-3')
end

it "should assign an elastic IP with retries and return response" do
ec2.stub_responses(:describe_addresses, elastic_ip_data)
ec2.stub_responses(:associate_address, associate_address_data)
allow(aws).to receive(:my_region).and_return('us-east-1')
allow(aws).to receive(:instance_id).and_return('i-12345')
allow(aws).to receive(:ec2).and_return(ec2)
allow(aws).to receive(:ec2_with_retries).and_return(ec2)

response = aws.assign_ip_address_with_retries(role: 'some_role')
expect(response.association_id).to eq('eipassoc-3')
end

it "should assign an elastic IP with retries and allow_reassociation flag" do
ec2.stub_responses(:describe_addresses, elastic_ip_data)
ec2.stub_responses(:associate_address, associate_address_data)
allow(aws).to receive(:my_region).and_return('us-east-1')
allow(aws).to receive(:instance_id).and_return('i-12345')
allow(aws).to receive(:ec2).and_return(ec2)
allow(aws).to receive(:ec2_with_retries).and_return(ec2)

response = aws.assign_ip_address_with_retries(role: 'some_role', allow_reassociation: true)
expect(response.association_id).to eq('eipassoc-3')
end
end

context 'elastic ips' do
Expand Down