[vhffs-dev] [578] Simplified record deletion code and fixed couple of bugs.

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 578
Author:   beuss
Date:     2007-04-18 20:46:39 +0000 (Wed, 18 Apr 2007)

Log Message:
-----------
Simplified record deletion code and fixed couple of bugs.

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Panel/DNS.pm
    trunk/vhffs-api/src/Vhffs/Services/DNS.pm
    trunk/vhffs-panel/dns/prefs.pl


Modified: trunk/vhffs-api/src/Vhffs/Panel/DNS.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/DNS.pm	2007-04-18 19:20:12 UTC (rev 577)
+++ trunk/vhffs-api/src/Vhffs/Panel/DNS.pm	2007-04-18 20:46:39 UTC (rev 578)
@@ -182,6 +182,33 @@
 
 =pod
 
+=head2 delete_record
+
+    eval { Vhffs::Panel::DNS::delete_record($dns, $id, $type); };
+    if($@) {
+        print "An error occured: $@\n";
+    } else {
+        print "$type Record deleted\n";
+    }
+
+=cut
+
+sub delete_record {
+    my ($dns, $id, $type) = @_;
+    die() unless(defined $dns && defined $id && defined $type);
+    my $rval = $dns->delete_record($id, $type);
+    return 1 if($rval > 0);
+    die(gettext('Invalid record')."\n") if($rval == -1);
+    die(gettext('Record type doesn\'t exists')."\n") if($rval == -2);
+    die(gettext('Record does not exists')."\n") if($rval == -3);
+    die(gettext('Database error')."\n") if($rval == -4);
+    die(gettext('Unknown error')."\n");
+}
+
+
+
+=pod
+
 =head2 add_a
 
     eval { Vhffs::Panel::DNS::add_a($dns, $redirect, $name, $ip); };
@@ -256,28 +283,6 @@
     die(gettext('Unknown error')."\n");
 }
 
-sub delete_aaaa {
-    my ($dns, $id) = @_;
-    die() unless(defined $dns && defined $id);
-    my $rval = $dns->delete_aaaa($id);
-    return 1 if($rval > 0);
-    die(gettext('Invalid record')."\n") if($rval == -1);
-    die(gettext('Record does not exists')."\n") if($rval == -2);
-    die(gettext('Database error')."\n") if($rval == -3);
-    die(gettext('Unknown error')."\n");
-}
-
-sub delete_a {
-    my ($dns, $id) = @_;
-    die() unless(defined $dns && defined $id);
-    my $rval = $dns->delete_a($id);
-    return 1 if($rval > 0);
-    die(gettext('Invalid record')."\n") if($rval == -1);
-    die(gettext('Record does not exists')."\n") if($rval == -2);
-    die(gettext('Database error')."\n") if($rval == -3);
-    die(gettext('Unknown error')."\n");
-}
-
 sub add_mx {
     my ($dns, $host, $priority) = @_;
     die() unless(defined $dns && defined $host && defined $priority);
@@ -302,28 +307,6 @@
     die(gettext('Unknown error')."\n"),;
 }
 
-sub delete_mx {
-    my ($dns, $id) = @_;
-    die() unless(defined $dns && defined $id);
-    my $rval = $dns->delete_mx($id);
-    return 1 if($rval > 0);
-    die(gettext('Invalid record')."\n") if($rval == -1);
-    die(gettext('Record does not exists')."\n") if($rval == -2);
-    die(gettext('Database error')."\n") if($rval == -3);
-    die(gettext('Unknown error')."\n");
-}
-
-sub delete_ns {
-    my ($dns, $id) = @_;
-    die() unless(defined $dns && defined $id);
-    my $rval = $dns->delete_ns($id);
-    return 1 if($rval > 0);
-    die(gettext('Invalid record')."\n") if($rval == -1);
-    die(gettext('Record does not exists')."\n") if($rval == -2);
-    die(gettext('Database error')."\n") if($rval == -3);
-    die(gettext('Unknown error')."\n");
-}
-
 sub add_ns {
     my ($dns, $host) = @_;
     die() unless(defined $dns && defined $host);
@@ -348,17 +331,6 @@
 
 }
 
