Config-Hosts

 view release on metacpan or  search on metacpan

lib/Config/Hosts.pm  view on Meta::CPAN

		}
		$self->{_hosts}{$host} = {
			ip => $ip,
			comment => $comment,
			line => scalar @{$self->{_contents}},
		}
	}
	return 1;
}

=head2 delete_host ($$)

Deletes an entry in hosts table. The entry is determined either by IP
or by hostname, all entries related to this host or IP are wiped out.

=cut

sub delete_host ($$) {

	my $self = shift;
	my $host = shift;

	my $type = $self->determine_ip_or_host($host);
	if (! $type) {
		print STDERR "Invalid host $host supplied\n";
		return 0;
	}
	if (! $self->{_hosts}{$host}) {
		print STDERR "No such host $host\n";
		return 0;
	}
	my $index = $self->{_hosts}{$host}{line};
	splice(@{$self->{_contents}}, $index, 1);
	if ($type == $TYPE_IP) {
		for my $h_host (@{$self->{_hosts}{$host}{hosts}}) {
			delete $self->{_hosts}{$h_host}
		}
		delete $self->{_hosts}{$host};
	}
	else {
		my $ip = $self->{_hosts}{$host}{ip};
		my $ip_hosts = $self->{_hosts}{$ip}{hosts};
		for my $h_host (@{$ip_hosts}) {
			delete $self->{_hosts}{$h_host}
		}
		delete $self->{_hosts}{$ip};
	}
	return 1;
}

=head2 update_host ($$)

Updates an entry in hosts table. Arguments should be of the following
format: $self->update_host($ip_or_host, ip => $new_ip, hosts => [
@new_hosts ]);

New hosts' argument may be a single scalar instead of arrayref.

=cut

sub update_host ($$%) {

	my $self   = shift;
	my $host   = shift;
	my %params = @_;

	my $type = $self->determine_ip_or_host($host);
	if (! $type) {
		print STDERR "Invalid host $host supplied\n";
		return 0;
	}
	if (! $self->{_hosts}{$host}) {
		print STDERR "No such host $host\n";
		return 0;
	}
	my $index = $self->{_hosts}{$host}{line};
	my $comment = $params{comment} ? " $params{comment}" : "";
	my $new_ip = $host;
	if ($params{ip} && !is_valid_ip($params{ip})) {
		print STDERR "Invalid argument IP given\n";
		return 0;
	}
	if ($params{hosts}) {
		if (! ref $params{hosts}) {
			$params{hosts} = [ $params{hosts} ];
		}
		if (ref $params{hosts} ne 'ARRAY') {
			print STDERR "New host names should be scalar value or array ref\n";
			return 0;
		}
		if (grep {
			!is_valid_host($_) &&
			print STDERR "Invalid host $_ passed, ignoring insert\n"
		} @{$params{hosts}}) {
			return 0;
		}
	}
	if ($type == $TYPE_IP && $params{ip}) {
		$new_ip = $params{ip};
		$self->{_hosts}{$new_ip} = delete $self->{_hosts}{$host};
		for my $h_host (@{$self->{_hosts}{$new_ip}{hosts}}) {
			$self->{_hosts}{$h_host}{ip} = $new_ip;
		}
	}
	if ($type == $TYPE_IP && $params{hosts}) {
		my @old_hosts = @{$self->{_hosts}{$new_ip}{hosts}};
		$self->{_hosts}{$new_ip}{hosts} = $params{hosts};
		for my $old_host (@old_hosts) {
			delete $self->{_hosts}{$old_host};
		}
		for my $new_host (@{$self->{_hosts}{$new_ip}{hosts}}) {
			$self->{_hosts}{$new_host} = {
				ip => $new_ip,
				comment => $comment,
				line => $self->{_hosts}{$new_ip}{line},
			}
		}
	}
	if ($type == $TYPE_HOST && $params{ip}) {
		my $old_ip = $self->{_hosts}{$host}{ip};
		$new_ip = $params{ip};



( run in 0.481 second using v1.01-cache-2.11-cpan-39bf76dae61 )