Class: DLDInternet::OpenSRS::Domain::Zone::Command

Inherits:
Thor::Command
  • Object
show all
Defined in:
lib/dldinternet/opensrs/domain/zone/command.rb

Instance Method Summary collapse

Instance Method Details

#get(domain) ⇒ Object



81
82
83
84
85
86
87
88
89
# 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

#set(domain, info) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# 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