-sub delete_cname {
-    my ($dns, $id) = @_;
-    die() unless(defined $dns && defined $id);
-    my $rval = $dns->delete_cname($id);
-    return 1 if($rval > 0);
-    die(gettext('Invalid record')."\n") if($rval == -1);
-    die(gettext('Record does not exists')."\n") if($rval == -2);
-    die(gettext('Database error')."\n") if($rval == -3);
-    die(gettext('Unknown error')."\n");
-}
-
 sub add_cname {
     my ($dns, $name, $dest) = @_;
     die() unless(defined $dns && defined $name && defined $dest);
@@ -402,17 +374,6 @@
     die(gettext('Unknown error')."\n");
 }
 
-sub delete_srv {
-    my ($dns, $id) = @_;
-    die() unless(defined $dns && defined $id);
-    my $rval = $dns->delete_srv($id);
-    return 1 if($rval > 0);
-    die(gettext('Invalid record')."\n") if($rval == -1);
-    die(gettext('Record does not exists')."\n") if($rval == -2);
-    die(gettext('Database error')."\n") if($rval == -3);
-    die(gettext('Unknown error')."\n");
-}
-
 sub add_txt {
     my ($dns, $name, $txt) = @_;
     die() unless(defined $dns && defined $name && defined $txt);
@@ -437,15 +398,4 @@
     die(gettext('Unknown error')."\n"),;
 }
 
-sub delete_txt {
-    my ($dns, $id) = @_;
-    die() unless(defined $dns && defined $id);
-    my $rval = $dns->delete_txt($id);
-    return 1 if($rval > 0);
-    die(gettext('Invalid record')."\n") if($rval == -1);
-    die(gettext('Record does not exists')."\n") if($rval == -2);
-    die(gettext('Database error')."\n") if($rval == -3);
-    die(gettext('Unknown error')."\n");
-}
-
 1;

Modified: trunk/vhffs-api/src/Vhffs/Services/DNS.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Services/DNS.pm	2007-04-18 19:20:12 UTC (rev 577)
+++ trunk/vhffs-api/src/Vhffs/Services/DNS.pm	2007-04-18 20:46:39 UTC (rev 578)
@@ -327,89 +327,37 @@
     return ($dbh->do($sql, undef, $name, $self->{dns_id}, @types) != 0);
 }
 
+=pod
 
-sub delete_cname
-{
-    my ($self, $id) = @_;
+=head2 delete_record
 
-    return -1 unless($id =~ /^\d+$/ );
-    my $rr = $self->{CNAME}{$id};
-    return -2 unless(defined $rr);
+    die("Unable to delete $type record #$id") unless($dns->delete_record($id, $type) > 0);
 
-    my $dbh = $self->get_main()->get_db_object();
-    my $sql = 'DELETE FROM vhffs_dns_rr WHERE id = ? AND type = \'CNAME\' AND zone = ?';
-    $dbh->do($sql, undef, $id, $self->{dns_id}) or return -3;
+Delete record of type C<$type> whose id is C<$id>.
 
-    $self->add_history('Deleted CNAME record '.$rr->{name});
-    delete $self->{CNAME}{$id};
+=cut
 
-    #Update SOA
-    $self->update_serial();
-    return 1;
-}
-
-
-sub delete_ns
+sub delete_record
 {
-    my ($self, $id) = @_;
+    my ($self, $id, $type) = @_;
 
-    return -1 unless( $id =~ /^\d+$/ );
-    my $rr = $self->{NS}{$id};
-    return -2 unless(defined $rr);
+    return -1 unless($id =~ /^\d+$/ );
+    return -2 unless(exists $self->{$type});
+    my $rr = $self->{$type}{$id};
+    return -3 unless(defined $rr);
 
-    my $dbh = $self->get_main->get_db_object();
-    my $sql = 'DELETE FROM vhffs_dns_rr WHERE id = ? AND type = \'NS\' AND zone = ?';
-    $dbh->do($sql, undef, $id, $self->{dns_id}) or return -3;
+    my $dbh = $self->get_main()->get_db_object();
+    my $sql = 'DELETE FROM vhffs_dns_rr WHERE id = ? AND type = ? AND zone = ?';
+    $dbh->do($sql, undef, $id, $type, $self->{dns_id}) or return -3;
 
-    $self->add_history('Deleted NS record '.$rr->{data});
+    $self->add_history("Deleted $type record ".$rr->{name});
+    delete $self->{$type}{$id};
 
-    delete $self->{NS}{$id};
-    
+    #Update SOA
     $self->update_serial();
-    
     return 1;
 }
 
