[vhffs-dev] [1270] Added records list, deletion and update (fully untested)

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


Revision: 1270
Author:   beuss
Date:     2008-10-08 16:18:10 +0200 (Wed, 08 Oct 2008)

Log Message:
-----------
Added records list, deletion and update (fully untested)

Modified Paths:
--------------
    trunk/vhffs-tools/src/vhffs-managedns


Modified: trunk/vhffs-tools/src/vhffs-managedns
===================================================================
--- trunk/vhffs-tools/src/vhffs-managedns	2008-10-08 14:17:16 UTC (rev 1269)
+++ trunk/vhffs-tools/src/vhffs-managedns	2008-10-08 14:18:10 UTC (rev 1270)
@@ -42,7 +42,7 @@
 				-label => 'Resource Records',
 				-submenu => [
 					{ -label => 'New	^B', -value => \&create_rr },
-					{ -label => 'List	^V', -value => '' }
+					{ -label => 'Modify	^V', -value => \&list_rr }
 				]
 			}
 		]
@@ -81,6 +81,10 @@
 	$w_new_rr->focus();
 }
 
+sub list_rr {
+    $w_list_rr->focus();
+}
+
 sub create_windows {
 	my %w_common_attrs = (
 		-padtop => 1, -border => 1, -ipad => 5, -padbottom => 1, -titlefullwidth => 1
@@ -160,7 +164,6 @@
 	$label = $w_new_rr->add( undef, 'Label', -text => 'Type: ', -y => 4 );
 	$field = $w_new_rr->add( 'new_rr_type', 'Popupmenu', -y => 4, -x => 20, -values => ['A', 'AAAA', 'CNAME', 'NS', 'MX', 'TXT'], 
 				-selected => 0, -onchange => \&rr_type_change );
-
 	$label = $w_new_rr->add( 'new_rr_ip_lbl', 'Label', -text => 'IP address: ', -y => 5, -width => 20);
 	$field = $w_new_rr->add( 'new_rr_ip', 'TextEntry', -sbborder => 1, -y => 5, -x => 20, -width => 30 );
 	$label = $w_new_rr->add( 'new_rr_priority_lbl', 'Label', -text => 'Priority: ', -y => 5, -hidden => 1, -focusable => 0, -width => 20 );
@@ -180,6 +183,44 @@
 		-x => 20,
 		-y => 6
 	);
+
+    #########################
+    # Resource records list #
+    #########################
+
+    $w_list_rr = $ui->add( 'list_rr_window', 'Window', -title => 'List Resource Records', %w_common_attrs);
+    $label = $w_list_rr->add( undef, 'Label', -text => 'Domain: ', -y => 1 );
+    $field = $w_list_rr->add( 'list_rr_domains', 'Popupmenu', -y => 1, -x => 20, -labels => \%domains_labels, -values => \@domains_values, -selected => 0,
+        -onchange => \&list_rr_domain_change );
+    $label = $w_list_rr->add( undef, 'Label', -text => 'Name: ', -y => 2);
+    $field = $w_list_rr->add( 'list_rr_rrs', 'Popupmenu', -y => 2, -x => 20, -width => 30, -onchange => \&list_rr_rr_change );
+    $label = $w_list_rr->add( 'list_rr_ip_lbl', 'Label', -text => 'IP address: ', -y => 3, -hidden => 1, -focusable => 0, -width => 20);
+    $label = $w_list_rr->add( 'list_rr_destination_lbl', 'Label', -text => 'Destination: ', -y => 3, -hidden => 1, -focusable => 0, -width => 20 );
+    $label = $w_list_rr->add( 'list_rr_text_lbl', 'Label', -text => 'Text: ', -y => 3, -hidden => 1, -focusable => 0, -width => 20 );
+    $field = $w_list_rr->add( 'list_rr_data', 'TextEntry', -sbborder => 1, -y => 3, -x => 20, -width => 30, -hidden => 1, -focusable => 0 );
+    $label = $w_list_rr->add( 'list_rr_priority_lbl', 'Label', -text => 'Priority: ', -y => 4, -hidden => 1, -focusable => 0, -width => 20 );
+    $field = $w_list_rr->add( 'list_rr_aux', 'TextEntry', -sbborder => 1, -y => 4, -x => 20, -width => 30, -hidden => 1, -focusable => 0 );
+    $label = $w_list_rr->add( 'list_rr_ttl_lbl', 'Label', -text => 'TTL: ', -y => 5, -hidden => 1, -focusable => 0, -width => 20 );
+    $field = $w_list_rr->add( 'list_rr_ttl', 'TextEntry', -sbborder => 1, -y => 5, -x => 20, -width => 30, -hidden => 1, -focusable => 0 );
+
+	$w_list_rr->add('modify_rr_submit', 'Buttonbox', 
+		-buttons => [
+			{
+				-label => '< OK >',
+				-onpress => \&update_rr_save
+			},
+            {
+                -label => '< Cancel >',
+                -onpress => \&update_rr_cancel
+            },
+            {
+                -label => '< Delete >',
+                -onpress => \&update_rr_delete
+            }
+		],
+		-x => 20,
+		-y => 6
+	);
 }
 
 sub rr_type_change {
@@ -205,6 +246,74 @@
 	$w_new_rr->intellidraw();
 }
 
