1
0
Fork 0
mirror of https://github.com/fog/fog-aws.git synced 2022-11-09 13:50:52 -05:00

Merge pull request #89 from freddy1666/support_weight_round_robin_mock

Support weight round robin mock
This commit is contained in:
Wesley Beary 2015-04-17 14:57:47 -05:00
commit 7f3650d1a8
2 changed files with 69 additions and 10 deletions

View file

@ -151,6 +151,19 @@ 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]
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_PREFIX + 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_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?
end
if !action_performed
errors << "Tried to delete resource record set #{change[:name]}. type #{change[:type]}, but it was not found"
end
end

View file

@ -59,6 +59,22 @@ module Fog
end
class Mock
def list_all_records(record, zone, name)
[].tap do |tmp_records|
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)
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 +92,23 @@ module Fog
records ||= []
tmp_records = []
if options[:name]
name = options[:name].gsub(zone[:name],"")
records.each do |r|
tmp_records += list_all_records(r, zone, name)
end
else
records.each do |r|
tmp_records += list_all_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 +132,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,