Example #1
0
function updateObjectAllocation()
{
    global $remote_username, $sic;
    if (!isset($_REQUEST['got_atoms'])) {
        unset($_GET['page']);
        unset($_GET['tab']);
        unset($_GET['op']);
        unset($_POST['page']);
        unset($_POST['tab']);
        unset($_POST['op']);
        return buildRedirectURL(NULL, NULL, $_REQUEST);
    }
    $object_id = getBypassValue();
    $changecnt = 0;
    // Get a list of all of this object's parents,
    // then trim the list to only include parents that are racks
    $objectParents = getEntityRelatives('parents', 'object', $object_id);
    $parentRacks = array();
    foreach ($objectParents as $parentData) {
        if ($parentData['entity_type'] == 'rack') {
            $parentRacks[] = $parentData['entity_id'];
        }
    }
    $workingRacksData = array();
    foreach ($_REQUEST['rackmulti'] as $cand_id) {
        if (!isset($workingRacksData[$cand_id])) {
            $rackData = spotEntity('rack', $cand_id);
            amplifyCell($rackData);
            $workingRacksData[$cand_id] = $rackData;
        }
        // It's zero-U mounted to this rack on the form, but not in the DB.  Mount it.
        if (isset($_REQUEST["zerou_{$cand_id}"]) && !in_array($cand_id, $parentRacks)) {
            $changecnt++;
            commitLinkEntities('rack', $cand_id, 'object', $object_id);
        }
        // It's not zero-U mounted to this rack on the form, but it is in the DB.  Unmount it.
        if (!isset($_REQUEST["zerou_{$cand_id}"]) && in_array($cand_id, $parentRacks)) {
            $changecnt++;
            commitUnlinkEntities('rack', $cand_id, 'object', $object_id);
        }
    }
    foreach ($workingRacksData as &$rd) {
        applyObjectMountMask($rd, $object_id);
    }
    $oldMolecule = getMoleculeForObject($object_id);
    foreach ($workingRacksData as $rack_id => $rackData) {
        if (!processGridForm($rackData, 'F', 'T', $object_id)) {
            continue;
        }
        $changecnt++;
        // Reload our working copy after form processing.
        $rackData = spotEntity('rack', $cand_id);
        amplifyCell($rackData);
        applyObjectMountMask($rackData, $object_id);
        $workingRacksData[$rack_id] = $rackData;
    }
    if ($changecnt) {
        // Log a record.
        $newMolecule = getMoleculeForObject($object_id);
        usePreparedInsertBlade('MountOperation', array('object_id' => $object_id, 'old_molecule_id' => count($oldMolecule) ? createMolecule($oldMolecule) : NULL, 'new_molecule_id' => count($newMolecule) ? createMolecule($newMolecule) : NULL, 'user_name' => $remote_username, 'comment' => empty($sic['comment']) ? NULL : $sic['comment']));
    }
    showFuncMessage(__FUNCTION__, 'OK', array($changecnt));
}
function updateObjectAllocation()
{
    global $remote_username, $sic;
    if (!isset($_REQUEST['got_atoms'])) {
        unset($_GET['page']);
        unset($_GET['tab']);
        unset($_GET['op']);
        unset($_POST['page']);
        unset($_POST['tab']);
        unset($_POST['op']);
        return buildRedirectURL(NULL, NULL, $_REQUEST);
    }
    $object_id = getBypassValue();
    $object = spotEntity('object', $object_id);
    $changecnt = 0;
    // Get a list of rack ids which are parents of the object
    $parentRacks = reduceSubarraysToColumn(getParents($object, 'rack'), 'id');
    $workingRacksData = array();
    foreach ($_REQUEST['rackmulti'] as $cand_id) {
        if (!isset($workingRacksData[$cand_id])) {
            $rackData = spotEntity('rack', $cand_id);
            amplifyCell($rackData);
            $workingRacksData[$cand_id] = $rackData;
        } else {
            $rackData = $workingRacksData[$cand_id];
        }
        $is_ro = !rackModificationPermitted($rackData, 'updateObjectAllocation', FALSE);
        // It's zero-U mounted to this rack on the form, but not in the DB.  Mount it.
        if (isset($_REQUEST["zerou_{$cand_id}"]) && !in_array($cand_id, $parentRacks)) {
            if ($is_ro) {
                continue;
            }
            $changecnt++;
            commitLinkEntities('rack', $cand_id, 'object', $object_id);
        }
        // It's not zero-U mounted to this rack on the form, but it is in the DB.  Unmount it.
        if (!isset($_REQUEST["zerou_{$cand_id}"]) && in_array($cand_id, $parentRacks)) {
            if ($is_ro) {
                continue;
            }
            $changecnt++;
            commitUnlinkEntities('rack', $cand_id, 'object', $object_id);
        }
    }
    foreach ($workingRacksData as &$rd) {
        applyObjectMountMask($rd, $object_id);
    }
    $oldMolecule = getMoleculeForObject($object_id);
    foreach ($workingRacksData as $rack_id => $rackData) {
        $is_ro = !rackModificationPermitted($rackData, 'updateObjectAllocation', FALSE);
        if ($is_ro || !processGridForm($rackData, 'F', 'T', $object_id)) {
            continue;
        }
        $changecnt++;
        // Reload our working copy after form processing.
        $rackData = spotEntity('rack', $cand_id);
        amplifyCell($rackData);
        applyObjectMountMask($rackData, $object_id);
        $workingRacksData[$rack_id] = $rackData;
    }
    if ($changecnt) {
        // Log a record.
        $newMolecule = getMoleculeForObject($object_id);
        usePreparedInsertBlade('MountOperation', array('object_id' => $object_id, 'old_molecule_id' => count($oldMolecule) ? createMolecule($oldMolecule) : NULL, 'new_molecule_id' => count($newMolecule) ? createMolecule($newMolecule) : NULL, 'user_name' => $remote_username, 'comment' => empty($sic['comment']) ? NULL : $sic['comment']));
    }
    ###############################################################################################
    # set hostgroup for zabbix host
    $allracks = getMoleculeForObject($object_id);
    $rackids = array();
    $racknames = array();
    foreach ($allracks as $rack) {
        if (in_array($rack['rack_id'], $rackids)) {
            continue;
        }
        array_push($rackids, $rack['rack_id']);
        $rackdata = spotEntity('rack', $rack['rack_id']);
        array_push($racknames, $rackdata['name']);
    }
    $result = updateHostGroup($object['name'], $racknames);
    if (isset($result['error'])) {
        showError('Setting hostgroup for zabbix host is failed. Error message:' . $result['error']);
    }
    # END
    ###############################################################################################
    showFuncMessage(__FUNCTION__, 'OK', array($changecnt));
}
Example #3
0
function renderRackSpaceForObject($object_id)
{
    // Always process occupied racks plus racks chosen by user. First get racks with
    // already allocated rackspace...
    $workingRacksData = getResidentRacksData($object_id);
    // ...and then add those chosen by user (if any).
    if (isset($_REQUEST['rackmulti'])) {
        foreach ($_REQUEST['rackmulti'] as $cand_id) {
            if (!isset($workingRacksData[$cand_id])) {
                $rackData = spotEntity('rack', $cand_id);
                amplifyCell($rackData);
                $workingRacksData[$cand_id] = $rackData;
            }
        }
    }
    // Get a list of all of this object's parents,
    // then trim the list to only include parents that are racks
    $objectParents = getEntityRelatives('parents', 'object', $object_id);
    $parentRacks = array();
    foreach ($objectParents as $parentData) {
        if ($parentData['entity_type'] == 'rack') {
            $parentRacks[] = $parentData['entity_id'];
        }
    }
    // Main layout starts.
    echo "<table border=0 class=objectview cellspacing=0 cellpadding=0><tr>";
    // Left portlet with rack list.
    echo "<td class=pcleft height='1%'>";
    startPortlet('Racks');
    $allRacksData = listCells('rack');
    // filter rack list to match only racks having common tags with the object (reducing $allRacksData)
    if (!isset($_REQUEST['show_all_racks']) and getConfigVar('FILTER_RACKLIST_BY_TAGS') == 'yes') {
        $matching_racks = array();
        $object = spotEntity('object', $object_id);
        $matched_tags = array();
        foreach ($allRacksData as $rack) {
            foreach ($object['etags'] as $tag) {
                if (tagOnChain($tag, $rack['etags']) or tagOnChain($tag, $rack['itags'])) {
                    $matching_racks[$rack['id']] = $rack;
                    $matched_tags[$tag['id']] = $tag;
                    break;
                }
            }
        }
        // add current object's racks even if they dont match filter
        foreach ($workingRacksData as $rack_id => $rack) {
            if (!isset($matching_racks[$rack_id])) {
                $matching_racks[$rack_id] = $rack;
            }
        }
        // if matching racks found, and rack list is reduced, show 'show all' link
        if (count($matching_racks) and count($matching_racks) != count($allRacksData)) {
            $filter_text = '';
            foreach ($matched_tags as $tag) {
                $filter_text .= (empty($filter_text) ? '' : ' or ') . '{' . $tag['tag'] . '}';
            }
            $href_show_all = trim($_SERVER['REQUEST_URI'], '&');
            $href_show_all .= htmlspecialchars('&show_all_racks=1');
            echo "(filtered by <span class='filter-text'>{$filter_text}</span>, <a href='{$href_show_all}'>show all</a>)<p>";
            $allRacksData = $matching_racks;
        }
    }
    if (count($allRacksData) <= getConfigVar('RACK_PRESELECT_THRESHOLD')) {
        foreach ($allRacksData as $rack) {
            if (!array_key_exists($rack['id'], $workingRacksData)) {
                amplifyCell($rack);
                $workingRacksData[$rack['id']] = $rack;
            }
        }
    }
    foreach (array_keys($workingRacksData) as $rackId) {
        applyObjectMountMask($workingRacksData[$rackId], $object_id);
    }
    printOpFormIntro('updateObjectAllocation');
    renderRackMultiSelect('rackmulti[]', $allRacksData, array_keys($workingRacksData));
    echo "<br><br>";
    finishPortlet();
    echo "</td>";
    // Middle portlet with comment and submit.
    echo "<td class=pcleft>";
    startPortlet('Comment (for Rackspace History)');
    echo "<textarea name=comment rows=10 cols=40></textarea><br>\n";
    echo "<input type=submit value='Save' name=got_atoms>\n";
    echo "<br><br>";
    finishPortlet();
    echo "</td>";
    // Right portlet with rendered racks. If this form submit is not final, we have to
    // reflect the former state of the grid in current form.
    echo "<td class=pcright rowspan=2 height='1%'>";
    startPortlet('Working copy');
    includeJQueryUI(false);
    addJS('js/racktables.js');
    addJS('js/bulkselector.js');
    echo '<table border=0 cellspacing=10 align=center><tr>';
    foreach ($workingRacksData as $rack_id => $rackData) {
        // Order is important here: only original allocation is highlighted.
        highlightObject($rackData, $object_id);
        markupAtomGrid($rackData, 'T');
        // If we have a form processed, discard user input and show new database
        // contents.
        if (isset($_REQUEST['rackmulti'][0])) {
            // is an update
            mergeGridFormToRack($rackData);
        }
        echo "<td valign=top>";
        echo "<center>\n<h2>{$rackData['name']}</h2>\n";
        echo "<table class=rack id=selectableRack border=0 cellspacing=0 cellpadding=1>\n";
        echo "<tr><th width='10%'>&nbsp;</th>";
        echo "<th width='20%'><a href='javascript:;' onclick=\"toggleColumnOfAtoms('{$rack_id}', '0', {$rackData['height']})\">Front</a></th>";
        echo "<th width='50%'><a href='javascript:;' onclick=\"toggleColumnOfAtoms('{$rack_id}', '1', {$rackData['height']})\">Interior</a></th>";
        echo "<th width='20%'><a href='javascript:;' onclick=\"toggleColumnOfAtoms('{$rack_id}', '2', {$rackData['height']})\">Back</a></th></tr>\n";
        renderAtomGrid($rackData);
        echo "<tr><th width='10%'>&nbsp;</th>";
        echo "<th width='20%'><a href='javascript:;' onclick=\"toggleColumnOfAtoms('{$rack_id}', '0', {$rackData['height']})\">Front</a></th>";
        echo "<th width='50%'><a href='javascript:;' onclick=\"toggleColumnOfAtoms('{$rack_id}', '1', {$rackData['height']})\">Interior</a></th>";
        echo "<th width='20%'><a href='javascript:;' onclick=\"toggleColumnOfAtoms('{$rack_id}', '2', {$rackData['height']})\">Back</a></th></tr>\n";
        echo "</table>\n<br>\n";
        // Determine zero-u checkbox status.
        // If form has been submitted, use form data, otherwise use DB data.
        if (isset($_REQUEST['op'])) {
            $checked = isset($_REQUEST['zerou_' . $rack_id]) ? 'checked' : '';
        } else {
            $checked = in_array($rack_id, $parentRacks) ? 'checked' : '';
        }
        echo "<label for=zerou_{$rack_id}>Zero-U:</label> <input type=checkbox {$checked} name=zerou_{$rack_id} id=zerou_{$rack_id}>\n<br><br>\n";
        echo "<input type='button' onclick='uncheckAll();' value='Uncheck all'>\n";
        echo '</center></td>';
    }
    echo "</tr></table>";
    finishPortlet();
    echo "</td>\n";
    echo "</form>\n";
    echo "</tr></table>\n";
}
function initRackTablesObject($rackDatas)
{
    // zabbix host data
    $params = array('output' => 'extend');
    $result = doPost('host.get', $params);
    $hosts = isset($result['result']) ? $result['result'] : array();
    $objectDatas = array();
    foreach ($hosts as $host) {
        $object_type_id = 4;
        $object_name = isset($host['host']) ? $host['host'] : "";
        $object_label = "";
        $object_asset_no = "";
        $taglist = array();
        $has_problems = $host['status'] == 0 ? 'no' : 'yes';
        $object_id = commitAddObject($object_name, $object_label, $object_type_id, $object_asset_no, $taglist);
        usePreparedUpdateBlade('Object', array('has_problems' => $has_problems), array('id' => $object_id));
        $objectDatas[$host['hostid']] = $object_id;
        // set hostgroup
        $params = array('output' => array('name'), 'hostids' => array($host['hostid']));
        $result = doPost('hostgroup.get', $params);
        $hostgroups = isset($result['result']) ? $result['result'] : array();
        $rack_ids = array();
        $_REQUEST = array();
        foreach ($hostgroups as $hostgroup) {
            if (isset($rackDatas[$hostgroup['name']])) {
                $rack = $rackDatas[$hostgroup['name']];
                amplifyCell($rack);
                array_push($rack_ids, $rack['id']);
                $height = $rack['height'];
                for ($i = 0; $i < 3; $i++) {
                    for ($j = $height; $j > 0; $j--) {
                        if ($rack[$j][$i]['state'] == 'F') {
                            # state == "T" : mounted
                            $_REQUEST['atom_' . $rack['id'] . "_{$j}" . "_{$i}"] = 'on';
                            break 2;
                        }
                    }
                }
            }
        }
        $_REQUEST['object_id'] = $object_id;
        $_REQUEST['rackmulti'] = $rack_ids;
        $object = spotEntity('object', $object_id);
        $changecnt = 0;
        // Get a list of rack ids which are parents of the object
        $parentRacks = reduceSubarraysToColumn(getParents($object, 'rack'), 'id');
        $workingRacksData = array();
        foreach ($_REQUEST['rackmulti'] as $cand_id) {
            if (!isset($workingRacksData[$cand_id])) {
                $rackData = spotEntity('rack', $cand_id);
                amplifyCell($rackData);
                $workingRacksData[$cand_id] = $rackData;
            } else {
                $rackData = $workingRacksData[$cand_id];
            }
            $is_ro = !rackModificationPermitted($rackData, 'updateObjectAllocation', FALSE);
            // It's zero-U mounted to this rack on the form, but not in the DB.  Mount it.
            if (isset($_REQUEST["zerou_{$cand_id}"]) && !in_array($cand_id, $parentRacks)) {
                if ($is_ro) {
                    continue;
                }
                $changecnt++;
                commitLinkEntities('rack', $cand_id, 'object', $object_id);
            }
            // It's not zero-U mounted to this rack on the form, but it is in the DB.  Unmount it.
            if (!isset($_REQUEST["zerou_{$cand_id}"]) && in_array($cand_id, $parentRacks)) {
                if ($is_ro) {
                    continue;
                }
                $changecnt++;
                commitUnlinkEntities('rack', $cand_id, 'object', $object_id);
            }
        }
        foreach ($workingRacksData as &$rd) {
            applyObjectMountMask($rd, $object_id);
        }
        $oldMolecule = getMoleculeForObject($object_id);
        foreach ($workingRacksData as $rack_id => $rackData) {
            $is_ro = !rackModificationPermitted($rackData, 'updateObjectAllocation', FALSE);
            if ($is_ro || !processGridForm($rackData, 'F', 'T', $object_id)) {
                continue;
            }
            $changecnt++;
            // Reload our working copy after form processing.
            $rackData = spotEntity('rack', $cand_id);
            amplifyCell($rackData);
            applyObjectMountMask($rackData, $object_id);
            $workingRacksData[$rack_id] = $rackData;
        }
        if ($changecnt) {
            // Log a record.
            $newMolecule = getMoleculeForObject($object_id);
            global $remote_username, $sic;
            usePreparedInsertBlade('MountOperation', array('object_id' => $object_id, 'old_molecule_id' => count($oldMolecule) ? createMolecule($oldMolecule) : NULL, 'new_molecule_id' => count($newMolecule) ? createMolecule($newMolecule) : NULL, 'user_name' => $remote_username, 'comment' => empty($sic['comment']) ? NULL : $sic['comment']));
        }
        // set IP
        $params = array('output' => 'extend', 'hostids' => $host['hostid']);
        $result = doPost('hostinterface.get', $params);
        $hostinterfaces = isset($result['result']) ? $result['result'] : array();
        foreach ($hostinterfaces as $interface) {
            if (isset($interface['ip']) && $interface['ip'] != '127.0.0.1') {
                $allocs = getObjectIPAllocations($object_id);
                $current_ips = array();
                foreach ($allocs as $alloc) {
                    $ip = $alloc["addrinfo"]["ip"];
                    $current_ips[$ip] = $ip;
                }
                $ip = $interface['ip'];
                if (!in_array($ip, array_values($current_ips))) {
                    // new IP
                    $ip_bin = ip_parse($ip);
                    if (null == getIPAddressNetworkId($ip_bin)) {
                        // if ip is not exists, adding it into RackTables IPv4Prefix.
                        $range = substr($ip, 0, strripos($ip, '.')) . '.0/24';
                        $vlan_ck = NULL;
                        $net_id = createIPv4Prefix($range, 'admim', isCheckSet('is_connected'), $taglist);
                        $net_cell = spotEntity('ipv4net', $net_id);
                        if (isset($vlan_ck)) {
                            if (considerConfiguredConstraint($net_cell, 'VLANIPV4NET_LISTSRC')) {
                                commitSupplementVLANIPv4($vlan_ck, $net_id);
                            }
                        }
                    }
                    bindIPToObject($ip_bin, $object_id, "", "");
                }
            }
        }
    }
    return $objectDatas;
}
Example #5
0
File: api.php Project: xtha/salt
     }
     // It's zero-U allocated to this rack in the API request, but not in the DB.  Mount it.
     if (in_array($rack_id, $zeroURacksNew) && !in_array($rack_id, $zeroURacksOld)) {
         $changecnt++;
         error_log("zero-u mounting object id: {$object_id} from rack id: {$rack_id}");
         commitLinkEntities('rack', $rack_id, 'object', $object_id);
     }
     // It's not zero-U allocated to this rack in the API request, but it is in the DB.  Unmount it.
     if (!in_array($rack_id, $zeroURacksNew) && in_array($rack_id, $zeroURacksOld)) {
         $changecnt++;
         error_log("zero-u UN- mounting object id: {$object_id} from rack id: {$rack_id}");
         commitUnlinkEntities('rack', $rack_id, 'object', $object_id);
     }
 }
 foreach ($workingRacksData as &$rd) {
     applyObjectMountMask($rd, $object_id);
 }
 // quick DB operation to save old data for logging
 $oldMolecule = getMoleculeForObject($object_id);
 foreach ($workingRacksData as $rack_id => $rackData) {
     $rackchanged = FALSE;
     $dbxlink->beginTransaction();
     for ($position = $rackData['height']; $position > 0; $position--) {
         for ($locidx = 0; $locidx < 3; $locidx++) {
             $atom = $loclist[$locidx];
             // atom can't be assigned to, skip.
             //     XXX: maybe should warn if attempted? (UI similarly ignores)
             if ($rackData[$position][$locidx]['enabled'] != TRUE) {
                 continue;
             }
             // F => free, can be assigned to