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