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