diff options
-rw-r--r-- | AdminPanel/Hosts/GHosts.pm | 34 | ||||
-rw-r--r-- | AdminPanel/Hosts/hosts.pm | 30 |
2 files changed, 57 insertions, 7 deletions
diff --git a/AdminPanel/Hosts/GHosts.pm b/AdminPanel/Hosts/GHosts.pm index 9f82ab0c..91959297 100644 --- a/AdminPanel/Hosts/GHosts.pm +++ b/AdminPanel/Hosts/GHosts.pm @@ -49,6 +49,11 @@ has 'table' => ( init_arg => undef ); +has 'cfgHosts' => ( + is => 'rw', + init_arg => undef +); + sub start { my $self = shift; @@ -122,12 +127,38 @@ sub _addHostDialog { if ($widget == $cancelButton) { last; } + elsif($widget == $okButton) { + print $textIPAddress->value(); + my $res = $self->cfgHosts->_insertHost($textIPAddress->value(),[$textHostName->value(), $textHostAlias->value()]); + print "Insertion result: $res\n"; + $res = $self->cfgHosts->_writeHosts(); + print "Write result: $res\n"; + last; + } } } destroy $dlg; } +#============================================================= + +=head2 setupTable + +=head3 INPUT + + $self: this object + + $data: reference to the array containaing the host data to show into the table + +=head3 DESCRIPTION + +This subroutine populates a previously created YTable with the hosts data +retrieved by the Config::Hosts module + +=cut + +#============================================================= sub setupTable { my $self = shift(); my $data = shift(); @@ -182,7 +213,8 @@ sub manageHostsDialog { $leftContent->setWeight($yui::YD_HORIZ,45); $self->table($factory->createTable($leftContent,$tableHeader)); - my @hosts = AdminPanel::Hosts::hosts::_getHosts(); + $self->cfgHosts(AdminPanel::Hosts::hosts->new()); + my @hosts = $self->cfgHosts->_getHosts(); $self->setupTable(\@hosts); my $rightContent = $factory->createRight($hbox_content); diff --git a/AdminPanel/Hosts/hosts.pm b/AdminPanel/Hosts/hosts.pm index c957ad42..97f9a90e 100644 --- a/AdminPanel/Hosts/hosts.pm +++ b/AdminPanel/Hosts/hosts.pm @@ -1,7 +1,6 @@ package AdminPanel::Hosts::hosts; -use Modern::Perl 2011; -use autodie; +use Moose; use diagnostics; use local::lib; use Config::Hosts; @@ -12,6 +11,10 @@ my $is_ip = 1; my $is_host = -1; my $is_none = 0; +has 'configHosts' => ( + is => 'rw', + init_arg => undef +); =pod @@ -30,13 +33,14 @@ retrieve data from the hosts file (/etc/hosts) using the Config::Hosts module =cut sub _getHosts { - my $configHosts = Config::Hosts->new(); - my $hosts = $configHosts->read_hosts(); + my $self = shift(); + $self->configHosts(Config::Hosts->new()); + my $hosts = $self->configHosts->read_hosts(); my @result = (); while( my ($key, $value) = each($hosts)){ - if($configHosts->determine_ip_or_host($key) == $is_ip){ + if($self->configHosts->determine_ip_or_host($key) == $is_ip){ my $tmp = {}; - $tmp = $configHosts->query_host($key); + $tmp = $self->configHosts->query_host($key); $tmp->{'ip'} = $key; push @result,$tmp; } @@ -44,4 +48,18 @@ sub _getHosts { return @result; } +sub _insertHost { + my $self = shift(); + # remember that the order matters! + my $ip = shift(); + my @host_definitions = @_; + # $self->configHosts = Config::Hosts->new(); + return $self->configHosts->insert_host(ip => $ip, hosts => @host_definitions); +} + +sub _writeHosts { + my $self = shift(); + return $self->configHosts->write_hosts(); +} + 1; |