make bwlimit None if it is not set to a value. Previously, an empty value
[plewww.git] / planetlab / common / actions.php
index ecde471..6b78666 100644 (file)
@@ -49,12 +49,16 @@ $known_actions []= "delete-node";
 //     expects:        node_id
 $known_actions []= "update-node";      
 //     expects:        node_id, hostname, model
+$known_actions []= "attach-pcu";
+//     expects:        node_id, pcu_id, port (pcu_id <0 means detach)
 
 //////////////////////////////////////// interfaces
 $known_actions []= "delete-interfaces";        
 //     expects:        interface_ids
 $known_actions []="add-interface";
 //     expects:        node_id & interface details
+$known_actions []="new-interface";
+//     expects:        node_id 
 $known_actions []="update-interface";
 //     expects:        interface_id & interface details
 
@@ -67,6 +71,8 @@ $known_actions []= "update-site";
 //     expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
 
 //////////////////////////////////////// slices
+$known_actions []= "delete-slice";
+//      expects:        slice_id
 $known_actions []= "update-slice";     
 //     expects:        slice_id, name, description, url
 $known_actions []= "renew-slice";
@@ -79,6 +85,10 @@ $known_actions []= 'remove-nodes-from-slice';
 //     expects:        slice_id & node_ids
 $known_actions []= 'add-nodes-in-slice';
 //     expects:        slice_id & node_ids
+$known_actions []= 'delete-slice-tags';
+//      expects:        slice_tag_id
+$known_actions []= 'add-slice-tag';
+//      expects:        slice_id & tag_type_id & node_id & nodegroup_id
 
 //////////////////////////////////////// tag types
 $known_actions []= "update-tag-type";