+sub list_rr_domain_change {
+    my $current = $_[0]->get();
+    my $domain = Vhffs::Services::DNS::get_by_domainname( $vhffs, $current );
+
+    my %rr_labels = ( -1 => '-- Select one --' );
+    my @rr_values = ( -1 );
+    my @record_types = ( 'A', 'AAAA', 'CNAME', 'NS', 'MX', 'TXT' );
+    foreach my $rr (@record_types) {
+        foreach(values %{$domain->{$rr}}) {
+            my $value = "$rr|$_->{id}";
+            if($rr eq 'NS') {
+                $rr_labels{$value} = 'Name server ('.$_->{data}.')';
+            } elsif($_->{name} eq '' or $_->{name} eq '@') {
+                $rr_labels{$value} = ($rr eq 'MX' ? 'MX for ' : '')."[ORIGIN] ($rr)";
+            } else {
+                $rr_labels{$value} = ($rr eq 'MX' ? 'MX for ' : '').$_->{name}." ($rr)";
+            }
+            push @rr_values, $value;
+        }
+    }
+
+    my $list = $w_list_rr->getobj( 'list_rr_rrs' );
+    $list->{-labels} = \%rr_labels;
+    $list->{-values} = \@rr_values;
+    $list->{-selected} = 0;
+    $list->layout();
+    $list->intellidraw();
+
+}
+
+sub list_rr_rr_change {
+    my $value = $_[0]->get();
+    my ($type, $id) = ($value =~ /^([A-Z]+)\|(\d+)$/);
+    my $domain = Vhffs::Services::DNS::get_by_domainname( $vhffs, $w_list_rr->getobj('list_rr_domains')->get() );
+    my $rr = $domain->{$type}{$id};
+
+    my $field = $w_list_rr->getobj( 'list_rr_ttl_lbl' );
+    $field->{-hidden} = 0;
+    $field = $w_list_rr->getobj( 'list_rr_ttl' );
+    $field->{-hidden} = 0;
+    $field->{-focusable} = 1;
+    $field->text($rr->{ttl});
+
+    $field = $w_list_rr->getobj( 'list_rr_data' );
+    $field->{-hidden} = 0;
+    $field->{-focusable} = 1;
+    $field->text($rr->{data});
+
+    $field = $w_list_rr->getobj( 'list_rr_aux' );
+    $field->{-hidden} = ($type ne 'MX');
+    $field->{-focusable} = ($type eq 'MX');
+    $field->text($rr->{aux});
+
+    $field = $w_list_rr->getobj( 'list_rr_priority_lbl' );
+    $field->{-hidden} = ($type ne 'MX');
+
+    $field = $w_list_rr->getobj( 'list_rr_ip_lbl' );
+    $field->{-hidden} = ($type ne 'A' && $type ne 'AAAA');
+    
+    $field = $w_list_rr->getobj( 'list_rr_destination_lbl' );
+    $field->{-hidden} = ($type ne 'CNAME');
+
+    $field = $w_list_rr->getobj( 'list_rr_text_lbl' );
+    $field->{-hidden} = ($type ne 'TXT');
+
+    $w_list_rr->draw();
+}
+
 sub create_domain_save {
 	my $domain = $w_new_domain->getobj( 'new_domain_name' )->get();
 	my $user = Vhffs::User::get_by_uid( $vhffs, $w_new_domain->getobj( 'new_domain_user' )->get() );
@@ -246,6 +355,92 @@
 	$w_new_domain->draw();
 }
 
