From: rettenbe Date: Thu, 24 Sep 2009 13:05:54 +0000 (+0000) Subject: new functions for gosa-si plugin opsi_com.pm X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3da8c574606f172bd29360b5373004f16b3dba2d;p=gosa.git new functions for gosa-si plugin opsi_com.pm git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@14340 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/server/events/opsi_com.pm b/gosa-si/server/events/opsi_com.pm index 3977ca915..15b3a63a2 100644 --- a/gosa-si/server/events/opsi_com.pm +++ b/gosa-si/server/events/opsi_com.pm @@ -30,10 +30,12 @@ my @events = ( "opsi_getSoftwareLicense_hash", "opsi_getLicensePool_hash", "opsi_getSoftwareLicenseUsages", + "opsi_getSoftwareLicenseUsagesForProductId", "opsi_getLicensePools_listOfHashes", "opsi_getLicenseInformationForProduct", "opsi_getPool", "opsi_removeLicense", + "opsi_getReservedLicenses", "opsi_test", ); @EXPORT = @events; @@ -1480,25 +1482,8 @@ sub opsi_getLicensePool_hash { return ( &create_xml_string($out_hash) ); } -################################ -# -# @brief Returns softwareLicenseId, notes, licenseKey, hostId and licensePoolId for optional given licensePoolId and hostId -# @param hostid Something like client_1.intranet.mydomain.de (optional). -# @param licensePoolId The name of the pool (optional). -# -sub opsi_getSoftwareLicenseUsages { - 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 $licensePoolId = defined $msg_hash->{'licensePoolId'} ? @{$msg_hash->{'licensePoolId'}}[0] : undef; - my $hostId = defined $msg_hash->{'hostId'} ? @{$msg_hash->{'hostId'}}[0] : undef; - my $out_hash; - - my ($res, $err) = &_getSoftwareLicenseUsages_listOfHashes('licensePoolId'=>$licensePoolId, 'hostId'=>$hostId); - if ($err){ - return &_giveErrorFeedback($msg_hash, "cannot fetch software licenses from license pool : ".$res, $session_id); - } +sub _parse_getSoftwareLicenseUsages { + my $res = shift; # Parse Opsi result my $tmp_licensePool_cache = {}; @@ -1526,6 +1511,32 @@ sub opsi_getSoftwareLicenseUsages { push( @{$res_hash->{hit}}, $license_hash ); } + return $res_hash; +} + +################################ +# +# @brief Returns softwareLicenseId, notes, licenseKey, hostId and licensePoolId for optional given licensePoolId and hostId +# @param hostid Something like client_1.intranet.mydomain.de (optional). +# @param licensePoolId The name of the pool (optional). +# +sub opsi_getSoftwareLicenseUsages { + 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 $licensePoolId = defined $msg_hash->{'licensePoolId'} ? @{$msg_hash->{'licensePoolId'}}[0] : undef; + my $hostId = defined $msg_hash->{'hostId'} ? @{$msg_hash->{'hostId'}}[0] : undef; + my $out_hash; + + my ($res, $err) = &_getSoftwareLicenseUsages_listOfHashes('licensePoolId'=>$licensePoolId, 'hostId'=>$hostId); + if ($err){ + return &_giveErrorFeedback($msg_hash, "cannot fetch software licenses from license pool : ".$res, $session_id); + } + + # Parse Opsi result + my $res_hash = &_parse_getSoftwareLicenseUsages($res); + # Create function result message $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); $out_hash->{result} = [$res_hash]; @@ -1533,6 +1544,48 @@ sub opsi_getSoftwareLicenseUsages { return ( &create_xml_string($out_hash) ); } +################################ +# +# @brief Returns softwareLicenseId, notes, licenseKey, hostId and licensePoolId. Function return is identical to opsi_getSoftwareLicenseUsages +# @param productId Something like 'firefox', 'python' or anything else . +# +sub opsi_getSoftwareLicenseUsagesForProductId { + my ($msg, $msg_hash, $session_id) = @_; + my $header = @{$msg_hash->{'header'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + + # Check input sanity + my $productId; + if (&_check_xml_tag_is_ok ($msg_hash, 'productId')) { + $productId= @{$msg_hash->{'productId'}}[0]; + } else { + return ( &_give_feedback($msg, $msg_hash, $session_id, $_) ); + } + + # Fetch licensePoolId for productId + my ($res, $err) = &_getLicensePoolId('productId'=>$productId); + if ($err){ + return &_giveErrorFeedback($msg_hash, "cannot fetch licensePoolId for given productId : ".$res, $session_id); + } + + my $licensePoolId; + + # Fetch softwareLiceceUsages for licensePoolId + ($res, $err) = &_getSoftwareLicenseUsages_listOfHashes('licensePoolId'=>$licensePoolId); + if ($err){ + return &_giveErrorFeedback($msg_hash, "cannot fetch software licenses from license pool : ".$res, $session_id); + } + + # Parse Opsi result + my $res_hash = &_parse_getSoftwareLicenseUsages($res); + + # Create function result message + my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); + $out_hash->{result} = [$res_hash]; + + return ( &create_xml_string($out_hash) ); +} + ################################ # # @brief Returns expirationDate, boundToHost, maxInstallation, licenseTyp, licensePoolIds and licenseKeys for a given softwareLicense ID. @@ -2044,7 +2097,7 @@ sub opsi_getPool { 'hostIds' => [], }; foreach my $licensePoolId (@{ $license->{'licensePoolIds'}}) { - push( @{$license_hash->{'licensePooIds'}}, $licensePoolId); + push( @{$license_hash->{'licensePoolIds'}}, $licensePoolId); $license_hash->{licenseKeys}->{$licensePoolId} = [ $license->{'licenseKeys'}->{$licensePoolId} ]; } foreach my $usage (@$usages_res) { @@ -2105,6 +2158,69 @@ sub opsi_removeLicense { return ( &create_xml_string($out_hash) ); } + +################################ +# +# @brief +# @param +# +#TODO +sub opsi_getReservedLicenses { + my ($msg, $msg_hash, $session_id) = @_; + my $header = @{$msg_hash->{'header'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + + # Check input sanity + my $hostId; + if (&_check_xml_tag_is_ok ($msg_hash, 'hostId')) { + $hostId = @{$msg_hash->{'hostId'}}[0]; + } else { + return ( &_give_feedback($msg, $msg_hash, $session_id, $_) ); + } + + # Fetch informations from Opsi server + my ($license_res, $license_err) = &_getSoftwareLicenses_listOfHashes(); + if ($license_err){ + return &_giveErrorFeedback($msg_hash, "cannot get software license information from Opsi server: ".$license_res, $session_id); + } + + + # Parse result + my $res_hash = { 'hit'=> [] }; + foreach my $license ( @$license_res) { + if ($license->{boundToHost} ne $hostId) { next; } + + my $license_hash = { 'softwareLicenseId' => [$license->{'softwareLicenseId'}], + 'maxInstallations' => [$license->{'maxInstallations'}], + 'boundToHost' => [$license->{'boundToHost'}], + 'expirationDate' => [$license->{'expirationDate'}], + 'licenseContractId' => [$license->{'licenseContractId'}], + 'licenseType' => [$license->{'licenseType'}], + 'licensePoolIds' => [], + }; + + foreach my $licensePoolId (@{$license->{'licensePoolIds'}}) { + # Fetch information for license pools containing a software license which is bound to given host + my ($pool_res, $pool_err) = &_getLicensePool_hash( 'licensePoolId'=>$licensePoolId ); + if ($pool_err){ + return &_giveErrorFeedback($msg_hash, "cannot get license pool from Opsi server: ".$pool_res, $session_id); + } + + # Add licensePool information to result hash + push (@{$license_hash->{licensePoolIds}}, $licensePoolId); + $license_hash->{$licensePoolId} = {'productIds'=>[], 'windowsSoftwareIds'=>[]}; + map (push (@{$license_hash->{$licensePoolId}->{productIds}}, $_), @{$pool_res->{productIds}}); + map (push (@{$license_hash->{$licensePoolId}->{windowsSoftwareIds}}, $_), @{$pool_res->{windowsSoftwareIds}}); + } + push( @{$res_hash->{hit}}, $license_hash ); + } + my $out_hash = &main::create_xml_hash("answer_$header", $main::server_address, $source); + $out_hash->{licenses} = [$res_hash]; + return ( &create_xml_string($out_hash) ); + + return; +} + sub opsi_test { my ($msg, $msg_hash, $session_id) = @_; my $header = @{$msg_hash->{'header'}}[0]; @@ -2251,4 +2367,28 @@ sub _deleteSoftwareLicense { return ($res->result, 0); } +sub _getLicensePoolId { + my %arg = ( + 'productId' => undef, + @_, + ); + + if (not defined $arg{productId} ) { + return ("function requires productId as parameter", 1); + } + + my $callobj = { + method => 'getLicensePoolId', + params => [ $arg{productId} ], + id => 1, + }; + my $res = $main::opsi_client->call($main::opsi_url, $callobj); + + # Check Opsi error + my ($res_error, $res_error_str) = &check_opsi_res($res); + if ($res_error){ return ( (caller(0))[3]." : ".$res_error_str, 1 ); } + + return ($res->result, 0); +} + 1; diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index edb70af7d..5b4833211 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -19,6 +19,7 @@ for($count = 1; $count <= $zahl; $count++) # Funktioniert noch nicht #$data = "
gosa_opsi_test
GOSA00:01:6C:9D:B9:FAacroread
"; + #$data = "
gosa_opsi_getLicenseInformationForProduct
GOSA00:01:6C:9D:B9:FApython
"; #$data = "
gosa_opsi_getLicenseInformationForProduct
GOSA00:01:6C:9D:B9:FAfirefox
"; #$data = "
gosa_opsi_getLicensePools_listOfHashes
GOSA00:01:6C:9D:B9:FA
"; @@ -38,15 +39,18 @@ for($count = 1; $count <= $zahl; $count++) #$data = "
gosa_opsi_unassignAllSoftwareLicensesFromHost
GOSA00:01:6C:9D:B9:FAlinux-cl-2.intranet.gonicus.de
"; #$data = "
gosa_opsi_getSoftwareLicenseUsages
GOSA00:01:6C:9D:B9:FAkrakenarme.intranet.gonicus.de
"; - $data = "
gosa_opsi_getSoftwareLicenseUsages
GOSA00:01:6C:9D:B9:FALicensePool
"; + #$data = "
gosa_opsi_getSoftwareLicenseUsages
GOSA00:01:6C:9D:B9:FALicensePool
"; #$data = "
gosa_opsi_getSoftwareLicenseUsages
GOSA00:01:6C:9D:B9:FA
"; + #$data = "
gosa_opsi_getSoftwareLicenseUsagesForProductId
GOSA00:01:6C:9D:B9:FAfirefox
"; - #$data = "
gosa_opsi_getSoftwareLicense_hash
GOSA00:01:6C:9D:B9:FAl_2009-09-22_09:50:58_0
"; + #$data = "
gosa_opsi_getSoftwareLicense_hash
GOSA00:01:6C:9D:B9:FAl_2009-09-22_09:51:11_0
"; #$data = "
gosa_opsi_getPool
GOSA00:01:6C:9D:B9:FALicensePool
"; #$data = "
gosa_opsi_removeLicense
GOSA00:01:6C:9D:B9:FALicensePooll_2009-09-22_14:06:11
"; + $data = "
gosa_opsi_getReservedLicenses
GOSA00:01:6C:9D:B9:FAkrakenarme.intranet.gonicus.de
"; + ############################## # periodical jobs