diff --git a/.travis.yml b/.travis.yml index 67b3803..69b67c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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= diff --git a/lib/glare.rb b/lib/glare.rb index 2eb0f46..3ec0849 100644 --- a/lib/glare.rb +++ b/lib/glare.rb @@ -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 @@ -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 diff --git a/lib/glare/cf_dns_records.rb b/lib/glare/cf_dns_records.rb index 08c4670..f1a0345 100644 --- a/lib/glare/cf_dns_records.rb +++ b/lib/glare/cf_dns_records.rb @@ -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 diff --git a/lib/glare/cf_dns_records/updater.rb b/lib/glare/cf_dns_records/updater.rb index 864426e..ef2a966 100644 --- a/lib/glare/cf_dns_records/updater.rb +++ b/lib/glare/cf_dns_records/updater.rb @@ -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 diff --git a/lib/glare/domain.rb b/lib/glare/domain.rb index 509ea5d..074f2f5 100644 --- a/lib/glare/domain.rb +++ b/lib/glare/domain.rb @@ -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 @@ -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 diff --git a/lib/glare/domain/record.rb b/lib/glare/domain/record.rb index ab91e42..65c8dd2 100644 --- a/lib/glare/domain/record.rb +++ b/lib/glare/domain/record.rb @@ -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 diff --git a/lib/glare/version.rb b/lib/glare/version.rb index 70cff2f..7de65e5 100644 --- a/lib/glare/version.rb +++ b/lib/glare/version.rb @@ -1,3 +1,3 @@ module Glare - VERSION = '0.6.0'.freeze + VERSION = '0.7.0'.freeze end diff --git a/spec/proxied_spec.rb b/spec/proxied_spec.rb new file mode 100644 index 0000000..3788b66 --- /dev/null +++ b/spec/proxied_spec.rb @@ -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 diff --git a/spec/units/glare_spec.rb b/spec/units/glare_spec.rb index 58b1d28..e726041 100644 --- a/spec/units/glare_spec.rb +++ b/spec/units/glare_spec.rb @@ -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 @@ -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 @@ -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 @@ -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', diff --git a/spec/units/operations_spec.rb b/spec/units/operations_spec.rb index 6bbf4f6..6e60b9f 100644 --- a/spec/units/operations_spec.rb +++ b/spec/units/operations_spec.rb @@ -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') @@ -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