ee5059f57c331626f0a4f8cb3b8f1da763c6c651
4 * This file is a library of database access functions for RackTables.
8 function escapeString ($value, $do_db_escape = TRUE)
10 $ret = htmlentities ($value, ENT_QUOTES
, 'UTF-8');
14 $ret = substr ($dbxlink->quote ($ret), 1, -1);
19 // This function returns detailed information about either all or one
20 // rack row depending on its argument.
21 function getRackRowInfo ($rackrow_id = 0)
24 "select dict_key, dict_value, count(Rack.id) as count, " .
25 "if(isnull(sum(Rack.height)),0,sum(Rack.height)) as sum " .
26 "from Chapter natural join Dictionary left join Rack on Rack.row_id = dict_key " .
27 "where chapter_name = 'RackRow' " .
28 ($rackrow_id > 0 ?
"and dict_key = ${rackrow_id} " : '') .
29 "group by dict_key order by dict_value";
30 $result = useSelectBlade ($query);
32 $clist = array ('dict_key', 'dict_value', 'count', 'sum');
33 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
34 foreach ($clist as $dummy => $cname)
35 $ret[$row['dict_key']][$cname] = $row[$cname];
36 $result->closeCursor();
38 return current ($ret);
43 // This function returns id->name map for all object types. The map is used
44 // to build <select> input for objects.
45 function getObjectTypeList ()
47 return readChapter ('RackObjectType');
50 function getObjectList ($type_id = 0)
53 "select distinct RackObject.id as id , RackObject.name as name, dict_value as objtype_name, " .
54 "RackObject.label as label, RackObject.barcode as barcode, " .
55 "dict_key as objtype_id, asset_no, rack_id, Rack.name as Rack_name from " .
56 "((RackObject inner join Dictionary on objtype_id=dict_key natural join Chapter) " .
57 "left join RackSpace on RackObject.id = object_id) " .
58 "left join Rack on rack_id = Rack.id " .
59 "where objtype_id = '${type_id}' and RackObject.deleted = 'no' " .
60 "and chapter_name = 'RackObjectType' order by name";
61 $result = useSelectBlade ($query);
63 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
76 $ret[$row['id']][$cname] = $row[$cname];
77 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
79 $result->closeCursor();
83 function getRacksForRow ($row_id = 0)
86 "select Rack.id, Rack.name, height, Rack.comment, row_id, " .
87 "'yes' as left_is_front, 'yes' as bottom_is_unit1, dict_value as row_name " .
88 "from Rack left join Dictionary on row_id = dict_key natural join Chapter " .
89 "where chapter_name = 'RackRow' and Rack.deleted = 'no' " .
90 (($row_id == 0) ?
"" : "and row_id = ${row_id} ") .
91 "order by row_name, Rack.id";
92 $result = useSelectBlade ($query);
105 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
106 foreach ($clist as $cname)
107 $ret[$row['id']][$cname] = $row[$cname];
108 $result->closeCursor();
109 usort ($ret, 'sortRacks');
110 $ret = restoreRackIDs ($ret);
114 // This is a popular helper for getting information about
115 // a particular rack and its rackspace at once.
116 function getRackData ($rack_id = 0, $silent = FALSE)
120 if ($silent == FALSE)
121 showError ('Invalid rack_id', __FUNCTION__
);
125 "select Rack.id, Rack.name, row_id, height, Rack.comment, " .
126 "'yes' as left_is_front, 'yes' as bottom_is_unit1, dict_value as row_name from " .
127 "Rack left join Dictionary on Rack.row_id = dict_key natural join Chapter " .
128 "where chapter_name = 'RackRow' and Rack.id='${rack_id}' and Rack.deleted = 'no' limit 1";
129 $result = useSelectBlade ($query);
130 if (($row = $result->fetch (PDO
::FETCH_ASSOC
)) == NULL)
132 if ($silent == FALSE)
133 showError ('Query #1 succeded, but returned no data', __FUNCTION__
);
149 foreach ($clist as $cname)
150 $rack[$cname] = $row[$cname];
151 $result->closeCursor();
154 // start with default rackspace
155 for ($i = $rack['height']; $i > 0; $i--)
156 for ($locidx = 0; $locidx < 3; $locidx++
)
157 $rack[$i][$locidx]['state'] = 'F';
161 "select unit_no, atom, state, object_id " .
162 "from RackSpace where rack_id = ${rack_id} and " .
163 "unit_no between 1 and " . $rack['height'] . " order by unit_no";
164 $result = useSelectBlade ($query);
166 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
168 $rack[$row['unit_no']][$loclist[$row['atom']]]['state'] = $row['state'];
169 $rack[$row['unit_no']][$loclist[$row['atom']]]['object_id'] = $row['object_id'];
171 $result->closeCursor();
175 // This is a popular helper.
176 function getObjectInfo ($object_id = 0)
180 showError ('Invalid object_id', __FUNCTION__
);
184 "select id, name, label, barcode, dict_value as objtype_name, asset_no, dict_key as objtype_id, has_problems, comment from " .
185 "RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter " .
186 "where id = '${object_id}' and deleted = 'no' and chapter_name = 'RackObjectType' limit 1";
187 $result = useSelectBlade ($query);
188 if (($row = $result->fetch (PDO
::FETCH_ASSOC
)) == NULL)
190 showError ('Query succeded, but returned no data', __FUNCTION__
);
195 $ret['id'] = $row['id'];
196 $ret['name'] = $row['name'];
197 $ret['label'] = $row['label'];
198 $ret['barcode'] = $row['barcode'];
199 $ret['objtype_name'] = $row['objtype_name'];
200 $ret['objtype_id'] = $row['objtype_id'];
201 $ret['has_problems'] = $row['has_problems'];
202 $ret['asset_no'] = $row['asset_no'];
203 $ret['dname'] = displayedName ($ret);
204 $ret['comment'] = $row['comment'];
206 $result->closeCursor();
210 function getPortTypes ()
212 return readChapter ('PortType');
215 function getObjectPortsAndLinks ($object_id = 0)
219 showError ('Invalid object_id', __FUNCTION__
);
223 "select Port.id as Port_id, ".
224 "Port.name as Port_name, ".
225 "Port.label as Port_label, ".
226 "Port.l2address as Port_l2address, ".
227 "Port.type as Port_type, ".
228 "Port.reservation_comment as Port_reservation_comment, " .
229 "dict_value as PortType_name, ".
230 "RemotePort.id as RemotePort_id, ".
231 "RemotePort.name as RemotePort_name, ".
232 "RemotePort.object_id as RemotePort_object_id, ".
233 "RackObject.name as RackObject_name ".
237 "Port inner join Dictionary on Port.type = dict_key natural join Chapter".
239 "left join Link on Port.id=Link.porta or Port.id=Link.portb ".
241 "left join Port as RemotePort on Link.portb=RemotePort.id or Link.porta=RemotePort.id ".
243 "left join RackObject on RemotePort.object_id=RackObject.id ".
244 "where chapter_name = 'PortType' and Port.object_id=${object_id} ".
245 "and (Port.id != RemotePort.id or RemotePort.id is null) ".
246 "order by Port_name";
247 $result = useSelectBlade ($query);
250 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
252 $ret[$count]['id'] = $row['Port_id'];
253 $ret[$count]['name'] = $row['Port_name'];
254 $ret[$count]['l2address'] = l2addressFromDatabase ($row['Port_l2address']);
255 $ret[$count]['label'] = $row['Port_label'];
256 $ret[$count]['type_id'] = $row['Port_type'];
257 $ret[$count]['type'] = $row['PortType_name'];
258 $ret[$count]['reservation_comment'] = $row['Port_reservation_comment'];
259 $ret[$count]['remote_id'] = $row['RemotePort_id'];
260 $ret[$count]['remote_name'] = htmlentities ($row['RemotePort_name'], ENT_QUOTES
);
261 $ret[$count]['remote_object_id'] = $row['RemotePort_object_id'];
262 $ret[$count]['remote_object_name'] = $row['RackObject_name'];
263 // Save on displayedName() calls.
264 if (empty ($row['RackObject_name']) and !empty ($row['RemotePort_object_id']))
266 $oi = getObjectInfo ($row['RemotePort_object_id']);
267 $ret[$count]['remote_object_name'] = displayedName ($oi);
271 $result->closeCursor();
275 function commitAddRack ($name, $height, $row_id, $comment)
278 $query = "insert into Rack(row_id, name, height, comment) values('${row_id}', '${name}', '${height}', '${comment}')";
279 $result1 = $dbxlink->query ($query);
280 if ($result1 == NULL)
282 showError ('SQL query failed', __FUNCTION__
);
285 // last_insert_id() is MySQL-specific
286 $query = 'select last_insert_id()';
287 $result2 = $dbxlink->query ($query);
288 if ($result2 == NULL)
290 showError ('Cannot get last ID', __FUNCTION__
);
293 // we always have a row
294 $row = $result2->fetch (PDO
::FETCH_NUM
);
295 $last_insert_id = $row[0];
296 $result2->closeCursor();
297 return recordHistory ('Rack', "id = ${last_insert_id}");
300 function commitAddObject ($new_name, $new_label, $new_barcode, $new_type_id, $new_asset_no)
303 // Maintain UNIQUE INDEX for common names and asset tags.
304 $new_asset_no = empty ($new_asset_no) ?
'NULL' : "'${new_asset_no}'";
305 $new_barcode = empty ($new_barcode) ?
'NULL' : "'${new_barcode}'";
306 $new_name = empty ($new_name) ?
'NULL' : "'${new_name}'";
308 "insert into RackObject(name, label, barcode, objtype_id, asset_no) " .
309 "values(${new_name}, '${new_label}', ${new_barcode}, '${new_type_id}', ${new_asset_no})";
310 $result1 = $dbxlink->query ($query);
311 if ($result1 == NULL)
313 $errorInfo = $dbxlink->errorInfo();
314 showError ("SQL query '${query}' failed: ${errorInfo[2]}", __FUNCTION__
);
317 if ($result1->rowCount() != 1)
319 showError ('Adding new object failed', __FUNCTION__
);
322 $query = 'select last_insert_id()';
323 $result2 = $dbxlink->query ($query);
324 if ($result2 == NULL)
326 $errorInfo = $dbxlink->errorInfo();
327 showError ("SQL query '${query}' failed: ${errorInfo[2]}", __FUNCTION__
);
330 // we always have a row
331 $row = $result2->fetch (PDO
::FETCH_NUM
);
332 $last_insert_id = $row[0];
333 $result2->closeCursor();
334 // Do AutoPorts magic
335 executeAutoPorts ($last_insert_id, $new_type_id);
336 return recordHistory ('RackObject', "id = ${last_insert_id}");
339 function commitUpdateObject ($object_id = 0, $new_name = '', $new_label = '', $new_barcode = '', $new_type_id = 0, $new_has_problems = 'no', $new_asset_no = '', $new_comment = '')
341 if ($object_id == 0 ||
$new_type_id == 0)
343 showError ('Not all required args are present.', __FUNCTION__
);
347 $new_asset_no = empty ($new_asset_no) ?
'NULL' : "'${new_asset_no}'";
348 $new_barcode = empty ($new_barcode) ?
'NULL' : "'${new_barcode}'";
349 $new_name = empty ($new_name) ?
'NULL' : "'${new_name}'";
350 $query = "update RackObject set name=${new_name}, label='${new_label}', barcode=${new_barcode}, objtype_id='${new_type_id}', " .
351 "has_problems='${new_has_problems}', asset_no=${new_asset_no}, comment='${new_comment}' " .
352 "where id='${object_id}' limit 1";
353 $result = $dbxlink->query ($query);
356 showError ("SQL query '${query}' failed", __FUNCTION__
);
359 $result->closeCursor();
360 return recordHistory ('RackObject', "id = ${object_id}");
363 function commitUpdateRack ($rack_id, $new_name, $new_height, $new_row_id, $new_comment)
365 if (empty ($rack_id) ||
empty ($new_name) ||
empty ($new_height))
367 showError ('Not all required args are present.', __FUNCTION__
);
371 $query = "update Rack set name='${new_name}', height='${new_height}', comment='${new_comment}', row_id=${new_row_id} " .
372 "where id='${rack_id}' limit 1";
373 $result1 = $dbxlink->query ($query);
374 if ($result1->rowCount() != 1)
376 showError ('Error updating rack information', __FUNCTION__
);
379 return recordHistory ('Rack', "id = ${rack_id}");
382 // This function accepts rack data returned by getRackData(), validates and applies changes
383 // supplied in $_REQUEST and returns resulting array. Only those changes are examined, which
384 // correspond to current rack ID.
385 // 1st arg is rackdata, 2nd arg is unchecked state, 3rd arg is checked state.
386 // If 4th arg is present, object_id fields will be updated accordingly to the new state.
387 // The function returns the modified rack upon success.
388 function processGridForm (&$rackData, $unchecked_state, $checked_state, $object_id = 0)
390 global $loclist, $dbxlink;
391 $rack_id = $rackData['id'];
392 $rack_name = $rackData['name'];
393 $rackchanged = FALSE;
394 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
396 for ($locidx = 0; $locidx < 3; $locidx++
)
398 if ($rackData[$unit_no][$locidx]['enabled'] != TRUE)
401 $state = $rackData[$unit_no][$locidx]['state'];
402 if (isset ($_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"]) and $_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"] == 'on')
403 $newstate = $checked_state;
405 $newstate = $unchecked_state;
406 if ($state == $newstate)
410 $atom = $loclist[$locidx];
411 // The only changes allowed are those introduced by checkbox grid.
414 !($state == $checked_state && $newstate == $unchecked_state) &&
415 !($state == $unchecked_state && $newstate == $checked_state)
417 return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, 'atom ${atom}', cannot change state from '${state}' to '${newstate}'");
418 // Here we avoid using ON DUPLICATE KEY UPDATE by first performing DELETE
419 // anyway and then looking for probable need of INSERT.
421 "delete from RackSpace where rack_id = ${rack_id} and " .
422 "unit_no = ${unit_no} and atom = '${atom}' limit 1";
423 $r = $dbxlink->query ($query);
425 return array ('code' => 500, 'message' => __FUNCTION__
. ": ${rack_name}: SQL DELETE query failed");
426 if ($newstate != 'F')
429 "insert into RackSpace(rack_id, unit_no, atom, state) " .
430 "values(${rack_id}, ${unit_no}, '${atom}', '${newstate}') ";
431 $r = $dbxlink->query ($query);
433 return array ('code' => 500, 'message' => __FUNCTION__
. ": ${rack_name}: SQL INSERT query failed");
435 if ($newstate == 'T' and $object_id != 0)
437 // At this point we already have a record in RackSpace.
439 "update RackSpace set object_id=${object_id} " .
440 "where rack_id=${rack_id} and unit_no=${unit_no} and atom='${atom}' limit 1";
441 $r = $dbxlink->query ($query);
442 if ($r->rowCount() == 1)
443 $rackData[$unit_no][$locidx]['object_id'] = $object_id;
445 return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, atom '${atom}' failed to set object_id to '${object_id}'");
451 resetThumbCache ($rack_id);
452 return array ('code' => 200, 'message' => "${rack_name}: All changes were successfully saved.");
455 return array ('code' => 300, 'message' => "${rack_name}: No changes.");
458 // This function builds a list of rack-unit-atom records, which are assigned to
459 // the requested object.
460 function getMoleculeForObject ($object_id = 0)
464 showError ("object_id == 0", __FUNCTION__
);
468 "select rack_id, unit_no, atom from RackSpace " .
469 "where state = 'T' and object_id = ${object_id} order by rack_id, unit_no, atom";
470 $result = useSelectBlade ($query);
471 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
472 $result->closeCursor();
476 // This function builds a list of rack-unit-atom records for requested molecule.
477 function getMolecule ($mid = 0)
481 showError ("mid == 0", __FUNCTION__
);
485 "select rack_id, unit_no, atom from Atom " .
486 "where molecule_id=${mid}";
487 $result = useSelectBlade ($query);
488 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
489 $result->closeCursor();
493 // This function creates a new record in Molecule and number of linked
494 // R-U-A records in Atom.
495 function createMolecule ($molData)
498 $query = "insert into Molecule values()";
499 $result1 = $dbxlink->query ($query);
500 if ($result1->rowCount() != 1)
502 showError ('Error inserting into Molecule', __FUNCTION__
);
505 $query = 'select last_insert_id()';
506 $result2 = $dbxlink->query ($query);
507 if ($result2 == NULL)
509 showError ('Cannot get last ID.', __FUNCTION__
);
512 $row = $result2->fetch (PDO
::FETCH_NUM
);
513 $molecule_id = $row[0];
514 $result2->closeCursor();
515 foreach ($molData as $dummy => $rua)
517 $rack_id = $rua['rack_id'];
518 $unit_no = $rua['unit_no'];
519 $atom = $rua['atom'];
521 "insert into Atom(molecule_id, rack_id, unit_no, atom) " .
522 "values (${molecule_id}, ${rack_id}, ${unit_no}, '${atom}')";
523 $result3 = $dbxlink->query ($query);
524 if ($result3 == NULL or $result3->rowCount() != 1)
526 showError ('Error inserting into Atom', __FUNCTION__
);
533 // History logger. This function assumes certain table naming convention and
535 // 1. History table name equals to dictionary table name plus 'History'.
536 // 2. History table must have the same row set (w/o keys) plus one row named
537 // 'ctime' of type 'timestamp'.
538 function recordHistory ($tableName, $whereClause)
540 global $dbxlink, $remote_username;
541 $query = "insert into ${tableName}History select *, current_timestamp(), '${remote_username}' from ${tableName} where ${whereClause}";
542 $result = $dbxlink->query ($query);
543 if ($result == NULL or $result->rowCount() != 1)
545 showError ("SQL query '${query}' failed for table ${tableName}", __FUNCTION__
);
551 function getRackspaceHistory ()
554 "select mo.id as mo_id, ro.id as ro_id, ro.name, mo.ctime, mo.comment, dict_value as objtype_name, user_name from " .
555 "MountOperation as mo inner join RackObject as ro on mo.object_id = ro.id " .
556 "inner join Dictionary on objtype_id = dict_key natural join Chapter " .
557 "where chapter_name = 'RackObjectType' order by ctime desc";
558 $result = useSelectBlade ($query);
559 $ret = $result->fetchAll(PDO
::FETCH_ASSOC
);
560 $result->closeCursor();
564 // This function is used in renderRackspaceHistory()
565 function getOperationMolecules ($op_id = 0)
569 showError ("Missing argument", __FUNCTION__
);
572 $query = "select old_molecule_id, new_molecule_id from MountOperation where id = ${op_id}";
573 $result = useSelectBlade ($query);
574 // We expect one row.
575 $row = $result->fetch (PDO
::FETCH_ASSOC
);
578 showError ("SQL query succeded, but returned no results.", __FUNCTION__
);
581 $omid = $row['old_molecule_id'];
582 $nmid = $row['new_molecule_id'];
583 $result->closeCursor();
584 return array ($omid, $nmid);
587 function getResidentRacksData ($object_id = 0, $fetch_rackdata = TRUE)
591 showError ('Invalid object_id', __FUNCTION__
);
594 $query = "select distinct rack_id from RackSpace where object_id = ${object_id} order by rack_id";
595 $result = useSelectBlade ($query);
596 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
597 $result->closeCursor();
599 foreach ($rows as $row)
601 if (!$fetch_rackdata)
603 $ret[$row[0]] = $row[0];
606 $rackData = getRackData ($row[0]);
607 if ($rackData == NULL)
609 showError ('getRackData() failed', __FUNCTION__
);
612 $ret[$row[0]] = $rackData;
614 $result->closeCursor();
618 function getObjectGroupInfo ($group_id = 0)
621 'select dict_key as id, dict_value as name, count(id) as count from ' .
622 'Dictionary natural join Chapter left join RackObject on dict_key = objtype_id ' .
623 'where chapter_name = "RackObjectType" ' .
624 (($group_id > 0) ?
"and dict_key = ${group_id} " : '') .
626 $result = useSelectBlade ($query);
628 $clist = array ('id', 'name', 'count');
629 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
630 foreach ($clist as $dummy => $cname)
631 $ret[$row['id']][$cname] = $row[$cname];
632 $result->closeCursor();
634 return current ($ret);
639 // This function returns objects, which have no rackspace assigned to them.
640 // Additionally it keeps rack_id parameter, so we can silently pre-select
641 // the rack required.
642 function getUnmountedObjects ()
645 'select dict_value as objtype_name, dict_key as objtype_id, name, label, barcode, id, asset_no from ' .
646 'RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter ' .
647 'left join RackSpace on id = object_id '.
648 'where rack_id is null and chapter_name = "RackObjectType" order by dict_value, name, label, asset_no, barcode';
649 $result = useSelectBlade ($query);
651 $clist = array ('id', 'name', 'label', 'barcode', 'objtype_name', 'objtype_id', 'asset_no');
652 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
654 foreach ($clist as $dummy => $cname)
655 $ret[$row['id']][$cname] = $row[$cname];
656 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
658 $result->closeCursor();
662 function getProblematicObjects ()
665 'select dict_value as objtype_name, dict_key as objtype_id, name, id, asset_no from ' .
666 'RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter '.
667 'where has_problems = "yes" and chapter_name = "RackObjectType" order by objtype_name, name';
668 $result = useSelectBlade ($query);
670 $clist = array ('id', 'name', 'objtype_name', 'objtype_id', 'asset_no');
671 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
673 foreach ($clist as $dummy => $cname)
674 $ret[$row['id']][$cname] = $row[$cname];
675 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
677 $result->closeCursor();
681 function commitAddPort ($object_id = 0, $port_name, $port_type_id, $port_label, $port_l2address)
685 showError ('Invalid object_id', __FUNCTION__
);
688 $port_l2address = l2addressForDatabase ($port_l2address);
689 $result = useInsertBlade
694 'name' => "'${port_name}'",
695 'object_id' => "'${object_id}'",
696 'label' => "'${port_label}'",
697 'type' => "'${port_type_id}'",
698 'l2address' => "${port_l2address}"
704 return 'SQL query failed';
707 function commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_reservation_comment)
710 $port_l2address = l2addressForDatabase ($port_l2address);
712 "update Port set name='$port_name', label='$port_label', " .
713 "reservation_comment = ${port_reservation_comment}, l2address=${port_l2address} " .
714 "where id='$port_id'";
715 $result = $dbxlink->exec ($query);
718 $errorInfo = $dbxlink->errorInfo();
719 // We could update nothing.
720 if ($errorInfo[0] == '00000')
722 return $errorInfo[2];
725 function delObjectPort ($port_id)
727 if (unlinkPort ($port_id) != '')
728 return __FUNCTION__
. ': unlinkPort() failed';
729 if (useDeleteBlade ('Port', 'id', $port_id) != TRUE)
730 return __FUNCTION__
. ': useDeleteBlade() failed';
734 function getObjectAddressesAndNames ()
737 "select object_id as object_id, ".
738 "RackObject.name as object_name, ".
739 "IPBonds.name as name, ".
740 "INET_NTOA(ip) as ip ".
741 "from IPBonds join RackObject on id=object_id ";
742 $result = useSelectBlade ($query);
745 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
747 $ret[$count]['object_id']=$row['object_id'];
748 $ret[$count]['object_name']=$row['object_name'];
749 $ret[$count]['name']=$row['name'];
750 $ret[$count]['ip']=$row['ip'];
753 $result->closeCursor();
758 function getEmptyPortsOfType ($type_id)
761 "select distinct Port.id as Port_id, ".
762 "Port.object_id as Port_object_id, ".
763 "RackObject.name as Object_name, ".
764 "Port.name as Port_name, ".
765 "Port.type as Port_type_id, ".
766 "dict_value as Port_type_name ".
769 " Port inner join Dictionary on Port.type = dict_key natural join Chapter ".
771 " join RackObject on Port.object_id = RackObject.id ".
773 "left join Link on Port.id=Link.porta or Port.id=Link.portb ".
774 "inner join PortCompat on Port.type = PortCompat.type2 ".
775 "where chapter_name = 'PortType' and PortCompat.type1 = '$type_id' and Link.porta is NULL ".
776 "and Port.reservation_comment is null order by Object_name, Port_name";
777 $result = useSelectBlade ($query);
780 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
782 $ret[$count]['Port_id']=$row['Port_id'];
783 $ret[$count]['Port_object_id']=$row['Port_object_id'];
784 $ret[$count]['Object_name']=$row['Object_name'];
785 $ret[$count]['Port_name']=$row['Port_name'];
786 $ret[$count]['Port_type_id']=$row['Port_type_id'];
787 $ret[$count]['Port_type_name']=$row['Port_type_name'];
790 $result->closeCursor();
794 function linkPorts ($porta, $portb)
796 if ($porta == $portb)
797 return "Ports can't be the same";
805 $query1 = "insert into Link set porta='${porta}', portb='{$portb}'";
806 $query2 = "update Port set reservation_comment = NULL where id = ${porta} or id = ${portb} limit 2";
807 // FIXME: who cares about the return value?
808 $result = $dbxlink->exec ($query1);
809 $result = $dbxlink->exec ($query2);
814 function unlinkPort ($port)
818 "delete from Link where porta='$port' or portb='$port'";
819 $result = $dbxlink->exec ($query);
824 // FIXME: after falling back to using existing getObjectInfo we don't
825 // need that large query. Shrink it some later.
826 function getObjectAddresses ($object_id = 0)
830 showError ('Invalid object_id', __FUNCTION__
);
835 "IPAddress.name as IPAddress_name, ".
836 "IPAddress.reserved as IPAddress_reserved, ".
837 "IPBonds.name as IPBonds_name, ".
838 "INET_NTOA(IPBonds.ip) as IPBonds_ip, ".
839 "IPBonds.type as IPBonds_type, ".
840 "RemoteBonds.name as RemoteBonds_name, ".
841 "RemoteBonds.type as RemoteBonds_type, ".
842 "RemoteBonds.object_id as RemoteBonds_object_id, ".
843 "RemoteObject.name as RemoteObject_name from IPBonds " .
844 "left join IPBonds as RemoteBonds on IPBonds.ip=RemoteBonds.ip " .
845 "and IPBonds.object_id!=RemoteBonds.object_id " .
846 "left join IPAddress on IPBonds.ip=IPAddress.ip " .
847 "left join RackObject as RemoteObject on RemoteBonds.object_id=RemoteObject.id ".
849 "IPBonds.object_id = ${object_id} ".
850 "order by IPBonds.ip, RemoteObject.name";
851 $result = useSelectBlade ($query);
856 // We are going to call getObjectInfo() for some rows,
857 // hence the connector must be unloaded from the
859 $rows = $result->fetchAll (PDO
::FETCH_ASSOC
);
860 $result->closeCursor();
861 foreach ($rows as $row)
863 if ($prev_ip != $row['IPBonds_ip'])
867 $prev_ip = $row['IPBonds_ip'];
868 $ret[$count]['address_name'] = $row['IPAddress_name'];
869 $ret[$count]['address_reserved'] = $row['IPAddress_reserved'];
870 $ret[$count]['ip'] = $row['IPBonds_ip'];
871 $ret[$count]['name'] = $row['IPBonds_name'];
872 $ret[$count]['type'] = $row['IPBonds_type'];
873 $ret[$count]['references'] = array();
876 if ($row['RemoteBonds_type'])
878 $ret[$count]['references'][$refcount]['type'] = $row['RemoteBonds_type'];
879 $ret[$count]['references'][$refcount]['name'] = $row['RemoteBonds_name'];
880 $ret[$count]['references'][$refcount]['object_id'] = $row['RemoteBonds_object_id'];
881 if (empty ($row['RemoteBonds_object_id']))
882 $ret[$count]['references'][$refcount]['object_name'] = $row['RemoteObject_name'];
885 $oi = getObjectInfo ($row['RemoteBonds_object_id']);
886 $ret[$count]['references'][$refcount]['object_name'] = displayedName ($oi);
894 function getAddressspaceList ($tagfilter = array())
896 if (!count ($tagfilter))
897 $whereclause = 'where tag_id is null';
900 $whereclause = 'where ';
902 foreach ($tagfilter as $tag_id)
904 $whereclause .= $orclause . 'tag_id = ' . $tag_id;
910 "id as IPRanges_id, ".
911 "INET_NTOA(ip) as IPRanges_ip, ".
912 "mask as IPRanges_mask, ".
913 "name as IPRanges_name ".
914 "from IPRanges left join TagStorage on IPRanges.id = TagStorage.target_id and target_realm = 'ipv4net' " .
917 $result = useSelectBlade ($query);
920 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
922 $ret[$count]['id'] = $row['IPRanges_id'];
923 $ret[$count]['ip'] = $row['IPRanges_ip'];
924 $ret[$count]['ip_bin'] = ip2long($row['IPRanges_ip']);
925 $ret[$count]['name'] = $row['IPRanges_name'];
926 $ret[$count]['mask'] = $row['IPRanges_mask'];
927 $ret[$count]['mask_bin'] = binMaskFromDec($row['IPRanges_mask']);
928 $ret[$count]['mask_bin_inv'] = binInvMaskFromDec($row['IPRanges_mask']);
931 $result->closeCursor();
936 function getRangeByIp ($ip = '', $id = 0)
941 "id, INET_NTOA(ip) as ip, mask, name ".
946 "id, INET_NTOA(ip) as ip, mask, name ".
947 "from IPRanges where id='$id'";
949 $result = useSelectBlade ($query);
951 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
953 $binmask=binMaskFromDec($row['mask']);
954 if ((ip2long($ip) & $binmask) == ip2long($row['ip']))
956 $ret['id'] = $row['id'];
957 $ret['ip'] = $row['ip'];
958 $ret['ip_bin'] = ip2long($row['ip']);
959 $ret['name'] = $row['name'];
960 $ret['mask'] = $row['mask'];
961 $result->closeCursor();
965 $result->closeCursor();
969 function updateRange ($id=0, $name='')
973 "update IPRanges set name='$name' where id='$id'";
974 $result = $dbxlink->exec ($query);
979 // This function is actually used not only to update, but also to create records,
980 // that's why ON DUPLICATE KEY UPDATE was replaced by DELETE-INSERT pair
981 // (MySQL 4.0 workaround).
982 function updateAddress ($ip=0, $name='', $reserved='no')
984 // DELETE may safely fail.
985 $r = useDeleteBlade ('IPAddress', 'ip', "INET_ATON('${ip}')", FALSE);
986 // INSERT may appear not necessary.
987 if ($name == '' and $reserved == 'no')
989 if (useInsertBlade ('IPAddress', array ('name' => "'${name}'", 'reserved' => "'${reserved}'", 'ip' => "INET_ATON('${ip}')")))
992 return 'useInsertBlade() failed in updateAddress()';
995 // FIXME: This function doesn't wipe relevant records from IPAddress table.
996 function commitDeleteRange ($id = 0)
999 return __FUNCTION__
. ': Invalid range ID';
1000 if (useDeleteBlade ('IPRanges', 'id', $id))
1003 return __FUNCTION__
. ': SQL query failed';
1006 function updateBond ($ip='', $object_id=0, $name='', $type='')
1011 "update IPBonds set name='$name', type='$type' where ip=INET_ATON('$ip') and object_id='$object_id'";
1012 $result = $dbxlink->exec ($query);
1016 function unbindIpFromObject ($ip='', $object_id=0)
1021 "delete from IPBonds where ip=INET_ATON('$ip') and object_id='$object_id'";
1022 $result = $dbxlink->exec ($query);
1026 // This function returns either all or one user account. Array key is user name.
1027 function getUserAccounts ()
1030 'select user_id, user_name, user_password_hash, user_realname, user_enabled ' .
1031 'from UserAccount order by user_name';
1032 $result = useSelectBlade ($query);
1034 $clist = array ('user_id', 'user_name', 'user_realname', 'user_password_hash', 'user_enabled');
1035 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1036 foreach ($clist as $dummy => $cname)
1037 $ret[$row['user_name']][$cname] = $row[$cname];
1038 $result->closeCursor();
1042 // This function returns permission array for all user accounts. Array key is user name.
1043 function getUserPermissions ()
1046 "select UserPermission.user_id, user_name, page, tab, access from " .
1047 "UserPermission natural left join UserAccount where (user_name is not null) or " .
1048 "(user_name is null and UserPermission.user_id = 0) order by user_name, page, tab";
1049 $result = useSelectBlade ($query);
1051 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1053 if ($row['user_id'] == 0)
1054 $row['user_name'] = '%';
1055 $ret[$row['user_name']][$row['page']][$row['tab']] = $row['access'];
1057 $result->closeCursor();
1061 function searchByl2address ($l2addr)
1063 $l2addr = l2addressForDatabase ($l2addr);
1064 $query = "select object_id, Port.id as port_id from RackObject as ro inner join Port on ro.id = Port.object_id " .
1065 "where l2address = ${l2addr}";
1066 $result = useSelectBlade ($query);
1067 $rows = $result->fetchAll (PDO
::FETCH_ASSOC
);
1068 $result->closeCursor();
1069 if (count ($rows) == 0) // No results.
1071 if (count ($rows) == 1) // Target found.
1073 showError ('More than one results was found. This is probably a broken unique key.', __FUNCTION__
);
1077 // This function returns either port ID or NULL for specified arguments.
1078 function getPortID ($object_id, $port_name)
1080 $query = "select id from Port where object_id=${object_id} and name='${port_name}' limit 2";
1081 $result = useSelectBlade ($query);
1082 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
1083 if (count ($rows) != 1)
1086 $result->closeCursor();
1090 function commitCreateUserAccount ($username, $realname, $password)
1092 return useInsertBlade
1097 'user_name' => "'${username}'",
1098 'user_realname' => "'${realname}'",
1099 'user_password_hash' => "'${password}'"
1104 function commitUpdateUserAccount ($id, $new_username, $new_realname, $new_password)
1108 "update UserAccount set user_name = '${new_username}', user_realname = '${new_realname}', " .
1109 "user_password_hash = '${new_password}' where user_id = ${id} limit 1";
1110 $result = $dbxlink->query ($query);
1111 if ($result == NULL)
1113 showError ('SQL query failed', __FUNCTION__
);
1119 function commitEnableUserAccount ($id, $new_enabled_value)
1123 "update UserAccount set user_enabled = '${new_enabled_value}' " .
1124 "where user_id = ${id} limit 1";
1125 $result = $dbxlink->query ($query);
1126 if ($result == NULL)
1128 showError ('SQL query failed', __FUNCTION__
);
1134 function commitGrantPermission ($userid, $page, $tab, $value)
1136 return useInsertBlade
1141 'user_id' => $userid,
1142 'page' => "'${page}'",
1143 'tab' => "'${tab}'",
1144 'access' => "'${value}'"
1149 function commitRevokePermission ($userid, $page, $tab)
1153 "delete from UserPermission where user_id = '${userid}' and page = '${page}' " .
1154 "and tab = '$tab' limit 1";
1155 $result = $dbxlink->query ($query);
1156 if ($result == NULL)
1158 showError ('SQL query failed', __FUNCTION__
);
1164 // This function returns an array of all port type pairs from PortCompat table.
1165 function getPortCompat ()
1168 "select type1, type2, d1.dict_value as type1name, d2.dict_value as type2name from " .
1169 "PortCompat as pc inner join Dictionary as d1 on pc.type1 = d1.dict_key " .
1170 "inner join Dictionary as d2 on pc.type2 = d2.dict_key " .
1171 "inner join Chapter as c1 on d1.chapter_no = c1.chapter_no " .
1172 "inner join Chapter as c2 on d2.chapter_no = c2.chapter_no " .
1173 "where c1.chapter_name = 'PortType' and c2.chapter_name = 'PortType'";
1174 $result = useSelectBlade ($query);
1175 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
1176 $result->closeCursor();
1180 function removePortCompat ($type1 = 0, $type2 = 0)
1183 if ($type1 == 0 or $type2 == 0)
1185 showError ('Invalid arguments', __FUNCTION__
);
1188 $query = "delete from PortCompat where type1 = ${type1} and type2 = ${type2} limit 1";
1189 $result = $dbxlink->query ($query);
1190 if ($result == NULL)
1192 showError ('SQL query failed', __FUNCTION__
);
1198 function addPortCompat ($type1 = 0, $type2 = 0)
1200 if ($type1 <= 0 or $type2 <= 0)
1202 showError ('Invalid arguments', __FUNCTION__
);
1205 return useInsertBlade
1208 array ('type1' => $type1, 'type2' => $type2)
1212 // This function returns the dictionary as an array of trees, each tree
1213 // representing a single chapter. Each element has 'id', 'name', 'sticky'
1214 // and 'word' keys with the latter holding all the words within the chapter.
1215 function getDict ($parse_links = FALSE)
1218 "select chapter_name, Chapter.chapter_no, dict_key, dict_value, sticky from " .
1219 "Chapter natural left join Dictionary order by chapter_name, dict_value";
1220 $result = useSelectBlade ($query1);
1222 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1224 $chapter_no = $row['chapter_no'];
1225 if (!isset ($dict[$chapter_no]))
1227 $dict[$chapter_no]['no'] = $chapter_no;
1228 $dict[$chapter_no]['name'] = $row['chapter_name'];
1229 $dict[$chapter_no]['sticky'] = $row['sticky'] == 'yes' ?
TRUE : FALSE;
1230 $dict[$chapter_no]['word'] = array();
1232 if ($row['dict_key'] != NULL)
1234 $dict[$chapter_no]['word'][$row['dict_key']] = $parse_links ?
1235 parseWikiLink ($row['dict_value'], 'a') : $row['dict_value'];
1236 $dict[$chapter_no]['refcnt'][$row['dict_key']] = 0;
1239 $result->closeCursor();
1241 // Find the list of all assigned values of dictionary-addressed attributes, each with
1242 // chapter/word keyed reference counters. Use the structure to adjust reference counters
1243 // of the returned disctionary words.
1244 $query2 = "select a.attr_id, am.chapter_no, uint_value, count(object_id) as refcnt " .
1245 "from Attribute as a inner join AttributeMap as am on a.attr_id = am.attr_id " .
1246 "inner join AttributeValue as av on a.attr_id = av.attr_id " .
1247 "inner join Dictionary as d on am.chapter_no = d.chapter_no and av.uint_value = d.dict_key " .
1248 "where attr_type = 'dict' group by a.attr_id, am.chapter_no, uint_value " .
1249 "order by a.attr_id, am.chapter_no, uint_value";
1250 $result = useSelectBlade ($query2);
1252 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1253 $dict[$row['chapter_no']]['refcnt'][$row['uint_value']] = $row['refcnt'];
1254 $result->closeCursor();
1258 function getDictStats ()
1260 $stock_chapters = array (1, 2, 3, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23);
1262 "select Chapter.chapter_no, chapter_name, count(dict_key) as wc from " .
1263 "Chapter natural left join Dictionary group by Chapter.chapter_no";
1264 $result = useSelectBlade ($query);
1265 $tc = $tw = $uc = $uw = 0;
1266 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1270 if (in_array ($row['chapter_no'], $stock_chapters))
1275 $result->closeCursor();
1277 $query = "select count(attr_id) as attrc from RackObject as ro left join " .
1278 "AttributeValue as av on ro.id = av.object_id group by ro.id";
1279 $result = useSelectBlade ($query);
1280 $to = $ta = $so = 0;
1281 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1284 if ($row['attrc'] != 0)
1287 $ta +
= $row['attrc'];
1290 $result->closeCursor();
1292 $ret['Total chapters in dictionary'] = $tc;
1293 $ret['Total words in dictionary'] = $tw;
1294 $ret['User chapters'] = $uc;
1295 $ret['Words in user chapters'] = $uw;
1296 $ret['Total objects'] = $to;
1297 $ret['Objects with stickers'] = $so;
1298 $ret['Total stickers attached'] = $ta;
1302 function getIPv4Stats()
1306 $subject[] = array ('q' => 'select count(id) from IPRanges', 'txt' => 'Networks');
1307 $subject[] = array ('q' => 'select count(ip) from IPAddress', 'txt' => 'Addresses commented/reserved');
1308 $subject[] = array ('q' => 'select count(ip) from IPBonds', 'txt' => 'Addresses allocated');
1309 $subject[] = array ('q' => 'select count(*) from PortForwarding', 'txt' => 'NAT rules');
1310 $subject[] = array ('q' => 'select count(id) from IPVirtualService', 'txt' => 'Virtual services');
1311 $subject[] = array ('q' => 'select count(id) from IPRSPool', 'txt' => 'Real server pools');
1312 $subject[] = array ('q' => 'select count(id) from IPRealServer', 'txt' => 'Real servers');
1313 $subject[] = array ('q' => 'select count(distinct object_id) from IPLoadBalancer', 'txt' => 'Load balancers');
1315 foreach ($subject as $item)
1317 $result = useSelectBlade ($item['q']);
1318 $row = $result->fetch (PDO
::FETCH_NUM
);
1319 $ret[$item['txt']] = $row[0];
1320 $result->closeCursor();
1326 function getRackspaceStats()
1330 $subject[] = array ('q' => 'select count(*) from Dictionary where chapter_no = 3', 'txt' => 'Rack rows');
1331 $subject[] = array ('q' => 'select count(*) from Rack', 'txt' => 'Racks');
1332 $subject[] = array ('q' => 'select avg(height) from Rack', 'txt' => 'Average rack height');
1333 $subject[] = array ('q' => 'select sum(height) from Rack', 'txt' => 'Total rack units in field');
1335 foreach ($subject as $item)
1337 $result = useSelectBlade ($item['q']);
1338 $row = $result->fetch (PDO
::FETCH_NUM
);
1339 $ret[$item['txt']] = empty ($row[0]) ?
0 : $row[0];
1340 $result->closeCursor();
1346 function commitUpdateDictionary ($chapter_no = 0, $dict_key = 0, $dict_value = '')
1348 if ($chapter_no <= 0 or $dict_key <= 0 or empty ($dict_value))
1350 showError ('Invalid args', __FUNCTION__
);
1355 "update Dictionary set dict_value = '${dict_value}' where chapter_no=${chapter_no} " .
1356 "and dict_key=${dict_key} limit 1";
1357 $result = $dbxlink->query ($query);
1358 if ($result == NULL)
1360 showError ('SQL query failed', __FUNCTION__
);
1366 function commitSupplementDictionary ($chapter_no = 0, $dict_value = '')
1368 if ($chapter_no <= 0 or empty ($dict_value))
1370 showError ('Invalid args', __FUNCTION__
);
1373 return useInsertBlade
1376 array ('chapter_no' => $chapter_no, 'dict_value' => "'${dict_value}'")
1380 function commitReduceDictionary ($chapter_no = 0, $dict_key = 0)
1382 if ($chapter_no <= 0 or $dict_key <= 0)
1384 showError ('Invalid args', __FUNCTION__
);
1389 "delete from Dictionary where chapter_no=${chapter_no} " .
1390 "and dict_key=${dict_key} limit 1";
1391 $result = $dbxlink->query ($query);
1392 if ($result == NULL)
1394 showError ('SQL query failed', __FUNCTION__
);
1400 function commitAddChapter ($chapter_name = '')
1402 if (empty ($chapter_name))
1404 showError ('Invalid args', __FUNCTION__
);
1407 return useInsertBlade
1410 array ('chapter_name' => "'${chapter_name}'")
1414 function commitUpdateChapter ($chapter_no = 0, $chapter_name = '')
1416 if ($chapter_no <= 0 or empty ($chapter_name))
1418 showError ('Invalid args', __FUNCTION__
);
1423 "update Chapter set chapter_name = '${chapter_name}' where chapter_no = ${chapter_no} " .
1424 "and sticky = 'no' limit 1";
1425 $result = $dbxlink->query ($query);
1426 if ($result == NULL)
1428 showError ('SQL query failed', __FUNCTION__
);
1434 function commitDeleteChapter ($chapter_no = 0)
1436 if ($chapter_no <= 0)
1438 showError ('Invalid args', __FUNCTION__
);
1443 "delete from Chapter where chapter_no = ${chapter_no} and sticky = 'no' limit 1";
1444 $result = $dbxlink->query ($query);
1445 if ($result == NULL)
1447 showError ('SQL query failed', __FUNCTION__
);
1453 // This is a dictionary accessor. We perform link rendering, so the user sees
1454 // nice <select> drop-downs.
1455 function readChapter ($chapter_name = '')
1457 if (empty ($chapter_name))
1459 showError ('invalid argument', __FUNCTION__
);
1463 "select dict_key, dict_value from Dictionary natural join Chapter " .
1464 "where chapter_name = '${chapter_name}'";
1465 $result = useSelectBlade ($query);
1467 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1468 $chapter[$row['dict_key']] = parseWikiLink ($row['dict_value'], 'o');
1469 $result->closeCursor();
1470 // SQL ORDER BY had no sense, because we need to sort after link rendering, not before.
1475 function getAttrMap ()
1478 "select a.attr_id, a.attr_type, a.attr_name, am.objtype_id, " .
1479 "d.dict_value as objtype_name, am.chapter_no, c2.chapter_name from " .
1480 "Attribute as a natural left join AttributeMap as am " .
1481 "left join Dictionary as d on am.objtype_id = d.dict_key " .
1482 "left join Chapter as c1 on d.chapter_no = c1.chapter_no " .
1483 "left join Chapter as c2 on am.chapter_no = c2.chapter_no " .
1484 "where c1.chapter_name = 'RackObjectType' or c1.chapter_name is null " .
1485 "order by attr_name";
1486 $result = useSelectBlade ($query);
1488 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1490 $attr_id = $row['attr_id'];
1491 if (!isset ($ret[$attr_id]))
1493 $ret[$attr_id]['id'] = $attr_id;
1494 $ret[$attr_id]['type'] = $row['attr_type'];
1495 $ret[$attr_id]['name'] = $row['attr_name'];
1496 $ret[$attr_id]['application'] = array();
1498 if ($row['objtype_id'] == '')
1500 $application['objtype_id'] = $row['objtype_id'];
1501 $application['objtype_name'] = $row['objtype_name'];
1502 if ($row['attr_type'] == 'dict')
1504 $application['chapter_no'] = $row['chapter_no'];
1505 $application['chapter_name'] = $row['chapter_name'];
1507 $ret[$attr_id]['application'][] = $application;
1509 $result->closeCursor();
1513 function commitUpdateAttribute ($attr_id = 0, $attr_name = '')
1515 if ($attr_id <= 0 or empty ($attr_name))
1517 showError ('Invalid args', __FUNCTION__
);
1522 "update Attribute set attr_name = '${attr_name}' " .
1523 "where attr_id = ${attr_id} limit 1";
1524 $result = $dbxlink->query ($query);
1525 if ($result == NULL)
1527 showError ("SQL query '${query}' failed", __FUNCTION__
);
1533 function commitAddAttribute ($attr_name = '', $attr_type = '')
1535 if (empty ($attr_name))
1537 showError ('Invalid args', __FUNCTION__
);
1548 showError ('Invalid args', __FUNCTION__
);
1551 return useInsertBlade
1554 array ('attr_name' => "'${attr_name}'", 'attr_type' => "'${attr_type}'")
1558 function commitDeleteAttribute ($attr_id = 0)
1562 showError ('Invalid args', __FUNCTION__
);
1565 return useDeleteBlade ('Attribute', 'attr_id', $attr_id);
1568 // FIXME: don't store garbage in chapter_no for non-dictionary types.
1569 function commitSupplementAttrMap ($attr_id = 0, $objtype_id = 0, $chapter_no = 0)
1571 if ($attr_id <= 0 or $objtype_id <= 0 or $chapter_no <= 0)
1573 showError ('Invalid args', __FUNCTION__
);
1576 return useInsertBlade
1581 'attr_id' => $attr_id,
1582 'objtype_id' => $objtype_id,
1583 'chapter_no' => $chapter_no
1588 function commitReduceAttrMap ($attr_id = 0, $objtype_id)
1590 if ($attr_id <= 0 or $objtype_id <= 0)
1592 showError ('Invalid args', __FUNCTION__
);
1597 "delete from AttributeMap where attr_id=${attr_id} " .
1598 "and objtype_id=${objtype_id} limit 1";
1599 $result = $dbxlink->query ($query);
1600 if ($result == NULL)
1602 showError ('SQL query failed', __FUNCTION__
);
1608 // This function returns all optional attributes for requested object
1609 // as an array of records. NULL is returned on error and empty array
1610 // is returned, if there are no attributes found.
1611 function getAttrValues ($object_id, $strip_optgroup = FALSE)
1613 if ($object_id <= 0)
1615 showError ('Invalid argument', __FUNCTION__
);
1620 "select A.attr_id, A.attr_name, A.attr_type, C.chapter_name, " .
1621 "AV.uint_value, AV.float_value, AV.string_value, D.dict_value from " .
1622 "RackObject as RO inner join AttributeMap as AM on RO.objtype_id = AM.objtype_id " .
1623 "inner join Attribute as A using (attr_id) " .
1624 "left join AttributeValue as AV on AV.attr_id = AM.attr_id and AV.object_id = RO.id " .
1625 "left join Dictionary as D on D.dict_key = AV.uint_value and AM.chapter_no = D.chapter_no " .
1626 "left join Chapter as C on AM.chapter_no = C.chapter_no " .
1627 "where RO.id = ${object_id} order by A.attr_type, A.attr_name";
1628 $result = useSelectBlade ($query);
1629 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1632 $record['id'] = $row['attr_id'];
1633 $record['name'] = $row['attr_name'];
1634 $record['type'] = $row['attr_type'];
1635 switch ($row['attr_type'])
1640 $record['value'] = $row[$row['attr_type'] . '_value'];
1641 $record['a_value'] = parseWikiLink ($record['value'], 'a');
1644 $record['value'] = parseWikiLink ($row[$row['attr_type'] . '_value'], 'o', $strip_optgroup);
1645 $record['a_value'] = parseWikiLink ($row[$row['attr_type'] . '_value'], 'a', $strip_optgroup);
1646 $record['chapter_name'] = $row['chapter_name'];
1647 $record['key'] = $row['uint_value'];
1650 $record['value'] = NULL;
1653 $ret[$row['attr_id']] = $record;
1655 $result->closeCursor();
1659 function commitResetAttrValue ($object_id = 0, $attr_id = 0)
1661 if ($object_id <= 0 or $attr_id <= 0)
1663 showError ('Invalid arguments', __FUNCTION__
);
1667 $query = "delete from AttributeValue where object_id = ${object_id} and attr_id = ${attr_id} limit 1";
1668 $result = $dbxlink->query ($query);
1669 if ($result == NULL)
1671 showError ('SQL query failed', __FUNCTION__
);
1677 // FIXME: don't share common code with use commitResetAttrValue()
1678 function commitUpdateAttrValue ($object_id = 0, $attr_id = 0, $value = '')
1680 if ($object_id <= 0 or $attr_id <= 0)
1682 showError ('Invalid arguments', __FUNCTION__
);
1686 return commitResetAttrValue ($object_id, $attr_id);
1688 $query1 = "select attr_type from Attribute where attr_id = ${attr_id}";
1689 $result = $dbxlink->query ($query1);
1690 if ($result == NULL)
1692 showError ('SQL query #1 failed', __FUNCTION__
);
1695 $row = $result->fetch (PDO
::FETCH_NUM
);
1698 showError ('SQL query #1 returned no results', __FUNCTION__
);
1701 $attr_type = $row[0];
1702 $result->closeCursor();
1708 $column = $attr_type . '_value';
1711 $column = 'uint_value';
1714 showError ("Unknown attribute type '${attr_type}' met", __FUNCTION__
);
1718 "delete from AttributeValue where " .
1719 "object_id = ${object_id} and attr_id = ${attr_id} limit 1";
1720 $result = $dbxlink->query ($query2);
1721 if ($result == NULL)
1723 showError ('SQL query #2 failed', __FUNCTION__
);
1726 // We know $value isn't empty here.
1728 "insert into AttributeValue set ${column} = '${value}', " .
1729 "object_id = ${object_id}, attr_id = ${attr_id} ";
1730 $result = $dbxlink->query ($query3);
1731 if ($result == NULL)
1733 showError ('SQL query #3 failed', __FUNCTION__
);
1739 function commitUseupPort ($port_id = 0)
1743 showError ("Invalid argument", __FUNCTION__
);
1747 $query = "update Port set reservation_comment = NULL where id = ${port_id} limit 1";
1748 $result = $dbxlink->exec ($query);
1749 if ($result == NULL)
1751 showError ("SQL query failed", __FUNCTION__
);
1758 // This is a swiss-knife blade to insert a record into a table.
1759 // The first argument is table name.
1760 // The second argument is an array of "name" => "value" pairs.
1761 // The function returns either TRUE or FALSE (we expect one row
1763 function useInsertBlade ($tablename, $values)
1766 $namelist = $valuelist = '';
1767 foreach ($values as $name => $value)
1769 $namelist = $namelist . ($namelist == '' ?
"(${name}" : ", ${name}");
1770 $valuelist = $valuelist . ($valuelist == '' ?
"(${value}" : ", ${value}");
1772 $query = "insert into ${tablename} ${namelist}) values ${valuelist})";
1773 $result = $dbxlink->exec ($query);
1779 // This swiss-knife blade deletes one record from the specified table
1780 // using the specified key name and value.
1781 function useDeleteBlade ($tablename, $keyname, $keyvalue, $quotekey = TRUE, $deleteall = FALSE)
1784 if ($quotekey == TRUE)
1785 $keyvalue = "'${keyvalue}'";
1786 $query = "delete from ${tablename} where ${keyname}=$keyvalue";
1788 $query .= ' limit 1';
1789 $result = $dbxlink->exec ($query);
1790 if ($result === NULL)
1792 elseif ($result != 1)
1798 function useSelectBlade ($query, $caller = 'N/A')
1801 $result = $dbxlink->query ($query);
1802 if ($result == NULL)
1804 $ei = $dbxlink->errorInfo();
1805 showError ("SQL query '${query}' failed in useSelectBlade with error ${ei[1]} (${ei[2]})", $caller);
1811 function loadConfigCache ()
1813 $query = 'SELECT varname, varvalue, vartype, is_hidden, emptyok, description FROM Config ORDER BY varname';
1814 $result = useSelectBlade ($query);
1816 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1817 $cache[$row['varname']] = $row;
1818 $result->closeCursor();
1822 // setConfigVar() is expected to perform all necessary filtering
1823 function storeConfigVar ($varname = NULL, $varvalue = NULL)
1826 if (empty ($varname) ||
$varvalue === NULL)
1828 showError ('Invalid arguments', __FUNCTION__
);
1831 $query = "update Config set varvalue='${varvalue}' where varname='${varname}' limit 1";
1832 $result = $dbxlink->query ($query);
1833 if ($result == NULL)
1835 showError ("SQL query '${query}' failed", __FUNCTION__
);
1838 $rc = $result->rowCount();
1839 $result->closeCursor();
1840 if ($rc == 0 or $rc == 1)
1842 showError ("Something went wrong for args '${varname}', '${varvalue}'", __FUNCTION__
);
1846 // Database version detector. Should behave corretly on any
1847 // working dataset a user might have.
1848 function getDatabaseVersion ()
1851 $query = "select varvalue from Config where varname = 'DB_VERSION' and vartype = 'string'";
1852 $result = $dbxlink->query ($query);
1853 if ($result == NULL)
1855 $errorInfo = $dbxlink->errorInfo();
1856 if ($errorInfo[0] == '42S02') // ER_NO_SUCH_TABLE
1858 die (__FUNCTION__
. ': SQL query #1 failed with error ' . $errorInfo[2]);
1860 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
1861 if (count ($rows) != 1 ||
empty ($rows[0][0]))
1863 $result->closeCursor();
1864 die (__FUNCTION__
. ': Cannot guess database version. Config table is present, but DB_VERSION is missing or invalid. Giving up.');
1867 $result->closeCursor();
1871 // Return an array of virtual services. For each of them list real server pools
1872 // with their load balancers and other stats.
1873 function getSLBSummary ()
1875 $query = 'select vs.id as vsid, inet_ntoa(vip) as vip, vport, proto, vs.name, object_id, ' .
1876 'lb.rspool_id, pool.name as pool_name, count(rs.id) as rscount ' .
1877 'from IPVirtualService as vs inner join IPLoadBalancer as lb on vs.id = lb.vs_id ' .
1878 'inner join IPRSPool as pool on lb.rspool_id = pool.id ' .
1879 'left join IPRealServer as rs on rs.rspool_id = lb.rspool_id ' .
1880 'group by vs.id, object_id order by vs.vip, object_id';
1881 $result = useSelectBlade ($query);
1883 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1885 $vsid = $row['vsid'];
1886 $object_id = $row['object_id'];
1887 if (!isset ($ret[$vsid]))
1889 $ret[$vsid] = array();
1890 foreach (array ('vip', 'vport', 'proto', 'name') as $cname)
1891 $ret[$vsid][$cname] = $row[$cname];
1892 $ret[$vsid]['lblist'] = array();
1894 // There's only one assigned RS pool possible for each LB-VS combination.
1895 $ret[$vsid]['lblist'][$row['object_id']] = array
1897 'id' => $row['rspool_id'],
1898 'size' => $row['rscount'],
1899 'name' => $row['pool_name']
1902 $result->closeCursor();
1906 // Get the detailed composition of a particular virtual service, namely the list
1907 // of all pools, each shown with the list of objects servicing it. VS/RS configs
1908 // will be returned as well.
1909 function getVServiceInfo ($vsid = 0)
1911 $query1 = "select inet_ntoa(vip) as vip, vport, proto, name, vsconfig, rsconfig " .
1912 "from IPVirtualService where id = ${vsid}";
1913 $result = useSelectBlade ($query1);
1915 $row = $result->fetch (PDO
::FETCH_ASSOC
);
1918 foreach (array ('vip', 'vport', 'proto', 'name', 'vsconfig', 'rsconfig') as $cname)
1919 $vsinfo[$cname] = $row[$cname];
1920 $vsinfo['rspool'] = array();
1921 $result->closeCursor();
1923 $query2 = "select pool.id, name, pool.vsconfig, pool.rsconfig, object_id, " .
1924 "lb.vsconfig as lb_vsconfig, lb.rsconfig as lb_rsconfig from " .
1925 "IPRSPool as pool left join IPLoadBalancer as lb on pool.id = lb.rspool_id " .
1926 "where vs_id = ${vsid} order by pool.name, object_id";
1927 $result = useSelectBlade ($query2);
1928 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1930 if (!isset ($vsinfo['rspool'][$row['id']]))
1932 $vsinfo['rspool'][$row['id']]['name'] = $row['name'];
1933 $vsinfo['rspool'][$row['id']]['vsconfig'] = $row['vsconfig'];
1934 $vsinfo['rspool'][$row['id']]['rsconfig'] = $row['rsconfig'];
1935 $vsinfo['rspool'][$row['id']]['lblist'] = array();
1937 if ($row['object_id'] == NULL)
1939 $vsinfo['rspool'][$row['id']]['lblist'][$row['object_id']] = array
1941 'vsconfig' => $row['lb_vsconfig'],
1942 'rsconfig' => $row['lb_rsconfig']
1945 $result->closeCursor();
1949 // Collect and return the following info about the given real server pool:
1950 // basic information
1951 // parent virtual service information
1952 // load balancers list (each with a list of VSes)
1953 // real servers list
1955 function getRSPoolInfo ($id = 0)
1957 $query1 = "select id, name, vsconfig, rsconfig from " .
1958 "IPRSPool where id = ${id}";
1959 $result = useSelectBlade ($query1);
1961 $row = $result->fetch (PDO
::FETCH_ASSOC
);
1964 foreach (array ('id', 'name', 'vsconfig', 'rsconfig') as $c)
1965 $ret[$c] = $row[$c];
1966 $result->closeCursor();
1968 $ret['lblist'] = array();
1969 $ret['rslist'] = array();
1970 $query2 = "select object_id, vs_id, vsconfig, rsconfig from IPLoadBalancer " .
1971 "where rspool_id = ${id} order by object_id, vs_id";
1972 $result = useSelectBlade ($query2);
1973 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1974 foreach (array ('vsconfig', 'rsconfig') as $c)
1975 $ret['lblist'][$row['object_id']][$row['vs_id']][$c] = $row[$c];
1976 $result->closeCursor();
1978 $query3 = "select id, inservice, inet_ntoa(rsip) as rsip, rsport, rsconfig from " .
1979 "IPRealServer where rspool_id = ${id} order by IPRealServer.rsip, rsport";
1980 $result = useSelectBlade ($query3);
1981 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1982 foreach (array ('inservice', 'rsip', 'rsport', 'rsconfig') as $c)
1983 $ret['rslist'][$row['id']][$c] = $row[$c];
1984 $result->closeCursor();
1988 function addRStoRSPool ($pool_id = 0, $rsip = '', $rsport = 0, $inservice = 'no', $rsconfig = '')
1990 if ($pool_id <= 0 or $rsport <= 0)
1992 showError ('Invalid arguments', __FUNCTION__
);
1995 return useInsertBlade
2000 'rsip' => "inet_aton('${rsip}')",
2001 'rsport' => $rsport,
2002 'rspool_id' => $pool_id,
2003 'inservice' => ($inservice == 'yes' ?
"'yes'" : "'no'"),
2004 'rsconfig' => (empty ($rsconfig) ?
'NULL' : "'${rsconfig}'")
2009 function commitCreateVS ($vip = '', $vport = 0, $proto = '', $name = '', $vsconfig, $rsconfig)
2011 if (empty ($vip) or $vport <= 0 or empty ($proto))
2013 showError ('Invalid arguments', __FUNCTION__
);
2016 return useInsertBlade
2021 'vip' => "inet_aton('${vip}')",
2023 'proto' => "'${proto}'",
2024 'name' => (empty ($name) ?
'NULL' : "'${name}'"),
2025 'vsconfig' => (empty ($vsconfig) ?
'NULL' : "'${vsconfig}'"),
2026 'rsconfig' => (empty ($rsconfig) ?
'NULL' : "'${rsconfig}'")
2031 function addLBtoRSPool ($pool_id = 0, $object_id = 0, $vs_id = 0, $vsconfig = '', $rsconfig = '')
2033 if ($pool_id <= 0 or $object_id <= 0 or $vs_id <= 0)
2035 showError ('Invalid arguments', __FUNCTION__
);
2038 return useInsertBlade
2043 'object_id' => $object_id,
2044 'rspool_id' => $pool_id,
2046 'vsconfig' => (empty ($vsconfig) ?
'NULL' : "'${vsconfig}'"),
2047 'rsconfig' => (empty ($rsconfig) ?
'NULL' : "'${rsconfig}'")
2052 function commitDeleteRS ($id = 0)
2056 return useDeleteBlade ('IPRealServer', 'id', $id);
2059 function commitDeleteVS ($id = 0)
2063 return useDeleteBlade ('IPVirtualService', 'id', $id);
2066 function commitDeleteLB ($object_id = 0, $pool_id = 0, $vs_id = 0)
2069 if ($object_id <= 0 or $pool_id <= 0 or $vs_id <= 0)
2071 $query = "delete from IPLoadBalancer where object_id = ${object_id} and " .
2072 "rspool_id = ${pool_id} and vs_id = ${vs_id} limit 1";
2073 $result = $dbxlink->exec ($query);
2074 if ($result === NULL)
2076 elseif ($result != 1)
2082 function commitUpdateRS ($rsid = 0, $rsip = '', $rsport = 0, $rsconfig = '')
2084 if ($rsid <= 0 or $rsport <= 0)
2086 showError ('Invalid args', __FUNCTION__
);
2089 if (long2ip (ip2long ($rsip)) !== $rsip)
2091 showError ("Invalid IP address '${rsip}'", __FUNCTION__
);
2096 "update IPRealServer set rsip = inet_aton('${rsip}'), rsport = ${rsport}, rsconfig = " .
2097 (empty ($rsconfig) ?
'NULL' : "'${rsconfig}'") .
2098 " where id = ${rsid} limit 1";
2099 $result = $dbxlink->query ($query);
2100 if ($result == NULL)
2102 showError ("SQL query '${query}' failed", __FUNCTION__
);
2108 function commitUpdateLB ($object_id = 0, $pool_id = 0, $vs_id = 0, $vsconfig = '', $rsconfig = '')
2110 if ($object_id <= 0 or $pool_id <= 0 or $vs_id <= 0)
2112 showError ('Invalid args', __FUNCTION__
);
2117 "update IPLoadBalancer set vsconfig = " .
2118 (empty ($vsconfig) ?
'NULL' : "'${vsconfig}'") .
2120 (empty ($rsconfig) ?
'NULL' : "'${rsconfig}'") .
2121 " where object_id = ${object_id} and rspool_id = ${pool_id} " .
2122 "and vs_id = ${vs_id} limit 1";
2123 $result = $dbxlink->exec ($query);
2124 if ($result === NULL)
2130 function commitUpdateVS ($vsid = 0, $vip = '', $vport = 0, $proto = '', $name = '', $vsconfig = '', $rsconfig = '')
2132 if ($vsid <= 0 or empty ($vip) or $vport <= 0 or empty ($proto))
2134 showError ('Invalid args', __FUNCTION__
);
2138 $query = "update IPVirtualService set " .
2139 "vip = inet_aton('${vip}'), " .
2140 "vport = ${vport}, " .
2141 "proto = '${proto}', " .
2142 'name = ' . (empty ($name) ?
'NULL,' : "'${name}', ") .
2143 'vsconfig = ' . (empty ($vsconfig) ?
'NULL,' : "'${vsconfig}', ") .
2144 'rsconfig = ' . (empty ($rsconfig) ?
'NULL' : "'${rsconfig}'") .
2145 " where id = ${vsid} limit 1";
2146 $result = $dbxlink->exec ($query);
2147 if ($result === NULL)
2153 // Return the list of virtual services, indexed by vs_id.
2154 // Each record will be shown with its basic info plus RS pools counter.
2155 function getVSList ()
2157 $query = "select vs.id, inet_ntoa(vip) as vip, vport, proto, vs.name, vs.vsconfig, vs.rsconfig, count(rspool_id) as poolcount " .
2158 "from IPVirtualService as vs left join IPLoadBalancer as lb on vs.id = lb.vs_id " .
2159 "group by vs.id order by vs.vip, proto, vport";
2160 $result = useSelectBlade ($query);
2162 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2163 foreach (array ('vip', 'vport', 'proto', 'name', 'vsconfig', 'rsconfig', 'poolcount') as $cname)
2164 $ret[$row['id']][$cname] = $row[$cname];
2165 $result->closeCursor();
2169 // Return the list of RS pool, indexed by pool id.
2170 function getRSPoolList ()
2172 $query = "select pool.id, pool.name, count(rspool_id) as refcnt, pool.vsconfig, pool.rsconfig " .
2173 "from IPRSPool as pool left join IPLoadBalancer as lb on pool.id = lb.rspool_id " .
2174 "group by pool.id order by pool.id, name";
2175 $result = useSelectBlade ($query);
2177 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2178 foreach (array ('name', 'refcnt', 'vsconfig', 'rsconfig') as $cname)
2179 $ret[$row['id']][$cname] = $row[$cname];
2180 $result->closeCursor();
2184 function loadThumbCache ($rack_id = 0)
2187 $query = "select thumb_data from Rack where id = ${rack_id} and thumb_data is not null limit 1";
2188 $result = useSelectBlade ($query);
2189 $row = $result->fetch (PDO
::FETCH_ASSOC
);
2191 $ret = base64_decode ($row['thumb_data']);
2192 $result->closeCursor();
2196 function saveThumbCache ($rack_id = 0, $cache = NULL)
2199 if ($rack_id == 0 or $cache == NULL)
2201 showError ('Invalid arguments', __FUNCTION__
);
2204 $data = base64_encode ($cache);
2205 $query = "update Rack set thumb_data = '${data}' where id = ${rack_id} limit 1";
2206 $result = $dbxlink->exec ($query);
2209 function resetThumbCache ($rack_id = 0)
2214 showError ('Invalid argument', __FUNCTION__
);
2217 $query = "update Rack set thumb_data = NULL where id = ${rack_id} limit 1";
2218 $result = $dbxlink->exec ($query);
2221 // Return the list of attached RS pools for the given object. As long as we have
2222 // the LB-VS UNIQUE in IPLoadBalancer table, it is Ok to key returned records
2223 // by vs_id, because there will be only one RS pool listed for each VS of the
2225 function getRSPoolsForObject ($object_id = 0)
2227 if ($object_id <= 0)
2229 showError ('Invalid object_id', __FUNCTION__
);
2232 $query = 'select vs_id, inet_ntoa(vip) as vip, vport, proto, vs.name, pool.id as pool_id, ' .
2233 'pool.name as pool_name, count(rsip) as rscount, lb.vsconfig, lb.rsconfig from ' .
2234 'IPLoadBalancer as lb inner join IPRSPool as pool on lb.rspool_id = pool.id ' .
2235 'inner join IPVirtualService as vs on lb.vs_id = vs.id ' .
2236 'left join IPRealServer as rs on lb.rspool_id = rs.rspool_id ' .
2237 "where lb.object_id = ${object_id} " .
2238 'group by lb.rspool_id, lb.vs_id order by vs.vip, vport, proto, pool.name';
2239 $result = useSelectBlade ($query);
2241 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2242 foreach (array ('vip', 'vport', 'proto', 'name', 'pool_id', 'pool_name', 'rscount', 'vsconfig', 'rsconfig') as $cname)
2243 $ret[$row['vs_id']][$cname] = $row[$cname];
2244 $result->closeCursor();
2248 function commitCreateRSPool ($name = '', $vsconfig = '', $rsconfig = '')
2250 return useInsertBlade
2255 'name' => (empty ($name) ?
'NULL' : "'${name}'"),
2256 'vsconfig' => (empty ($vsconfig) ?
'NULL' : "'${vsconfig}'"),
2257 'rsconfig' => (empty ($rsconfig) ?
'NULL' : "'${rsconfig}'")
2262 function commitDeleteRSPool ($pool_id = 0)
2267 $query = "delete from IPRSPool where id = ${pool_id} limit 1";
2268 $result = $dbxlink->exec ($query);
2269 if ($result === NULL)
2271 elseif ($result != 1)
2277 function commitUpdateRSPool ($pool_id = 0, $name = '', $vsconfig = '', $rsconfig = '')
2281 showError ('Invalid arg', __FUNCTION__
);
2285 $query = "update IPRSPool set " .
2286 'name = ' . (empty ($name) ?
'NULL,' : "'${name}', ") .
2287 'vsconfig = ' . (empty ($vsconfig) ?
'NULL,' : "'${vsconfig}', ") .
2288 'rsconfig = ' . (empty ($rsconfig) ?
'NULL' : "'${rsconfig}'") .
2289 " where id = ${pool_id} limit 1";
2290 $result = $dbxlink->exec ($query);
2291 if ($result === NULL)
2293 elseif ($result != 1)
2299 function getRSList ()
2301 $query = "select id, inservice, inet_ntoa(rsip) as rsip, rsport, rspool_id, rsconfig " .
2302 "from IPRealServer order by rspool_id, IPRealServer.rsip, rsport";
2303 $result = useSelectBlade ($query);
2305 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2306 foreach (array ('inservice', 'rsip', 'rsport', 'rspool_id', 'rsconfig') as $cname)
2307 $ret[$row['id']][$cname] = $row[$cname];
2308 $result->closeCursor();
2312 // Return the list of all currently configured load balancers with their pool count.
2313 function getLBList ()
2315 $query = "select object_id, count(rspool_id) as poolcount " .
2316 "from IPLoadBalancer group by object_id order by object_id";
2317 $result = useSelectBlade ($query);
2319 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2320 $ret[$row['object_id']] = $row['poolcount'];
2321 $result->closeCursor();
2325 // For the given object return: it vsconfig/rsconfig; the list of RS pools
2326 // attached (each with vsconfig/rsconfig in turn), each with the list of
2327 // virtual services terminating the pool. Each pool also lists all real
2328 // servers with rsconfig.
2329 function buildLBConfig ($object_id)
2331 if ($object_id <= 0)
2333 showError ('Invalid arg', __FUNCTION__
);
2337 $query = 'select vs_id, inet_ntoa(vip) as vip, vport, proto, vs.name as vs_name, ' .
2338 'vs.vsconfig as vs_vsconfig, vs.rsconfig as vs_rsconfig, ' .
2339 'lb.vsconfig as lb_vsconfig, lb.rsconfig as lb_rsconfig, pool.id as pool_id, pool.name as pool_name, ' .
2340 'pool.vsconfig as pool_vsconfig, pool.rsconfig as pool_rsconfig, ' .
2341 'rs.id as rs_id, inet_ntoa(rsip) as rsip, rsport, rs.rsconfig as rs_rsconfig from ' .
2342 'IPLoadBalancer as lb inner join IPRSPool as pool on lb.rspool_id = pool.id ' .
2343 'inner join IPVirtualService as vs on lb.vs_id = vs.id ' .
2344 'inner join IPRealServer as rs on lb.rspool_id = rs.rspool_id ' .
2345 "where lb.object_id = ${object_id} and rs.inservice = 'yes' " .
2346 "order by vs.vip, vport, proto, pool.name, rs.rsip, rs.rsport";
2347 $result = useSelectBlade ($query);
2348 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2350 $vs_id = $row['vs_id'];
2351 if (!isset ($ret[$vs_id]))
2353 foreach (array ('vip', 'vport', 'proto', 'vs_name', 'vs_vsconfig', 'vs_rsconfig', 'lb_vsconfig', 'lb_rsconfig', 'pool_vsconfig', 'pool_rsconfig', 'pool_id', 'pool_name') as $c)
2354 $ret[$vs_id][$c] = $row[$c];
2355 $ret[$vs_id]['rslist'] = array();
2357 foreach (array ('rsip', 'rsport', 'rs_rsconfig') as $c)
2358 $ret[$vs_id]['rslist'][$row['rs_id']][$c] = $row[$c];
2360 $result->closeCursor();
2364 function commitSetInService ($rs_id = 0, $inservice = '')
2366 if ($rs_id <= 0 or empty ($inservice))
2368 showError ('Invalid args', __FUNCTION__
);
2372 $query = "update IPRealServer set inservice = '${inservice}' where id = ${rs_id} limit 1";
2373 $result = $dbxlink->exec ($query);
2374 if ($result === NULL)
2376 elseif ($result != 1)
2382 function executeAutoPorts ($object_id = 0, $type_id = 0)
2384 if ($object_id == 0 or $type_id == 0)
2386 showError ('Invalid arguments', __FUNCTION__
);
2390 foreach (getAutoPorts ($type_id) as $autoport)
2391 $ret = $ret and '' == commitAddPort ($object_id, $autoport['name'], $autoport['type'], '', '');
2395 // Return only implicitly listed tags, the rest of the trail will be
2396 // generated/deducted later at higher levels.
2397 function loadEntityTags ($entity_realm = '', $entity_id = 0)
2399 if ($entity_realm == '' or $entity_id <= 0)
2401 showError ('Invalid or missing arguments', __FUNCTION__
);
2405 $result = useSelectBlade ("select tt.id, tag from TagStorage as ts inner join TagTree as tt on ts.tag_id = tt.id where target_realm = '${entity_realm}' and target_id = ${entity_id}");
2406 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2407 $ret[$row['id']] = $row;
2408 $result->closeCursor();
2412 function loadRackObjectTags ($id)
2414 return loadEntityTags ('object', $id);
2417 function loadIPv4PrefixTags ($id)
2419 return loadEntityTags ('ipv4net', $id);
2422 function loadRackTags ($id)
2424 return loadEntityTags ('rack', $id);
2427 function loadIPv4VSTags ($id)
2429 return loadEntityTags ('ipv4vs', $id);
2432 function loadIPv4RSPoolTags ($id)
2434 return loadEntityTags ('ipv4rspool', $id);
2437 function getTagList ()
2440 $result = useSelectBlade ("select id, parent_id, tag from TagTree order by tag");
2441 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2442 $taglist[$row['id']] = array
2445 'tag' => $row['tag'],
2446 'parent_id' => $row['parent_id']
2448 $result->closeCursor();
2452 function commitCreateTag ($tagname = '', $parent_id = 0)
2454 if ($tagname == '' or $parent_id === 0)
2455 return "Invalid args to " . __FUNCTION__
;
2456 $result = useInsertBlade
2461 'tag' => "'${tagname}'",
2462 'parent_id' => $parent_id
2468 return "SQL query failed in " . __FUNCTION__
;
2471 function commitDestroyTag ($tagid = 0)
2474 return 'Invalid arg to ' . __FUNCTION__
;
2475 if (useDeleteBlade ('TagTree', 'id', $tagid, FALSE))
2478 return 'useDeleteBlade() failed in ' . __FUNCTION__
;
2481 function wipeTags ($realm, $id)
2484 $query = "delete from TagStorage where target_realm = '${realm}' and target_id = ${id}";
2485 $result = $dbxlink->exec ($query);
2486 if ($result === NULL)