class DLDInternet::OpenSRS::Domain::Zone::Command

Public Instance Methods

get(domain) click to toggle source
# File lib/dldinternet/opensrs/domain/zone/command.rb, line 81
def get(domain)
  command_pre(domain)
  zone_records_format
  answer = DLDInternet::OpenSRS::API::Domain::Zone.new(options, @logger).GetDomainDNSZone(domain)
  # showDNSZoneAnswer(answer, domain)
  res = mapDNSZoneAnswer(answer, domain)
  command_out(res)
  0
end
mapDNSZoneAnswer(answer,domain) click to toggle source

noinspection RubyStringKeysInHashInspection

# File lib/dldinternet/opensrs/domain/zone/command.rb, line 48
def mapDNSZoneAnswer(answer,domain)
  if answer['records']
    case @config[:format].to_s
    when /none|text|awesome/
      records = []
      answer['records'].each { |type,list|
        list.each { |entry|
          case type
          when %r'^A'
            records << { 'subdomain' => entry['subdomain'], 'type' => type, 'priority' => '', 'data' => entry['ip_address'], 'weight' => '', 'port' => '' }
          when 'MX'
            records << { 'subdomain' => entry['subdomain'], 'type' => type, 'priority' => entry['priority'], 'data' => entry['hostname'], 'weight' => '', 'port' => '' }
          when 'SRV'
            records << { 'subdomain' => entry['subdomain'], 'type' => type, 'priority' => entry['priority'], 'data' => entry['hostname'], 'weight' => entry['weight'], 'port' => entry['port'] }
          when 'TXT'
            records << { 'subdomain' => entry['subdomain'], 'type' => type, 'priority' => '', 'data' => entry['text'], 'weight' => '', 'port' => '' }
          when 'CNAME'
            records << { 'subdomain' => entry['subdomain'], 'type' => type, 'priority' => '', 'data' => entry['hostname'], 'weight' => '', 'port' => '' }
          else
            records << { 'subdomain' => '', 'type' => type, 'priority' => '', 'data' => entry.to_s, 'weight' => '', 'port' => '' }
          end
        }
      }
      records
    else
      answer['records']
    end
  end
end
set(domain, info) click to toggle source
# File lib/dldinternet/opensrs/domain/zone/command.rb, line 92
def set(domain, info)
  command_pre(domain)
  zone_records_format

  answer = DLDInternet::OpenSRS::API::Domain::Zone.new(options, @logger).GetDomainDNSZone(domain)
  if answer['records']
    records = answer['records']
    delta = nil
    begin
      delta = JSON.parse(File.exists?(info) ? File.read(info) : info)
      op = '='
    rescue JSON::ParserError => e
      logger.debug e.ai
      matches = info.match(%r{^([+\-]*)([a-z\.0-9]*)\s*(A|AAAA|CNAME|MX|SRV|TXT)\s+(.*)$})
      if matches
        logger.debug matches.ai
        _, op, subdomain, type, ip_address = matches.to_a
        op = '+' if op.nil? || op.empty?
        case type
        when /^A+/
          delta = { 'A' => [ { 'ip_address' => ip_address } ] }
          delta['A'][0]['subdomain'] = subdomain unless subdomain.empty?
        when 'CNAME'
          raise "ERROR: #{type} not implemented"
        when 'MX'
          raise "ERROR: #{type} not implemented"
        when 'SRV'
          raise "ERROR: #{type} not implemented"
        when 'TXT'
          raise "ERROR: #{type} not implemented"
        else
          raise "ERROR: #{info} cannot be parsed!"
        end
      else
        delta = {}
      end
    end
    logger.info delta.ai
    if op == '='
      records = delta
    else
      delta.each do |typ,lst|
        lst.each{ |ent|
          case typ
          when %r'^A'
            case op
            when  '+'
              existing = records[typ].select { |rec|
                (ent['subdomain'] && ent['subdomain'].eql?(rec['subdomain'])) ||
                    (ent['subdomain'].nil? && rec['subdomain'].nil?)
              }
              logger.warn existing.ai
              if existing.size > 0
                existing.each do |rec|
                  rec['ip_address'] = ent['ip_address']
                end
              else
                records[typ].push(ent)
              end
            when '-'
              records[typ] = records[typ].select { |rec|
                ((ent['subdomain'] && !ent['subdomain'].eql?(rec['subdomain'])) ||
                    (ent['subdomain'].nil? && rec['subdomain'].nil?)) && !ent['ip_address'].eql?(rec['ip_address'])
              }
            else
              logger.error "Invalid operation: #{op}"
              raise
            end
          when 'MX'
            raise "#{typ} update not implemented"
          when 'SRV'
            raise "#{typ} update not implemented"
          when 'TXT'
            raise "#{typ} update not implemented"
          when 'CNAME'
          else
            logger.error "Unknown entry: #{ent}"
            raise
          end
        }
      end
    end
    logger.debug records.ai
    answer = DLDInternet::OpenSRS::API::Domain::Zone.new(options, @logger).SetDomainDNSZone(domain,records)
    # showDNSZoneAnswer(answer, domain)
    res = mapDNSZoneAnswer(answer, domain)
    command_out(res)
  else
    logger.error "Unable to retrieve DNS zone info for #{domain}"
  end
  0
end
showDNSZoneAnswer(answer,domain) click to toggle source
# File lib/dldinternet/opensrs/domain/zone/command.rb, line 13
def showDNSZoneAnswer(answer,domain)
  if answer['records']
    case @config[:format].to_s
    when /none|text/
      #@config[:format] = :awesome
      output domain if options[:verbose]
      output header_line unless options[:header] === false
      width = 32
      answer['records'].map { |type,list|
        list.map{ |entry|
          case type
          when %r'^A'
            output sprintf("%#{width}s\t%3s\t%3s\t%s", entry['subdomain'], type, '', entry['ip_address'])
          when 'MX'
            output sprintf("%#{width}s\t%3s\t%3s\t%s", entry['subdomain'], type, entry['priority'], entry['hostname'])
          when 'SRV'
            output sprintf("%#{width}s\t%3s\t%3s\t%s\t%s\t%s", entry['subdomain'], type, entry['priority'], entry['weight'], entry['hostname'], entry['port'])
          when 'TXT'
            output sprintf("%#{width}s\t%3s\t%3s\t%s", entry['subdomain'], type, '', entry['text'])
          when 'CNAME'
            output sprintf("%#{width}s\t%3s\t%3s\t%s", entry['subdomain'], type, '', entry['hostname'])
          else
            output "#{type}\t#{entry.to_s}"
          end
        }
      }
    else
      output answer
    end
  else
    output answer
  end
end