From 105665a1988a2ad4248f0bffc796f2495933d4db Mon Sep 17 00:00:00 2001 From: Freddy Chu Date: Tue, 14 Apr 2015 14:38:57 +0800 Subject: [PATCH 1/4] add support for weight round robin mock --- .../dns/change_resource_record_sets.rb | 41 ++++++++++++++++--- .../requests/dns/list_resource_record_sets.rb | 37 ++++++++++++++--- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/lib/fog/aws/requests/dns/change_resource_record_sets.rb b/lib/fog/aws/requests/dns/change_resource_record_sets.rb index 10396601d..accdd269a 100644 --- a/lib/fog/aws/requests/dns/change_resource_record_sets.rb +++ b/lib/fog/aws/requests/dns/change_resource_record_sets.rb @@ -151,6 +151,19 @@ module Fog end class Mock + + def record_exist?(zone,change,change_name) + return false if zone[:records][change[:type]].nil? + current_records = zone[:records][change[:type]][change_name] + return false if current_records.nil? + + if !change[:set_identifier].empty? + !current_records[change[:SetIdentifier]].nil? + else + !current_records.empty? + end + end + def change_resource_record_sets(zone_id, change_batch, options = {}) response = Excon::Response.new errors = [] @@ -171,9 +184,9 @@ module Fog zone[:records][change[:type]] = {} end - if zone[:records][change[:type]][change_name].nil? + if !record_exist?(zone, change, change_name) # raise change.to_s if change[:resource_records].nil? - zone[:records][change[:type]][change_name] = + new_record = if change[:alias_target] record = { :alias_target => change[:alias_target] @@ -183,17 +196,35 @@ module Fog :ttl => change[:ttl].to_s, } end - zone[:records][change[:type]][change_name] = { + + new_record = { :change_id => change_id, :resource_records => change[:resource_records] || [], :name => change_name, - :type => change[:type] + :type => change[:type], + :set_identifier => change[:set_identifier], + :weight => change[:weight] }.merge(record) + + if change[:set_identifier].nil? + zone[:records][change[:type]][change_name] = new_record + else + zone[:records][change[:type]][change_name] = {} if zone[:records][change[:type]][change_name].nil? + zone[:records][change[:type]][change_name]['SET_' + change[:set_identifier]] = new_record + end else errors << "Tried to create resource record set #{change[:name]}. type #{change[:type]}, but it already exists" end when "DELETE" - if zone[:records][change[:type]].nil? || zone[:records][change[:type]].delete(change_name).nil? + action_performed = false + if !zone[:records][change[:type]].nil? && !zone[:records][change[:type]][change_name].nil? && !change[:set_identifier].nil? + action_performed = true unless zone[:records][change[:type]][change_name].delete('SET_' + change[:set_identifier]).nil? + zone[:records][change[:type]].delete(change_name) if zone[:records][change[:type]][change_name].empty? + elsif !zone[:records][change[:type]].nil? + action_performed = true unless zone[:records][change[:type]].delete(change_name).nil? + end + + if !action_performed errors << "Tried to delete resource record set #{change[:name]}. type #{change[:type]}, but it was not found" end end diff --git a/lib/fog/aws/requests/dns/list_resource_record_sets.rb b/lib/fog/aws/requests/dns/list_resource_record_sets.rb index 4425b7c14..7667402c4 100644 --- a/lib/fog/aws/requests/dns/list_resource_record_sets.rb +++ b/lib/fog/aws/requests/dns/list_resource_record_sets.rb @@ -59,6 +59,20 @@ module Fog end class Mock + def set_identified_records(record, zone, name) + [].tap do |tmp_records| + record.each do |key,subr| + if subr.is_a?(Hash) && key.start_with?('SET_') + if name.nil? + tmp_records.append(subr) + else + tmp_records.append(subr) if !subr[:name].nil? && subr[:name].gsub(zone[:name],"") >= name + end + end + end + end + end + def list_resource_record_sets(zone_id, options = {}) maxitems = [options[:max_items]||100,100].min @@ -76,13 +90,24 @@ module Fog records ||= [] + tmp_records = [] + if options[:name] + name = options[:name].gsub(zone[:name],"") + + records.each do |r| + tmp_records.append(r) if !r[:name].nil? && r[:name].gsub(zone[:name],"") >= name + tmp_records += set_identified_records(r, zone, name) + end + else + records.each do |r| + tmp_records += set_identified_records(r, zone, nil) + end + end + records = tmp_records + # sort for pagination records.sort! { |a,b| a[:name].gsub(zone[:name],"") <=> b[:name].gsub(zone[:name],"") } - if options[:name] - name = options[:name].gsub(zone[:name],"") - records = records.select{|r| r[:name].gsub(zone[:name],"") >= name } - end next_record = records[maxitems] records = records[0, maxitems] @@ -106,7 +131,9 @@ module Fog { 'ResourceRecords' => r[:resource_records], 'Name' => r[:name], - 'Type' => r[:type] + 'Type' => r[:type], + 'SetIdentifier' => r[:set_identifier], + 'Weight' => r[:weight] }.merge(record) end, 'MaxItems' => maxitems, From ff72924210a99e70db00d6aa84a69e9dca05d193 Mon Sep 17 00:00:00 2001 From: Freddy Chu Date: Tue, 14 Apr 2015 16:25:46 +0800 Subject: [PATCH 2/4] fix hardcoded constant --- lib/fog/aws/requests/dns/change_resource_record_sets.rb | 6 +++--- lib/fog/aws/requests/dns/list_resource_record_sets.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fog/aws/requests/dns/change_resource_record_sets.rb b/lib/fog/aws/requests/dns/change_resource_record_sets.rb index accdd269a..409e47418 100644 --- a/lib/fog/aws/requests/dns/change_resource_record_sets.rb +++ b/lib/fog/aws/requests/dns/change_resource_record_sets.rb @@ -151,7 +151,7 @@ module Fog end class Mock - + SET_PREFIX = 'SET_' def record_exist?(zone,change,change_name) return false if zone[:records][change[:type]].nil? current_records = zone[:records][change[:type]][change_name] @@ -210,7 +210,7 @@ module Fog zone[:records][change[:type]][change_name] = new_record else zone[:records][change[:type]][change_name] = {} if zone[:records][change[:type]][change_name].nil? - zone[:records][change[:type]][change_name]['SET_' + change[:set_identifier]] = new_record + zone[:records][change[:type]][change_name][SET_PREFIX + change[:set_identifier]] = new_record end else errors << "Tried to create resource record set #{change[:name]}. type #{change[:type]}, but it already exists" @@ -218,7 +218,7 @@ module Fog when "DELETE" action_performed = false if !zone[:records][change[:type]].nil? && !zone[:records][change[:type]][change_name].nil? && !change[:set_identifier].nil? - action_performed = true unless zone[:records][change[:type]][change_name].delete('SET_' + change[:set_identifier]).nil? + action_performed = true unless zone[:records][change[:type]][change_name].delete(SET_PREFIX + change[:set_identifier]).nil? zone[:records][change[:type]].delete(change_name) if zone[:records][change[:type]][change_name].empty? elsif !zone[:records][change[:type]].nil? action_performed = true unless zone[:records][change[:type]].delete(change_name).nil? diff --git a/lib/fog/aws/requests/dns/list_resource_record_sets.rb b/lib/fog/aws/requests/dns/list_resource_record_sets.rb index 7667402c4..f93c1e0af 100644 --- a/lib/fog/aws/requests/dns/list_resource_record_sets.rb +++ b/lib/fog/aws/requests/dns/list_resource_record_sets.rb @@ -62,7 +62,7 @@ module Fog def set_identified_records(record, zone, name) [].tap do |tmp_records| record.each do |key,subr| - if subr.is_a?(Hash) && key.start_with?('SET_') + if subr.is_a?(Hash) && key.start_with?(Fog::DNS::AWS::Mock::SET_PREFIX) if name.nil? tmp_records.append(subr) else From fddd1b0eb8d7b23b8ec92bf52898c95ce309db01 Mon Sep 17 00:00:00 2001 From: Freddy Chu Date: Fri, 17 Apr 2015 17:45:03 +0800 Subject: [PATCH 3/4] fix some bugs & rename confusing function name --- lib/fog/aws/requests/dns/list_resource_record_sets.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/fog/aws/requests/dns/list_resource_record_sets.rb b/lib/fog/aws/requests/dns/list_resource_record_sets.rb index f93c1e0af..883106dc0 100644 --- a/lib/fog/aws/requests/dns/list_resource_record_sets.rb +++ b/lib/fog/aws/requests/dns/list_resource_record_sets.rb @@ -59,10 +59,12 @@ module Fog end class Mock - def set_identified_records(record, zone, name) + def list_all_records(record, zone, name) [].tap do |tmp_records| + tmp_records.push(record) if name.nil? || (!record[:name].nil? && record[:name].gsub(zone[:name],"") >= name) record.each do |key,subr| - if subr.is_a?(Hash) && key.start_with?(Fog::DNS::AWS::Mock::SET_PREFIX) + if subr.is_a?(Hash) && key.is_a?(String) && + key.start_with?(Fog::DNS::AWS::Mock::SET_PREFIX) if name.nil? tmp_records.append(subr) else @@ -95,12 +97,11 @@ module Fog name = options[:name].gsub(zone[:name],"") records.each do |r| - tmp_records.append(r) if !r[:name].nil? && r[:name].gsub(zone[:name],"") >= name - tmp_records += set_identified_records(r, zone, name) + tmp_records += list_all_records(r, zone, name) end else records.each do |r| - tmp_records += set_identified_records(r, zone, nil) + tmp_records += list_all_records(r, zone, nil) end end records = tmp_records From 95cad83ae00fd894e660c91cd3addcf907d39684 Mon Sep 17 00:00:00 2001 From: Freddy Chu Date: Fri, 17 Apr 2015 17:56:46 +0800 Subject: [PATCH 4/4] fix null on record[:name] --- lib/fog/aws/requests/dns/list_resource_record_sets.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/aws/requests/dns/list_resource_record_sets.rb b/lib/fog/aws/requests/dns/list_resource_record_sets.rb index 883106dc0..243b19f5d 100644 --- a/lib/fog/aws/requests/dns/list_resource_record_sets.rb +++ b/lib/fog/aws/requests/dns/list_resource_record_sets.rb @@ -61,7 +61,7 @@ module Fog class Mock def list_all_records(record, zone, name) [].tap do |tmp_records| - tmp_records.push(record) if name.nil? || (!record[:name].nil? && record[:name].gsub(zone[:name],"") >= name) + tmp_records.push(record) if !record[:name].nil? && ( name.nil? || record[:name].gsub(zone[:name],"") >= name) record.each do |key,subr| if subr.is_a?(Hash) && key.is_a?(String) && key.start_with?(Fog::DNS::AWS::Mock::SET_PREFIX)