person;
$_roles= $_person['role_ids'];
// if sent here from another page remove then redirect
if( $_GET['remove'] && $_GET['nodegroup_id'] ) {
$ng_id= $_GET['nodegroup_id'];
$node_id= $_GET['remove'];
$api->DeleteNodeFromNodeGroup( intval( $node_id ), intval( $ng_id ) );
header( "location: index.php?id=$node_id" );
exit();
}
// Print header
require_once 'plc_drupal.php';
drupal_set_title('Node Groups');
include 'plc_header.php';
// if no id display list of nodegroups
if( !$_GET['id'] && !$_GET['nodegroup_id'] ) {
$nodegroup_info= $api->GetNodeGroups( NULL, array( "nodegroup_id", "name", "description" ) );
echo "
Node Groups
\n
Name | Description | ";
// if admin we need to more cells
if( in_array( "10", $_person['role_ids'] ) )
echo " | | ";
echo "
";
foreach( $nodegroup_info as $type ) {
echo "". $type['name'] ." | ". $type['description'] ." | ";
// if admin display edit/delet links
if( in_array( "10", $_person['role_ids'] ) ) {
echo "Edit | ";
echo plc_delete_link_button('node_groups.php?del_type=' . $type['nodegroup_id'],
$type['name']);
echo "";
}
echo "
\n";
}
echo "
\n";
}
// if id is set then show nodegroup info
elseif( $_GET['id'] ) {
$nodegroup_id= $_GET['id'];
$nodegroup_info= $api->GetNodeGroups( array( intval( $nodegroup_id ) ),
array( "name", "nodegroup_id", "node_ids" ) );
$node_info= $api->GetNodes( $nodegroup_info[0]['node_ids'], array( "node_id", "hostname" ) );
//display info
echo "Node Group ". $nodegroup_info[0]['name'] ."
\n";
if( empty( $nodegroup_info[0]['node_ids'] ) )
echo "No nodes in node group.";
else {
echo "
Hostname | ";
// if admin need more cells
if( in_array( 10, $_roles ) )
echo "Remove | ";
echo "
\n";
foreach( $node_info as $node ) {
echo "". $node['hostname'] ." | ";
if( in_array( 10, $_roles ) )
echo "remove | ";
echo "
";
}
echo "
\n";
}
}
// if no id add else update
elseif( $_GET['add'] ) {
// add node group and redirect to update nodes for it
if( $_POST['add_sub'] ) {
$name= $_POST['name'];
$description= $_POST['description'];
$fields= array( 'name'=>$name, 'description'=>$description );
// add it
$api->AddNodeGroup( $fields );
// get nodegroup_id
$group_info= $api->GetNodeGroups( array( $name ), array( "nodegroup_id" ) );
// redirect
header( "location: node_groups.php?id=". $group_info[0]['nodegroup_id'] );
exit();
}
// add form
echo "Back to Node Index\n";
}
elseif( $_GET['nodegroup_id'] )
{
// get node group id
$node_group_id= $_GET['nodegroup_id'];
// if add node submitted, add
if( $_POST['add'] )
{
$add_nodes= $_POST['add_nodes'];
// add nodes to node group
foreach( $add_nodes as $add_node )
{
$api->AddNodeToNodeGroup( intval( $add_node ), intval( $node_group_id ) );
}
}
// if remove node submitted, remove
if( $_POST['remove'] )
{
$rem_nodes= $_POST['rem_nodes'];
// remove nodes from node group
foreach( $rem_nodes as $rem_node )
{
$api->DeleteNodeFromNodeGroup( intval( $rem_node ), intval( $node_group_id ) );
}
}
// update name and description
$name= $_POST['name'];
$description= $_POST['description'];
$fields= array();
if( $name )
$fields['name']= $name;
if( $description )
$fields['description']= $description;
// api call
if( !empty( $fields ) )
$api->UpdateNodeGroup( intval( $node_group_id ), $fields );
// get node_group info
$group_info= $api->GetNodeGroups( array( intval( $node_group_id ) ),
array( "node_ids", "name", "conf_file_ids", "description" ) );
$node_ids = $group_info[0]['node_ids'];
$name = $group_info[0]['name'];
$conf_file_ids = $group_info[0]['conf_file_ids'];
$description = $group_info[0]['description'];
// get node info
if( !empty( $node_ids ) )
$node_info= $api->GetNodes( $node_ids,
array( "hostname", "node_id" ) );
// get site names and ids
$site_info= $api->GetSites( NULL, array( "site_id", "name" ) );
sort_sites( $site_info );
// if site_id is in post use it, if not use the user's primary
if( $_POST['site_id'] )
$site_id= $_POST['site_id'];
else
$site_id= $_person['site_ids'][0];
// get site nodes for $site_id
$sid= intval( $site_id );
$site_node_info= $api->GetSites( array( $sid ), array( "node_ids" ) );
$site_nodes= $site_node_info[0]['node_ids'];
// gets all node_ids from site that arent already associated with the node group
foreach( $site_nodes as $snode) {
if( !in_array( $snode, $node_ids ) )
$snodes[]= $snode;
}
// Get node info from new list
if( !empty( $snodes ) )
$snode_info= $api->GetNodes( $snodes, array( "hostname", "node_id" ) );
// start form
echo "\n";
echo "
Back to Node Group Index\n";
}
// Print footer
include 'plc_footer.php';
?>