Skip to content

Commit

Permalink
Merge pull request #10 from josacar/SREFTP-2005
Browse files Browse the repository at this point in the history
[SREFTP-2005] Fix proxied updater logic
  • Loading branch information
joseluis-fw authored Aug 28, 2019
2 parents d69b470 + 5ac1fe7 commit b97def0
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ rvm:
- 2.3.7
- 2.4.4
- 2.5.1
- 2.6.3
- jruby
script:
- if [ "$TRAVIS_REPO_SLUG" != "peertransfer/glare" ]; then rake spec:unit; fi
before_install: gem install bundler -v 1.16.1
env:
global:
- secure: VvUXF0jh/Etc5bDredzC52PrH9163PlaeITIib7l6PP1tFWoqRER08JFq11Zft6b8O+4WwaMAI+4laGrK7bZNHdMWMNAeutUoZuzLE6doYgAPET56MBGgha1k+jGL2K0vrjQoV2ITIu+Hr0IG/MM92AXkWUIryrFHI41M++1VklXXNn+8VWk7ubae2Mq/KmpIu94N3G/7Rj0PyJ8PHDK/bppq3kgnFyvQvgFTNOqhbAnB8GXz44StVDa9EbdNQN0SBHWdAJBNyEEDpHbtSLDWc8o3e0hLlzTpH+KmsV3WhxLR0STGI8Ji3L9ebXGgXqaR8SRCL96g1pG6RBN7O554svXsvgfqMDQzAQ3xdtTSnmajYOPmgDPXxnC60Vuw4Zg415meL4efZYkhjNZmUJQFCeuaw3nr++c6E5GY5jA4eoBJnf8dxKt+tqSUCvE66JMjWj5mJBGKPHfIYEmlMPJgeSA5xB6+EYE9z18OUcpPx8tboeBD2ZgZKWk+kyviygzprgcSCz1WSNUcoMMOccvy0Yp5kJyPdP+zdMHfLximA5iuOYfK95jgFRfFQygk1UEjskffpH62Ty+DnAQFRxRxmz7rFJphlDryaPjUkzmwmHNw/Jyroo0Ke4KZZ498xQdVzWbHHncKe9QNqyCQ5gbgxLA1yLazjub0VypJP90vp8=
Expand Down
9 changes: 7 additions & 2 deletions lib/glare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def deregister(fqdn, type)
Domain.new(client).deregister(fqdn, type)
end

def proxied?(fqdn, type)
client = build_client
Domain.new(client).proxied?(fqdn, type)
end

private

CF_EMAIL = 'CF_EMAIL'.freeze
Expand All @@ -35,8 +40,8 @@ def client(credentials)
end

def default_credentials
email = ENV[CF_EMAIL]
auth_key = ENV[CF_AUTH_KEY]
email = ENV.fetch(CF_EMAIL)
auth_key = ENV.fetch(CF_AUTH_KEY)
Credentials.new(email, auth_key)
end

Expand Down
4 changes: 4 additions & 0 deletions lib/glare/cf_dns_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def contents
@records.map(&:content)
end

def all_proxied?
@records.all? { |r| r.proxied == true }
end

def each
@records.each { |record| yield(record) }
end
Expand Down
3 changes: 2 additions & 1 deletion lib/glare/cf_dns_records/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ def updated_records
operations = []

@current_records.delete_if do |record|
if new_record = @new_contents.shift
if (new_record = @new_contents.shift)
final_record = record.dup
final_record.content = new_record.content
final_record.proxied = new_record.proxied
operations << Operation.new(final_record, :update)
true
else
Expand Down
8 changes: 7 additions & 1 deletion lib/glare/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(client)
@client = client
end

def register(fqdn, destinations, type, proxied = false)
def register(fqdn, destinations, type, proxied=false)
dns_records = Array(destinations).map do |destination|
DnsRecord.new(type: type, name: fqdn, content: destination, proxied: proxied)
end
Expand All @@ -27,5 +27,11 @@ def deregister(fqdn, type)
dns_records = zone.records(type)
Record.deregister(@client, zone, dns_records)
end

def proxied?(fqdn, type)
zone = Zone.new(@client, fqdn)
records = zone.records(type)
records.all_proxied?
end
end
end
2 changes: 1 addition & 1 deletion lib/glare/domain/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Glare
class Domain
class Record
class << self
def register(client, zone, dns_records, proxied = false)
def register(client, zone, dns_records)
@client = client
existing_records = zone.records(dns_records.first.type)
zone_id = zone.id
Expand Down
2 changes: 1 addition & 1 deletion lib/glare/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Glare
VERSION = '0.6.0'.freeze
VERSION = '0.7.0'.freeze
end
27 changes: 27 additions & 0 deletions spec/proxied_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'glare'

RSpec.describe 'retrieves proxied values', integration: true do
context 'when a domain contains more than one destination' do
let(:domain) { 'a.flywire.com.cn' }
let(:type) { 'A' }
before do
register_domain(domain, destination)
end

context 'two new records' do
let(:destination) { ['1.2.3.4', '5.6.7.8'] }

it 'resolves to right destination' do
expect(proxied?(domain)).to eq(true)
end
end
end

def register_domain(domain, destination)
Glare.register(domain, destination, type, true)
end

def proxied?(domain)
Glare.proxied?(domain, type)
end
end
25 changes: 13 additions & 12 deletions spec/units/glare_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

