Changeset 710 for trunk


Ignore:
Timestamp:
03/17/16 15:06:32 (9 years ago)
Author:
Kris Deugau
Message:

/trunk

Refine/fix behaviour of updateRevSet() and chained subs

  • properly represent add, update, and delete actions all in one set
  • do nothing on null "updates" to keep log noise down
  • properly target requests to a single location to avoid stomping unrelated records
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r709 r710  
    51835183## DNSDB::getZonesByCIDR()
    51845184# Get a list of zone names and IDs that records for a passed CIDR block are within.
     5185# Optionally restrict to a specific location/view
    51855186sub getZonesByCIDR {
    51865187  my $self = shift;
     
    51885189  my %args = @_;
    51895190
    5190   my $result = $dbh->selectall_arrayref("SELECT rdns_id,revnet FROM revzones WHERE revnet >>= ? OR revnet <<= ?",
    5191         { Slice => {} }, ($args{cidr}, $args{cidr}) );
     5191  my $sql = "SELECT rdns_id,revnet,default_location FROM revzones WHERE revnet >>= ? OR revnet <<= ?".
     5192        ($args{location} ? " AND default_location = ?" : '');
     5193  my @svals = ($args{cidr}, $args{cidr});
     5194  push @svals, $args{location} if $args{location};
     5195
     5196  my $result = $dbh->selectall_arrayref($sql, { Slice => {} }, @svals );
    51925197  return $result;
    51935198} # end getZonesByCIDR()
     
    53515356##fixme:  serial
    53525357      $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)", undef,
    5353         ($zone, $group, $args{status}) );
     5358        ($zone, $group, $args{status}) ) or die $dbh->errstr;
    53545359      # get domain id so we can do the records
    53555360      ($zone_id) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
  • trunk/dns-rpc.cgi

    r700 r710  
    137137##
    138138
     139##
     140## Internal utility subs
     141##
     142
    139143# Check RPC ACL
    140144sub _aclcheck {
     
    174178}
    175179
    176 # set ttl to zone defailt minttl if none is specified
     180# set ttl to zone default minttl if none is specified
    177181sub _ttlcheck {
    178182  my $argref = shift;
     
    182186  }
    183187}
     188
     189# Check if the hashrefs passed in refer to identical record data, so we can skip
     190# the actual update if nothing has actually changed.  This is mainly useful for
     191# reducing log noise due to chained calls orginating with updateRevSet() since
     192# "many" records could be sent for update but only one or two have actually changed.
     193sub _checkRecMod {
     194  my $oldrec = shift;
     195  my $newrec = shift;
     196
     197  # Because we don't know which fields we've even been passed
     198  no warnings qw(uninitialized);
     199
     200  my $modflag = 0;
     201  # order by most common change.  host should be first, due to rDNS RPC calls
     202  for my $field qw(host type val) {
     203    return 1 if (
     204        defined($newrec->{$field}) &&
     205        $oldrec->{$field} ne $newrec->{$field} );
     206  }
     207
     208  return 0;
     209} # _checRecMod
     210
     211
     212##
     213## Shims for DNSDB core subs
     214##
    184215
    185216#sub connectDB {
     
    534565} # rpc_updateRec
    535566
     567
    536568# Takes a passed CIDR block and DNS pattern;  adds a new record or updates the record(s) affected
    537569sub addOrUpdateRevRec {
     
    540572  _commoncheck(\%args, 'y');
    541573  my $cidr = new NetAddr::IP $args{cidr};
    542 
    543 ##fixme:  Minor edge case; if we receive calls one after the other to update
    544 # to the same thing, we bulk out the log with useless notices.  Leaving this
    545 # for future development since this should be rare in practice.
    546574
    547575  my $zonelist = $dnsdb->getZonesByCIDR(%args);
     
    570598                || $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
    571599          next unless $rec->{val} eq $filt;     # make sure we really update the record we want to update.
     600          my %newrec = (host => $args{name}, val => $args{cidr}, type => $args{type});
    572601          rpc_updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
    573             parent_id => $zonelist->[0]->{rdns_id}, address => "$cidr", %args);
     602                parent_id => $zonelist->[0]->{rdns_id}, address => "$cidr", %args)
     603                if _checkRecMod($rec, \%newrec);        # and only do the update if there really is something to change
    574604          $flag = 1;
    575605          last; # only do one record.
     
    623653    next unless $key =~ m{^host_((?:[\d.]+|[\da-f:]+)(?:/\d+)?)$};
    624654    my $ip = $1;
    625     push @ret, addOrUpdateRevRec(cidr => $ip, name => $args{$key}, %args);
    626   }
     655    push @ret, addOrUpdateRevRec(%args, cidr => $ip, name => $args{$key});
     656  }
     657
     658  # now we check the parts of the block that didn't get passed to see if they should be deleted
     659  my $block = new NetAddr::IP $args{cidr};
     660  foreach my $ip (@{$block->splitref(32)}) {
     661    my $bare = $ip->addr;
     662    next if $args{"host_$bare"};
     663    delByCIDR(delforward => 1, delsubs => 0, cidr => $bare, location => $args{location},
     664        rpcuser => $args{rpcuser}, rpcsystem => $args{rpcsystem});
     665  }
     666
    627667##fixme:  what about errors?  what about warnings?
    628668  return \@ret;
     
    901941          my $reccidr = new NetAddr::IP $rec->{val};
    902942          next unless $cidr == $reccidr;
     943          # restrict to the right location
     944          next unless ( defined($rec->{locname}) && defined($args{location}) &&
     945                      $rec->{locname} eq $args{location} );
    903946          next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
    904947                      $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
Note: See TracChangeset for help on using the changeset viewer.