@@ -178,7 +188,7 @@ switch ($action) {
 
  case 'become-person' : {
    $plc->BecomePerson (intval($person_id));
-   plc_redirect (l_persons());
+   plc_redirect (l_person(intval($person_id)));
  }
 
  case 'delete-person' : {
@@ -234,7 +244,7 @@ switch ($action) {
    
    $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
    
-   if ( $key_id == 1) 
+   if ( $key_id >= 1) 
      drupal_set_message ("New key added");
    else
      drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
@@ -308,11 +318,12 @@ switch ($action) {
  }
 
  case 'update-node': {
+   $node_id=intval($_POST['node_id']);
    $hostname= $_POST['hostname'];
    $model= $_POST['model'];
 
    $fields= array( "hostname"=>$hostname, "model"=>$model );
-   $api->UpdateNode( intval( $node_id ), $fields );
+   $api->UpdateNode( $node_id, $fields );
    $error= $api->error();
 
    if( empty( $error ) ) {
@@ -324,6 +335,35 @@ switch ($action) {
    break;
  }
 
+ // this code will ensure that at most one PCU gets attached to the node
+ case 'attach-pcu': {
+   $node_id=intval($_POST['node_id']);
+   $pcu_id=intval($_POST['pcu_id']);
+   $port=intval($_POST['port']);
+   // always start with deleting former PCUs
+   $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
+   $former_pcu_ids = $nodes[0]['pcu_ids'];
+   if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
+       if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1) 
+        drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
+       else 
+        drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
+     }
+   // re-attach only if provided pcu_id >=0
+   if ($pcu_id >= 0) {
+     if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
+       drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
+     else
+       drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
+   } else {
+     drupal_set_message ('Detached node from all PCUs');
+   }
+   
+   plc_redirect(l_node($node_id));
+   break;
+ }
+   
+
 //////////////////////////////////////////////////////////// interfaces
  case 'delete-interfaces' : {
    $interface_ids=$_POST['interface_ids'];
@@ -345,7 +385,9 @@ switch ($action) {
      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
    plc_redirect(l_node($_POST['node_id']));
  }
-
+ case 'new-interface': {
+   plc_redirect(l_interface_add($_POST['node_id']));
+ }
  case 'add-interface': {
    $node_id=$_POST['node_id'];
    foreach ($interface_details as $field) {
@@ -354,11 +396,20 @@ switch ($action) {
        $interface[$field]= intval( $interface[$field] );
      }
    }
-   $result=$api->AddInterface( intval( $node_id ), $interface );
-   if ($result >0 ) 
-     drupal_set_message ("Interface $result added into node $node_id");
-   else
+   $interface_id =$api->AddInterface( intval( $node_id ), $interface );
+   if ($interface_id >0 ) {
+     $api->begin();
+        $api->AddInterfaceTag($interface_id,"alias",strval($interface_id));
+        $api->AddInterfaceTag($interface_id,"ifname","eth0");
+     list($id1, $id2) = $api->commit();
+     if ( $id1 > 0 && $id2 > 0 ) {
+         drupal_set_message ("Interface $interface_id added into node $node_id");
+     } else {
+         drupal_set_error ("Could not add interface tags to interface $interface_id");
+     }
+   } else {
      drupal_set_error ("Could not create interface");
+   }
    plc_redirect (l_node($node_id));
  }
    
@@ -367,7 +418,11 @@ switch ($action) {
    foreach ($interface_details as $field) {
      $interface[$field]= $_POST[$field];
      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
-       $interface[$field]= intval( $interface[$field] );
+       if ( intval($interface[$field]) != 0 ) {
+           $interface[$field]= intval( $interface[$field]);
+       } elseif ($field=='bwlimit' ) {
+           $interface[$field] = NULL;
+       }
      }
    }
    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
@@ -428,6 +483,9 @@ switch ($action) {
      $fields['login_base'] = $_POST['login_base'];
    if ($_POST['max_slices']) 
      $fields['max_slices'] = intval($_POST['max_slices']);
+   if (isset($_POST['enabled'])) {
+     $fields['enabled'] = (bool)$_POST['enabled'];
+   }
    
    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
    if ($retcod == 1) 
@@ -440,6 +498,17 @@ switch ($action) {
  }
 
 //////////////////////////////////////////////////////////// slices
+ case 'delete-slice': {
+   $slice_id = $_POST['slice_id'];
+   if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
+     drupal_set_message("Slice $slice_id deleted");
+     plc_redirect(l_slices());
+   } else {
+     drupal_set_error("Could not delete slice $slice_id " . $api->error());
+   }
+   break;
+ }
+     
  case 'update-slice': {
    $slice_id = $_POST['slice_id'];
    $name = $_POST['name'];
@@ -486,16 +555,33 @@ switch ($action) {
    $slice_id = intval ($_POST['slice_id']);    
    $person_ids = $_POST['person_ids'];
    
+   $slice_name = "";
+   $tmp_slices = $api->GetSlices($slice_id, array("name"));
+   if (count($tmp_slices) > 0) {
+       $tmp_slice = $tmp_slices[0];
+       $slice_name = $tmp_slice["name"];
+   }
+   $notify_subject = "Removed from slice: " . $slice_name;
+   $notify_body = sprintf("You have been removed from the slice %s.
+
+Our support team will be glad to answer any question that you might have.
+",$slice_name);
+   $notify_person_ids = array();
+   
    $success=true;
    $counter=0;
    foreach( $person_ids as $person_id ) {
      if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1) 
        $success=false;
-     else
+     else {
+         array_push($notify_person_ids, intval($person_id));
        $counter++;
+     }
    }
-   if ($success) 
+   if ($success) {
+     $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
      drupal_set_message ("Deleted $counter person(s)");
+   }
    else
      drupal_set_error ("Could not delete all selected persons, only $counter were removed");
    plc_redirect(l_slice($slice_id) . " &show_persons=true");
@@ -505,17 +591,37 @@ switch ($action) {
  case 'add-persons-in-slice': {
    $slice_id = intval ($_POST['slice_id']);    
    $person_ids = $_POST['person_ids'];
+
+   $slice_name = "";
+   $tmp_slices = $api->GetSlices($slice_id, array("name"));
+   if (count($tmp_slices) > 0) {
+     $tmp_slice = $tmp_slices[0];
+     $slice_name = $tmp_slice["name"];
+   }
+   $notify_subject = "Added to slice: " . $slice_name;
+   $notify_body = sprintf("You have been added to the slice %s as a user.
+
+You can go to your slice page following the link below:
+https://%s:%d/db/slices/index.php?id=%d
+
+Our support team will be glad to answer any question that you might have.
+",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
+   $notify_person_ids = array();
    
    $success=true;
    $counter=0;
    foreach ($person_ids as $person_id) {
      if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1) 
        $success=false;
-     else
+     else {
+       array_push($notify_person_ids, intval($person_id));
        $counter++;
+     }
    }
-   if ($success) 
+   if ($success) {
+     $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
      drupal_set_message ("Added $counter person(s)");
+   }
    else
      drupal_set_error ("Could not add all selected persons, only $counter were added");
    plc_redirect(l_slice($slice_id) . "&show_persons=true" );
@@ -547,6 +653,50 @@ switch ($action) {
    break;
  }
 
+ case 'delete-slice-tags': {
+   $slice_id = intval($_POST['slice_id']);
+   $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
+   $count = 0;
+   $success = true;
+   foreach($slice_tag_ids as $slice_tag_id) {
+     if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
+     else {
+       drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
+       $success = false;
+     }
+   }
+   if ($success)
+     drupal_set_message ("Deleted $count slice tag(s)");
+   plc_redirect(l_slice($slice_id) . "&show_tags=true" );
+   break;
+ }
+  
+ case 'add-slice-tag': {
+   $slice_id = intval($_POST['slice_id']);
+   $tag_type_id = intval($_POST['tag_type_id']);
+   $value = $_POST['value'];
+   $node_id = intval($_POST['node_id']);
+   $nodegroup_id = intval($_POST['nodegroup_id']);
+  
+   $result = null;
+   if ($node_id) {
+     $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
+   } elseif ($nodegroup_id) {
+     $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
+   } else {
+     $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
+   }
+   if ($result)
+     drupal_set_message ("Added slice tag.");
+   else 
+       drupal_set_error("Could not add slice tag");
+   if ($_POST['sliver_action'])
+       plc_redirect(l_sliver($node_id,$slice_id));
+   else
+       plc_redirect(l_slice($slice_id) . "&show_tags=true" );
+   break;
+ }
+
 //////////////////////////////////////////////////////////// tag types
 
  case 'update-tag-type': {
@@ -555,7 +705,7 @@ switch ($action) {
    $tagname = $_POST['tagname'];
    $min_role_id= intval( $_POST['min_role_id'] );
    $description= $_POST['description'];  
-   $category= $_POST['category'];  
+   $category= $_POST['category'];
   
    // make tag_type_fields dict
    $tag_type_fields= array( "min_role_id" => $min_role_id,