RSpec.describe Glare do
before do
allow(ENV).to receive(:[]).with('CF_EMAIL').and_return('an_email')
allow(ENV).to receive(:[]).with('CF_AUTH_KEY').and_return('an_auth_key')
allow(ENV).to receive(:fetch).with('CF_EMAIL').and_return('an_email')
allow(ENV).to receive(:fetch).with('CF_AUTH_KEY').and_return('an_auth_key')

allow(Glare::Client).to receive(:new).and_return(client)
end
Expand Down Expand Up @@ -102,19 +102,19 @@
name: 'not-exist.example.com', type: 'CNAME'
).and_return(empty_result)

Glare.register('not-exist.example.com', ['a_destination', 'another_destination'].shuffle, 'CNAME')
Glare.register('not-exist.example.com', ['a_destination', 'another_destination'].shuffle, 'CNAME', true)

expect(client).not_to have_received(:put).
with('/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records', any_args)

expect(client).to have_received(:post).with(
'/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
type: 'CNAME', name: 'not-exist.example.com', content: 'a_destination', proxied: false
type: 'CNAME', name: 'not-exist.example.com', content: 'a_destination', proxied: true
)

expect(client).to have_received(:post).with(
'/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
type: 'CNAME', name: 'not-exist.example.com', content: 'another_destination', proxied: false
type: 'CNAME', name: 'not-exist.example.com', content: 'another_destination', proxied: true
)
end

Expand Down Expand Up @@ -165,25 +165,25 @@
end
end

context 'all records contents are different' do
context 'all records proxied attributes are different' do
it 'sends registration data to update endpoint' do
Glare.register('wadus.example.com', ['a_destination.com', 'yet_another_destination.com'].shuffle, 'CNAME')
destinations = ['destination.com', 'another_destination.com'].shuffle
Glare.register('wadus.example.com', destinations, 'CNAME', true)

expect(client).not_to have_received(:post).
with('/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records', any_args)

expect(client).to have_received(:put).with(
any_args,
type: 'CNAME', name: 'wadus.example.com', content: 'a_destination.com', proxied: false
type: 'CNAME', name: 'wadus.example.com', content: 'destination.com', proxied: true
)

expect(client).to have_received(:put).with(
any_args,
type: 'CNAME', name: 'wadus.example.com', content: 'yet_another_destination.com', proxied: false
type: 'CNAME', name: 'wadus.example.com', content: 'another_destination.com', proxied: true
)
end
end

end

it 'updates different records and deletes extra ones' do
Expand All @@ -194,14 +194,15 @@

expect(client).to have_received(:put).with(
any_args,
{ type: 'CNAME', name: 'wadus.example.com', content: 'a_destination.com', proxied: false }
type: 'CNAME', name: 'wadus.example.com', content: 'a_destination.com', proxied: false
)

expect(client).to have_received(:delete).once
end

it 'updates different records and creates new ones' do
Glare.register('wadus.example.com', ['destination.com', 'another_destination.com', 'a_third_destination.com'].shuffle, 'CNAME')
destinations = ['destination.com', 'another_destination.com', 'a_third_destination.com'].shuffle
Glare.register('wadus.example.com', destinations, 'CNAME')

expect(client).not_to have_received(:put).with(
'/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/a1f984afe5544840505494298f54c33e',
Expand Down
19 changes: 11 additions & 8 deletions spec/units/operations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,23 @@
current_records = Glare::CfDnsRecords.new([current_record2, current_record, current_record3])

new_record = dns_record(content: '1.2.3.8')
update_record = dns_record(content: '1.2.3.4')
update_record = dns_record(content: '1.2.3.4', proxied: true)
new_records = [new_record, update_record].shuffle

operations = Glare::CfDnsRecords::Updater.new(current_records, new_records).calculate

current_record2.content = '1.2.3.8'
update_operation = Glare::CfDnsRecords::Updater::Operation.new(current_record2, :update)
updated_record = current_record.dup.tap { |r| r.proxied = true }
updated_record2 = current_record2.dup.tap { |r| r.content = '1.2.3.8' }

update_operation = Glare::CfDnsRecords::Updater::Operation.new(updated_record2, :update)
update_operation2 = Glare::CfDnsRecords::Updater::Operation.new(updated_record, :update)
delete_operation = Glare::CfDnsRecords::Updater::Operation.new(current_record3, :delete)

expect(operations.updates).to eq([update_operation])
expect(operations.deletions).to eq([delete_operation])
expect(operations.updates).to match_array([update_operation, update_operation2])
expect(operations.deletions).to match_array([delete_operation])
end

it 'can detects new records to delete and update' do
it 'can detect new records to delete and update' do
current_record = existing_record(content: '1.2.3.4')
current_record2 = existing_record(content: '1.2.3.6')
current_record3 = existing_record(content: '1.2.3.5')
Expand All @@ -104,7 +107,7 @@ def existing_record(id: 1_234, name: 'name', type: 'A', content:)
Glare::CfDnsRecord.new(id: id, name: name, type: type, content: content)
end

def dns_record(name: 'name', type: 'A', content:)
Glare::DnsRecord.new(name: name, type: type, content: content)
def dns_record(name: 'name', type: 'A', content:, proxied: false)
Glare::DnsRecord.new(name: name, type: type, content: content, proxied: proxied)
end
end

0 comments on commit b97def0

Please sign in to comment.