reservations now effective
[plewww.git] / planetlab / common / actions.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 //print header
11 require_once 'plc_drupal.php';
12
13 // Common functions
14 require_once 'plc_functions.php';
15
16 $known_actions=array();
17 ////////////////////////////////////////////////////////////
18 // interface :
19 // (*) use POST 
20 // (*) set 'action' to one of the following
21 //////////////////////////////////////// persons
22 $known_actions []= "add-person-to-site";
23 //      expects:        person_id & site_id
24 $known_actions []= "remove-person-from-sites";
25 //      expects:        person_id & site_ids
26 $known_actions []= "remove-roles-from-person";
27 //      expects:        person_id & role_ids
28 $known_actions []= "add-role-to-person";
29 //      expects:        role_person_id & id
30 $known_actions []= "enable-person";
31 //      expects:        person_id
32 $known_actions []= "disable-person";
33 //      expects:        person_id
34 $known_actions []= "become-person";
35 //      expects:        person_id
36 $known_actions []= "delete-person";
37 //      expects:        person_id
38 $known_actions []= "delete-keys";
39 //      expects:        key_ids & person_id (for redirecting to the person's page)
40 $known_actions []= "upload-key";
41 //      expects:        person_id & $_FILES['key']
42 $known_actions []= "update-person";
43 //      expects:        person_id & first_name last_name title email phone url bio + [password1 password2]
44
45 //////////////////////////////////////// nodes
46 $known_actions []= "node-boot-state";   
47 //      expects:        node_id boot_state
48 $known_actions []= "delete-node";       
49 //      expects:        node_id
50 $known_actions []= "update-node";       
51 //      expects:        node_id, hostname, model
52 $known_actions []= "attach-pcu";
53 //      expects:        node_id, pcu_id, port (pcu_id <0 means detach)
54
55 //////////////////////////////////////// interfaces
56 $known_actions []= "delete-interfaces"; 
57 //      expects:        interface_ids
58 $known_actions []="add-interface";
59 //      expects:        node_id & interface details
60 $known_actions []="new-interface";
61 //      expects:        node_id 
62 $known_actions []="update-interface";
63 //      expects:        interface_id & interface details
64
65 //////////////////////////////////////// sites
66 $known_actions []= "delete-site";       
67 //      expects:        site_id
68 $known_actions []= "expire-all-slices-in-site";
69 //      expects:        slice_ids
70 $known_actions []= "update-site";
71 //      expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
72
73 //////////////////////////////////////// slices
74 $known_actions []= "delete-slice";
75 //      expects:        slice_id
76 $known_actions []= "update-slice";      
77 //      expects:        slice_id, name, description, url
78 $known_actions []= "renew-slice";
79 //      expects:        slice_id & expires
80 $known_actions []= 'remove-persons-from-slice';
81 //      expects:        slice_id & person_ids
82 $known_actions []= 'add-persons-in-slice';
83 //      expects:        slice_id & person_ids
84 $known_actions []= 'remove-nodes-from-slice';
85 //      expects:        slice_id & node_ids
86 $known_actions []= 'add-nodes-in-slice';
87 //      expects:        slice_id & node_ids
88 $known_actions []= 'delete-slice-tags';
89 //      expects:        slice_tag_id
90 $known_actions []= 'add-slice-tag';
91 //      expects:        slice_id & tag_type_id & node_id & nodegroup_id
92
93 //////////////////////////////////////// tag types
94 $known_actions []= "update-tag-type";
95 //      expects:        tag_type_id & name & description & category & min_role_id  
96 $known_actions []= "add-tag-type";
97 //      expects:        tag_type_id & tagname & description & category & min_role_id  
98 $known_actions []= "delete-tag-types";
99 //      expects:        tag_type_ids
100
101 //////////////////////////////////////// tags
102 $known_actions []= "set-tag-on-node";
103 //      expects:        node_id tagname value
104 $known_actions []= "set-tag-on-interface";
105 //      expects:        interface_id tagname value
106 $known_actions []= "delete-node-tags";
107 //      expects:        node_id & node_tag_ids
108 $known_actions []= "delete-interface-tags";
109 //      expects:        interface_id & interface_tag_ids
110
111 //////////////////////////////////////// nodegroups
112 $known_actions []= "update-nodegroup";
113 //      expects nodegroup_id groupname value
114 $known_actions []= "add-nodegroup";
115 //      expects groupname, tag_type_id, value
116 $known_actions []= 'delete-nodegroups';
117 //      expects nodegroup_ids
118
119 //////////////////////////////////////// leases
120 $known_actions []= "manage-leases";
121 //      expects as 'actions' a list of 'action' of the form
122 //      either [ 'add-leases', [nodenames], slicename, t_from, t_until ]
123 //      or     [ 'delete-leases', lease_id ]
124
125 ////////////////////////////////////////////////////////////
126 $interface_details= array ('method','type', 'ip', 'gateway', 'network', 
127                            'broadcast', 'netmask', 'dns1', 'dns2', 
128                            'hostname', 'mac', 'bwlimit' );
129
130 //////////////////////////////
131 // sometimes we don't set 'action', but use the submit button name instead
132 // so if 'action' not set, see if $_POST has one of the actions as a key
133 if ($_POST['action']) 
134   $action=$_POST['action'];
135 else 
136   foreach ($known_actions as $known_action) 
137     if ($_POST[$known_action]) {
138       $action=$known_action;
139       break;
140     }
141
142 //uncomment for debugging incoming data
143 //$action='debug';
144
145 $person_id = $_POST['person_id'];       // usually needed
146
147 if ( ! $action ) {
148   drupal_set_message ("actions.php: action not set or not in known_actions");
149   plc_debug('POST',$_POST);
150   return;
151  }
152
153 switch ($action) {
154
155  case 'add-person-to-site': {
156    $site_id = $_POST['site_id'];
157    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
158    plc_redirect (l_person($person_id));
159  }
160
161  case 'remove-person-from-sites': {
162    $site_ids = $_POST['site_ids'];
163    if ( ! $site_ids) {
164      drupal_set_message("action=$action - No site selected");
165      return;
166    }
167    foreach ( $site_ids as $site_id ) {
168      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
169    }
170    plc_redirect (l_person($person_id));
171  }
172
173  case 'remove-roles-from-person' : {
174    $role_ids=$_POST['role_ids'];
175    if ( ! $role_ids) {
176      drupal_set_error("You have not selected role(s) to remove");
177    } else {
178      foreach( $role_ids as $role_id)  
179        if ( $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) ) != 1 ) 
180          drupal_set_error ("Could not remove role $role_id from person $person_id");
181    }
182    plc_redirect (l_person_roles($person_id));
183  }
184      
185  case 'add-role-to-person' : {
186    $role_id=$_POST['role_id'];
187    if ( ! $role_id) {
188      drupal_set_error ("You have not selected a role to add");
189    } else if ($api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) ) != 1) {
190      drupal_set_error("Could not add role $role_id to person $person_id");
191    }
192    plc_redirect (l_person_roles($person_id));
193  }
194
195  case 'enable-person' : {
196    $fields = array( "enabled"=>true );
197    $api->UpdatePerson( intval( $person_id ), $fields );
198    plc_redirect (l_person($person_id));
199  }
200
201  case 'disable-person' : {
202    $fields = array( "enabled"=>false );
203    $api->UpdatePerson( intval( $person_id ), $fields );
204    plc_redirect (l_person($person_id));
205  }
206
207  case 'become-person' : {
208    $plc->BecomePerson (intval($person_id));
209    plc_redirect (l_person(intval($person_id)));
210  }
211
212  case 'delete-person' : {
213   $api->DeletePerson( intval( $person_id ) );
214    plc_redirect (l_persons());
215  }
216
217  case 'delete-keys' : {
218    $key_ids=$_POST['key_ids'];
219    if ( ! $key_ids) {
220      drupal_set_message("action=$action - No key selected");
221      return;
222    }
223    $success=true;
224    $counter=0;
225    foreach( $key_ids as $key_id ) {
226      if ($api->DeleteKey( intval( $key_id )) != 1) 
227        $success=false;
228      else
229        $counter++;
230    }
231    if ($success) 
232      drupal_set_message ("Deleted $counter key(s)");
233    else
234      drupal_set_error ("Could not delete all selected keys, only $counter were removed");
235    plc_redirect(l_person($person_id));
236  }
237
238
239  case 'upload-key' : {
240    if ( ! isset( $_FILES['key'] ) ) {
241      drupal_set_message ("action=$action, no key file set");
242      return;
243    }
244    
245    $key_file= $_FILES['key']['tmp_name'];
246    if ( ! $key_file ) {
247      plc_error("Please select a valid SSH key file to upload");
248      return;
249    } 
250    $fp = fopen( $key_file, "r" );
251    $key = "";
252    if( ! $fp ) {
253      plc_error("Unable to open key file $key_file");
254      return;
255    }
256    // opened the key file, read the one line of contents
257    // The POST operation always creates a file even if the filename
258    // the user specified was garbage.  If there was some problem
259    // with the source file, we'll get a zero length read here.
260    $key = fread($fp, filesize($key_file));
261    fclose($fp);
262    
263    $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
264    
265    if ( $key_id >= 1) 
266      drupal_set_message ("New key added");
267    else
268      drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
269    
270    plc_redirect(l_person($person_id));
271  }
272
273  case 'update-person': {
274    $person_id=$_POST['person_id'];
275    // attempt to update this person
276    $first_name= $_POST['first_name'];
277    $last_name= $_POST['last_name'];
278    $title= $_POST['title'];
279    $email= $_POST['email'];
280    $phone= $_POST['phone'];
281    $url= $_POST['url'];
282    $bio= str_replace("\r", "", $_POST['bio']);
283    $password1= $_POST['password1'];
284    $password2= $_POST['password2'];
285
286    if( $password1 != $password2 ) {
287      drupal_set_error ("The passwords do not match");
288      plc_redirect(l_person($person_id));
289   }
290
291    $fields= array();
292    $fields['first_name']= $first_name;
293    $fields['last_name']= $last_name;
294    $fields['title']= $title;
295    $fields['email']= $email;
296    $fields['phone']= $phone;
297    $fields['url']= $url;
298    $fields['bio']= $bio;
299                 
300    if ( $password1 != "" )
301      $fields['password']= $password1;
302     
303     if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
304       drupal_set_message("$first_name $last_name updated");
305     else 
306       drupal_set_error ("Could not update person $person_id" . $api->error());
307
308     plc_redirect(l_person($person_id));
309     break;
310   }
311
312 //////////////////////////////////////////////////////////// nodes
313  case 'node-boot-state': {
314    $node_id=intval($_POST['node_id']);
315    $boot_state=$_POST['boot_state'];
316    $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
317    if ($result==1) {
318      drupal_set_message("boot state updated");
319      plc_redirect (l_node($node_id));
320    } else {
321      drupal_set_error("Could not set boot_state '$boot_state'");
322    }
323    break;
324  }
325
326  case 'delete-node': {
327    $node_id=intval($_POST['node_id']);
328    $result=$api->DeleteNode( intval( $node_id ) );
329    if ($api==1) {
330      drupal_set_message("Node $node_id deleted");
331      plc_redirect (l_nodes());
332    } else {
333      drupal_set_error ("Could not delete node $node_id");
334    }
335    break;
336  }
337
338  case 'update-node': {
339    $node_id=intval($_POST['node_id']);
340    $hostname= $_POST['hostname'];
341    $model= $_POST['model'];
342
343    $fields= array( "hostname"=>$hostname, "model"=>$model );
344    $api->UpdateNode( $node_id, $fields );
345    $error= $api->error();
346
347    if( empty( $error ) ) {
348      drupal_set_message("Update node $hostname");
349      plc_redirect(l_node($node_id));
350    } else {
351      drupal_set_error($error);
352    }
353    break;
354  }
355
356  // this code will ensure that at most one PCU gets attached to the node
357  case 'attach-pcu': {
358    $node_id=intval($_POST['node_id']);
359    $pcu_id=intval($_POST['pcu_id']);
360    $port=intval($_POST['port']);
361    // always start with deleting former PCUs
362    $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
363    $former_pcu_ids = $nodes[0]['pcu_ids'];
364    if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
365        if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1) 
366          drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
367        else 
368          drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
369      }
370    // re-attach only if provided pcu_id >=0
371    if ($pcu_id >= 0) {
372      if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
373        drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
374      else
375        drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
376    } else {
377      drupal_set_message ('Detached node from all PCUs');
378    }
379    
380    plc_redirect(l_node($node_id));
381    break;
382  }
383    
384
385 //////////////////////////////////////////////////////////// interfaces
386  case 'delete-interfaces' : {
387    $interface_ids=$_POST['interface_ids'];
388    if ( ! $interface_ids) {
389      drupal_set_message("action=$action - No interface selected");
390      return;
391    }
392    $success=true;
393    $counter=0;
394    foreach( $interface_ids as $interface_id ) {
395      if ($api->DeleteInterface( intval( $interface_id )) != 1) 
396        $success=false;
397      else
398        $counter++;
399    }
400    if ($success) 
401      drupal_set_message ("Deleted $counter interface(s)");
402    else
403      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
404    plc_redirect(l_node($_POST['node_id']));
405  }
406
407  case 'new-interface': {
408    plc_redirect(l_interface_add($_POST['node_id']));
409  }
410
411  case 'add-interface': {
412    $node_id=intval($_POST['node_id']);
413    foreach ($interface_details as $field) {
414      $interface[$field]= $_POST[$field];
415      // these must be integers
416      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
417        if ( empty ($interface[$field]) ) 
418          unset ($interface[$field]);
419        else 
420          $interface[$field]= intval( $interface[$field] );
421      }
422    }
423    $interface_id =$api->AddInterface( $node_id , $interface );
424    if ($interface_id <= 0 ) {
425      drupal_set_error ("Could not create interface");
426      drupal_set_error ($api->error());
427    } else {
428      $ip=$interface['ip'];
429      drupal_set_message ("Interface $ip added into node $node_id");
430      if ($_POST['is-virtual']) {
431        $ifname=$_POST['ifname'];
432        if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0) 
433          drupal_set_error ("Could not set tag 'ifname'=$ifname");
434        else 
435          drupal_set_message ("Set tag 'ifname'=$ifname");
436        $alias=$_POST['alias'];
437        // deafult to interface_id
438        if ( ! $alias ) $alias=strval($interface_id);
439        if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0) 
440          drupal_set_error ("Could not set tag 'alias'=$alias");
441        else 
442          drupal_set_message ("Set tag 'alias'=$alias");
443      }
444    }
445    plc_redirect (l_node_interfaces($node_id));
446  }
447    
448  case 'update-interface': {
449    $interface_id=$_POST['interface_id'];
450    foreach ($interface_details as $field) {
451      $interface[$field]= $_POST[$field];
452      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
453        if ( intval($interface[$field]) != 0 ) {
454            $interface[$field]= intval( $interface[$field]);
455        } elseif ($field=='bwlimit' ) {
456            $interface[$field] = NULL;
457        }
458      }
459    }
460    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
461    if ($result == 1 ) 
462      drupal_set_message ("Interface $interface_id updated");
463    else
464      drupal_set_error ("Could not update interface: " . $api->error());
465    plc_redirect (l_interface($interface_id));
466  }
467    
468 //////////////////////////////////////////////////////////// sites
469  case 'delete-site': {
470    $site_id = intval($_POST['site_id']);
471    if ($api->DeleteSite($site_id) ==1) 
472      drupal_set_message ("Site $site_id deleted");
473    else
474      drupal_set_error("Failed to delete site $site_id");
475    plc_redirect (l_sites());
476    break;
477  }
478
479  case 'expire-all-slices-in-site': {
480    // xxx todo
481    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
482    return;
483
484    //// old code from sites/expire.php
485    $sites = $api->GetSites( array( intval( $site_id )));
486    $site=$sites[0];
487    // xxx why not 'now?'
488    $expiration= strtotime( $_POST['expires'] );
489    // loop through all slices for site
490    foreach ($site['slice_ids'] as $slice_id) {
491      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
492    }
493    // update site to not allow slice creation or renewal
494    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
495    plc_redirect (l_site($site_id));
496    break;
497  }
498
499  case 'update-site': {
500    $site_id=intval($_POST['site_id']);
501    $name= $_POST['name'];
502    $abbreviated_name= $_POST['abbreviated_name'];
503    $url= $_POST['url'];
504    $latitude= floatval($_POST['latitude']);
505    $longitude= floatval($_POST['longitude']);
506    //$max_slivers= $_POST['max_slivers'];
507    
508    $fields= array( "name" => $name, 
509                    "abbreviated_name" => $abbreviated_name, 
510                    "url" => $url, 
511                    "latitude" => floatval( $latitude ), 
512                    "longitude" => floatval( $longitude ));
513
514    if ($_POST['login_base']) 
515      $fields['login_base'] = $_POST['login_base'];
516    if (isset($_POST['max_slices']))
517      $fields['max_slices'] = intval($_POST['max_slices']);
518    if (isset($_POST['enabled'])) {
519      $fields['enabled'] = (bool)$_POST['enabled'];
520    }
521    
522    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
523    if ($retcod == 1) 
524      drupal_set_message("Site $name updated");
525    else 
526      drupal_set_error ("Could not update site $site_id");
527      
528    plc_redirect(l_site($site_id));
529    break;
530  }
531
532 //////////////////////////////////////////////////////////// slices
533  case 'delete-slice': {
534    $slice_id = $_POST['slice_id'];
535    if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
536      drupal_set_message("Slice $slice_id deleted");
537      plc_redirect(l_slices());
538    } else {
539      drupal_set_error("Could not delete slice $slice_id " . $api->error());
540    }
541    break;
542  }
543      
544  case 'update-slice': {
545    $slice_id = $_POST['slice_id'];
546    $name = $_POST['name'];
547    $description= $_POST['description'];
548    $url= $_POST['url'];
549
550    $fields= array( "description"=>$description, "url"=>$url );
551    $api->UpdateSlice( intval( $slice_id ), $fields );
552    $error= $api->error();
553
554    if( empty( $error ) ) {
555      drupal_set_message("Update slice $name");
556      plc_redirect(l_slice($slice_id));
557    } else {
558      drupal_set_error($error);
559    }
560    break;
561  }
562
563  case 'renew-slice': {
564    $slice_id = intval ($_POST['slice_id']);     
565    $expires = intval ($_POST['expires']);
566    // 8 weeks from now
567    // xxx
568    $now=mktime();
569    $WEEK=7*24*3600;
570    $WEEKS=8;
571    $MAX_FUTURE=$WEEKS*$WEEK;
572    if ( ($expires-$now) > $MAX_FUTURE) {
573      drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
574      plc_redirect(l_slice($slice_id));
575    }
576    if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
577      drupal_set_message("Slice renewed");
578    else
579      drupal_set_error("Could not update slice $slice_id");
580    plc_redirect(l_slice($slice_id));
581    break;
582  }
583
584  case 'remove-persons-from-slice': {
585    $slice_id = intval ($_POST['slice_id']);     
586    $person_ids = $_POST['person_ids'];
587    
588    $slice_name = "";
589    $tmp_slices = $api->GetSlices($slice_id, array("name"));
590    if (count($tmp_slices) > 0) {
591        $tmp_slice = $tmp_slices[0];
592        $slice_name = $tmp_slice["name"];
593    }
594    $notify_subject = "Removed from slice: " . $slice_name;
595    $notify_body = sprintf("You have been removed from the slice %s.
596
597 Our support team will be glad to answer any question that you might have.
598 ",$slice_name);
599    $notify_person_ids = array();
600    
601    $success=true;
602    $counter=0;
603    foreach( $person_ids as $person_id ) {
604      if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1) 
605        $success=false;
606      else {
607          array_push($notify_person_ids, intval($person_id));
608        $counter++;
609      }
610    }
611    if ($success) {
612      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
613      drupal_set_message ("Deleted $counter person(s)");
614    }
615    else
616      drupal_set_error ("Could not delete all selected persons, only $counter were removed");
617    plc_redirect(l_slice($slice_id) . " &show_persons=true");
618    break;
619  }
620
621  case 'add-persons-in-slice': {
622    $slice_id = intval ($_POST['slice_id']);     
623    $person_ids = $_POST['person_ids'];
624
625    $slice_name = "";
626    $tmp_slices = $api->GetSlices($slice_id, array("name"));
627    if (count($tmp_slices) > 0) {
628      $tmp_slice = $tmp_slices[0];
629      $slice_name = $tmp_slice["name"];
630    }
631    $notify_subject = "Added to slice: " . $slice_name;
632    $notify_body = sprintf("You have been added to the slice %s as a user.
633
634 You can go to your slice page following the link below:
635 https://%s:%d/db/slices/index.php?id=%d
636
637 Our support team will be glad to answer any question that you might have.
638 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
639    $notify_person_ids = array();
640    
641    $success=true;
642    $counter=0;
643    foreach ($person_ids as $person_id) {
644      if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1) 
645        $success=false;
646      else {
647        array_push($notify_person_ids, intval($person_id));
648        $counter++;
649      }
650    }
651    if ($success) {
652      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
653      drupal_set_message ("Added $counter person(s)");
654    }
655    else
656      drupal_set_error ("Could not add all selected persons, only $counter were added");
657    plc_redirect(l_slice($slice_id) . "&show_persons=true" );
658    break;
659  }
660
661  case 'remove-nodes-from-slice': {
662    $slice_id = intval ($_POST['slice_id']);     
663    $node_ids = array_map("intval",$_POST['node_ids']);
664    $count=count($node_ids);
665    
666    if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1) 
667      drupal_set_message ("Removed $count node(s)");
668    else
669      drupal_set_error ("Could not remove selected nodes");
670    plc_redirect(l_slice_nodes($slice_id));
671    break;
672  }
673
674  case 'add-nodes-in-slice': {
675    $slice_id = intval ($_POST['slice_id']);     
676    $node_ids = array_map("intval",$_POST['node_ids']);
677    $count=count($node_ids);
678    if ($api->AddSliceToNodes($slice_id,$node_ids) == 1) 
679      drupal_set_message ("Added $count node(s)");
680    else
681      drupal_set_error ("Could not add all selected nodes");
682    plc_redirect(l_slice_nodes($slice_id));
683    break;
684  }
685
686  case 'delete-slice-tags': {
687    $slice_id = intval($_POST['slice_id']);
688    $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
689    $count = 0;
690    $success = true;
691    foreach($slice_tag_ids as $slice_tag_id) {
692      if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
693      else {
694        drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
695        $success = false;
696      }
697    }
698    if ($success)
699      drupal_set_message ("Deleted $count slice tag(s)");
700    plc_redirect(l_slice($slice_id) . "&show_tags=true" );
701    break;
702  }
703   
704  case 'add-slice-tag': {
705    $slice_id = intval($_POST['slice_id']);
706    $tag_type_id = intval($_POST['tag_type_id']);
707    $value = $_POST['value'];
708    $node_id = intval($_POST['node_id']);
709    $nodegroup_id = intval($_POST['nodegroup_id']);
710   
711    $result = null;
712    if ($node_id) {
713      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
714    } elseif ($nodegroup_id) {
715      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
716    } else {
717      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
718    }
719    if ($result)
720      drupal_set_message ("Added slice tag.");
721    else 
722        drupal_set_error("Could not add slice tag");
723    if ($_POST['sliver_action'])
724        plc_redirect(l_sliver($node_id,$slice_id));
725    else
726        plc_redirect(l_slice($slice_id) . "&show_tags=true" );
727    break;
728  }
729
730 //////////////////////////////////////////////////////////// tag types
731
732  case 'update-tag-type': {
733   // get post vars 
734    $tag_type_id= intval( $_POST['tag_type_id'] );
735    $tagname = $_POST['tagname'];
736    $min_role_id= intval( $_POST['min_role_id'] );
737    $description= $_POST['description'];  
738    $category= $_POST['category'];
739   
740    // make tag_type_fields dict
741    $tag_type_fields= array( "min_role_id" => $min_role_id, 
742                             "tagname" => $tagname, 
743                             "description" => $description,
744                             "category" => $category,
745                             );
746
747    if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
748      drupal_set_message ("Tag type $tagname updated");
749    else 
750      drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
751    plc_redirect(l_tag($tag_type_id));
752    break;
753  }
754
755  case 'add-tag-type': {
756   // get post vars 
757    $tagname = $_POST['tagname'];
758    $min_role_id= intval( $_POST['min_role_id'] );
759    $description= $_POST['description'];  
760    $category= $_POST['category'];  
761   
762    // make tag_type_fields dict
763    $tag_type_fields= array( "min_role_id" => $min_role_id, 
764                             "tagname" => $tagname, 
765                             "description" => $description,
766                             "category" => $category,
767                             );
768
769   // Add it!
770    $tag_type_id=$api->AddTagType( $tag_type_fields );
771    if ($tag_type_id > 0) 
772      drupal_set_message ("tag type $tag_type_id created");
773    else
774      drupal_set_error ("Could not create tag type $tagname");
775    plc_redirect( l_tags());
776    break;
777  }
778
779  case 'delete-tag-types': {
780    $tag_type_ids = $_POST['tag_type_ids'];
781    if ( ! $tag_type_ids) {
782      drupal_set_message("action=$action - No tag selected");
783      return;
784    }
785    $success=true;
786    $counter=0;
787    foreach ($tag_type_ids as $tag_type_id) 
788      if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
789        $success=false;
790      else
791        $counter++;
792    if ($success) 
793      drupal_set_message ("Deleted $counter tag(s)");
794    else
795      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
796    plc_redirect (l_tags());
797    break;
798  }
799
800 //////////////////////////////////////// tags   
801  case 'set-tag-on-node': 
802  case 'set-tag-on-interface': {
803    
804    $node_mode = false;
805    if ($action == 'set-tag-on-node') $node_mode=true;
806
807    if ($node_mode)
808      $node_id = intval($_POST['node_id']);
809    else 
810      $interface_id=intval($_POST['interface_id']);
811    $tag_type_id = intval($_POST['tag_type_id']);
812    $value = $_POST['value'];
813
814    $tag_types=$api->GetTagTypes(array($tag_type_id));
815    if (count ($tag_types) != 1) {
816      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
817    } else {
818      if ($node_mode) 
819        $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
820      else
821        $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
822      if ( count ($tags) == 1) {
823        $tag=$tags[0];
824        if ($node_mode) {
825          $tag_id=$tag['node_tag_id'];
826          $result=$api->UpdateNodeTag($tag_id,$value);
827        } else {
828          $tag_id=$tag['interface_tag_id'];
829          $result=$api->UpdateInterfaceTag($tag_id,$value);
830        }
831        if ($result == 1) 
832          drupal_set_message ("Updated tag, new value = $value");
833        else
834          drupal_set_error ("Could not update tag");
835      } else {
836        if ($node_mode)
837          $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
838        else
839          $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
840        if ($tag_id) 
841          drupal_set_message ("Created tag, new value = $value");
842        else
843          drupal_set_error ("Could not create tag");
844      }
845    }
846    
847    if ($node_mode)
848      plc_redirect (l_node_tags($node_id));
849    else
850      plc_redirect (l_interface_tags($interface_id));
851  }
852
853  case 'delete-node-tags' : 
854  case 'delete-interface-tags' : {
855
856    $node_mode = false;
857    if ($action == 'delete-node-tags') $node_mode=true;
858
859    if ($node_mode)
860      $tag_ids=$_POST['node_tag_ids'];
861    else
862      $tag_ids=$_POST['interface_tag_ids'];
863
864    if ( ! $tag_ids) {
865      drupal_set_message("action=$action - No tag selected");
866      return;
867    }
868    $success=true;
869    $counter=0;
870    foreach( $tag_ids as $tag_id ) {
871      if ($node_mode)
872        $retcod = $api->DeleteNodeTag( intval( $tag_id ));
873      else
874        $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
875      if ($retcod != 1) 
876        $success=false;
877      else
878        $counter++;
879    }
880    if ($success) 
881      drupal_set_message ("Deleted $counter tag(s)");
882    else
883      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
884    if ($node_mode)
885      plc_redirect(l_node_tags($_POST['node_id']));
886    else
887      plc_redirect(l_interface_tags($_POST['interface_id']));
888  }
889
890 //////////////////////////////////////// nodegroups
891  case 'update-nodegroup': {
892    $nodegroup_id = $_POST['nodegroup_id'];
893    $groupname = $_POST['groupname'];
894    $value = $_POST['value'];
895
896    $fields=array();
897    $fields['groupname']=$groupname;
898    $fields['value']=$value;
899    if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1) 
900      drupal_set_message ('Nodegroup updated');
901    else 
902      drupal_set_error ("Could not update nodegroup $groupname");
903    
904    plc_redirect(l_nodegroup($nodegroup_id));
905
906  }
907
908  case 'add-nodegroup': {
909    $groupname=$_POST['groupname'];
910    if ( ! $groupname ) {
911      drupal_set_error ('Empty groupname');
912      plc_redirect (l_nodegroups());
913    }
914    $tag_type_id=intval($_POST['tag_type_id']);
915    if ( ! $tag_type_id ) {
916      drupal_set_error ('You must select a tag in the dropdown list');
917      plc_redirect (l_nodegroups());
918    }
919    $value=$_POST['value'];
920    if ( ! $value ) {
921      drupal_set_message ("Empty value.. let's see ..");
922    }
923    if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0) 
924      drupal_set_message ("Nodegroup $groupname created");
925    else
926      drupal_set_error ("Could not create nodegroup $groupname");
927
928    plc_redirect (l_nodegroups());
929    break;
930  }
931
932  case 'delete-nodegroups': {
933    $nodegroup_ids=$_POST['nodegroup_ids'];
934    if ( ! $nodegroup_ids ) {
935      drupal_set_message("action=delete-nodegroups - No group selected");
936      plc_redirect(l_nodegroups());
937    }
938    $success=true;
939    $counter=0;
940    foreach ($nodegroup_ids as $nodegroup_id) 
941      if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1) 
942        $success=false;
943      else
944        $counter++;
945    if ($success) 
946      drupal_set_message ("Deleted $counter group(s)");
947    else
948      drupal_set_error ("Could not delete all selected groups, only $counter were removed");
949    plc_redirect (l_nodegroups());
950    break;
951  }
952
953 //////////////////////////////////////// leases
954  case 'manage-leases': {
955    $actions=json_decode($_POST['actions']);
956    $add_requested=0;
957    $add_done=0;
958    $del_requested=0;
959    $del_done=0;
960    $errors=array();
961    foreach ($actions as $action) {
962      if ($action[0] == 'add-leases') {
963        $nodenames=$action[1];
964        $add_requested += count($nodenames);
965        $slicename=$action[2];
966        $t_from=intval($action[3]);
967        $t_until=intval($action[4]);
968        $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until);
969        // update number of added leases
970        $ids=$hash['new_ids'];
971        $add_done += count($ids);
972        // update global errors array
973        foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error;
974      } else if ($action[0]=='delete-leases') {
975        $lease_id=intval($action[1]);
976        $del_requested += 1;
977        if ($api->DeleteLeases(array($lease_id)) == 1) {
978          $del_done += 1;
979        } else {
980          $errors []= "Could not delete lease " . $lease_id;
981        }
982      } else {
983        $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
984      }
985    }
986    
987    if (count($errors)==0) {
988      echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
989    } else {
990      foreach ($errors as $error) echo($error. "\n");
991      echo("Leases updated only partially (" . 
992           $add_done . "/" . $add_requested . " added and " . 
993           $del_done . "/" . $del_requested . " deleted)");
994    }     
995
996    break;
997  }
998
999 ////////////////////////////////////////
1000
1001  case 'debug': {
1002    plc_debug('GET',$_GET);
1003    plc_debug('POST',$_POST);
1004    plc_debug('FILES',$_FILES);
1005    return;
1006  }
1007
1008  default: {
1009    plc_error ("Unknown action $action in actions.php");
1010    return;
1011  }
1012
1013  }
1014
1015 ?>