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