From 676d43b7b85f638f832e968c5eaec269e582dc26 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Wed, 11 Feb 2009 17:58:30 +0000 Subject: [PATCH] interface form --- planetlab/actions.php | 184 ++++++++++----- planetlab/includes/plc_details.php | 8 +- planetlab/includes/plc_forms.php | 28 ++- planetlab/nodes/interface.js | 136 +++++++++++ planetlab/nodes/interface.php | 359 +++++++++++++---------------- planetlab/nodes/node.php | 9 +- planetlab/persons/person.php | 6 +- planetlab/tags/tags.php | 2 +- 8 files changed, 459 insertions(+), 273 deletions(-) create mode 100644 planetlab/nodes/interface.js diff --git a/planetlab/actions.php b/planetlab/actions.php index ea142d5..0a0bec4 100644 --- a/planetlab/actions.php +++ b/planetlab/actions.php @@ -53,6 +53,10 @@ $known_actions []= "update-node"; //////////////////////////////////////// interfaces $known_actions []= "delete-interfaces"; // expects: interface_ids +$known_actions []="add-interface"; +// expects: node_id & interface details +$known_actions []="update-interface"; +// expects: interface_id & interface details //////////////////////////////////////// sites $known_actions []= "delete-site"; @@ -62,19 +66,28 @@ $known_actions []= "expire-all-slices-in-site"; $known_actions []= "update-site"; // expects: site_id & name abbreviated_name url latitude longitude [login_base max_slices] -//////////////////////////////////////// tags +//////////////////////////////////////// tag types $known_actions []= "update-tag-type"; // expects: tag_type_id & name & description & category & min_role_id $known_actions []= "add-tag-type"; // expects: tag_type_id & tagname & description & category & min_role_id -$known_actions []= "set-tag-on-node"; -// expects: node_id tagname value $known_actions []= "delete-tag-types"; // expects: tag_type_ids -//////////////////////////////////////// nodetags +//////////////////////////////////////// tags +$known_actions []= "set-tag-on-node"; +// expects: node_id tagname value +$known_actions []= "set-tag-on-interface"; +// expects: interface_id tagname value $known_actions []= "delete-node-tags"; // expects: node_id & node_tag_ids +$known_actions []= "delete-interface-tags"; +// expects: interface_id & interface_tag_ids + +//////////////////////////////////////////////////////////// +$interface_details= array ('method','type', 'ip', 'gateway', 'network', + 'broadcast', 'netmask', 'dns1', 'dns2', + 'hostname', 'mac', 'bwlimit' ); ////////////////////////////// // sometimes we don't set 'action', but use the submit button name instead @@ -233,19 +246,19 @@ switch ($action) { plc_redirect(l_person($person_id)); } - $update_vals= array(); - $update_vals['first_name']= $first_name; - $update_vals['last_name']= $last_name; - $update_vals['title']= $title; - $update_vals['email']= $email; - $update_vals['phone']= $phone; - $update_vals['url']= $url; - $update_vals['bio']= $bio; + $fields= array(); + $fields['first_name']= $first_name; + $fields['last_name']= $last_name; + $fields['title']= $title; + $fields['email']= $email; + $fields['phone']= $phone; + $fields['url']= $url; + $fields['bio']= $bio; if ( $password1 != "" ) - $update_vals['password']= $password1; + $fields['password']= $password1; - if ( $api->UpdatePerson( intval( $person_id ), $update_vals) == 1 ) + if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 ) drupal_set_message("$first_name $last_name updated"); else drupal_set_error ("Could not update person $person_id" . $api->error()); @@ -319,8 +332,38 @@ switch ($action) { plc_redirect(l_node($_POST['node_id'])); } - - + case 'add-interface': { + $node_id=$_POST['node_id']; + foreach ($interface_details as $field) { + $interface[$field]= $_POST[$field]; + if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) { + $interface[$field]= intval( $interface[$field] ); + } + } + $result=$api->AddInterface( intval( $node_id ), $interface ); + if ($result >0 ) + drupal_set_message ("Interface $result added into node $node_id"); + else + drupal_set_error ("Could not create interface"); + plc_redirect (l_node($node_id)); + } + + case 'update-interface': { + $interface_id=$_POST['interface_id']; + foreach ($interface_details as $field) { + $interface[$field]= $_POST[$field]; + if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) { + $interface[$field]= intval( $interface[$field] ); + } + } + $result=$api->UpdateInterface( intval( $interface_id ), $interface ); + if ($result == 1 ) + drupal_set_message ("Interface $interface_id updated"); + else + drupal_set_error ("Could not update interface"); + plc_redirect (l_interface($interface_id)); + } + //////////////////////////////////////////////////////////// sites case 'delete-site': { $site_id = intval($_POST['site_id']); @@ -382,7 +425,7 @@ switch ($action) { break; } -//////////////////////////////////////////////////////////// tags +//////////////////////////////////////////////////////////// tag types case 'update-tag-type': { // get post vars @@ -429,9 +472,38 @@ switch ($action) { plc_redirect( l_tags()); } - case 'set-tag-on-node': { + case 'delete-tag-types': { + $tag_type_ids = $_POST['tag_type_ids']; + if ( ! $tag_type_ids) { + drupal_set_message("action=$action - No tag selected"); + return; + } + $success=true; + $counter=0; + foreach ($tag_type_ids as $tag_type_id) + if ($api->DeleteTagType(intval($tag_type_id)) != 1) + $success=false; + else + $counter++; + if ($success) + drupal_set_message ("Deleted $counter tag(s)"); + else + drupal_set_error ("Could not delete all selected tags, only $counter were removed"); + plc_redirect (l_tags()); + break; + } + +//////////////////////////////////////// tags + case 'set-tag-on-node': + case 'set-tag-on-interface': { + + $node_mode = false; + if ($action == 'set-tag-on-node') $node_mode=true; - $node_id = intval($_POST['node_id']); + if ($node_mode) + $node_id = intval($_POST['node_id']); + else + $interface_id=intval($_POST['interface_id']); $tag_type_id = intval($_POST['tag_type_id']); $value = $_POST['value']; @@ -439,17 +511,28 @@ switch ($action) { if (count ($tag_types) != 1) { drupal_set_error ("Could not locate tag_type_id $tag_type_id
Tag not set."); } else { - $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id)); + if ($node_mode) + $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id)); + else + $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id)); if ( count ($tags) == 1) { $tag=$tags[0]; - $tag_id=$tag['node_tag_id']; - $result=$api->UpdateNodeTag($tag_id,$value); + if ($node_mode) { + $tag_id=$tag['node_tag_id']; + $result=$api->UpdateNodeTag($tag_id,$value); + } else { + $tag_id=$tag['interface_tag_id']; + $result=$api->UpdateInterfaceTag($tag_id,$value); + } if ($result == 1) drupal_set_message ("Updated tag, new value = $value"); else drupal_set_error ("Could not update tag"); } else { - $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value); + if ($node_mode) + $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value); + else + $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value); if ($tag_id) drupal_set_message ("Created tag, new value = $value"); else @@ -457,50 +540,47 @@ switch ($action) { } } - plc_redirect (l_node($node_id)); + if ($node_mode) + plc_redirect (l_node($node_id)); + else + plc_redirect (l_interface($interface_id)); } - case 'delete-tag-types': { - $tag_type_ids = $_POST['tag_type_ids']; - if ( ! $tag_type_ids) { + case 'delete-node-tags' : + case 'delete-interface-tags' : { + + $node_mode = false; + if ($action == 'delete-node-tags') $node_mode=true; + + if ($node_mode) + $tag_ids=$_POST['node_tag_ids']; + else + $tag_ids=$_POST['interface_tag_ids']; + + if ( ! $tag_ids) { drupal_set_message("action=$action - No tag selected"); return; } $success=true; $counter=0; - foreach ($tag_type_ids as $tag_type_id) - if ($api->DeleteTagType(intval($tag_type_id)) != 1) + foreach( $tag_ids as $tag_id ) { + if ($node_mode) + $retcod = $api->DeleteNodeTag( intval( $tag_id )); + else + $retcod = $api->DeleteInterfaceTag( intval( $tag_id )); + if ($retcod != 1) $success=false; else $counter++; + } if ($success) drupal_set_message ("Deleted $counter tag(s)"); else drupal_set_error ("Could not delete all selected tags, only $counter were removed"); - plc_redirect (l_tags()); - break; - } - -//////////////////////////////////////// node tags - case 'delete-node-tags' : { - $node_tag_ids=$_POST['node_tag_ids']; - if ( ! $node_tag_ids) { - drupal_set_message("action=$action - No node tag selected"); - return; - } - $success=true; - $counter=0; - foreach( $node_tag_ids as $node_tag_id ) { - if ($api->DeleteNodeTag( intval( $node_tag_id )) != 1) - $success=false; - else - $counter++; - } - if ($success) - drupal_set_message ("Deleted $counter node tag(s)"); + if ($node_mode) + plc_redirect(l_node($_POST['node_id'])); else - drupal_set_error ("Could not delete all selected node tags, only $counter were removed"); - plc_redirect(l_node($_POST['node_id'])); + plc_redirect(l_interface($_POST['interface_id'])); } diff --git a/planetlab/includes/plc_details.php b/planetlab/includes/plc_details.php index 5d3c50e..c03f756 100644 --- a/planetlab/includes/plc_details.php +++ b/planetlab/includes/plc_details.php @@ -132,8 +132,14 @@ class PlcDetails { if ($height) $html .= " rows=$height"; $html .= ">$value"; } else { - $html .= "'your javascript code' static function text_html ($name,$value,$options=NULL) { $default_options = array('width'=>20); if ( ! $options) $options=array(); $options = array_merge($default_options,$options); $html="'submit()' + // (*) standard callbacks + + static function select_html ($name,$selectors,$options) { + if ( ! $options) $options=array(); + if ( $options ['autosubmit'] ) $options['onChange']='submit()'; $html=""; $html.=""; +switch ($mode) { + case 'add': + $details->tr($add_button,"right"); + break; + case 'update': + $details->tr($update_button . $add_button . $dbg,"right"); + break; + } -// Print header -require_once 'plc_drupal.php'; -drupal_set_title($node['hostname']); -include 'plc_header.php'; +$details->end(); +$form->end(); -// Start form -$action= "interface.php"; -if( isset( $id ) ) { - $action.= "?id=" . $interface['interface_id']; -} -elseif( isset($node_id)) { - $action.= "?node_id=" . $node_id; -} +// no tags if the interface has not been created yet +if ($mode == 'add') return; -foreach( array( 'static', 'dhcp', 'proxy', 'tap', 'ipmi' ) as $option ) { - ${$option . "_selected"} = ( $method == $option ) ? 'selected="selected"' : ''; -} -// XXX Query methods and types -echo << -function updateStaticFields() -{ - var is_static= document.fm.method.options[document.fm.method.selectedIndex].text == 'Static'; - var is_tap= document.fm.method.options[document.fm.method.selectedIndex].text == 'TUN/TAP'; - - document.fm.netmask.disabled= !is_static; - document.fm.network.disabled= !is_static; - document.fm.gateway.disabled= !is_static && !is_tap; - document.fm.broadcast.disabled= !is_static; - document.fm.dns1.disabled= !is_static; - document.fm.dns2.disabled= !is_static; -} - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -EOF; +//////////////////////////////////////// tags +$form = new PlcForm (l_actions(),array('interface_id'=>$interface_id)); +$form->start(); -if ($can_update) { - echo ''; +$tags=$api->GetInterfaceTags (array('interface_id'=>$interface_id)); +function get_tagname ($tag) { return $tag['tagname'];} +$tagnames = array_map ("get_tagname",$tags); + +plc_section("Tags"); +$headers=array("Name"=>"string", + "Value"=>"string", + ); +if ($can_update) $headers[plc_delete_icon()]="none"; + +$table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10); +$table=new PlcTable("interface_tags",$headers,0,$table_options); +$table->start(); +if ($tags) foreach ($tags as $tag) { + $table->row_start(); + $table->cell(l_tag_obj($tag)); + $table->cell($tag['value']); + // the remove checkbox + if ($can_update) $table->cell ($form->checkbox_html('interface_tag_ids[]',$tag['interface_tag_id'])); + $table->row_end(); } - -echo << -
Method: - -
Type: - -
IP:
BW Limit:

Gateway:
Network:
Broadcast:
Netmask:
DNS 1:
DNS 2:
Hostname:
MAC Address:
BW Limit (bps):
'; - echo ''; - if (isset($id)) { - echo ''; - echo ''; - } - echo ''; - echo '
-
-EOF; - -$is_admin=in_array( 10, $_roles ); -$is_pi=in_array( 20, $_roles ); -print "
"; - -if (empty ($interface['interface_tag_ids'])) { - print "

This network interface has no additional setting

"; - if( $is_admin || $is_pi ) - // xxx check the destination page - echo "

Add an Interface Setting

\n"; - } else { - $interface_tags = $api->GetInterfaceTags($interface['interface_tag_ids']); - sort_interface_tags ($interface_tags); - print ""; - print ""; - // the column for the delete button - if( $is_admin ) - print ""; - print ""; - foreach ($interface_tags as $setting) { - echo ""; - if ($is_admin) { - echo(""); - } - if ($is_admin || $is_pi) - // xxx check the destination page - printf ("",$setting['interface_tag_id'],$setting['tagname']); - else - printf ("",$setting['tagname']); - printf ("", - $setting['category'], - $setting['description'], - $setting['value']); - } - if( $is_admin || $is_pi ) - // xxx check the destination page - echo "
Additional Settings
NameCategoryDescriptionValue
"); - echo plc_delete_link_button('setting_action.php?rem_id=' . $setting['interface_tag_id'], - '\\n [ ' . $setting['tagname'] . ' = ' . $setting['value']); - echo(" %s %s %s %s %s
Add a Network Setting\n"; - print "
"; +if ($can_update) { + $table->tfoot_start(); + + // remove tag + $table->row_start(); + $table->cell($form->submit_html("delete-interface-tags","Remove Tags"), + // use the whole columns and right adjust + $table->columns(), "right"); + $table->row_end(); + + // set tag area + $table->row_start(); + // get list of tag names in the interface/* category + $all_tags= $api->GetTagTypes( array ("category"=>"interface*"), array("tagname","tag_type_id")); + // xxx cannot use onchange=submit() - would need to somehow pass action name + function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); } + $selector=array_map("tag_selector",$all_tags); + $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose"))); + $table->cell($form->text_html("value","",array('width'=>8))); + $table->cell($form->submit_html("set-tag-on-interface","Set Tag"),2,"left"); + $table->row_end(); } - -echo <<Back to Node - -EOF; + +$table->end(); +$form->end(); // Print footer include 'plc_footer.php'; diff --git a/planetlab/nodes/node.php b/planetlab/nodes/node.php index 77350e1..98de24b 100644 --- a/planetlab/nodes/node.php +++ b/planetlab/nodes/node.php @@ -172,7 +172,7 @@ if ( ! ($local_peer && $privileges)) { if ($dbname == $boot_state) $selector['selected']=true; $selectors []= $selector; } - $boot_value .= $boot_form->select_html("boot_state",$selectors,NULL,true); + $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true)); $boot_value .= $boot_form->end_html(); } $details->th_td ("Boot state",$boot_value); @@ -191,7 +191,8 @@ if ( $local_peer && $privileges) { array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"), array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"), array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)")); - $download_value .= $download_form->select_html("action",$selectors,"Download mode",true); + $download_value .= $download_form->select_html("action",$selectors, + array('label'=>"Download mode",'autosubmit'=>true)); $download_value .= $download_form->end_html(); $details->th_td ("Download",$download_value); } @@ -263,7 +264,7 @@ if ( $local_peer ) { // xxx cannot use onchange=submit() - would need to somehow pass action name function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); } $selector=array_map("tag_selector",$all_tags); - $table->cell($form->select_html("tag_type_id",$selector,"Choose")); + $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose"))); $table->cell($form->text_html("value","",array('width'=>8))); $table->cell($form->submit_html("set-tag-on-node","Set Tag"),2,"left"); $table->row_end(); @@ -330,7 +331,7 @@ if ( $local_peer ) { if ($privileges) { $table->tfoot_start(); $table->row_start(); - $add_button=new PlcFormButton (l_interface_add($node_id),"add_interface","Add interface"); + $add_button=new PlcFormButton (l_interface_add($node_id),"add_interface","Add interface","GET"); // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove) $table->cell($add_button->html(), 3,"left"); $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), $table->columns()-3,"right"); diff --git a/planetlab/persons/person.php b/planetlab/persons/person.php index 7a4d013..6b3f659 100644 --- a/planetlab/persons/person.php +++ b/planetlab/persons/person.php @@ -281,7 +281,7 @@ if ($can_manage_sites) { // xxx cannot use onchange=submit() - would need to somehow pass action name function site_selector($site) { return array('display'=>$site['name'],"value"=>$site['site_id']); } $selectors = array_map ("site_selector",$relevant_sites); - $table->cell ($form->select_html("site_id",$selectors,"Choose a site to add"). + $table->cell ($form->select_html("site_id",$selectors,array('label'=>"Choose a site to add")). $form->submit_html("add-person-to-site","Add in site"), $table->columns(),"right"); $table->row_end(); @@ -327,9 +327,9 @@ if ($can_manage_roles) { $table->row_start(); $selectors=$form->role_selectors_excluding($api,$role_ids); - $add_role_left_area=$form->select_html("role_id",$selectors,"Choose role"); + $add_role_left_area=$form->select_html("role_id",$selectors,array('label'=>"Choose role")); // add a role : the button - $add_role_right_area=$form->submit_html("add-role-to-person","Add role"); + $add_role_right_area=$form->submit_html("add-role-to-person",array('label'=>"Add role")); $table->cell ($add_role_left_area . $add_role_right_area, $table->columns(),"right"); $table->row_end(); diff --git a/planetlab/tags/tags.php b/planetlab/tags/tags.php index 4403f72..282c9c5 100644 --- a/planetlab/tags/tags.php +++ b/planetlab/tags/tags.php @@ -94,7 +94,7 @@ if (plc_is_admin()) { $relevant_roles = $api->GetRoles( array("~role_id"=>$role_ids)); function selector_argument ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); } $selectors=array_map("selector_argument",$relevant_roles); - $role_input=$form->select_html("min_role_id",$selectors,"Role"); + $role_input=$form->select_html("min_role_id",$selectors,array('label'=>"Role")); $table->cell($form->text_html('tagname','')); $table->cell($form->textarea_html('description','',$description_width,2)); -- 2.43.0