[racktables-users] Re: New to Racktables: Patchpanel, Autogenerate ports

Hi again,

I wrote on Tuesday, 16. March 2010 um 16:35:54 +0100:
> Now I have some questions:
> 
> 2. Is AutoPorts the only way to create lots of identical ports?
>    We have different types of Patchpanels (12 Ports, 24 Ports, fibre and
>    copper), so do I have to create a separate object in the
>    dictionary or is there another possibility?


I did some hacking to create a more flexible autogeneration.
Attached is a small patch which implements multiple port generation.
Please have a look. Especially the error handling could and must be
improved! Please have a look.


-- 
Jörg Friedrich

There are only 10 types of people:
Those who understand binary and those who don't.
diff -Nurd RackTables-0.17.9.orig//inc/database.php 
RackTables-0.17.9-multi//inc/database.php
--- RackTables-0.17.9.orig//inc/database.php    2010-02-11 18:54:06.000000000 
+0100
+++ RackTables-0.17.9-multi//inc/database.php   2010-03-18 21:29:56.000000000 
+0100
@@ -2962,6 +2962,24 @@
        return $ret;
 }
 
+function commitAddManyPorts ($object_id = 0, $port_name = '', $port_type_id = 
0 , $port_label = '', $port_numbering_start = 0, $port_numbering_count = 0)
+{
+
+       if ($object_id == 0 or $port_type_id == 0 or $port_name == '' or 
$port_numbering_count == 0)
+       {
+               showError ('Invalid arguments', __FUNCTION__);
+               //echo "o_id: $object_id; type: $port_type_id; port_name: 
$port_name; count: $port_numbering_count";
+               die;
+       }
+       $ret = TRUE;
+       for ($i=0,$c=$port_numbering_start; $i<=$port_numbering_count; 
$i++,$c++)
+               $ret = $ret and '' == commitAddPort ($object_id, 
@sprintf($port_name,$c), $port_type_id, @sprintf($port_label,$c), '');
+                //echo "commitAddPort ($object_id, @sprintf($port_name,$i), 
$port_type_id,'', '');";
+       return ($ret?'':"Failed");
+
+
+}
+
 // Return only implicitly listed tags, the rest of the chain will be
 // generated/deducted later at higher levels.
 // Result is a chain: randomly indexed taginfo list.
diff -Nurd RackTables-0.17.9.orig//inc/interface.php 
RackTables-0.17.9-multi//inc/interface.php
--- RackTables-0.17.9.orig//inc/interface.php   2010-02-11 18:54:06.000000000 
+0100
+++ RackTables-0.17.9-multi//inc/interface.php  2010-03-18 21:12:33.000000000 
+0100
@@ -1100,11 +1100,9 @@
                echo "<td colspan=3>&nbsp;</td><td>";
                printImageHREF ('add', 'add a port', TRUE, 104);
                echo "</td></tr></form>";
+
        }
-       if (getConfigVar('ENABLE_MULTIPORT_FORM') == 'yes')
-               startPortlet ('Ports and interfaces');
-       else
-               echo '<br>';
+       startPortlet ('Ports and interfaces');
        $object = spotEntity ('object', $object_id);
        amplifyCell ($object);
        echo "<table cellspacing=0 cellpadding='5' align='center' 
class='widetable'>\n";
@@ -1199,8 +1197,26 @@
        if (getConfigVar ('ADDNEW_AT_TOP') != 'yes')
                printNewItemTR ($prefs);
        echo "</table><br>\n";