-sub delete_mx
-{
-    my ($self, $id) = @_;
-
-    return -1 unless( $id =~ /^\d+$/ );
-    my $rr = $self->{MX}{$id};
-    return -2 unless(defined $rr);
-
-    my $dbh = $self->get_main->get_db_object();
-    my $sql = 'DELETE FROM vhffs_dns_rr WHERE id = ? AND type = \'MX\' AND zone = ?';
-    $dbh->do($sql, undef, $id, $self->{dns_id}) or return -3;
-
-    $self->add_history('Deleted the MX record '.$rr->{name}.' (priority: '.$rr->{aux}.')');
-
-    delete $self->{MX}{$id};
-
-    $self->update_serial();
-    return 1;
-}
-
-
-sub delete_a
-{
-    my ($self, $id) = @_;
-
-    return -1 unless($id =~ /^\d+$/ );
-    return -2 unless(defined $self->{A}{$id});
-
-    my $dbh = $self->get_main->get_db_object();
-    my $sql = 'DELETE FROM vhffs_dns_rr WHERE type = \'A\' AND zone = ? AND id = ?';
-    $dbh->do($sql, undef, $self->{dns_id}, $id) or return -3;
-
-    $self->add_history('Deleted A record '.$self->{A}{$id}{name});
-    delete $self->{A}{$id};
-    
-    $self->update_serial();
-    return 1;
-}
-
-
 sub update_a
 {
     my ( $self , $id, $ip ) = @_;
@@ -608,8 +556,8 @@
     $svc = "_$svc" unless($svc =~ /^_/);
     $svc = lc($svc);
     my $name = "$svc.$proto";
+    $host .= '.' unless($host =~ /[.]$/);
     my $data = "$weight $port $host";
-    $host .= '.' unless(/[.]$/);
 
 
     # Looks if this host is already registered for the same service
@@ -661,7 +609,7 @@
     return -5 unless($priority =~ /^\d+$/ && $priority <= 65535 && $priority > 0);
     return -6 unless($weight =~ /^\d+$/ && $weight <= 65535 && $weight > 0);
 
-    $host .= '.' unless(/[.]$/);
+    $host .= '.' unless($host =~ /[.]$/);
 
     my $data = "$weight $port $host";
 
@@ -683,34 +631,6 @@
 
 =pod
 
-=head2 delete_srv
-
-    die("Unable to delete SRV record #$id\n") unless($dns->delete_srv($id));
-
-Deletes SRV record which id is $id.
-
-=cut
-
-sub delete_srv
-{
-    my ($self, $id) = @_;
-
-    return -1 unless($id =~ /^\d+$/ );
-    return -2 unless(defined $self->{SRV}{$id});
-
-    my $dbh = $self->get_main->get_db_object();
-    my $sql = 'DELETE FROM vhffs_dns_rr WHERE type = \'SRV\' AND zone = ? AND id = ?';
-    $dbh->do($sql, undef, $self->{dns_id}, $id) or return -3;
-
-    $self->add_history('Deleted SRV record '.$self->{SRV}{$id}{name});
-    delete $self->{SRV}{$id};
-
-    $self->update_serial();
-    return 1;
-}
-
-=pod
-
 =head2 add_aaaa
 
     die('Unable to add A Record\n') unless($dns->($name, $ip) > 0)
@@ -794,34 +714,6 @@
 
 =pod
 
-=head2 delete_aaaa
-
-    die("Unable to delete AAAA record #$id\n") unless($dns->delete_aaaa($id) > 0);
-
-Delete AAAA record whose id is C<$id>.
-
-=cut
-
-sub delete_aaaa
-{
-    my ($self, $id) = @_;
-
-    return -1 unless($id =~ /^\d+$/ );
-    return -2 unless(defined $self->{AAAA}{$id});
-
-    my $dbh = $self->get_main->get_db_object();
-    my $sql = 'DELETE FROM vhffs_dns_rr WHERE type = \'AAAA\' AND zone = ? AND id = ?';
-    $dbh->do($sql, undef, $self->{dns_id}, $id) or return -3;
-
-    $self->add_history('Deleted AAAA record '.$self->{AAAA}{$id}{name});
-    delete $self->{AAAA}{$id};
-    
-    $self->update_serial();
-    return 1;
-}
-
-=pod
-
 =head2 add_txt
 
     die('Unable to add TXT record') unless($dns->add_txt($prefix, $txt));
@@ -894,34 +786,6 @@
     $self->update_serial();
 }
 
