[racktables-users] Re: Patch for VLAN support
- From: "Justin Ellison" <justin@xxxxxxxxxxxxxx>
- To: racktables-users@xxxxxxxxxxxxx, pilot@xxxxxxxxxx
- Date: Tue, 30 Sep 2008 15:42:10 -0500
I'll work on that - shouldn't be too bad.
Here's another little patch. It enables an optional attribute type of
boolean (bool) that is displayed as a checkbox. Internally, it's
represented as an int of 1 in the db.
Also, I added a hook for user defined triggers that can determine
whether or not to make an optional attribute be editable or not.
You'll see the code in the patch on interface.php.
Here's the use case, and I could see it being reusable. I'm creating
VTP support, so I create an optional attribute named "VTP Enabled".
However, I only want the attribute to appear if the "VLAN Enabled"
checkbox is checked. So, I just create a function in triggers.php
like so:
function trigger_attribute_vtp_enabled ()
{
assertUIntArg ('object_id', __FUNCTION__);
$object_id = $_REQUEST['object_id'];
$object = getObjectInfo ($object_id);
// Only if Vlans are enabled should vtp be editable
return (trigger_vlans());
}
The code in interfaces.php looks to see if a function exists that is
named "trigger_attribute" + attribute name lowecased and spaces
replaced with underscores. If it exists, it calls it. If the return
value is false, it doesn't display the attribute.
Feedback appreciated.
Justin
Index: /home/justintime/workspace/RackTables/install/init-structure.sql
===================================================================
--- /home/justintime/workspace/RackTables/install/init-structure.sql
(revision 2225)
+++ /home/justintime/workspace/RackTables/install/init-structure.sql
(working copy)
@@ -10,7 +10,7 @@
CREATE TABLE `Attribute` (
`attr_id` int(10) unsigned NOT NULL auto_increment,
- `attr_type` enum('string','uint','float','dict') default NULL,
+ `attr_type` enum('string','uint','float','bool','dict') default NULL,
`attr_name` char(64) default NULL,
PRIMARY KEY (`attr_id`),
UNIQUE KEY `attr_name` (`attr_name`)
Index: /home/justintime/workspace/RackTables/inc/ophandlers.php
===================================================================
--- /home/justintime/workspace/RackTables/inc/ophandlers.php (revision 2225)
+++ /home/justintime/workspace/RackTables/inc/ophandlers.php (working copy)
@@ -725,6 +725,7 @@
switch ($oldvalues[$attr_id]['type'])
{
case 'uint':
+ case 'bool':
case 'float':
case 'string':
$oldvalue = $oldvalues[$attr_id]['value'];
Index: /home/justintime/workspace/RackTables/inc/interface.php
===================================================================
--- /home/justintime/workspace/RackTables/inc/interface.php (revision 2225)
+++ /home/justintime/workspace/RackTables/inc/interface.php (working copy)
@@ -457,6 +457,18 @@
echo '<td class=pcright>';
startPortlet ('Optional attributes');
$values = getAttrValues ($object_id);
+ $i = 0;
+ foreach ($values as $record)
+ {
+ $triggerFunctionName = "trigger_attribute_";
+ $triggerFunctionName .= strtolower(str_replace("
","_",$record['name']));
+ if (function_exists($triggerFunctionName)) {
+ if (! call_user_func($triggerFunctionName)) {
+ array_splice($values,$i,1);
+ }
+ }
+ $i++;
+ }
echo "<table cellspacing=0 cellpadding=5 align=center
class=widetable>\n";
echo
"<tr><th> </th><th>Attribute</th><th>Value</th><th> </th></tr>\n";
printOpFormIntro ('updateStickers');
@@ -467,7 +479,7 @@
{
echo "<input type=hidden name=${i}_attr_id
value=${record['id']}>";
echo '<tr><td>';
- if (!empty ($record['value']))
+ if (!empty ($record['value']) && $record['type'] != "bool")
{
echo "<a
href='${root}process.php?page=${pageno}&tab=${tabno}&op=clearSticker&object_id=${object_id}&attr_id=${record['id']}'>";
printImageHREF ('clear', 'Clear value');
@@ -484,6 +496,12 @@
case 'string':
echo "<input type=text name=${i}_value
value='${record['value']}'>";
break;
+ case 'bool':
+ echo "<input type=checkbox name=${i}_value
value=1";
+ if ($record['value'] > 0)
+ echo " checked=\"checked\"";
+ echo ">";
+ break;
case 'dict':
$chapter = readChapter
($record['chapter_name']);
$chapter[0] = '-- NOT SET --';
@@ -3492,6 +3525,7 @@
echo "</td><td><input type=text name=attr_name></td>";
echo '<td><select name=attr_type>';
echo '<option value=uint>uint</option>';
+ echo '<option value=bool>bool</option>';
echo '<option value=float>float</option>';
echo '<option value=string>string</option>';
echo '<option value=dict>dict</option>';
@@ -3535,6 +3569,7 @@
printImageHREF ('add', '', TRUE);
echo "</td><td><select name=attr_id>";
$shortType['uint'] = 'U';
+ $shortType['bool'] = 'B';
$shortType['float'] = 'F';
$shortType['string'] = 'S';
$shortType['dict'] = 'D';
Index: /home/justintime/workspace/RackTables/inc/database.php
===================================================================
--- /home/justintime/workspace/RackTables/inc/database.php (revision 2225)
+++ /home/justintime/workspace/RackTables/inc/database.php (working copy)
@@ -1889,6 +1895,7 @@
}
switch ($attr_type)
{
+ case 'bool':
case 'uint':
case 'float':
case 'string':
@@ -1984,6 +1991,9 @@
$record['type'] = $row['attr_type'];
switch ($row['attr_type'])
{
+ case 'bool':
+ $record['value'] = $row['uint_value'];
+ break;
case 'uint':
case 'float':
case 'string':
@@ -2057,6 +2067,7 @@
case 'string':
$column = $attr_type . '_value';
break;
+ case 'bool':
case 'dict':
$column = 'uint_value';
break;
- References:
- [racktables-users] Patch for VLAN support
- From: Justin Ellison
- [racktables-users] Re: Patch for VLAN support
- From: Denis Ovsienko
Other related posts:
- » [racktables-users] Patch for VLAN support
- » [racktables-users] Re: Patch for VLAN support
- » [racktables-users] Re: Patch for VLAN support
- » [racktables-users] Re: Patch for VLAN support
- » [racktables-users] Re: Patch for VLAN support
- » [racktables-users] Re: Patch for VLAN support
- » [racktables-users] Re: Patch for VLAN support
- [racktables-users] Patch for VLAN support
- From: Justin Ellison
- [racktables-users] Re: Patch for VLAN support
- From: Denis Ovsienko