From: rettenbe Date: Wed, 6 Aug 2008 15:34:59 +0000 (+0000) Subject: update: opsi functions partly with input sanity check and doxygen headerlines X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=30090ee26d4fda3c244fbc6bcc8352c0af6c37fe;p=gosa.git update: opsi functions partly with input sanity check and doxygen headerlines git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@12169 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/server/events/opsi_com.pm b/gosa-si/server/events/opsi_com.pm index dcffe9f15..10778ca23 100644 --- a/gosa-si/server/events/opsi_com.pm +++ b/gosa-si/server/events/opsi_com.pm @@ -42,188 +42,266 @@ sub get_events { return \@events; } - +## @method opsi_add_product_to_client +# Adds an Opsi product to an Opsi client. +# @param msg - STRING - xml message with tags hostId and productId +# @param msg_hash - HASHREF - message information parsed into a hash +# @param session_id - INTEGER - POE session id of the processing of this message +# @return out_msg - STRING - feedback to GOsa in success and error case sub opsi_add_product_to_client { - my ($msg, $msg_hash, $session_id) = @_; - my $header = @{$msg_hash->{'header'}}[0]; - my $source = @{$msg_hash->{'source'}}[0]; - my $target = @{$msg_hash->{'target'}}[0]; - my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0]; - my ($hostId, $productId); - - # TODO: check dependencies later on? + my ($msg, $msg_hash, $session_id) = @_; + my $header = @{$msg_hash->{'header'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0]; + my ($hostId, $productId); + my $error = 0; - # build return message with twisted target and source - my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); - if (defined $forward_to_gosa) { - &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); - } + # Build return message + my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); + if (defined $forward_to_gosa) { + &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); + } - # Get hostID - $hostId = @{$msg_hash->{'hostId'}}[0]; - &add_content2xml_hash($out_hash, "hostId", $hostId); + # Sanity check of needed parameter + if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1)) { + $error++; + &add_content2xml_hash($out_hash, "hostId_error", "no hostId specified or hostId tag invalid"); + &add_content2xml_hash($out_hash, "error", "hostId"); + &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); - # Get productID - $productId = @{$msg_hash->{'productId'}}[0]; - &add_content2xml_hash($out_hash, "productId", $productId); + } + if ((not exists $msg_hash->{'productId'}) || (@{$msg_hash->{'productId'}} != 1)) { + $error++; + &add_content2xml_hash($out_hash, "productId_error", "no productId specified or productId tag invalid"); + &add_content2xml_hash($out_hash, "error", "productId"); + &main::daemon_log("$session_id ERROR: no productId specified or procutId tag invalid: $msg", 1); + } - # Do an action request for all these -> "setup". - my $callobj = { - method => 'setProductActionRequest', - params => [ $productId, $hostId, "setup" ], - id => 1, }; - - my $sres = $main::opsi_client->call($main::opsi_url, $callobj); - my ($sres_err, $sres_err_string) = &check_opsi_res($sres); - if ($sres_err){ - &main::daemon_log("$session_id ERROR: cannot add product: ".$sres_err_string, 1); - &add_content2xml_hash($out_hash, "error", $sres_err_string); - } - - # return message - return ( &create_xml_string($out_hash) ); -} + if (not $error) { + # Get hostID + $hostId = @{$msg_hash->{'hostId'}}[0]; + &add_content2xml_hash($out_hash, "hostId", $hostId); + # Get productID + $productId = @{$msg_hash->{'productId'}}[0]; + &add_content2xml_hash($out_hash, "productId", $productId); -sub opsi_del_product_from_client { - my ($msg, $msg_hash, $session_id) = @_; - my $header = @{$msg_hash->{'header'}}[0]; - my $source = @{$msg_hash->{'source'}}[0]; - my $target = @{$msg_hash->{'target'}}[0]; - my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0]; - my ($hostId, $productId); + # Do an action request for all these -> "setup". + my $callobj = { + method => 'setProductActionRequest', + params => [ $productId, $hostId, "setup" ], + id => 1, }; - # build return message with twisted target and source - my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); - if (defined $forward_to_gosa) { - &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); - } + my $sres = $main::opsi_client->call($main::opsi_url, $callobj); + my ($sres_err, $sres_err_string) = &check_opsi_res($sres); + if ($sres_err){ + &main::daemon_log("$session_id ERROR: cannot add product: ".$sres_err_string, 1); + &add_content2xml_hash($out_hash, "error", $sres_err_string); + } + } - # Get hostID - $hostId = @{$msg_hash->{'hostId'}}[0]; - &add_content2xml_hash($out_hash, "hostId", $hostId); + # return message + return ( &create_xml_string($out_hash) ); +} - # Get productID - $productId = @{$msg_hash->{'productId'}}[0]; - &add_content2xml_hash($out_hash, "productId", $productId); +## @method opsi_del_product_from_client +# Deletes an Opsi-product from an Opsi-client. +# @param msg - STRING - xml message with tags hostId and productId +# @param msg_hash - HASHREF - message information parsed into a hash +# @param session_id - INTEGER - POE session id of the processing of this message +# @return out_msg - STRING - feedback to GOsa in success and error case +sub opsi_del_product_from_client { + my ($msg, $msg_hash, $session_id) = @_; + my $header = @{$msg_hash->{'header'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0]; + my ($hostId, $productId); + my $error = 0; + my ($sres, $sres_err, $sres_err_string); + # Build return message + my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); + if (defined $forward_to_gosa) { + &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); + } - #TODO: check the results for more than one entry which is currently installed - #$callobj = { - # method => 'getProductDependencies_listOfHashes', - # params => [ $productId ], - # id => 1, }; - # - #my $sres = $main::opsi_client->call($main::opsi_url, $callobj); - #my ($sres_err, $sres_err_string) = &check_opsi_res($sres); - #if ($sres_err){ - # &main::daemon_log("ERROR: cannot perform dependency check: ".$sres_err_string, 1); - # &add_content2xml_hash($out_hash, "error", $sres_err_string); - # return ( &create_xml_string($out_hash) ); - #} + # Sanity check of needed parameter + if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1)) { + $error++; + &add_content2xml_hash($out_hash, "hostId_error", "no hostId specified or hostId tag invalid"); + &add_content2xml_hash($out_hash, "error", "hostId"); + &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); + } + if ((not exists $msg_hash->{'productId'}) || (@{$msg_hash->{'productId'}} != 1)) { + $error++; + &add_content2xml_hash($out_hash, "productId_error", "no productId specified or productId tag invalid"); + &add_content2xml_hash($out_hash, "error", "productId"); + &main::daemon_log("$session_id ERROR: no productId specified or procutId tag invalid: $msg", 1); + } - # Check for uninstall method - my $callobj = { - method => 'getPossibleProductActions_list', - params => [ $productId ], - id => 1, }; - my $sres = $main::opsi_client->call($main::opsi_url, $callobj); - my ($sres_err, $sres_err_string) = &check_opsi_res($sres); - if ($sres_err){ - &main::daemon_log("$session_id ERROR: cannot get product action list: ".$sres_err_string, 1); - &add_content2xml_hash($out_hash, "error", $sres_err_string); - return ( &create_xml_string($out_hash) ); - } - my $uninst_possible= 0; - foreach my $r (@{$sres->result}) { - if ($r eq 'uninstall') { - $uninst_possible= 1; + # All parameter available + if (not $error) { + # Get hostID + $hostId = @{$msg_hash->{'hostId'}}[0]; + &add_content2xml_hash($out_hash, "hostId", $hostId); + + # Get productID + $productId = @{$msg_hash->{'productId'}}[0]; + &add_content2xml_hash($out_hash, "productId", $productId); + + + #TODO: check the results for more than one entry which is currently installed + #$callobj = { + # method => 'getProductDependencies_listOfHashes', + # params => [ $productId ], + # id => 1, }; + # + #my $sres = $main::opsi_client->call($main::opsi_url, $callobj); + #my ($sres_err, $sres_err_string) = &check_opsi_res($sres); + #if ($sres_err){ + # &main::daemon_log("ERROR: cannot perform dependency check: ".$sres_err_string, 1); + # &add_content2xml_hash($out_hash, "error", $sres_err_string); + # return ( &create_xml_string($out_hash) ); + #} + + + # Check to get product action list + my $callobj = { + method => 'getPossibleProductActions_list', + params => [ $productId ], + id => 1, }; + $sres = $main::opsi_client->call($main::opsi_url, $callobj); + ($sres_err, $sres_err_string) = &check_opsi_res($sres); + if ($sres_err){ + &main::daemon_log("$session_id ERROR: cannot get product action list: ".$sres_err_string, 1); + &add_content2xml_hash($out_hash, "error", $sres_err_string); + $error++; + } } - } - if (!$uninst_possible){ - &main::daemon_log("$session_id ERROR: cannot uninstall product '$productId', product do not has the action 'uninstall'", 1); - &add_content2xml_hash($out_hash, "error", "cannot uninstall product '$productId', product do not has the action 'uninstall'"); - return ( &create_xml_string($out_hash) ); - } - # Set product state to "none" - # Do an action request for all these -> "setup". - $callobj = { - method => 'setProductActionRequest', - params => [ $productId, $hostId, "none" ], - id => 1, }; + # Check action uninstall of product + if (not $error) { + my $uninst_possible= 0; + foreach my $r (@{$sres->result}) { + if ($r eq 'uninstall') { + $uninst_possible= 1; + } + } + if (!$uninst_possible){ + &main::daemon_log("$session_id ERROR: cannot uninstall product '$productId', product do not has the action 'uninstall'", 1); + &add_content2xml_hash($out_hash, "error", "cannot uninstall product '$productId', product do not has the action 'uninstall'"); + $error++; + } + } -print STDERR Dumper($callobj); + # Set product state to "none" + # Do an action request for all these -> "setup". + if (not $error) { + my $callobj = { + method => 'setProductActionRequest', + params => [ $productId, $hostId, "none" ], + id => 1, + }; + $sres = $main::opsi_client->call($main::opsi_url, $callobj); + ($sres_err, $sres_err_string) = &check_opsi_res($sres); + if ($sres_err){ + &main::daemon_log("$session_id ERROR: cannot delete product: ".$sres_err_string, 1); + &add_content2xml_hash($out_hash, "error", $sres_err_string); + } + } - my $res = $main::opsi_client->call($main::opsi_url, $callobj); - my ($res_err, $res_err_string) = &check_opsi_res($res); - if ($res_err){ - &main::daemon_log("$session_id ERROR: cannot delete product: ".$res_err_string, 1); - &add_content2xml_hash($out_hash, "error", $res_err_string); - return ( &create_xml_string($out_hash) ); - } - - # return message - return ( &create_xml_string($out_hash) ); + # Return message + return ( &create_xml_string($out_hash) ); } - +## @method opsi_add_client +# Adds an Opsi client to Opsi. +# @param msg - STRING - xml message with tags hostId and macaddress +# @param msg_hash - HASHREF - message information parsed into a hash +# @param session_id - INTEGER - POE session id of the processing of this message +# @return out_msg - STRING - feedback to GOsa in success and error case sub opsi_add_client { - my ($msg, $msg_hash, $session_id) = @_; - my $header = @{$msg_hash->{'header'}}[0]; - my $source = @{$msg_hash->{'source'}}[0]; - my $target = @{$msg_hash->{'target'}}[0]; - my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0]; - my $hostId; + my ($msg, $msg_hash, $session_id) = @_; + my $header = @{$msg_hash->{'header'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0]; + my ($hostId, $mac); + my $error = 0; + my ($sres, $sres_err, $sres_err_string); - # build return message with twisted target and source - my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); - if (defined $forward_to_gosa) { - &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); - } + # build return message with twisted target and source + my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); + if (defined $forward_to_gosa) { + &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); + } - # Get hostID - $hostId = @{$msg_hash->{'hostId'}}[0]; - &add_content2xml_hash($out_hash, "hostId", $hostId); - my $name= $hostId; - $name=~ s/^([^.]+).*$/$1/; - my $domain= $hostId; - $domain=~ s/^[^.]+\.(.*)$/$1/; - my ($description, $notes, $ip, $mac); + # Sanity check of needed parameter + if ((not exists $msg_hash->{'hostId'}) || (@{$msg_hash->{'hostId'}} != 1)) { + $error++; + &add_content2xml_hash($out_hash, "hostId_error", "no hostId specified or hostId tag invalid"); + &add_content2xml_hash($out_hash, "error", "hostId"); + &main::daemon_log("$session_id ERROR: no hostId specified or hostId tag invalid: $msg", 1); + } + if ((not exists $msg_hash->{'macaddress'}) || (@{$msg_hash->{'macaddress'}} != 1)) { + $error++; + &add_content2xml_hash($out_hash, "macaddress_error", "no macaddress specified or macaddress tag invalid"); + &add_content2xml_hash($out_hash, "error", "macaddress"); + &main::daemon_log("$session_id ERROR: no macaddress specified or macaddress tag invalid: $msg", 1); + } - if (defined @{$msg_hash->{'description'}}[0]){ - $description = @{$msg_hash->{'description'}}[0]; - } - if (defined @{$msg_hash->{'notes'}}[0]){ - $notes = @{$msg_hash->{'notes'}}[0]; - } - if (defined @{$msg_hash->{'ip'}}[0]){ - $ip = @{$msg_hash->{'ip'}}[0]; - } - if (defined @{$msg_hash->{'macaddress'}}[0]){ - $mac = @{$msg_hash->{'macaddress'}}[0]; - } - - my $callobj; - $callobj = { - method => 'createClient', - params => [ $name, $domain, $description, $notes, $ip, $mac ], - id => 1, - }; + if (not $error) { + # Get hostID + $hostId = @{$msg_hash->{'hostId'}}[0]; + &add_content2xml_hash($out_hash, "hostId", $hostId); + + # Get macaddress + $mac = @{$msg_hash->{'macaddress'}}[0]; + &add_content2xml_hash($out_hash, "macaddress", $mac); + + my $name= $hostId; + $name=~ s/^([^.]+).*$/$1/; + my $domain= $hostId; + $domain=~ s/^[^.]+\.(.*)$/$1/; + my ($description, $notes, $ip); + + if (defined @{$msg_hash->{'description'}}[0]){ + $description = @{$msg_hash->{'description'}}[0]; + } + if (defined @{$msg_hash->{'notes'}}[0]){ + $notes = @{$msg_hash->{'notes'}}[0]; + } + if (defined @{$msg_hash->{'ip'}}[0]){ + $ip = @{$msg_hash->{'ip'}}[0]; + } - my $sres = $main::opsi_client->call($main::opsi_url, $callobj); - my ($sres_err, $sres_err_string) = &check_opsi_res($sres); - if ($sres_err){ - &main::daemon_log("$session_id ERROR: cannot create client: ".$sres_err_string, 1); - &add_content2xml_hash($out_hash, "error", $sres_err_string); - } + my $callobj; + $callobj = { + method => 'createClient', + params => [ $name, $domain, $description, $notes, $ip, $mac ], + id => 1, + }; - # return message - return ( &create_xml_string($out_hash) ); + $sres = $main::opsi_client->call($main::opsi_url, $callobj); + ($sres_err, $sres_err_string) = &check_opsi_res($sres); + if ($sres_err){ + &main::daemon_log("$session_id ERROR: cannot create client: ".$sres_err_string, 1); + &add_content2xml_hash($out_hash, "error", $sres_err_string); + } + } + + # Return message + return ( &create_xml_string($out_hash) ); } - +## @method +# ??? +# @param msg - STRING - xml message with tag hostId +# @param msg_hash - HASHREF - message information parsed into a hash +# @param session_id - INTEGER - POE session id of the processing of this message sub opsi_modify_client { my ($msg, $msg_hash, $session_id) = @_; my $header = @{$msg_hash->{'header'}}[0]; diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index f5d8036a7..3481915df 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -157,13 +157,15 @@ for($count = 1; $count <= $zahl; $count++) #$data = "
job_opsi_install_client
GOSA 00:01:6c:9d:b9:fa limux-cl-2.intranet.gonicus.de 00:11:25:4b:8c:e5
"; # Add Opsi client - #$data = "
gosa_opsi_add_client
GOSA 00:01:6c:9d:b9:fa limux-cl-1.intranet.gonicus.de 00:11:25:4b:8c:e6 Test halt
"; + $data = "
gosa_opsi_add_client
GOSA 00:01:6c:9d:b9:fa limux-cl-1.intranet.gonicus.de 00:11:25:4b:8c:e6 Test halt 1.2.3.4 Im a note
"; + #$data = "
gosa_opsi_add_client
GOSA 00:01:6c:9d:b9:fa
"; # Add product to Opsi client - $data = "
gosa_opsi_add_product_to_client
GOSA 00:01:6c:9d:b9:fa limux-cl-1.intranet.gonicus.de 00:11:25:4b:8c:e6 softprod
"; + #$data = "
gosa_opsi_add_product_to_client
GOSA 00:01:6c:9d:b9:fa 00:11:25:4b:8c:e6
"; # Delete product from Opsi client - $data = "
gosa_opsi_del_product_from_client
GOSA 00:01:6c:9d:b9:fa limux-cl-1.intranet.gonicus.de 00:11:25:4b:8c:e6 softprod
"; + #$data = "
gosa_opsi_del_product_from_client
GOSA 00:01:6c:9d:b9:fa limux-cl-1.intranet.gonicus.de 00:11:25:4b:8c:e6 softprod
"; + #$data = "
gosa_opsi_del_product_from_client
GOSA 00:01:6c:9d:b9:fa
"; ######################### # Mailqueue communication