first draft for togglable sections
[plewww.git] / planetlab / nodes / interface.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 // Common functions
11 require_once 'plc_functions.php';
12 require_once 'plc_minitabs.php';
13 require_once 'plc_details.php';
14 require_once 'plc_tables.php';
15 require_once 'prototype.php';
16
17 require_once 'plc_drupal.php';
18 include 'plc_header.php';
19
20 // purpose : display, update or add an interface
21
22 // interface:
23 // updating : _GET['id'] :  
24 //      if id is set: display the interface, allows to update/add a new if the user has privileges
25 // adding: _GET['node_id']: 
26 //      otherwise, node_id is needed and the form only allows to add
27
28 if ( isset ($_GET['id'])) {
29   $mode='update';
30   $interface_id=intval($_GET['id']);
31   $interfaces=$api->GetInterfaces(array('interface_id'=>$interface_id));
32   $interface=$interfaces[0];
33   $node_id=$interface['node_id'];
34  } else if (isset ($_GET['node_id'])) {
35   $mode='add';
36   $interface=array();
37   $node_id=$_GET['node_id'];
38  } 
39 // check
40 if ( ! $node_id) {
41   drupal_set_error('Malformed URL in interface.php, need id or node_id');
42   plc_redirect(l_nodes());
43   return;
44  }
45
46 $tabs=array();
47 $tabs[] = array('label'=>'Back to node', 'url'=>l_node($node_id),
48                 'bubble'=>'Cancel pending changes');
49 plc_tabs($tabs);
50
51 $fields=array( 'method', 'type', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 
52                'dns1', 'dns2', 'hostname', 'mac', 'bwlimit', 'node_id' );
53
54 //////////////////////////////
55 $nodes= $api->GetNodes( array( intval($node_id) ), array( 'node_id', 'hostname', 'site_id' ) );
56 $node= $nodes[0];
57 $site_id=$node['site_id'];
58
59 $can_update= plc_is_admin() || ( plc_in_site ($site_id) && ( plc_is_pi() || plc_is_tech()));
60
61 drupal_set_title("Interface on " . $node['hostname']);
62
63 // include javacsript helpers
64 drupal_set_title ('
65 <script type="text/javascript" src="/planetlab/nodes/interface.js"></script>
66 ');
67
68 $details=new PlcDetails($can_update);
69
70 // xxx hardwire network type for now
71 $form_variables = array('node_id'=>$node_id,'type'=>"ipv4");
72 if ($mode == "update") $form_variables['interface_id']=$interface_id;
73 $form=$details->form_start(l_actions(),$form_variables);
74
75 $details->start();
76
77 //>>> GetNetworkMethods()
78 //[u'static', u'dhcp', u'proxy', u'tap', u'ipmi', u'unknown']
79 function method_selectors ($api, $method) {
80   $builtin_methods=array("static"=>"Static", "dhcp"=>"DHCP", "proxy"=>"Proxy",  
81                          "tap"=>"TUN/TAP", "ipmi"=>"IPMI");
82   $selectors=array();
83   foreach ($builtin_methods as $value=>$display) {
84     $selector=array('display'=>$display, 'value'=>$value);
85     if ($value == $method) $selector['selected']=true;
86     $selectors []= $selector;
87   }
88   return $selectors;
89 }
90 $method_select = $form->select_html ("method",method_selectors($api,$interface['method']),
91                                      array('id'=>'method','onChange'=>'updateMethodFields()'));
92 $details->th_td("Method",$method_select,"method",array('input_type'=>'select','value'=>$interface['method']));
93
94 // dont display the 'type' selector as it contains only ipv4
95 //>>> GetNetworkTypes()
96 //[u'ipv4']
97
98 $details->th_td("IP",$interface['ip'],"ip",array('width'=>15,
99                                                  'onKeyup'=>'networkHelper()',
100                                                  'onChange'=>'networkHelper()'));
101 $details->th_td("Netmask",$interface['netmask'],"netmask",array('width'=>15,
102                                                                 'onKeyup'=>'networkHelper()',
103                                                                 'onChange'=>'networkHelper()'));
104 $details->th_td("Network",$interface['network'],"network",array('width'=>15));
105 $details->th_td("Broadcast",$interface['broadcast'],"broadcast",array('width'=>15));
106 $details->th_td("Gateway",$interface['gateway'],"gateway",array('width'=>15,
107                                                                 'onChange'=>'subnetChecker("gateway",false)'));
108 $details->th_td("DNS 1",$interface['dns1'],"dns1",array('width'=>15,
109                                                                 'onChange'=>'subnetChecker("dns1",false)'));
110 $details->th_td("DNS 2",$interface['dns2'],"dns2",array('width'=>15,
111                                                                 'onChange'=>'subnetChecker("dns2",true)'));
112 $details->space();
113 $details->th_td("BW limit (bps)",$interface['bwlimit'],"bwlimit",array('width'=>11));
114 $details->th_td("Hostname",$interface['hostname'],"hostname");
115 # should the user be allowed to change this ?
116 $mac=$interface['mac'];
117 if ($mac) $details->th_td("MAC address",$mac);
118
119 // the buttons
120 $update_button = $form->submit_html ("update-interface","Update",
121                                      array('onSubmit'=>'interfaceSubmit()'));
122 $add_button = $form->submit_html ("add-interface","Add as new",
123                                   array('onSubmit'=>'interfaceSubmit()'));
124 switch ($mode) {
125  case 'add':
126    $details->tr($add_button,"right");
127    break;
128  case 'update':
129    $details->tr($update_button . "&nbsp" . $add_button,"right");
130    break;
131  }
132
133 $details->end();
134 $form->end();
135
136 // no tags if the interface has not been created yet
137 if ($mode == 'add') return;
138
139
140 //////////////////////////////////////// tags
141 $form = new PlcForm (l_actions(),array('interface_id'=>$interface_id));
142 $form->start();
143
144 $tags=$api->GetInterfaceTags (array('interface_id'=>$interface_id));
145 function get_tagname ($tag) { return $tag['tagname'];}
146 $tagnames = array_map ("get_tagname",$tags);
147   
148 plc_section("Tags");
149 $headers=array("Name"=>"string",
150                "Value"=>"string",
151                );
152 if ($can_update) $headers[plc_delete_icon()]="none";
153   
154 $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
155 $table=new PlcTable("interface_tags",$headers,0,$table_options);
156 $table->start();
157 if ($tags) foreach ($tags as $tag) {
158   $table->row_start();
159   $table->cell(l_tag_obj($tag));
160   $table->cell($tag['value']);
161   // the remove checkbox
162   if ($can_update) $table->cell ($form->checkbox_html('interface_tag_ids[]',$tag['interface_tag_id']));
163   $table->row_end();
164 }
165   
166 if ($can_update) {
167   $table->tfoot_start();
168
169   // remove tag 
170   $table->row_start();
171   $table->cell($form->submit_html("delete-interface-tags","Remove Tags"),
172                // use the whole columns and right adjust
173                $table->columns(), "right");
174   $table->row_end();
175
176   // set tag area
177   $table->row_start();
178   // get list of tag names in the interface/* category    
179   $all_tags= $api->GetTagTypes( array ("category"=>"interface*"), array("tagname","tag_type_id"));
180   // xxx cannot use onchange=submit() - would need to somehow pass action name 
181   function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
182   $selector=array_map("tag_selector",$all_tags);
183   $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
184   $table->cell($form->text_html("value","",array('width'=>8)));
185   $table->cell($form->submit_html("set-tag-on-interface","Set Tag"),2,"left");
186   $table->row_end();
187  }
188   
189 $table->end();
190 $form->end();
191
192 plc_tabs ($tabs,"bottom");
193
194 // Print footer
195 include 'plc_footer.php';
196
197 ?>