more inline updates & cleaned up tags
[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_sorts.php';
13
14 // find person roles
15 $_person= $plc->person;
16 $_roles= $_person['role_ids'];
17
18 $interface = array();
19
20 // If interface_id is specified, load data
21 if( isset( $_GET['id'] ) ) {
22   $id= intval( $_GET['id'] );
23   $interfaces= $api->GetInterfaces( array( $id ) );
24   if( $interfaces ) {
25     $interface= $interfaces[0];
26     $node_id= $interface['node_id'];
27   }
28 }
29
30 if( $_GET['node_id'] ) 
31   $node_id= $_GET['node_id'];
32
33 // Override fields with specified data
34 foreach( array( 'method', 'type', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 'dns1', 'dns2', 'hostname', 'mac', 'bwlimit', 'node_id' ) as $field ) {
35   if( isset( $_POST[$field] ) ) {
36     if( $_POST[$field] == "" ) {
37       $interface[$field]= NULL;
38     } else {
39       $interface[$field]= $_POST[$field];
40       if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
41         $interface[$field]= intval( $interface[$field] );
42       }
43     }
44   }
45   if( isset( $interface[$field] ) ) {
46     // E.g., $method = $interface['method'];
47     $$field= $interface[$field];
48   }
49 }
50
51 // Either interface_id or node_id must be specified in URL
52 if( !isset( $_GET['node_id'] ) && !( $nodes= $api->GetNodes( array( intval($node_id) ), array( 'node_id', 'hostname', 'site_id' ) ) ) ) {
53   drupal_set_error ("Malformed URL");
54   plc_redirect(l_nodes());
55 }
56
57
58 $nodes= $api->GetNodes( array( intval($node_id) ), array( 'node_id', 'hostname', 'site_id' ) );
59 $node= $nodes[0];
60
61 $can_update= True;
62 if( !in_array( 10, $_roles ) ) {
63   if ( !( in_array( 20, $_roles ) || in_array( 40, $_roles ) ) || !in_array( $node['site_id'], $_person['site_ids'] ) ) {
64     $can_update= False;
65   }
66 }
67
68 if( $can_update && (isset( $_POST['submitted'] ) || isset ($_GET['submitted'])) ) {
69   if( isset( $_POST['add'] ) ) {
70     $api->AddInterface( intval( $node_id ), $interface );
71   }
72   elseif ( isset( $_POST['delete'] ) || isset( $_GET['delete']) || isset( $_POST['update'] ) ) {
73     // interface_id must be specified in URL
74     if( !isset( $id ) ) {
75       plc_redirect(l_node($node_id));
76     }
77     if( isset( $_POST['delete'] ) || isset ($_GET['delete']) ) {
78       $api->DeleteInterface( $id );
79     }
80     elseif( isset( $_POST['update'] ) ) {
81       $api->UpdateInterface( $id, $interface );
82     }
83   }
84
85   $error= $api->error();
86
87   if( !empty( $error ) ) {
88     echo '<div class="plc-warning">' . $error . '.</div>';
89   } else {
90     plc_redirect(l_node($node_id));
91   }
92   
93 }
94
95 // Print header
96 require_once 'plc_drupal.php';
97 drupal_set_title($node['hostname']);
98 include 'plc_header.php';
99
100 // Start form
101 $action= "interface.php";
102 if( isset( $id ) ) {
103   $action.= "?id=" . $interface['interface_id'];
104
105 elseif( isset($node_id)) {
106   $action.= "?node_id=" . $node_id;
107 }
108
109 foreach( array( 'static', 'dhcp', 'proxy', 'tap', 'ipmi' ) as $option ) {
110   ${$option . "_selected"} = ( $method == $option ) ? 'selected="selected"' : '';
111 }
112
113 // XXX Query methods and types
114 echo <<<EOF
115
116 <script type="text/javascript">
117 function updateStaticFields()
118 {
119   var is_static= document.fm.method.options[document.fm.method.selectedIndex].text == 'Static';
120   var is_tap= document.fm.method.options[document.fm.method.selectedIndex].text == 'TUN/TAP';
121
122   document.fm.netmask.disabled= !is_static;
123   document.fm.network.disabled= !is_static;
124   document.fm.gateway.disabled= !is_static && !is_tap;
125   document.fm.broadcast.disabled= !is_static;
126   document.fm.dns1.disabled= !is_static;
127   document.fm.dns2.disabled= !is_static;
128 }
129 </script>
130
131 <form action="$action" method="post" name="fm">
132 <table cellpadding="2">
133 <tbody>
134
135 <tr>
136   <th>Method: </th>
137   <td>
138     <select name="method" onchange="updateStaticFields()">
139       <option value="static" $static_selected>Static</option>
140       <option value="dhcp" $dhcp_selected>DHCP</option>
141       <option value="proxy" $proxy_selected>Proxy</option>
142       <option value="tap" $tap_selected>TUN/TAP</option>
143       <option value="ipmi" $ipmi_selected>IPMI</option>
144     </select>
145   </td>
146 </tr>
147
148 <tr>
149   <th>Type: </th>
150   <td>
151     <select name='type' onchange='updateStaticFields()'>
152       <option value="ipv4" selected="selected">ipv4</option>
153     </select>
154   </td>
155 </tr>
156
157 <tr><th>IP: </th><td><input type="text" name="ip" value="$ip" size="30" maxlength="256"/></td></tr>
158 <tr><th>BW Limit: </th><td> <input type="text" name="bwlimit" value="$bwlimit" size="30" maxlength="256"/></td></tr>
159 <tr><td colspan=2> <hr> </td></tr>
160 <tr><th>Gateway: </th><td> <input type="text" name="gateway" value="$gateway" size="30" maxlength="256"/></td></tr>
161 <tr><th>Network: </th><td> <input type="text" name="network" value="$network" size="30" maxlength="256"/></td></tr>
162 <tr><th>Broadcast: </th><td> <input type="text" name="broadcast" value="$broadcast" size="30" maxlength="256"/></td></tr>
163 <tr><th>Netmask: </th><td> <input type="text" name="netmask" value="$netmask" size="30" maxlength="256"/></td></tr>
164 <tr><th>DNS 1: </th><td> <input type="text" name="dns1" value="$dns1" size="30" maxlength="256"/></td></tr>
165 <tr><th>DNS 2: </th><td> <input type="text" name="dns2" value="$dns2" size="30" maxlength="256"/></td></tr>
166 <tr><th>Hostname: </th><td> <input type="text" name="hostname" value="$hostname" size="30" maxlength="256"/></td></tr>
167 <tr><th>MAC Address: </th><td> <input type="text" name="mac" value="$mac" size="30" maxlength="256"/></td></tr>
168 <tr><th>BW Limit (bps): </th><td> <input type="text" name="bwlimit" value="$bwlimit" size="30" maxlength="256"/></td></tr>
169
170 EOF;
171
172 if ($can_update) {
173   echo '<tr><td/><td>';
174   echo '<input type="hidden" name="submitted" value="1" />';
175   if (isset($id)) {
176     echo '<input type="submit" name="update" value="Update"/>';
177     echo '<input type="submit" name="delete" value="Delete"/>';
178   }
179   echo '<input type="submit" name="add" value="Add As New"/>';
180   echo '</td></tr>';
181 }
182
183 echo <<<EOF
184 </tbody>
185 </table>
186 </form>
187 EOF;
188
189 $is_admin=in_array( 10, $_roles );
190 $is_pi=in_array( 20, $_roles );
191 print "<hr />";
192
193 if (empty ($interface['interface_tag_ids'])) {
194   print "<p> This network interface has no additional setting</p>";
195   if( $is_admin || $is_pi )
196     // xxx check the destination page
197     echo "<p><a href='settings.php?add=$id'>Add an Interface Setting</a></p>\n";
198  } else {
199   $interface_tags = $api->GetInterfaceTags($interface['interface_tag_ids']);
200   sort_interface_tags ($interface_tags);
201   print "<table cellpadding='5' cellspacing='5' class='list_set'><caption class='list_set'>Additional Settings</caption>";
202   print "<thead><tr class='list_set'>";
203   // the column for the delete button
204   if( $is_admin )
205     print "<th></th>";
206   print "<th class='list_set'>Name</th><th class='list_set'>Category</th><th class='list_set'>Description</th><th class='list_set'>Value</th></tr></thead><tbody>";
207   foreach ($interface_tags as $setting) {
208     echo "<tr class='list_set'>";
209     if ($is_admin) {
210       echo("<td>");
211       echo plc_delete_link_button('setting_action.php?rem_id=' . $setting['interface_tag_id'],
212                                   '\\n [ ' . $setting['tagname'] . ' = ' . $setting['value']);
213       echo("</td>");
214     }
215     if ($is_admin || $is_pi) 
216     // xxx check the destination page
217       printf ("<td class='list_set'> <a href='settings.php?id=%s'>%s </a></td>",$setting['interface_tag_id'],$setting['tagname']);
218     else
219       printf ("<td class='list_set'> %s </td>",$setting['tagname']);
220     printf ("<td class='list_set'> %s</td><td class='list_set'> %s</td><td class='list_set'> %s </td></tr>",
221             $setting['category'],
222             $setting['description'],
223             $setting['value']);
224   }
225   if( $is_admin || $is_pi )
226     // xxx check the destination page
227     echo "<tr><td colspan=4><a href='settings.php?add=$id'>Add a Network Setting</td</tr>\n";
228   
229   print "</tbody></table>";
230  }
231
232 echo <<<EOF
233 <hr /><a href="index.php?id=$node_id">Back to Node</a>
234 <script type="text/javascript">
235 updateStaticFields();
236 </script>
237 EOF;
238
239 // Print footer
240 include 'plc_footer.php';
241
242 ?>