Newer
Older
function set_default_ip() {
// Retrieve the first available IP for the selected network
// and update the IP field
var network_id = $("#network_id").val();
$.getJSON(
$SCRIPT_ROOT + "/network/_retrieve_first_available_ip/" + network_id,
$("#ip").val(json.data);
// Enable / disable item_id field depending on machine_type
// Item can only be assigned for physical hosts
// And check / uncheck random_mac checkbox
function update_machine_type_attributes() {
var machine_type = $("#machine_type_id option:selected").text();
if( machine_type.startsWith("Physical") ) {
$("#item_id").prop("disabled", false);
$("#random_mac").prop("checked", false).change();
} else {
$("#item_id").val("");
$("#item_id").prop("disabled", true);
$("#random_mac").prop("checked", true).change();
}
}
// If random_mac is checked, generate a random address
// Empty the field otherwise
function fill_mac_address() {
if( $("#random_mac").prop("checked") ) {
$.getJSON(
$SCRIPT_ROOT + "/network/_generate_random_mac",
function(json) {
$("#mac_address").val(json.data.mac);
$("#mac_address").prop("readonly", true);
}
);
} else {
$("#mac_address").val("");
$("#mac_address").prop("readonly", false);
}
}
// Set the default IP on first page load for:
// - register new host
// - add interface
// Do NOT replace the IP on edit interface page load!
// (we have to keep the existing IP)
if( $("#hostForm").length || $("#interfaceForm").length ) {
// Set the default IP when changing network
$("#network_id").on('change', function() {
// On register and edit host first page load
if( $("#hostForm").length || $("#editHostForm").length ) {
update_machine_type_attributes();
}
$("#machine_type_id").on('change', function() {
update_machine_type_attributes();
});
// Prefill interface name with hostname
$("#name").keyup(function(event) {
var hostname = $(this).val();
$("#interface_name").val(hostname);
// If an invalid hostname was submitted, the interface name
// will be invalid as well
// Remove the invalid messages when starting typing a new name
$("#interface_name").removeClass("is-invalid");
// Fill MAC address on page load
if( $("#random_mac").length ) {
fill_mac_address();
}
// Fill or clear MAC address depending on random_mac checkbox
$("#random_mac").on('change', function() {
fill_mac_address();
});
// Force the first interface to have the hostname as name
// This only applies to the hostForm (not when editing or adding interfaces)
$("#hostForm input[name=interface_name]").prop("readonly", true);
var hosts_table = $("#hosts_table").DataTable({
"ajax": function(data, callback, settings) {
$.getJSON(
$SCRIPT_ROOT + "/network/_retrieve_hosts",
function(json) {
callback(json);
});
},
"pagingType": "full_numbers",
"pageLength": 20,
"lengthMenu": [[20, 50, 100, -1], [20, 50, 100, "All"]],
"columnDefs": [
{
"targets": [0],
"render": function(data, type, row) {
// render funtion to create link to Item view page
if ( data === null ) {
return data;
}
var url = $SCRIPT_ROOT + "/network/hosts/view/" + data;
return '<a href="'+ url + '">' + data + '</a>';
}
}
]