-=pod
-
-=head2 delete_txt
-
-    die("Unable to delete TXT record #$id\n") unless($dns->delete_txt($id));
-
-Deletes TXT record which id is $id.
-
-=cut
-
-sub delete_txt
-{
-    my ($self, $id) = @_;
-
-    return -1 unless($id =~ /^\d+$/ );
-    return -2 unless(defined $self->{TXT}{$id});
-
-    my $dbh = $self->get_main->get_db_object();
-    my $sql = 'DELETE FROM vhffs_dns_rr WHERE type = \'TXT\' AND zone = ? AND id = ?';
-    $dbh->do($sql, undef, $self->{dns_id}, $id) or return -3;
-
-    $self->add_history('Deleted TXT record '.$self->{TXT}{$id}{name});
-    delete $self->{TXT}{$id};
-
-    $self->update_serial();
-    return 1;
-}
-
 sub update_cname
 {
     my ($self, $id, $dest) = @_;

Modified: trunk/vhffs-panel/dns/prefs.pl
===================================================================
--- trunk/vhffs-panel/dns/prefs.pl	2007-04-18 19:20:12 UTC (rev 577)
+++ trunk/vhffs-panel/dns/prefs.pl	2007-04-18 20:46:39 UTC (rev 578)
@@ -121,19 +121,19 @@
                 else { $panel->add_info(gettext('A Record updated')); }
             } else {
                 # User wants to delete it
-                eval { Vhffs::Panel::DNS::delete_a($dns, $id); };
+                eval { Vhffs::Panel::DNS::delete_record($dns, $id, 'A'); };
                 if($@) { $panel->add_error(sprintf(gettext('Unable to delete A record: %s'), $@)); }
                 else { $panel->add_info(gettext('A Record deleted')); }
             }
         } elsif($action eq 'manage_aaaa') {
             if(defined $cgi->param('modify_aaaa_submit')) {
-                # User just want to modify an A record
+                # User just want to modify an AAAA record
                 eval { Vhffs::Panel::DNS::update_aaaa($dns, $id, $data); };
                 if($@) { $panel->add_error(sprintf(gettext('Unable to modify AAAA record: %s'), $@)); }
                 else { $panel->add_info(gettext('AAAA Record updated')); }
             } else {
                 # User wants to delete it
-                eval { Vhffs::Panel::DNS::delete_aaaa($dns, $id); };
+                eval { Vhffs::Panel::DNS::delete_record($dns, $id, 'AAAA'); };
                 if($@) { $panel->add_error(sprintf(gettext('Unable to delete AAAA record: %s'), $@)); }
                 else { $panel->add_info(gettext('AAAA Record deleted')); }
             }
@@ -155,7 +155,7 @@
                 else { $panel->add_info(gettext('MX Record updated')); }
             } else {
                 # MX deletion
-                eval { Vhffs::Panel::DNS::delete_mx($dns, $id); };
+                eval { Vhffs::Panel::DNS::delete_record($dns, $id, 'MX'); };
                 if($@) { $panel->add_error(sprintf(gettext('Unable to delete MX record: %s'), $@)); }
                 else { $panel->add_info(gettext('MX Record deleted')); }
             }
