Permalink
Browse files

Allow admins to change hostname of servers

  • Loading branch information...
abh committed May 25, 2012
1 parent 6213fce commit 61850c68f9608810ac7c8b9864b3c57154523b09
Showing with 62 additions and 2 deletions.
  1. +21 −0 docs/shared/static/js/admin.js
  2. +5 −1 docs/shared/tpl/server.html
  3. +36 −1 lib/NTPPool/API/Staff.pm
@@ -64,5 +64,26 @@ if (!NP) { var NP = {}; }
$('#server_edit_zones').hide();
});
}
+
+ var server_hostname = $('#server_hostname');
+
+ if (server_hostname) {
+ server_hostname.editable('/api/staff/edit_server', {
+ submitdata: {
+ id: 'hostname',
+ auth_token: NP.token,
+ server: zone_list.data('server-ip')
+ },
+ placeholder: "hostname",
+ callback: function(data, editable) {
+ data = JSON.parse(data);
+ server_hostname.html(data.hostname);
+ if (data.error) {
+ alert(data.error + ": " + data.input);
+ }
+ }
+ });
+ }
+
});
})(jQuery);
@@ -22,7 +22,11 @@
[% page.title = "Stats for %1" | l(server.ip) %]
-<h3>[% server.ip %] [% IF server.hostname; '/ ' _ server.hostname; END %]</h3>
+<h3>[% server.ip %]
+ [% IF server.hostname OR (manage_site AND combust.user.is_staff) %]
+ / <span id="server_hostname">[% server.hostname || 'add hostname' %]</span>
+ [% END %]
+</h3>
<p>
View
@@ -2,6 +2,7 @@ package NTPPool::API::Staff;
use strict;
use base qw(NTPPool::API::Base);
use NP::Model;
+use Net::IP;
sub search {
my $self = shift;
@@ -104,13 +105,16 @@ sub edit_server {
# TODO:
# check auth_token
- my ($field, $value, $server_ip) = $self->_required_param(qw(id value server));
+ my ($field, $server_ip) = $self->_required_param(qw(id server));
+ my $value = $self->_optional_param('value') || '';
+
my $server = NP::Model->server->find_server($server_ip)
or die "Could not find server";
if ($field eq 'zone_list') {
my %zones = map { $_->name => $_ } $server->zones_display;
my %new_zones = map { $_ => 1 } split /[,\s]+/, $value;
+ %new_zones = %zones unless %new_zones; # don't allow removing all zones
for my $zone (keys %new_zones) {
if ($zones{$zone}) {
# ok already
@@ -126,6 +130,37 @@ sub edit_server {
$server->save;
return [ map { $_->name } $server->zones_display ];
}
+ elsif ($field eq 'hostname') {
+ my $hostname = $value;
+ my $server_ip = Net::IP->new($server->ip);
+
+ my $res = Net::DNS::Resolver->new;
+ my $reply = $res->query($hostname, $server->ip_version eq 'v4' ? 'A' : 'AAAA');
+
+ my $error = "";
+ my $found = 0;
+
+ if ($reply) {
+ for my $rr ($reply->answer) {
+ next unless $rr->type eq 'A' or $rr->type eq 'AAAA';
+ $found++ if Net::IP->new($rr->address)->short eq $server_ip->short;
+ }
+ }
+
+ if ($found) {
+ $server->hostname( lc $hostname );
+ $server->save;
+ }
+ else {
+ $error = "That hostname doesn't resolve to the IP address of the server";
+ }
+
+ return {
+ hostname => $server->hostname,
+ input => $hostname,
+ error => $error
+ };
+ }
else {
die "Don't know how to edit $field";
}

0 comments on commit 61850c6

Please sign in to comment.