Module: Zonify::Mappings

Extended by:
Mappings
Included in:
Mappings
Defined in:
lib/zonify.rb

Instance Method Summary collapse

Instance Method Details

#apply(name, mappings) ⇒ Object

Apply mappings to the name in order. (A hash can be used for mappings but then one will not be able to predict the order.) If no mappings apply, the empty list is returned.



580
581
582
583
584
585
586
587
# File 'lib/zonify.rb', line 580

def apply(name, mappings)
  name_ = Zonify.dot_(name)
  mappings.map do |k, v|
    _k_ = Zonify.dot_(Zonify._dot(k))
    before = Zonify::Mappings.unsuffix(name_, _k_)
    v.map{|s| Zonify.dot_(before + Zonify._dot(s)) } if before
  end.compact.flatten
end

#names(name, mappings) ⇒ Object

Get the names that result from the mappings, or the original name if none apply. The first name in the list is taken to be the canonical name, the one used for groups of servers in SRV records.



591
592
593
594
# File 'lib/zonify.rb', line 591

def names(name, mappings)
  mapped = Zonify::Mappings.apply(name, mappings)
  mapped.empty? ? [name] : mapped
end

#parse(s) ⇒ Object



573
574
575
576
# File 'lib/zonify.rb', line 573

def parse(s)
  k, *v = s.split(':')
  [k, v] if k and v and not v.empty?
end

#rewrite(tree, mappings) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/zonify.rb', line 599

def rewrite(tree, mappings)
  tree.inject({}) do |acc, pair|
    name, info = pair
    names = Zonify::Mappings.names(name, mappings)
    names.each do |name|
      acc[name] ||= {}
      info.inject(acc[name]) do |acc_, pair_|
        type, data = pair_
        acc_[type] ||= {}
        prefix_ = Zonify.dot_(Zonify::Resolve::SRV_PREFIX)
        rrs = if type == 'SRV' and name.start_with? prefix_ and data[:value]
                data[:value].map do |rr|
                  if /^(.+) ([^ ]+)$/.match(rr)
                    "#{$1} #{Zonify::Mappings.names($2, mappings).first}"
                  else
                    rr
                  end
                end
              end
        normed = (rrs + (acc_[type][:value] or [])).sort.uniq if rrs
        addenda = normed ? { :value => normed } : {}
        acc_[type] = data.merge(addenda)
        acc_
      end
    end
    acc
  end
end

#unsuffix(s, suffix) ⇒ Object



595
596
597
598
# File 'lib/zonify.rb', line 595

def unsuffix(s, suffix)
  before, _, after = s.rpartition(suffix)
  before if after.empty?
end