lib/rubber/dns/fog.rb in rubber-2.0.1 vs lib/rubber/dns/fog.rb in rubber-2.0.2

- old
+ new

@@ -10,29 +10,64 @@ def initialize(env) super(env) creds = Rubber::Util.symbolize_keys(env.credentials) @client = ::Fog::DNS.new(creds) + @name_includes_domain = env.name_includes_domain + @name_includes_trailing_period = env.name_includes_trailing_period end + + def normalize_name(name, domain) + domain = domain.gsub(/\.$/, "") if @name_includes_trailing_period + name = name.gsub(/\.$/, "") if @name_includes_trailing_period + name = name.gsub(/.?#{domain}$/, "") if @name_includes_domain + + return name, domain + end + + def denormalize_name(name, domain) + if @name_includes_domain + if name && name.strip.empty? + name = "#{domain}" + else + name = "#{name}.#{domain}" + end + end + + if @name_includes_trailing_period + name = "#{name}." + domain = "#{domain}." + end + + return name, domain + end + def host_to_opts(host) + name, domain = normalize_name(host.name || '', host.zone.domain) + opts = {} - opts[:id] = host.id - opts[:host] = host.name || '' + opts[:id] = host.id if host.respond_to?(:id) && host.id + opts[:host] = name + opts[:domain] = domain opts[:type] = host.type - opts[:data] = host.value if host.value - opts[:ttl] = host.ttl if host.ttl - opts[:priority] = host.priority if host.priority + opts[:data] = Array(host.value).first if host.value + opts[:ttl] = host.ttl.to_i if host.ttl + opts[:priority] = host.priority if host.respond_to?(:priority) && host.priority + return opts end def opts_to_host(opts, host={}) - host[:name] = opts[:host] + name, domain = denormalize_name(opts[:host], opts[:domain]) + + host[:name] = name host[:type] = opts[:type] host[:value] = opts[:data] if opts[:data] host[:ttl] = opts[:ttl] if opts[:ttl] host[:priority] = opts[:priority] if opts[:priority] + return host end def find_or_create_zone(domain) zone = @client.zones.all.find {|z| z.domain =~ /^#{domain}\.?/} @@ -58,22 +93,38 @@ fqdn = "" fqdn << "#{hostname}." if hostname fqdn << "#{opts[:domain]}" end - hosts = fqdn ? (zone.records.find(fqdn) rescue []) : zone.records.all + # TODO: revert this when fog gets fixed + # hosts = fqdn ? (zone.records.all(:name => fqdn) rescue []) : zone.records.all + hosts = zone.records.all + if fqdn + hosts = hosts.find_all do |r| + attributes = host_to_opts(r) + host, domain = attributes[:host], attributes[:domain] + + fog_fqdn = "" + fog_fqdn << "#{host}." if host && ! host.strip.empty? + fog_fqdn << "#{domain}" + + fqdn == fog_fqdn + end + end + hosts.each do |h| keep = true + attributes = host_to_opts(h) - if host_type && h.type != host_type && host_type != '*' + if host_type && host_type != '*' && attributes[:type] != host_type keep = false end - if host_data && h.value != host_data + if host_data && attributes[:data] != host_data keep = false end - + result << h if keep end result end @@ -101,14 +152,23 @@ def update_host_record(old_opts={}, new_opts={}) old_opts = setup_opts(old_opts, [:host, :domain]) new_opts = setup_opts(new_opts, [:host, :domain, :type, :data]) find_hosts(old_opts).each do |h| - opts_to_host(new_opts).each do |k, v| - h.send("#{k}=", v) + changes = opts_to_host(new_opts) + result = nil + if h.respond_to?(:modify) + result = h.modify(changes) + elsif h.respond_to?(:update_host) + result = h.update_host(changes) + else + changes.each do |k, v| + h.send("#{k}=", v) + end + result = h.save end - h.save || raise("Failed to update host #{h.hostname}, #{h.errors.full_messages.join(', ')}") + result || raise("Failed to update host #{h.hostname}, #{h.errors.full_messages.join(', ')}") end end end