Trac#56 Improvements to the SOAP web services and to the first operation CreateIncidentTicket

SVN:trunk[230]
This commit is contained in:
Romain Quetiez
2010-01-08 15:24:57 +00:00
parent 9fc06ff6e4
commit 0f71144a25
10 changed files with 815 additions and 98 deletions

View File

@@ -1,5 +1,7 @@
<?php
require_once('../webservices/itopsoaptypes.class.inc.php');
/**
* Create Ticket web service
* Web Service API wrapper
@@ -52,11 +54,51 @@ class WebServiceResult
public function __construct()
{
$this->m_bStatus = true;
$this->m_aResult = array();
$this->m_aErrors = array();
$this->m_aWarnings = array();
$this->m_aInfos = array();
}
public function ToSoapStructure()
{
$aResults = array();
foreach($this->m_aResult as $sLabel => $aData)
{
$aValues = array();
foreach($aData as $sKey => $value)
{
$aValues[] = new SoapResultData($sKey, $value);
}
$aResults[] = new SoapResultMessage($sLabel, $aValues);
}
$aInfos = array();
foreach($this->m_aInfos as $sMessage)
{
$aInfos[] = new SoapLogMessage($sMessage);
}
$aWarnings = array();
foreach($this->m_aWarnings as $sMessage)
{
$aWarnings[] = new SoapLogMessage($sMessage);
}
$aErrors = array();
foreach($this->m_aErrors as $sMessage)
{
$aErrors[] = new SoapLogMessage($sMessage);
}
$oRet = new SOAPResult(
$this->m_bStatus,
$aResults,
new SOAPResultLog($aErrors),
new SOAPResultLog($aWarnings),
new SOAPResultLog($aInfos)
);
return $oRet;
}
/**
* Did the current processing encounter a stopper issue ?
*
@@ -67,21 +109,37 @@ class WebServiceResult
return $this->m_bStatus;
}
/**
* Add result details - object reference
*
* @param string sLabel
* @param object oObject
*/
public function AddResultObject($sLabel, $oObject)
{
$this->m_aResult[$sLabel] = array(
'id' => $oObject->GetKey(),
'name' => $oObject->GetName(),
'url' => $oObject->GetHyperlink(),
);
}
/**
* Log an error
*
* @param description $sDescription
* @param string sDescription
*/
public function LogError($sDescription)
{
$this->m_aErrors[] = $sDescription;
$this->m_bStatus = false;
// Note: SOAP do transform false into null
$this->m_bStatus = 0;
}
/**
* Log a warning
*
* @param description $sDescription
* @param string sDescription
*/
public function LogWarning($sDescription)
{
@@ -91,8 +149,8 @@ class WebServiceResult
/**
* Log an error or a warning
*
* @param string $sDescription
* @param boolean $bIsStopper
* @param string sDescription
* @param boolean bIsStopper
*/
public function LogIssue($sDescription, $bIsStopper = true)
{
@@ -109,15 +167,77 @@ class WebServiceResult
{
$this->m_aInfos[] = $sDescription;
}
protected static function LogToText($aLog)
{
return implode("\n", $aLog);
}
public function GetInfoAsText()
{
return self::LogToText($this->m_aInfos);
}
public function GetWarningsAsText()
{
return self::LogToText($this->m_aWarnings);
}
public function GetErrorsAsText()
{
return self::LogToText($this->m_aErrors);
}
public function GetReturnedDataAsText()
{
$sRet = '';
foreach ($this->m_aResult as $sKey => $value)
{
$sRet .= "===== $sKey =====\n";
$sRet .= print_r($value, true);
}
return $sRet;
}
}
class WebServiceResultFailedLogin extends WebServiceResult
{
public function __construct($sLogin)
{
parent::__construct();
$this->LogError("Wrong credentials: '$sLogin'");
}
}
class WebServices
{
/**
* Helper to log a service delivery
*
* @param string sVerb
* @param array aArgs
* @param WebServiceResult oRes
*
*/
protected function LogUsage($sVerb, $oRes)
{
$oLog = new EventWebService();
$oLog->Set('userinfo', UserRights::GetUser());
$oLog->Set('verb', $sVerb);
$oLog->Set('result', $oRes->IsOk());
$oLog->Set('log_info', $oRes->GetInfoAsText());
$oLog->Set('log_warning', $oRes->GetWarningsAsText());
$oLog->Set('log_error', $oRes->GetErrorsAsText());
$oLog->Set('data', $oRes->GetReturnedDataAsText());
$oLog->DBInsertNoReload();
}
/**
* Helper to set an external key
*
* @param string sAttCode
* @param array aCallerDesc
* @param array aExtKeyDesc
* @param DBObject oTargetObj
* @param WebServiceResult oRes
*
@@ -190,6 +310,11 @@ class WebServices
$aItemsNotFound = array();
foreach ($aLinkList as $aItemData)
{
if (!array_key_exists('class', $aItemData))
{
$oRes->LogWarning("Linked object descriptor: missing 'class' specification");
continue; // skip
}
$sTargetClass = $aItemData['class'];
if (!MetaModel::IsValidClass($sTargetClass))
{
@@ -273,6 +398,84 @@ class WebServices
return $aItemsNotFound;
}
static protected function SoapStructToExternalKeySearch(SoapExternalKeySearch $oExternalKeySearch)
{
$aRes = array();
foreach($oExternalKeySearch->conditions as $oSearchCondition)
{
$aRes[$oSearchCondition->attcode] = $oSearchCondition->value;
}
return $aRes;
}
static protected function SoapStructToLinkCreationSpec(SoapLinkCreationSpec $oLinkCreationSpec)
{
$aRes = array
(
'class' => $oLinkCreationSpec->class,
'search' => array(),
'link_values' => array(),
);
foreach($oLinkCreationSpec->conditions as $oSearchCondition)
{
$aRes['search'][$oSearchCondition->attcode] = $oSearchCondition->value;
}
foreach($oLinkCreationSpec->attributes as $oAttributeValue)
{
$aRes['link_values'][$oAttributeValue->attcode] = $oAttributeValue->value;
}
return $aRes;
}
/**
* Get the server version (TODO: get it dynamically, where ?)
*
* @return WebServiceResult
*/
public function GetVersion()
{
return "0.8";
}
public function CreateIncidentTicket($sLogin, $sPassword, $sType, $sDescription, $sInitialSituation, $sImpact, $oCallerDesc, $oCustomerDesc, $oWorkgroupDesc, $aSOAPImpactedCIs, $sSeverity)
{
if (!UserRights::Login($sLogin, $sPassword))
{
$oRes = new WebServiceResultFailedLogin($sLogin);
$this->LogUsage(__FUNCTION__, $oRes);
return $oRes->ToSoapStructure();
}
$aCallerDesc = self::SoapStructToExternalKeySearch($oCallerDesc);
$aCustomerDesc = self::SoapStructToExternalKeySearch($oCustomerDesc);
$aWorkgroupDesc = self::SoapStructToExternalKeySearch($oWorkgroupDesc);
$aImpactedCIs = array();
foreach($aSOAPImpactedCIs as $oImpactedCIs)
{
$aImpactedCIs[] = self::SoapStructToLinkCreationSpec($oImpactedCIs);
}
$oRes = $this->_CreateIncidentTicket
(
$sType,
$sDescription,
$sInitialSituation,
$sImpact,
$aCallerDesc,
$aCustomerDesc,
$aWorkgroupDesc,
$aImpactedCIs,
$sSeverity
);
return $oRes->ToSoapStructure();
}
/**
* Create an incident ticket from a monitoring system
* Some CIs might be specified (by their name/IP)
@@ -287,36 +490,61 @@ class WebServices
*
* @return WebServiceResult
*/
function CreateIncidentTicket($sDescription, $sInitialSituation, $aCallerDesc, $aCustomerDesc, $aWorkgroupDesc, $aImpactedCIs, $sSeverity)
protected function _CreateIncidentTicket($sType, $sDescription, $sInitialSituation, $sImpact, $aCallerDesc, $aCustomerDesc, $aWorkgroupDesc, $aImpactedCIs, $sSeverity)
{
$oRes = new WebServiceResult();
new CMDBChange();
$oMyChange = MetaModel::NewObject("CMDBChange");
$oMyChange->Set("date", time());
$oMyChange->Set("userinfo", "Administrator");
$iChangeId = $oMyChange->DBInsertNoReload();
$oNewTicket = MetaModel::NewObject('bizIncidentTicket');
$oNewTicket->Set('title', $sDescription);
$oNewTicket->Set('initial_situation', $sInitialSituation);
$oNewTicket->Set('severity', $sSeverity);
$this->SetExternalKey('org_id', $aCustomerDesc, $oNewTicket, $oRes);
$this->SetExternalKey('caller_id', $aCallerDesc, $oNewTicket, $oRes);
$this->SetExternalKey('workgroup_id', $aWorkgroupDesc, $oNewTicket, $oRes);
$aDevicesNotFound = $this->AddLinkedObjects('impacted_infra_manual', 'logInfra', $aImpactedCIs, $oNewTicket, $oRes);
if (count($aDevicesNotFound) > 0)
try
{
$oTargetObj->Set('impact', implode(', ', $aDevicesNotFound));
new CMDBChange();
$oMyChange = MetaModel::NewObject("CMDBChange");
$oMyChange->Set("date", time());
$oMyChange->Set("userinfo", "Administrator");
$iChangeId = $oMyChange->DBInsertNoReload();
$oNewTicket = MetaModel::NewObject('bizIncidentTicket');
$oNewTicket->Set('type', $sType);
$oNewTicket->Set('title', $sDescription);
$oNewTicket->Set('initial_situation', $sInitialSituation);
$oNewTicket->Set('severity', $sSeverity);
$this->SetExternalKey('org_id', $aCustomerDesc, $oNewTicket, $oRes);
$this->SetExternalKey('caller_id', $aCallerDesc, $oNewTicket, $oRes);
$this->SetExternalKey('workgroup_id', $aWorkgroupDesc, $oNewTicket, $oRes);
$aDevicesNotFound = $this->AddLinkedObjects('impacted_infra_manual', 'logInfra', $aImpactedCIs, $oNewTicket, $oRes);
if (count($aDevicesNotFound) > 0)
{
$oNewTicket->Set('impact', $sImpact.' - Related CIs: '.implode(', ', $aDevicesNotFound));
}
else
{
$oNewTicket->Set('impact', $sImpact);
}
if (!$oNewTicket->CheckToInsert())
{
$oRes->LogError("The ticket could not be created due to forbidden values (or inconsistent values)");
}
if ($oRes->IsOk())
{
$iId = $oNewTicket->DBInsertTrackedNoReload($oMyChange);
$oRes->LogInfo("Created ticket #$iId");
$oRes->AddResultObject('created', $oNewTicket);
}
}
catch (CoreException $e)
{
$oRes->LogError($e->getMessage());
}
catch (Exception $e)
{
$oRes->LogError($e->getMessage());
}
if ($oRes->IsOk())
{
$iId = $oNewTicket->DBInsertTrackedNoReload($oMyChange);
$oRes->LogInfo("Created ticket #$iId");
}
$this->LogUsage(__FUNCTION__, $oRes);
return $oRes;
}
}