more renamings
[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 'linetabs.php';
13 require_once 'details.php';
14 require_once 'table.php';
15 require_once 'toggle.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 plekit_linetabs($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 PlekitDetails($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 $toggle=new PlekitToggle ('tags','Tags',array('trigger-tagname'=>'h2'));
142 $toggle->start();
143
144 $form = new PlekitForm (l_actions(),array('interface_id'=>$interface_id));
145 $form->start();
146
147 $tags=$api->GetInterfaceTags (array('interface_id'=>$interface_id));
148 function get_tagname ($tag) { return $tag['tagname'];}
149 $tagnames = array_map ("get_tagname",$tags);
150   
151
152 $headers=array("Name"=>"string",
153                "Value"=>"string",
154                );
155 if ($can_update) $headers[plc_delete_icon()]="none";
156   
157 $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
158 $table=new PlekitTable("interface_tags",$headers,0,$table_options);
159 $table->start();
160 if ($tags) foreach ($tags as $tag) {
161   $table->row_start();
162   $table->cell(l_tag_obj($tag));
163   $table->cell($tag['value']);
164   // the remove checkbox
165   if ($can_update) $table->cell ($form->checkbox_html('interface_tag_ids[]',$tag['interface_tag_id']));
166   $table->row_end();
167 }
168   
169 if ($can_update) {
170   $table->tfoot_start();
171
172   // remove tag 
173   $table->row_start();
174   $table->cell($form->submit_html("delete-interface-tags","Remove Tags"),
175                // use the whole columns and right adjust
176                $table->columns(), "right");
177   $table->row_end();
178
179   // set tag area
180   $table->row_start();
181   // get list of tag names in the interface/* category    
182   $all_tags= $api->GetTagTypes( array ("category"=>"interface*"), array("tagname","tag_type_id"));
183   // xxx cannot use onchange=submit() - would need to somehow pass action name 
184   function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
185   $selector=array_map("tag_selector",$all_tags);
186   $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
187   $table->cell($form->text_html("value","",array('width'=>8)));
188   $table->cell($form->submit_html("set-tag-on-interface","Set Tag"),2,"left");
189   $table->row_end();
190  }
191   
192 $table->end();
193 $form->end();
194 $toggle->end();
195
196 //plekit_linetabs ($tabs,"bottom");
197
198 // Print footer
199 include 'plc_footer.php';
200
201 ?>