+sub update_rr_cancel {
+    my $value = $w_list_rr->getobj('list_rr_rrs')->get();
+    my ($type, $id) = ($value =~ /^([A-Z]+)\|(\d+)$/);
+    my $domain = Vhffs::Services::DNS::get_by_domainname( $vhffs, $w_list_rr->getobj('list_rr_domains')->get() );
+    my $rr = $domain->{$type}{$id};
+
+    $w_list_rr->getobj('list_rr_data')->text($rr->{data});
+    $w_list_rr->getobj('list_rr_aux')->text($rr->{aux});
+    $w_list_rr->getobj('list_rr_ttl')->text($rr->{ttl});
+
+    $w_list_rr->draw();
+
+}
+
+sub update_rr_save {
+    my $value = $w_list_rr->getobj('list_rr_rrs')->get();
+    my ($type, $id) = ($value =~ /^([A-Z]+)\|(\d+)$/);
+    my $domain = Vhffs::Services::DNS::get_by_domainname( $vhffs, $w_list_rr->getobj('list_rr_domains')->get() );
+    my $data = $w_list_rr->getobj('list_rr_data')->get();
+    my $aux = $w_list_rr->getobj('list_rr_aux')->get();
+    my $ttl = $w_list_rr->getobj('list_rr_ttl')->get();
+
+    # TODO improve me
+    my $messages = {
+        'A' => {
+            -1 => 'Invalid record ID',
+            -2 => 'RR does not exists',
+            -3 => 'Invalid IP address',
+            -4 => 'DB Error while updating record'
+        },
+        'MX' => {
+            -1 => 'Invalid record ID',
+            -2 => 'RR does not exists',
+            -3 => 'Invalid hostname for MX',
+            -4 => 'DB Error while updating record'
+        },
+        'TXT' => {
+            -1 => 'Invalid record ID',
+            -2 => 'RR does not exists',
+            -3 => 'TXT Record shouldn\'t be empty',
+            -4 => 'DB Error while updating record'
+ 
+        },
+        'CNAME' => {
+            -1 => 'Invalid record ID',
+            -2 => 'RR does not exists',
+            -3 => 'Invalid IP address',
+            -4 => 'DB Error while updating record'
+
+        }
+    };
+    my $rval;
+UPDATE_RR_SAVE_SWITCH: {
+    $rval = $domain->update_a($id, $data, $ttl), last UPDATE_RR_SAVE_SWITCH if($type eq 'A');
+    $rval = $domain->update_aaaa($id, $data, $ttl), last UPDATE_RR_SAVE_SWITCH if($type eq 'AAAA');
+    $rval = $domain->update_mx($id, $data, $aux, $ttl), last UPDATE_RR_SAVE_SWITCH if($type eq 'MX');
+    $rval = $domain->update_txt($id, $data, $ttl), last UPDATE_RR_SAVE_SWITCH if($type eq 'TXT');
+    $rval = $domain->update_cname($id, $data, $ttl), last UPDATE_RR_SAVE_SWITCH if($type eq 'CNAME');
+}
+
+    if($rval < 0) {
+        $ui->error($messages->{($type eq 'AAAA' ? 'A' : $type)}{$rval});
+    } else {
+        $ui->dialog(
+            -title => 'RR updated',
+            -message => 'RR has been successfuly updated'
+        );
+    }
+}
+
+
+# Called upon RR deletion
+sub update_rr_delete {
+    my $value = $w_list_rr->getobj('list_rr_rrs')->get();
+    my ($type, $id) = ($value =~ /^([A-Z]+)\|(\d+)$/);
+    my $domain = Vhffs::Services::DNS::get_by_domainname( $vhffs, $w_list_rr->getobj('list_rr_domains')->get() );
+    my $rval = $domain->delete_record($id, $type);
+    $ui->error('Invalid ID') if($rval == -1);
+    $ui->error("No RR of type $type for this domain") if($rval == -2);
+    $ui->error('RR not found') if($rval == -3);
+    $ui->dialog( -title => 'RR deleted', -message => 'RR successfuly deleted' ) if($rval > 0);
+    &list_rr_domain_change($w_list_rr->getobj('list_rr_domains'));
+}
+
+# Called upon RR creation, calls the appropriate
+# create_xx_rr depending on record type.
 sub create_rr_save {
 	my $domain = Vhffs::Services::DNS::get_by_domainname( $vhffs, $w_new_rr->getobj( 'new_rr_domain' )->get() );
 	my $type = $w_new_rr->getobj( 'new_rr_type' )->get();
@@ -263,6 +458,8 @@
 }
 }
 
+# Creates a a or AAAA record
+# create_a_rr($domain, $type)
 sub create_a_rr {
 	my ($domain, $type) = @_;
 	my $name = $w_new_rr->getobj('new_rr_name')->get();
@@ -280,6 +477,7 @@
 	) if($rval > 0);
 }
 
+
 sub create_cname_rr {
 	my $domain = shift;
 	my $name = $w_new_rr->getobj('new_rr_name')->get();
@@ -409,4 +607,3 @@
 
 }
 
-


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