-       if (getConfigVar('ENABLE_MULTIPORT_FORM') == 'yes')
-               finishPortlet();
+       finishPortlet();
+
+       startPortlet ('Add many Ports');
+
+       echo "<table cellspacing=0 cellpadding='5' align='center' 
class='widetable'>\n";
+       echo "<tr><th>&nbsp;</th><th class=tdleft>Local name</th><th 
class=tdleft>Visible label</th><th class=tdleft>Interface</th><th 
class=tdleft>Start Number</th>";
+       echo "<th class=tdleft>Count</th><th>&nbsp;</th></tr>\n";
+       printOpFormIntro ('addManyPorts');
+       echo "<tr><td>";
+       printImageHREF ('add', 'add ports', TRUE);
+       echo "</td><td><input type=text size=8 name=port_name 
tabindex=105></td>\n";
+       echo "<td><input type=text name=port_label tabindex=106></td><td>";
+       printNiftySelect (getNewPortTypeOptions(), array ('name' => 
'port_type_id', 'tabindex' => 107), $prefs['selected']);
+       echo "<td><input type=text name=port_numbering_start tabindex=108 
size=3 maxlength=3></td>\n";
+       echo "<td><input type=text name=port_numbering_count tabindex=109 
size=3 maxlength=3></td>\n";
+       echo "<td>&nbsp;</td><td>";
+       printImageHREF ('add', 'add ports', TRUE, 110);
+       echo "</td></tr></form>";
+       echo "</table><br>\n";
+
        if (getConfigVar('ENABLE_MULTIPORT_FORM') != 'yes')
                return;
 
@@ -1427,6 +1443,7 @@
                                79 => array ('code' => 'success', 'format' => 
'Rack "%s" was deleted successfully'),
                                80 => array ('code' => 'success', 'format' => 
"Added new object '%s'"),
                                81 => array ('code' => 'success', 'format' => 
"SNMP: completed '%s' work"),
+                               82 => array ('code' => 'success', 'format' => 
"Ports have been created successfully"),
 
 // records 100~199 with fatal error messages
                                100 => array ('code' => 'error', 'format' => 
'%s'),
diff -Nurd RackTables-0.17.9.orig//inc/navigation.php 
RackTables-0.17.9-multi//inc/navigation.php
--- RackTables-0.17.9.orig//inc/navigation.php  2010-02-11 18:54:06.000000000 
+0100
+++ RackTables-0.17.9-multi//inc/navigation.php 2010-03-18 20:46:45.000000000 
+0100
@@ -114,6 +114,7 @@
 $trigger['object']['tags'] = 'trigger_tags';
 $ophandler['object']['rackspace']['updateObjectAllocation'] = 
'updateObjectAllocation';
 $ophandler['object']['ports']['addPort'] = 'addPortForObject';
+$ophandler['object']['ports']['addManyPorts'] = 'addManyPortsForObject';
 $ophandler['object']['ports']['delPort'] = 'delPortFromObject';
 $ophandler['object']['ports']['editPort'] = 'editPortForObject';
 $ophandler['object']['ports']['linkPort'] = 'linkPortForObject';
diff -Nurd RackTables-0.17.9.orig//inc/ophandlers.php 
RackTables-0.17.9-multi//inc/ophandlers.php
--- RackTables-0.17.9.orig//inc/ophandlers.php  2010-02-11 18:54:06.000000000 
+0100
+++ RackTables-0.17.9-multi//inc/ophandlers.php 2010-03-18 21:16:26.000000000 
+0100
@@ -2039,5 +2039,24 @@
        return buildRedirectURL (__FUNCTION__, 'ERR');
 
 }
+$msgcode['addManyPortsForObject']['OK'] = 82;
+$msgcode['addManyPortsForObject']['ERR1'] = 101;
+$msgcode['addManyPortsForObject']['ERR2'] = 100;
+function addManyPortsForObject ()
+{
+       assertUIntArg ('object_id', __FUNCTION__);
+       assertStringArg ('port_name', __FUNCTION__, TRUE);
+       assertStringArg ('port_label', __FUNCTION__, TRUE);
+       assertUIntArg('port_numbering_start', __FUNCTION__);
+       assertUIntArg('port_numbering_count', __FUNCTION__);
+       if (!strlen ($_REQUEST['port_name']))
+               return buildRedirectURL (__FUNCTION__, 'ERR1');
+       $error = commitAddManyPorts ($_REQUEST['object_id'], 
$_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], 
$_REQUEST['port_numbering_start'], $_REQUEST['port_numbering_count']);
+       if ($error != '')
+               return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
+       else
+               return buildRedirectURL (__FUNCTION__, 'OK' );
+}
+
 
 ?>

Other related posts: