Changeset 3 for trunk/DNSDB.pm


Ignore:
Timestamp:
08/18/09 18:04:14 (15 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint
Continued development to reach ~ VegaDNS equivalence

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r2 r3  
    2323@ISA            = qw(Exporter);
    2424@EXPORT_OK      = qw(
    25         &initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec
     25        &initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs
     26        &addRec &delRec &domStatus
    2627        %typemap %reverse_typemap
    2728        );
     
    2930@EXPORT         = (); # Export nothing by default.
    3031%EXPORT_TAGS    = ( ALL => [qw(
    31                 &initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec
     32                &initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs
     33                &addRec &delRec &domStatus
    3234                %typemap %reverse_typemap
    3335                )]
     
    159161  return ('FAIL',"Need domain status") if !defined($state);
    160162
     163  my $dom_id;
     164
    161165  # Allow transactions, and raise an exception on errors so we can catch it later.
    162166  # Use local to make sure these get "reset" properly on exiting this block
     
    173177    $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
    174178    $sth->execute;
    175     my ($dom_id) = $sth->fetchrow_array();
     179    ($dom_id) = $sth->fetchrow_array();
    176180
    177181    # ... and now we construct the standard records from the default set.  NB:  group should be variable.
    178     $sth = $dbh->prepare("select host,type,val,distance,ttl from default_records where group_id=$group");
    179     my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,ttl)".
    180         " values ($dom_id,?,?,?,?,?)");
     182    $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group");
     183    my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)".
     184        " values ($dom_id,?,?,?,?,?,?,?)");
    181185    $sth->execute;
    182     while (my ($host,$type,$val,$dist,$ttl) = $sth->fetchrow_array()) {
     186    while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
    183187      $host =~ s/DOMAIN/$domain/g;
    184       $sth_in->execute($host,$type,$val,$dist,$ttl);
     188      $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
    185189    }
     190
     191    # once we get here, we should have suceeded.
     192    $dbh->commit;
     193  }; # end eval
     194
     195  if ($@) {
     196    my $msg = $@;
     197    eval { $dbh->rollback; };
     198    return ('FAIL',$msg);
     199  } else {
     200    return ('OK',$dom_id);
     201  }
     202} # end addDomain
     203
     204
     205## DNSDB::delDomain()
     206# Delete a domain.
     207# for now, just delete the records, then the domain.
     208# later we may want to archive it in some way instead (status code 2, for example?)
     209sub delDomain {
     210  my $dbh = shift;
     211  my $domain = shift;
     212
     213  # Allow transactions, and raise an exception on errors so we can catch it later.
     214  # Use local to make sure these get "reset" properly on exiting this block
     215  local $dbh->{AutoCommit} = 0;
     216  local $dbh->{RaiseError} = 1;
     217
     218  # Wrap all the SQL in a transaction
     219  eval {
     220    # brute force.
     221    my $sth = $dbh->prepare("select domain_id from domains where domain=?");
     222    $sth->execute($domain);
     223    die "Domain not found, can't delete\n" if $sth->rows < 1;
     224    my ($id) = $sth->fetchrow_array;
     225
     226    $sth = $dbh->prepare("delete from records where domain_id=$id");
     227    $sth->execute;
     228    $sth = $dbh->prepare("delete from domains where domain=?");
     229    $sth->execute($domain);
    186230
    187231    # once we get here, we should have suceeded.
     
    196240    return ('OK','OK');
    197241  }
    198 } # end addDomain
     242
     243} # end delDomain()
    199244
    200245
     
    249294    $sql .= " records where domain_id=$id and type=$reverse_typemap{SOA}";
    250295  }
    251 print "MDEBUG: $sql<br>\n";
     296#print "getSOA DEBUG: $sql<br>\n";
    252297  my $sth = $dbh->prepare($sql);
    253298  $sth->execute;
     
    292337    return undef;
    293338  }
    294 my %ret;
     339  my %ret;
    295340  $ret{recid}   = $recid;
    296341  $ret{host}    = $host;
     
    338383
    339384
    340 ## DNDB::addRec()
     385## DNSDB::addRec()
    341386# Add a new record to a domain or a group's default records
    342387# Takes a database handle, default/live flag, group/domain ID,
     
    378423  my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)";
    379424# something is bugging me about this...
     425print "DEBUG: $sql<br>\n";
    380426  my $sth = $dbh->prepare($sql);
    381427  $sth->execute;
     
    387433
    388434
     435## DNSDB::delRec()
     436# Delete a record. 
     437sub delRec {
     438  $errstr = '';
     439  my $dbh = shift;
     440  my $defrec = shift;
     441  my $id = shift;
     442
     443  my $sth = $dbh->prepare("delete from ".($defrec eq 'y' ? 'default_' : '')."records where record_id=?");
     444  $sth->execute($id);
     445
     446  return ('FAIL',$sth->errstr) if $sth->err;
     447
     448  return ('OK','OK');
     449} # end delRec()
     450
     451
     452## DNSDB::domStatus()
     453# Sets and/or returns a domain's status
     454# Takes a database handle, domain ID and optionally a status argument
     455# Returns undef on errors.
     456sub domStatus {
     457  my $dbh = shift;
     458  my $id = shift;
     459  my $newstatus = shift;
     460
     461  return undef if $id !~ /^\d+$/;
     462
     463  my $sth;
     464
     465# ooo, fun!  let's see what we were passed for status
     466  if ($newstatus) {
     467    $sth = $dbh->prepare("update domains set status=? where domain_id=?");
     468    # ass-u-me caller knows what's going on in full
     469    if ($newstatus =~ /^[01]$/) {       # only two valid for now.
     470      $sth->execute($newstatus,$id);
     471    } elsif ($newstatus =~ /^domo(?:n|ff)$/) {
     472      $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id);
     473    }
     474  }
     475
     476  $sth = $dbh->prepare("select status from domains where domain_id=?");
     477  $sth->execute($id);
     478  my ($status) = $sth->fetchrow_array;
     479  return $status;
     480} # end domStatus()
     481
     482
    389483# shut Perl up
    3904841;
Note: See TracChangeset for help on using the changeset viewer.