Skip to content
Snippets Groups Projects
hosts.js 3.82 KiB
Newer Older
$(document).ready(function() {

  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,
      function(json) {
        $("#ip").val(json.data);
  // Select the IOC tag by default depending on the device type
  function update_default_tags(device_type) {
    var ioc_device_types = ["PhysicalMachine", "VirtualMachine", "MicroTCA", "VME"];
    var is_ioc_selected = $.inArray(device_type, ioc_device_types) > -1;
    $("#tags option").filter(function() {
      return this.text == "IOC";
    }).prop("selected", is_ioc_selected).change();
  }

  // And check / uncheck random_mac checkbox
  // and update default tags
  function update_device_type_attributes() {
    var device_type = $("#device_type_id option:selected").text();
    if( device_type.startsWith("Virtual") ) {
      $("#random_mac").prop("checked", true).change();
    } else {
      $("#random_mac").prop("checked", false).change();
    update_default_tags(device_type);
  // 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)
  // Do NOT replace the IP if the form was submitted and an error was raised
  // (we should display the same value that was submitted)
  if( ($("#hostForm").length || $("#interfaceForm").length) && ! $("input").hasClass("is-invalid") ) {
  // Set the default IP when changing network
  $("#network_id").on('change', function() {
  // On register host first page load
  if( $("#hostForm").length ) {
    update_device_type_attributes();
  $("#device_type_id").on('change', function() {
    update_device_type_attributes();
  // The interface name field is hidden in the create host form
  // because it should be forced to the hostname
  // Do that when submitting the form
  $("#hostForm").submit(function(event) {
    var hostname = $("#name").val();
    $("#interface_name").val(hostname);
  });

  // 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>';
        }
      }
    ]