more inline updates & cleaned up tags
[plewww.git] / planetlab / actions.php
index b69af23..ea142d5 100644 (file)
@@ -18,6 +18,7 @@ $known_actions=array();
 // interface :
 // (*) use POST 
 // (*) set 'action' to one of the following
+//////////////////////////////////////// persons
 $known_actions []= "add-person-to-site";
 //     expects:        person_id & site_id
 $known_actions []= "remove-person-from-sites";
@@ -38,12 +39,42 @@ $known_actions []= "delete-keys";
 //     expects:        key_ids & person_id (for redirecting to the person's page)
 $known_actions []= "upload-key";
 //     expects:        person_id & $_FILES['key']
+$known_actions []= "update-person";
+//     expects:        person_id & first_name last_name title email phone url bio + [password1 password2]
+
+//////////////////////////////////////// nodes
+$known_actions []= "node-boot-state";  
+//     expects:        node_id boot_state
+$known_actions []= "delete-node";      
+//     expects:        node_id
+$known_actions []= "update-node";      
+//     expects:        node_id, hostname, model
+
+//////////////////////////////////////// interfaces
+$known_actions []= "delete-interfaces";        
+//     expects:        interface_ids
+
+//////////////////////////////////////// sites
+$known_actions []= "delete-site";      
+//     expects:        site_id
+$known_actions []= "expire-all-slices-in-site";
+//     expects:        slice_ids
+$known_actions []= "update-site";
+//     expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
+
+//////////////////////////////////////// tags
 $known_actions []= "update-tag-type";
 //     expects:        tag_type_id & name & description & category & min_role_id  
 $known_actions []= "add-tag-type";
-//     expects:        tag_type_id & name & description & category & min_role_id  
-$known_actions []= "expire-all-slices-in-site";
-//     expects:        slice_ids
+//     expects:        tag_type_id & tagname & description & category & min_role_id  
+$known_actions []= "set-tag-on-node";
+//     expects:        node_id tagname value
+$known_actions []= "delete-tag-types";
+//     expects:        tag_type_ids
+
+//////////////////////////////////////// nodetags
+$known_actions []= "delete-node-tags";
+//     expects:        node_id & node_tag_ids
 
 //////////////////////////////
 // sometimes we don't set 'action', but use the submit button name instead
@@ -57,11 +88,13 @@ else
       break;
     }
 
-//
+//uncomment for debugging incoming data
+//$action='debug';
+
 $person_id = $_POST['person_id'];      // usually needed
 
 if ( ! $action ) {
-  drupal_set_message ("actions.php: action not set");
+  drupal_set_message ("actions.php: action not set or not in known_actions");
   plc_debug('POST',$_POST);
   return;
  }
