Add 'test' parameter to RebootNodeWithPCU
[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    $test = $_POST['test'];
397    settype($test, "boolean");
398
399    $ret = $api->RebootNodeWithPCU( $node_id, $test );
400    $error= $api->error();
401
402    if( empty( $error ) ) {
403      drupal_set_message("Reboot node $hostname: $ret");
404      plc_redirect(l_node($node_id));
405    } else {
406      drupal_set_error($error);
407    }
408    break;
409  }
410    
411
412 //////////////////////////////////////////////////////////// interfaces
413  case 'delete-interfaces' : {
414    $interface_ids=$_POST['interface_ids'];
415    if ( ! $interface_ids) {
416      drupal_set_message("action=$action - No interface selected");
417      return;
418    }
419    $success=true;
420    $counter=0;
421    foreach( $interface_ids as $interface_id ) {
422      if ($api->DeleteInterface( intval( $interface_id )) != 1) 
423        $success=false;
424      else
425        $counter++;
426    }
427    if ($success) 
428      drupal_set_message ("Deleted $counter interface(s)");
429    else
430      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
431    plc_redirect(l_node($_POST['node_id']));
432  }
433
434  case 'new-interface': {
435    plc_redirect(l_interface_add($_POST['node_id']));
436  }
437
438  case 'add-interface': {
439    $node_id=intval($_POST['node_id']);
440    foreach ($interface_details as $field) {
441      $interface[$field]= $_POST[$field];
442      // these must be integers
443      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
444        if ( empty ($interface[$field]) ) 
445          unset ($interface[$field]);
446        else 
447          $interface[$field]= intval( $interface[$field] );
448      }
449    }
450    $interface_id =$api->AddInterface( $node_id , $interface );
451    if ($interface_id <= 0 ) {
452      drupal_set_error ("Could not create interface");
453      drupal_set_error ($api->error());
454    } else {
455      $ip=$interface['ip'];
456      drupal_set_message ("Interface $ip added into node $node_id");
457      if ($_POST['is-virtual']) {
458        $ifname=$_POST['ifname'];
459        if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0) 
460          drupal_set_error ("Could not set tag 'ifname'=$ifname");
461        else 
462          drupal_set_message ("Set tag 'ifname'=$ifname");
463        $alias=$_POST['alias'];
464        // deafult to interface_id
465        if ( ! $alias ) $alias=strval($interface_id);
466        if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0) 
467          drupal_set_error ("Could not set tag 'alias'=$alias");
468        else 
469          drupal_set_message ("Set tag 'alias'=$alias");
470      }
471    }
472    plc_redirect (l_node_interfaces($node_id));
473  }
474    
475  case 'update-interface': {
476    $interface_id=$_POST['interface_id'];
477    foreach ($interface_details as $field) {
478      $interface[$field]= $_POST[$field];
479      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
480        if ( intval($interface[$field]) != 0 ) {
481            $interface[$field]= intval( $interface[$field]);
482        } elseif ($field=='bwlimit' ) {
483            $interface[$field] = NULL;
484        }
485      }
486    }
487    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
488    if ($result == 1 ) 
489      drupal_set_message ("Interface $interface_id updated");
490    else
491      drupal_set_error ("Could not update interface: " . $api->error());
492    plc_redirect (l_interface($interface_id));
493  }
494    
495 //////////////////////////////////////////////////////////// sites
496  case 'delete-site': {
497    $site_id = intval($_POST['site_id']);
498    if ($api->DeleteSite($site_id) ==1) 
499      drupal_set_message ("Site $site_id deleted");
500    else
501      drupal_set_error("Failed to delete site $site_id");
502    plc_redirect (l_sites());
503    break;
504  }
505
506  case 'expire-all-slices-in-site': {
507    // xxx todo
508    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
509    return;
510
511    //// old code from sites/expire.php
512    $sites = $api->GetSites( array( intval( $site_id )));
513    $site=$sites[0];
514    // xxx why not 'now?'
515    $expiration= strtotime( $_POST['expires'] );
516    // loop through all slices for site
517    foreach ($site['slice_ids'] as $slice_id) {
518      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
519    }
520    // update site to not allow slice creation or renewal
521    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
522    plc_redirect (l_site($site_id));
523    break;
524  }
525
526  case 'update-site': {
527    $site_id=intval($_POST['site_id']);
528    $name= $_POST['name'];
529    $abbreviated_name= $_POST['abbreviated_name'];
530    $url= $_POST['url'];
531    $latitude= floatval($_POST['latitude']);
532    $longitude= floatval($_POST['longitude']);
533    //$max_slivers= $_POST['max_slivers'];
534    
535    $fields= array( "name" => $name, 
536                    "abbreviated_name" => $abbreviated_name, 
537                    "url" => $url, 
538                    "latitude" => floatval( $latitude ), 
539                    "longitude" => floatval( $longitude ));
540
541    if ($_POST['login_base']) 
542      $fields['login_base'] = $_POST['login_base'];
543    if (isset($_POST['max_slices']))
544      $fields['max_slices'] = intval($_POST['max_slices']);
545    if (isset($_POST['enabled'])) {
546      $fields['enabled'] = (bool)$_POST['enabled'];
547    }
548    
549    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
550    if ($retcod == 1) 
551      drupal_set_message("Site $name updated");
552    else 
553      drupal_set_error ("Could not update site $site_id");
554      
555    plc_redirect(l_site($site_id));
556    break;
557  }
558
559 //////////////////////////////////////////////////////////// slices
560  case 'delete-slice': {
561    $slice_id = $_POST['slice_id'];
562    if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
563      drupal_set_message("Slice $slice_id deleted");
564      plc_redirect(l_slices());
565    } else {
566      drupal_set_error("Could not delete slice $slice_id " . $api->error());
567    }
568    break;
569  }
570      
571  case 'update-slice': {
572    $slice_id = $_POST['slice_id'];
573    $name = $_POST['name'];
574    $description= $_POST['description'];
575    $url= $_POST['url'];
576
577    $fields= array( "description"=>$description, "url"=>$url );
578    $api->UpdateSlice( intval( $slice_id ), $fields );
579    $error= $api->error();
580
581    if( empty( $error ) ) {
582      drupal_set_message("Update slice $name");
583      plc_redirect(l_slice($slice_id));
584    } else {
585      drupal_set_error($error);
586    }
587    break;
588  }
589
590  case 'renew-slice': {
591    $slice_id = intval ($_POST['slice_id']);     
592    $expires = intval ($_POST['expires']);
593    // 8 weeks from now
594    // xxx
595    $now=mktime();
596    $WEEK=7*24*3600;
597    $WEEKS=8;
598    $MAX_FUTURE=$WEEKS*$WEEK;
599    if ( ($expires-$now) > $MAX_FUTURE) {
600      drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
601      plc_redirect(l_slice($slice_id));
602    }
603    if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
604      drupal_set_message("Slice renewed");
605    else
606      drupal_set_error("Could not update slice $slice_id");
607    plc_redirect(l_slice($slice_id));
608    break;
609  }
610
611  case 'remove-persons-from-slice': {
612    $slice_id = intval ($_POST['slice_id']);     
613    $person_ids = $_POST['person_ids'];
614    
615    $slice_name = "";
616    $tmp_slices = $api->GetSlices($slice_id, array("name"));
617    if (count($tmp_slices) > 0) {
618        $tmp_slice = $tmp_slices[0];
619        $slice_name = $tmp_slice["name"];
620    }
621    $notify_subject = "Removed from slice: " . $slice_name;
622    $notify_body = sprintf("You have been removed from the slice %s.
623
624 Our support team will be glad to answer any question that you might have.
625 ",$slice_name);
626    $notify_person_ids = array();
627    
628    $success=true;
629    $counter=0;
630    foreach( $person_ids as $person_id ) {
631      if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1) 
632        $success=false;
633      else {
634          array_push($notify_person_ids, intval($person_id));
635        $counter++;
636      }
637    }
638    if ($success) {
639      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
640      drupal_set_message ("Deleted $counter person(s)");
641    }
642    else
643      drupal_set_error ("Could not delete all selected persons, only $counter were removed");
644    plc_redirect(l_slice($slice_id) . " &show_persons=true");
645    break;
646  }
647
648  case 'add-persons-in-slice': {
649    $slice_id = intval ($_POST['slice_id']);     
650    $person_ids = $_POST['person_ids'];
651
652    $slice_name = "";
653    $tmp_slices = $api->GetSlices($slice_id, array("name"));
654    if (count($tmp_slices) > 0) {
655      $tmp_slice = $tmp_slices[0];
656      $slice_name = $tmp_slice["name"];
657    }
658    $notify_subject = "Added to slice: " . $slice_name;
659    $notify_body = sprintf("You have been added to the slice %s as a user.
660
661 You can go to your slice page following the link below:
662 https://%s:%d/db/slices/index.php?id=%d
663
664 Our support team will be glad to answer any question that you might have.
665 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
666    $notify_person_ids = array();
667    
668    $success=true;
669    $counter=0;
670    foreach ($person_ids as $person_id) {
671      if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1) 
672        $success=false;
673      else {
674        array_push($notify_person_ids, intval($person_id));
675        $counter++;
676      }
677    }
678    if ($success) {
679      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
680      drupal_set_message ("Added $counter person(s)");
681    }
682    else
683      drupal_set_error ("Could not add all selected persons, only $counter were added");
684    plc_redirect(l_slice($slice_id) . "&show_persons=true" );
685    break;
686  }
687
688  case 'remove-nodes-from-slice': {
689    $slice_id = intval ($_POST['slice_id']);     
690    $node_ids = array_map("intval",$_POST['node_ids']);
691    $count=count($node_ids);
692    
693    if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1) 
694      drupal_set_message ("Removed $count node(s)");
695    else
696      drupal_set_error ("Could not remove selected nodes");
697    plc_redirect(l_slice_nodes($slice_id));
698    break;
699  }
700
701  case 'add-nodes-in-slice': {
702    $slice_id = intval ($_POST['slice_id']);     
703    $node_ids = array_map("intval",$_POST['node_ids']);
704    $count=count($node_ids);
705    if ($api->AddSliceToNodes($slice_id,$node_ids) == 1) 
706      drupal_set_message ("Added $count node(s)");
707    else
708      drupal_set_error ("Could not add all selected nodes");
709    plc_redirect(l_slice_nodes($slice_id));
710    break;
711  }
712
713  case 'update-initscripts': {
714 //      expects:        slice_id & name & previous-initscript & previous-initscript-code 
715 //                      & initscript & initscript-code
716    $slice_id = intval ($_POST['slice_id']);     
717    $previous_initscript=$_POST['previous-initscript'];
718    $initscript=$_POST['initscript'];
719    $previous_initscript_code=html_entity_decode($_POST['previous-initscript-code']);
720    $initscript_code=$_POST['initscript-code'];
721
722    $changes=FALSE;
723    if (strcmp($initscript,$previous_initscript) != 0) {
724      $newvalue=$api->SetSliceInitscript($slice_id,$initscript);
725      $status = (strcmp($newvalue,$initscript)==0) ? "OK" : "failed";
726      if (! $initscript)         drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status);
727      else                       drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status);
728      $changes=TRUE;
729    }
730
731    // somehow some \r chars make it here; just ignore them
732    $previous_initscript_code=str_replace("\r","",$previous_initscript_code);
733    //   plc_debug_txt('previous initscript_code after cr',$previous_initscript_code);
734
735    $initscript_code=str_replace("\r","",$initscript_code);
736    // make sure the script ends with a single \n 
737    $initscript_code=trim($initscript_code);
738    if (!empty($initscript_code) && $initscript_code[strlen($initscript_code)-1] != "\n")
739      $initscript_code.="\n";
740    // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code);
741
742    if (strcmp($initscript_code,$previous_initscript_code) != 0) {
743      $newvalue=$api->SetSliceInitscriptCode($slice_id,$initscript_code);
744      // plc_debug_txt('newvalue',$newvalue);
745      $status=(strcmp($newvalue,$initscript_code)==0) ? "OK" : "failed";
746      if (! $initscript_code)    drupal_set_message("Removed initscript code " . $status);
747      else                       drupal_set_message("Installed new initscript code " . $status);
748      $changes=TRUE;
749    }
750    if (!$changes) drupal_set_message("No changes required in initscript");
751    plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1" );
752    break;
753  }
754
755
756  case 'delete-slice-tags': {
757    $slice_id = intval($_POST['slice_id']);
758    $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
759    $count = 0;
760    $success = true;
761    foreach($slice_tag_ids as $slice_tag_id) {
762      if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
763      else {
764        drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
765        $success = false;
766      }
767    }
768    if ($success)
769      drupal_set_message ("Deleted $count slice tag(s)");
770    plc_redirect(l_slice($slice_id) . "&show_tags=1" );
771    break;
772  }
773   
774  case 'add-slice-tag': {
775    $slice_id = intval($_POST['slice_id']);
776    $tag_type_id = intval($_POST['tag_type_id']);
777    $value = $_POST['value'];
778    $node_id = intval($_POST['node_id']);
779    $nodegroup_id = intval($_POST['nodegroup_id']);
780   
781    $result = null;
782    if ($node_id) {
783      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
784    } elseif ($nodegroup_id) {
785      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
786    } else {
787      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
788    }
789    if ($result)
790      drupal_set_message ("Added slice tag.");
791    else 
792        drupal_set_error("Could not add slice tag");
793    if ($_POST['sliver_action'])
794        plc_redirect(l_sliver($node_id,$slice_id));
795    else
796        plc_redirect(l_slice($slice_id) . "&show_tags=true" );
797    break;
798  }
799
800 //////////////////////////////////////////////////////////// tag types
801
802  case 'update-tag-type': {
803   // get post vars 
804    $tag_type_id= intval( $_POST['tag_type_id'] );
805    $tagname = $_POST['tagname'];
806    $min_role_id= intval( $_POST['min_role_id'] );
807    $description= $_POST['description'];  
808    $category= $_POST['category'];
809   
810    // make tag_type_fields dict
811    $tag_type_fields= array( "min_role_id" => $min_role_id, 
812                             "tagname" => $tagname, 
813                             "description" => $description,
814                             "category" => $category,
815                             );
816
817    if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
818      drupal_set_message ("Tag type $tagname updated");
819    else 
820      drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
821    plc_redirect(l_tag($tag_type_id));
822    break;
823  }
824
825  case 'add-tag-type': {
826   // get post vars 
827    $tagname = $_POST['tagname'];
828    $min_role_id= intval( $_POST['min_role_id'] );
829    $description= $_POST['description'];  
830    $category= $_POST['category'];  
831   
832    // make tag_type_fields dict
833    $tag_type_fields= array( "min_role_id" => $min_role_id, 
834                             "tagname" => $tagname, 
835                             "description" => $description,
836                             "category" => $category,
837                             );
838
839   // Add it!
840    $tag_type_id=$api->AddTagType( $tag_type_fields );
841    if ($tag_type_id > 0) 
842      drupal_set_message ("tag type $tag_type_id created");
843    else
844      drupal_set_error ("Could not create tag type $tagname");
845    plc_redirect( l_tags());
846    break;
847  }
848
849  case 'delete-tag-types': {
850    $tag_type_ids = $_POST['tag_type_ids'];
851    if ( ! $tag_type_ids) {
852      drupal_set_message("action=$action - No tag selected");
853      return;
854    }
855    $success=true;
856    $counter=0;
857    foreach ($tag_type_ids as $tag_type_id) 
858      if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
859        $success=false;
860      else
861        $counter++;
862    if ($success) 
863      drupal_set_message ("Deleted $counter tag(s)");
864    else
865      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
866    plc_redirect (l_tags());
867    break;
868  }
869
870  case 'remove-roles-from-tag-type' : {
871    $tag_type_id=$_POST['tag_type_id'];
872    $role_ids=$_POST['role_ids'];
873    if ( ! $role_ids) {
874      drupal_set_error("You have not selected role(s) to remove");
875    } else {
876      foreach( $role_ids as $role_id)  
877        if ( $api->DeleteRoleFromTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1 ) 
878          drupal_set_error ("Could not remove role $role_id from tag type $tag_type_id");
879    }
880    plc_redirect (l_tag_roles($tag_type_id));
881  }
882      
883  case 'add-role-to-tag-type' : {
884    $tag_type_id=$_POST['tag_type_id'];
885    $role_id=$_POST['role_id'];
886    if ( ! $role_id) {
887      drupal_set_error ("You have not selected a role to add");
888    } else if ($api->AddRoleToTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1) {
889      drupal_set_error("Could not add role $role_id to tag $tag_type_id");
890    }
891    plc_redirect (l_tag_roles($tag_type_id));
892  }
893
894 //////////////////////////////////////// tags   
895  case 'set-tag-on-node': 
896  case 'set-tag-on-interface': {
897    
898    $node_mode = false;
899    if ($action == 'set-tag-on-node') $node_mode=true;
900
901    if ($node_mode)
902      $node_id = intval($_POST['node_id']);
903    else 
904      $interface_id=intval($_POST['interface_id']);
905    $tag_type_id = intval($_POST['tag_type_id']);
906    $value = $_POST['value'];
907
908    $tag_types=$api->GetTagTypes(array($tag_type_id));
909    if (count ($tag_types) != 1) {
910      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
911    } else {
912      if ($node_mode) 
913        $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
914      else
915        $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
916      if ( count ($tags) == 1) {
917        $tag=$tags[0];
918        if ($node_mode) {
919          $tag_id=$tag['node_tag_id'];
920          $result=$api->UpdateNodeTag($tag_id,$value);
921        } else {
922          $tag_id=$tag['interface_tag_id'];
923          $result=$api->UpdateInterfaceTag($tag_id,$value);
924        }
925        if ($result == 1) 
926          drupal_set_message ("Updated tag, new value = $value");
927        else
928          drupal_set_error ("Could not update tag");
929      } else {
930        if ($node_mode)
931          $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
932        else
933          $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
934        if ($tag_id) 
935          drupal_set_message ("Created tag, new value = $value");
936        else
937          drupal_set_error ("Could not create tag");
938      }
939    }
940    
941    if ($node_mode)
942      plc_redirect (l_node_tags($node_id));
943    else
944      plc_redirect (l_interface_tags($interface_id));
945  }
946
947  case 'delete-node-tags' : 
948  case 'delete-interface-tags' : {
949
950    $node_mode = false;
951    if ($action == 'delete-node-tags') $node_mode=true;
952
953    if ($node_mode)
954      $tag_ids=$_POST['node_tag_ids'];
955    else
956      $tag_ids=$_POST['interface_tag_ids'];
957
958    if ( ! $tag_ids) {
959      drupal_set_message("action=$action - No tag selected");
960      return;
961    }
962    $success=true;
963    $counter=0;
964    foreach( $tag_ids as $tag_id ) {
965      if ($node_mode)
966        $retcod = $api->DeleteNodeTag( intval( $tag_id ));
967      else
968        $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
969      if ($retcod != 1) 
970        $success=false;
971      else
972        $counter++;
973    }
974    if ($success) 
975      drupal_set_message ("Deleted $counter tag(s)");
976    else
977      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
978    if ($node_mode)
979      plc_redirect(l_node_tags($_POST['node_id']));
980    else
981      plc_redirect(l_interface_tags($_POST['interface_id']));
982  }
983
984 //////////////////////////////////////// nodegroups
985  case 'update-nodegroup': {
986    $nodegroup_id = $_POST['nodegroup_id'];
987    $groupname = $_POST['groupname'];
988    $value = $_POST['value'];
989
990    $fields=array();
991    $fields['groupname']=$groupname;
992    $fields['value']=$value;
993    if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1) 
994      drupal_set_message ('Nodegroup updated');
995    else 
996      drupal_set_error ("Could not update nodegroup $groupname");
997    
998    plc_redirect(l_nodegroup($nodegroup_id));
999
1000  }
1001
1002  case 'add-nodegroup': {
1003    $groupname=$_POST['groupname'];
1004    if ( ! $groupname ) {
1005      drupal_set_error ('Empty groupname');
1006      plc_redirect (l_nodegroups());
1007    }
1008    $tag_type_id=intval($_POST['tag_type_id']);
1009    if ( ! $tag_type_id ) {
1010      drupal_set_error ('You must select a tag in the dropdown list');
1011      plc_redirect (l_nodegroups());
1012    }
1013    $value=$_POST['value'];
1014    if ( ! $value ) {
1015      drupal_set_message ("Empty value.. let's see ..");
1016    }
1017    if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0) 
1018      drupal_set_message ("Nodegroup $groupname created");
1019    else
1020      drupal_set_error ("Could not create nodegroup $groupname");
1021
1022    plc_redirect (l_nodegroups());
1023    break;
1024  }
1025
1026  case 'delete-nodegroups': {
1027    $nodegroup_ids=$_POST['nodegroup_ids'];
1028    if ( ! $nodegroup_ids ) {
1029      drupal_set_message("action=delete-nodegroups - No group selected");
1030      plc_redirect(l_nodegroups());
1031    }
1032    $success=true;
1033    $counter=0;
1034    foreach ($nodegroup_ids as $nodegroup_id) 
1035      if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1) 
1036        $success=false;
1037      else
1038        $counter++;
1039    if ($success) 
1040      drupal_set_message ("Deleted $counter group(s)");
1041    else
1042      drupal_set_error ("Could not delete all selected groups, only $counter were removed");
1043    plc_redirect (l_nodegroups());
1044    break;
1045  }
1046
1047 //////////////////////////////////////// leases
1048  case 'manage-leases': {
1049    $actions=json_decode($_POST['actions']);
1050    $add_requested=0;
1051    $add_done=0;
1052    $del_requested=0;
1053    $del_done=0;
1054    $errors=array();
1055    foreach ($actions as $action) {
1056      if ($action[0] == 'add-leases') {
1057        $nodenames=$action[1];
1058        $add_requested += count($nodenames);
1059        $slicename=$action[2];
1060        $t_from=intval($action[3]);
1061        $t_until=intval($action[4]);
1062        $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until);
1063        // update number of added leases
1064        $ids=$hash['new_ids'];
1065        $add_done += count($ids);
1066        // update global errors array
1067        foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error;
1068      } else if ($action[0]=='delete-leases') {
1069        $lease_id=intval($action[1]);
1070        $del_requested += 1;
1071        if ($api->DeleteLeases(array($lease_id)) == 1) {
1072          $del_done += 1;
1073        } else {
1074          $errors []= "Could not delete lease " . $lease_id;
1075        }
1076      } else {
1077        $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
1078      }
1079    }
1080    
1081    if (count($errors)==0) {
1082      echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1083    } else {
1084      foreach ($errors as $error) echo($error. "\n");
1085      echo("Leases updated only partially (" . 
1086           $add_done . "/" . $add_requested . " added and " . 
1087           $del_done . "/" . $del_requested . " deleted)");
1088    }     
1089
1090    break;
1091  }
1092
1093 ////////////////////////////////////////
1094
1095  case 'debug': {
1096    plc_debug('GET',$_GET);
1097    plc_debug('POST',$_POST);
1098    plc_debug('FILES',$_FILES);
1099    return;
1100  }
1101
1102  default: {
1103    plc_error ("Unknown action $action in actions.php");
1104    return;
1105  }
1106
1107  }
1108
1109 ?>