Automatically add the ifname and alias InterfaceTags to extra interfaces.
[plewww.git] / planetlab / common / actions.php
index 294dec0..5649a8c 100644 (file)
@@ -50,13 +50,15 @@ $known_actions []= "delete-node";
 $known_actions []= "update-node";      
 //     expects:        node_id, hostname, model
 $known_actions []= "attach-pcu";
-//     expects:        node_id, pcu_id, port_id (pcu_id <0 means detach)
+//     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
 
@@ -335,15 +337,17 @@ switch ($action) {
 
  // this code will ensure that at most one PCU gets attached to the node
  case 'attach-pcu': {
-   $node_id=$_POST['node_id'];
-   $pcu_id=$_POST['node_id'];
-   $port=$_POST['port'];
+   $node_id=intval($_POST['node_id']);
+   $pcu_id=intval($_POST['pcu_id']);
+   $port=intval($_POST['port']);
    // always start with deleting former PCUs
-   $former_pcu_ids = $api->GetNodes(array($node_id),array('pcu_ids'));
+   $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_error ('Could not detach from PCU ' . $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) {
@@ -381,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) {
@@ -390,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));
  }
    
@@ -536,16 +551,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");
@@ -555,17 +587,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" );