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