function setSwitchVLANs($object_id = 0, $setcmd) { global $remote_username; $objectInfo = spotEntity('object', $object_id); $endpoints = findAllEndpoints($object_id, $objectInfo['name']); if (count($endpoints) == 0) { throw new RTGatewayError('no management address set'); } if (count($endpoints) > 1) { throw new RTGatewayError('cannot pick management address'); } $hwtype = $swtype = 'unknown'; foreach (getAttrValues($object_id) as $record) { if ($record['name'] == 'SW type' && strlen($record['o_value'])) { $swtype = strtr(execGMarker($record['o_value']), ' ', '+'); } if ($record['name'] == 'HW type' && strlen($record['o_value'])) { $hwtype = strtr(execGMarker($record['o_value']), ' ', '+'); } } $endpoint = str_replace(' ', '+', $endpoints[0]); $data = queryGateway('switchvlans', array("connect {$endpoint} {$hwtype} {$swtype} {$remote_username}", $setcmd)); // Finally we can parse the response into message array. foreach (explode(';', substr($data[1], strlen('OK!'))) as $text) { $message = 'gw: ' . substr($text, 2); if (strpos($text, 'I!') === 0) { showSuccess($message); } elseif (strpos($text, 'W!') === 0) { showWarning($message); } elseif (strpos($text, 'E!') === 0) { showError($message); } else { // All improperly formatted messages must be treated as error conditions. showError('unexpected line from gw: ' . $text); } } }
function gwDeployDeviceConfig($object_id, $breed, $text) { if ($text == '') { throw new InvalidArgException('text', '', 'deploy text is empty'); } $objectInfo = spotEntity('object', $object_id); $endpoints = findAllEndpoints($object_id, $objectInfo['name']); if (count($endpoints) == 0) { throw new RTGatewayError('no management address set'); } if (count($endpoints) > 1) { throw new RTGatewayError('cannot pick management address'); } $endpoint = str_replace(' ', '\\ ', str_replace(' ', '+', $endpoints[0])); $tmpfilename = tempnam('', 'RackTables-deviceconfig-'); if (FALSE === file_put_contents($tmpfilename, $text)) { unlink($tmpfilename); throw new RTGatewayError('failed to write to temporary file'); } try { queryGateway('deviceconfig', array("deploy {$endpoint} {$breed} {$tmpfilename}")); unlink($tmpfilename); } catch (RTGatewayError $e) { unlink($tmpfilename); throw $e; } }