@@ -165,7 +165,7 @@
             else { $panel->add_info(gettext('MX Record added')); }
         } elsif($action eq 'manage_ns') {
             # Only deletion is allowed for NS record
-            eval { Vhffs::Panel::DNS::delete_ns($dns, $id); };
+            eval { Vhffs::Panel::DNS::delete_record($dns, $id, 'NS'); };
             if($@) { $panel->add_error(sprintf(gettext('Unable to delete NS record: %s'), $@)); }
             else { $panel->add_info(gettext('NS Record deleted')); }
         } elsif($action eq 'add_ns') {
@@ -178,7 +178,7 @@
                 if($@) { $panel->add_error(sprintf(gettext('Unable to modify CNAME record: %s'), $@)); }
                 else { $panel->add_info(gettext('CNAME Record updated')); }
             } else {
-                eval { Vhffs::Panel::DNS::delete_cname($dns, $id); };
+                eval { Vhffs::Panel::DNS::delete_record($dns, $id, 'CNAME'); };
                 if($@) { $panel->add_error(sprintf(gettext('Unable to delete CNAME record: %s'), $@)); }
                 else { $panel->add_info(gettext('CNAME Record deleted')); }
             }
@@ -195,7 +195,7 @@
                 if($@) { $panel->add_error(sprintf(gettext('Unable to modify SRV record: %s'), $@)); }
                 else { $panel->add_info(gettext('SRV Record updated')); }
             } else {
-                eval { Vhffs::Panel::DNS::delete_srv($dns, $id); };
+                eval { Vhffs::Panel::DNS::delete_record($dns, $id, 'SRV'); };
                  if($@) { $panel->add_error(sprintf(gettext('Unable to delete SRV record: %s'), $@)); }
                  else { $panel->add_info(gettext('SRV Record deleted')); }
             }
@@ -222,7 +222,7 @@
                 else { $panel->add_info(gettext('TXT Record updated')); }
             } else {
                 # TXT deletion
-                eval { Vhffs::Panel::DNS::delete_txt($dns, $id); };
+                eval { Vhffs::Panel::DNS::delete_record($dns, $id, 'TXT'); };
                 if($@) { $panel->add_error(sprintf(gettext('Unable to delete TXT record: %s'), $@)); }
                 else { $panel->add_info(gettext('TXT Record deleted')); }
             }
@@ -384,7 +384,7 @@
     }
 
     if( scalar(keys %{$srv}) ) {
-        my @list_srv = sort {$a->{data} cmp $b->{data}} values(%{$srv});
+        my @list_srv = sort {$a->{name} cmp $b->{name}} values(%{$srv});
         $thirdtemplate = new HTML::Template( filename => $templatedir.'/dns/list_srv_sub.tmpl', global_vars => 1, die_on_bad_params => 0 );
         $thirdtemplate->param( DEL => gettext( 'Delete' ) );
         $thirdtemplate->param( MODIFY => gettext( 'Modify' ) );
@@ -394,9 +394,9 @@
         $thirdtemplate->param( PRIORITY_TXT => gettext( 'Priority' ) );
         $thirdtemplate->param( WEIGHT_TXT => gettext( 'Weight' ) );
         $thirdtemplate->param( SRV_RR => \@list_srv );
-        $template->param( LIST_TXT => $thirdtemplate->output );
+        $template->param( LIST_SRV => $thirdtemplate->output );
     } else {
-        $template->param( LIST_TXT => gettext( "No TXT available on this domain") );
+        $template->param( LIST_SRV => gettext( 'No SRV available on this domain') );
     }
 
     if( scalar(keys %{$txt}) ) {
@@ -409,7 +409,7 @@
         $thirdtemplate->param( TXT_RR => \@list_txt );
         $template->param( LIST_TXT => $thirdtemplate->output );
     } else {
-        $template->param( LIST_TXT => gettext( "No TXT available on this domain") );
+        $template->param( LIST_TXT => gettext( 'No TXT available on this domain') );
     }
 
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/