Changeset 681


Ignore:
Timestamp:
06/16/15 18:07:30 (10 years ago)
Author:
Kris Deugau
Message:

/trunk

Add resizeTemplate() to RPC handler. Arguably most of the code could
be moved into DNSDB.pm, but most of the core deals with records on a
record-id basis, not on a CIDR-match basis.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dns-rpc.cgi

    r680 r681  
    8989        'dnsdb.updateRevSet'    => \&updateRevSet,
    9090        'dnsdb.splitTemplate'   => \&splitTemplate,
    91 #       'dnsdb.shrinkTemplate'  => \&shrinkTemplate,
     91        'dnsdb.resizeTemplate'  => \&resizeTemplate,
    9292        'dnsdb.delRec'          => \&delRec,
    9393        'dnsdb.delByCIDR'       => \&delByCIDR,
     
    683683} # done splitTemplate()
    684684
     685# Resize a template according to an old/new CIDR pair
     686# Takes the old cidr in $args{oldcidr} and the new in $args{newcidr}
     687sub resizeTemplate {
     688  my %args = @_;
     689
     690  _commoncheck(\%args, 'y');
     691
     692  my $oldcidr = new NetAddr::IP $args{oldcidr};
     693  my $newcidr = new NetAddr::IP $args{newcidr};
     694  die "$oldcidr and $newcidr do not overlap"
     695      unless $oldcidr->contains($newcidr) || $newcidr->contains($oldcidr);
     696  $args{cidr} = $args{oldcidr};
     697
     698  my $up_res;
     699
     700  my $zonelist = $dnsdb->getZonesByCIDR(%args);
     701  if (scalar(@$zonelist) == 0) {
     702    # enhh....  WTF?
     703
     704  } elsif (scalar(@$zonelist) == 1) {
     705    my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
     706    if ($zone->contains($oldcidr)) {
     707      # Find record(s) matching the old and new CIDR
     708
     709      my $sql = q(
     710          SELECT record_id,host,val
     711          FROM records
     712          WHERE rdns_id = ?
     713              AND type IN (12, 65280, 65281, 65282, 65283, 65284)
     714              AND (val = ? OR val = ?)
     715          ORDER BY masklen(inetlazy(val)) ASC
     716      );
     717      my $sth = $dnsdb->{dbh}->prepare($sql);
     718      $sth->execute($zonelist->[0]->{rdns_id}, "$oldcidr", "$newcidr");
     719      my $upd_id;
     720      my $oldhost;
     721      while (my ($recid, $host, $val) = $sth->fetchrow_array) {
     722        my $tcidr = NetAddr::IP->new($val);
     723        if ($tcidr == $newcidr) {
     724          # Match found for new CIDR.  Delete this record.
     725          $up_res = $dnsdb->delRec('n', 'y', $recid);
     726        } else {
     727          # Update this record, then exit the loop
     728          $up_res = rpc_updateRec(%args, val => $newcidr, id => $recid, defrec => 'n', revrec => 'y');
     729          last;
     730        }
     731        # Your llama is on fire
     732      }
     733      $sth->finish;
     734
     735      return "Template record for $oldcidr changed to $newcidr.";
     736
     737    } else {  # $cidr > $zone but we only have one zone
     738      # ebbeh?  CIDR is only partly represented in DNS.  This needs manual intervention.
     739      return "Warning:  $args{cidr} is only partly represented in DNS.  Check and update DNS records manually.";
     740    } # done single-zone-contains-$cidr
     741
     742  } else {
     743    # multiple zones nominally "contain" $cidr
     744  }
     745
     746  return $up_res;
     747} # done resizeTemplate()
     748
    685749sub delRec {
    686750  my %args = @_;
Note: See TracChangeset for help on using the changeset viewer.