@@ -71,8 +104,7 @@ switch ($action) {
  case 'add-person-to-site': {
    $site_id = $_POST['site_id'];
    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'remove-person-from-sites': {
@@ -84,8 +116,7 @@ switch ($action) {
    foreach ( $site_ids as $site_id ) {
      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
    }
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'remove-roles-from-person' : {
@@ -97,41 +128,35 @@ switch ($action) {
    foreach( $role_ids as $role_id)  {
      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
    }
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
      
  case 'add-role-to-person' : {
    $role_id=$_POST['role_id'];
    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'enable-person' : {
    $fields = array( "enabled"=>true );
    $api->UpdatePerson( intval( $person_id ), $fields );
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'disable-person' : {
    $fields = array( "enabled"=>false );
    $api->UpdatePerson( intval( $person_id ), $fields );
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'become-person' : {
    $plc->BecomePerson (intval($person_id));
-   header ("location: " . l_persons());
-   exit();
+   plc_redirect (l_persons());
  }
 
  case 'delete-person' : {
   $api->DeletePerson( intval( $person_id ) );
-  header( "location: " . l_persons() );
-  exit();
+   plc_redirect (l_persons());
  }
 
  case 'delete-keys' : {
@@ -140,13 +165,22 @@ switch ($action) {
      drupal_set_message("action=$action - No key selected");
      return;
    }
+   $success=true;
+   $counter=0;
    foreach( $key_ids as $key_id ) {
-     $api->DeleteKey( intval( $key_id ) );
+     if ($api->DeleteKey( intval( $key_id )) != 1) 
+       $success=false;
+     else
+       $counter++;
    }
-   header( "location: " . l_person($person_id));
-   exit();
+   if ($success) 
+     drupal_set_message ("Deleted $counter key(s)");
+   else
+     drupal_set_error ("Could not delete all selected keys, only $counter were removed");
+   plc_redirect(l_person($person_id));
  }
 
+
  case 'upload-key' : {
    if ( ! isset( $_FILES['key'] ) ) {
      drupal_set_message ("action=$action, no key file set");
@@ -171,81 +205,307 @@ switch ($action) {
    $key = fread($fp, filesize($key_file));
    fclose($fp);
    
-   $key_id= $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
+   $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
+   
+   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());
    
-   if ( ! $key_id ) {
-     $error=  $api->error();
-     plc_error("$error");
-     plc_error("Please verify your SSH  file content");
+   plc_redirect(l_person($person_id));
+ }
+
+ case 'update-person': {
+   $person_id=$_POST['person_id'];
+   // attempt to update this person
+   $first_name= $_POST['first_name'];
+   $last_name= $_POST['last_name'];
+   $title= $_POST['title'];
+   $email= $_POST['email'];
+   $phone= $_POST['phone'];
+   $url= $_POST['url'];
+   $bio= str_replace("\r", "", $_POST['bio']);
+   $password1= $_POST['password1'];
+   $password2= $_POST['password2'];
+
+   if( $password1 != $password2 ) {
+     drupal_set_error ("The passwords do not match");
+     plc_redirect(l_person($person_id));
+  }
+
+   $update_vals= array();
+   $update_vals['first_name']= $first_name;
+   $update_vals['last_name']= $last_name;
+   $update_vals['title']= $title;
+   $update_vals['email']= $email;
+   $update_vals['phone']= $phone;
+   $update_vals['url']= $url;
+   $update_vals['bio']= $bio;
+               
+   if ( $password1 != "" )
+     $update_vals['password']= $password1;
+    
+    if ( $api->UpdatePerson( intval( $person_id ), $update_vals) == 1 )
+      drupal_set_message("$first_name $last_name updated");
+    else 
+      drupal_set_error ("Could not update person $person_id" . $api->error());
+
+    plc_redirect(l_person($person_id));
+    break;
+  }
+
+//////////////////////////////////////////////////////////// nodes
+ case 'node-boot-state': {
+   $node_id=intval($_POST['node_id']);
+   $boot_state=$_POST['boot_state'];
+   $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
+   if ($result==1) {
+     drupal_set_message("boot state updated");
+     plc_redirect (l_node($node_id));
+   } else {
+     drupal_set_error("Could not set boot_state '$boot_state'");
+   }
+   break;
+ }
+
+ case 'delete-node': {
+   $node_id=intval($_POST['node_id']);
+   $result=$api->DeleteNode( intval( $node_id ) );
+   if ($api==1) {
+     drupal_set_message("Node $node_id deleted");
+     plc_redirect (l_nodes());
+   } else {
+     drupal_set_error ("Could not delete node $node_id");
+   }
+   break;
+ }
+
+ case 'update-node': {
+   $hostname= $_POST['hostname'];
+   $model= $_POST['model'];
+
+   $fields= array( "hostname"=>$hostname, "model"=>$model );
+   $api->UpdateNode( intval( $node_id ), $fields );
+   $error= $api->error();
+
+   if( empty( $error ) ) {
+     drupal_set_message("Update node $hostname");
+     plc_redirect(l_node($node_id));
+   } else {
+     drupal_set_error($error);
+   }
+   break;
+ }
+
+//////////////////////////////////////////////////////////// interfaces
+ case 'delete-interfaces' : {
+   $interface_ids=$_POST['interface_ids'];
+   if ( ! $interface_ids) {
+     drupal_set_message("action=$action - No interface selected");
      return;
    }
-   header( "location: " . l_person($person_id));
-   exit();
+   $success=true;
+   $counter=0;
+   foreach( $interface_ids as $interface_id ) {
+     if ($api->DeleteInterface( intval( $interface_id )) != 1) 
+       $success=false;
+     else
+       $counter++;
+   }
+   if ($success) 
+     drupal_set_message ("Deleted $counter interface(s)");
+   else
+     drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
+   plc_redirect(l_node($_POST['node_id']));
+ }
+
+
+
+//////////////////////////////////////////////////////////// sites
+ case 'delete-site': {
+   $site_id = intval($_POST['site_id']);
+   if ($api->DeleteSite($site_id) ==1) 
+     drupal_set_message ("Site $site_id deleted");
+   else
+     drupal_set_error("Failed to delete site $site_id");
+   plc_redirect (l_sites());
+   break;
  }
 
+ case 'expire-all-slices-in-site': {
+   // xxx todo
+   drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
+   return;
+
+   //// old code from sites/expire.php
+   $sites = $api->GetSites( array( intval( $site_id )));
+   $site=$sites[0];
+   // xxx why not 'now?'
+   $expiration= strtotime( $_POST['expires'] );
+   // loop through all slices for site
+   foreach ($site['slice_ids'] as $slice_id) {
+     $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
+   }
+   // update site to not allow slice creation or renewal
+   $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
+   plc_redirect (l_site($site_id));
+   break;
+ }
+
+ case 'update-site': {
+   $site_id=intval($_POST['site_id']);
+   $name= $_POST['name'];
+   $abbreviated_name= $_POST['abbreviated_name'];
+   $url= $_POST['url'];
+   $latitude= floatval($_POST['latitude']);
+   $longitude= floatval($_POST['longitude']);
+   //$max_slivers= $_POST['max_slivers'];
+   
+   $fields= array( "name" => $name, 
+                  "abbreviated_name" => $abbreviated_name, 
+                  "url" => $url, 
+                  "latitude" => floatval( $latitude ), 
+                  "longitude" => floatval( $longitude ));
+
+   if ($_POST['login_base']) 
+     $fields['login_base'] = $_POST['login_base'];
+   if ($_POST['max_slices']) 
+     $fields['max_slices'] = intval($_POST['max_slices']);
+   
+   $retcod=$api->UpdateSite( intval( $site_id ), $fields );
+   if ($retcod == 1) 
+     drupal_set_message("Site $name updated");
+   else 
+     drupal_set_error ("Could not update site $site_id");
+     
+   plc_redirect(l_site($site_id));
+   break;
+ }
+
+//////////////////////////////////////////////////////////// tags
+
  case 'update-tag-type': {
   // get post vars 
    $tag_type_id= intval( $_POST['tag_type_id'] );
-   $name = $_POST['name'];
+   $tagname = $_POST['tagname'];
    $min_role_id= intval( $_POST['min_role_id'] );
    $description= $_POST['description'];  
    $category= $_POST['category'];  
   
    // make tag_type_fields dict
    $tag_type_fields= array( "min_role_id" => $min_role_id, 
-                           "tagname" => $name, 
+                           "tagname" => $tagname, 
                            "description" => $description,
                            "category" => $category,
                            );
 
-   // Update it!
-   $api->UpdateTagType( $tag_type_id, $tag_type_fields );
-   
-   header( "location: " . l_tag($tag_type_id));
-   exit();
+   if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
+     drupal_set_message ("Tag type $tagname updated");
+   else 
+     drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
+   plc_redirect(l_tag($tag_type_id));
  }
 
  case 'add-tag-type': {
   // get post vars 
-   $name = $_POST['name'];
+   $tagname = $_POST['tagname'];
    $min_role_id= intval( $_POST['min_role_id'] );
    $description= $_POST['description'];  
    $category= $_POST['category'];  
   
    // make tag_type_fields dict
    $tag_type_fields= array( "min_role_id" => $min_role_id, 
-                           "tagname" => $name, 
+                           "tagname" => $tagname, 
                            "description" => $description,
                            "category" => $category,
                            );
 
   // Add it!
-   $id=$api->AddTagType( $tag_type_fields );
-   drupal_set_message ("tag type $id created");
-  
-   header( "location: " . l_tag($id));
-   exit();
+   $tag_type_id=$api->AddTagType( $tag_type_fields );
+   if ($tag_type_id > 0) 
+     drupal_set_message ("tag type $tag_type_id created");
+   else
+     drupal_set_error ("Could not create tag type $tagname");
+   plc_redirect( l_tags());
  }
 
- case 'expire-all-slices-in-site': {
-   drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
-   return;
+ case 'set-tag-on-node': {
 
-   //// old code from sites/expire.php
-   $sites = $api->GetSites( array( intval( $site_id )));
-   $site=$sites[0];
-   // xxx why not 'now?'
-   $expiration= strtotime( $_POST['expires'] );
-   // loop through all slices for site
-   foreach ($site['slice_ids'] as $slice_id) {
-     $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
+   $node_id = intval($_POST['node_id']);
+   $tag_type_id = intval($_POST['tag_type_id']);
+   $value = $_POST['value'];
+
+   $tag_types=$api->GetTagTypes(array($tag_type_id));
+   if (count ($tag_types) != 1) {
+     drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
+   } else {
+     $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
+     if ( count ($tags) == 1) {
+       $tag=$tags[0];
+       $tag_id=$tag['node_tag_id'];
+       $result=$api->UpdateNodeTag($tag_id,$value);
+       if ($result == 1) 
+        drupal_set_message ("Updated tag, new value = $value");
+       else
+        drupal_set_error ("Could not update tag");
+     } else {
+       $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
+       if ($tag_id) 
+        drupal_set_message ("Created tag, new value = $value");
+       else
+        drupal_set_error ("Could not create tag");
+     }
    }
-   // update site to not allow slice creation or renewal
-   $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
-   header ("location: " l_site($site_id));
-   exit(0);
+   
+   plc_redirect (l_node($node_id));
  }
 
+ case 'delete-tag-types': {
+   $tag_type_ids = $_POST['tag_type_ids'];
+   if ( ! $tag_type_ids) {
+     drupal_set_message("action=$action - No tag selected");
+     return;
+   }
+   $success=true;
+   $counter=0;
+   foreach ($tag_type_ids as $tag_type_id) 
+     if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
+       $success=false;
+     else
+       $counter++;
+   if ($success) 
+     drupal_set_message ("Deleted $counter tag(s)");
+   else
+     drupal_set_error ("Could not delete all selected tags, only $counter were removed");
+   plc_redirect (l_tags());
+   break;
+ }
+
+//////////////////////////////////////// node tags   
+ case 'delete-node-tags' : {
+   $node_tag_ids=$_POST['node_tag_ids'];
+   if ( ! $node_tag_ids) {
+     drupal_set_message("action=$action - No node tag selected");
+     return;
+   }
+   $success=true;
+   $counter=0;
+   foreach( $node_tag_ids as $node_tag_id ) {
+     if ($api->DeleteNodeTag( intval( $node_tag_id )) != 1) 
+       $success=false;
+     else
+       $counter++;
+   }
+   if ($success) 
+     drupal_set_message ("Deleted $counter node tag(s)");
+   else
+     drupal_set_error ("Could not delete all selected node tags, only $counter were removed");
+   plc_redirect(l_node($_POST['node_id']));
+ }
+
+
+////////////////////////////////////////
+
  case 'debug': {
    plc_debug('GET',$_GET);
    plc_debug('POST',$_POST);