Data Exchange - Finalized deletion, changed the definition of attributes (update/reconcile), added an automated test

SVN:trunk[1083]
This commit is contained in:
Romain Quetiez
2011-02-11 09:01:16 +00:00
parent 1da6a264d0
commit d92f89fbe6
5 changed files with 717 additions and 84 deletions

View File

@@ -695,7 +695,7 @@ class AttributeBoolean extends AttributeInteger
public function MakeRealValue($proposedValue)
{
if (is_null($proposedValue)) return null;
if ($proposedValue == '') return null;
if ($proposedValue === '') return null;
if ((int)$proposedValue) return true;
return false;
}

View File

@@ -77,6 +77,13 @@ $aPageParams = array
'default' => '',
'description' => 'local data file, replaces csvdata if specified',
),
'synchronize' => array
(
'mandatory' => false,
'modes' => 'http,cli',
'default' => '1',
'description' => 'If set to 1, then the synchronization will be executed right after the data load',
),
'charset' => array
(
'mandatory' => false,
@@ -224,10 +231,6 @@ else
$oP = new CSVPage("iTop - Data Exchange");
$sCSVData = utils::ReadPostedParam('csvdata');
$sCSVData = "primary_key;brand;model
100;Hewlett;PC100".microtime()."
101;Hewlett;PC101
";
}
@@ -238,6 +241,7 @@ try
// Read parameters
//
$iDataSourceId = ReadMandatoryParam($oP, 'data_source_id');
$sSynchronize = ReadParam($oP, 'synchronize');
$sSep = ReadParam($oP, 'separator');
$sQualifier = ReadParam($oP, 'qualifier');
$sCharSet = ReadParam($oP, 'charset');
@@ -246,6 +250,8 @@ try
$sSimulate = ReadParam($oP, 'simulate');
$sComment = ReadParam($oP, 'comment');
$oLoadStartDate = new DateTime(); // Now
//////////////////////////////////////////////////
//
// Statistics
@@ -305,6 +311,15 @@ try
$bSimulate = false;
}
if ($sSynchronize == '1')
{
$bSynchronize = true;
}
else
{
$bSynchronize = false;
}
//////////////////////////////////////////////////
//
// Security
@@ -444,6 +459,20 @@ try
}
}
//////////////////////////////////////////////////
//
// Synchronize
//
if ($bSynchronize && !$bSimulate)
{
$aDataToReplica = array();
$oDataSource->Synchronize($aDataToReplica, $oLoadStartDate);
//echo "#@# Synchronize() returned :<br/>\n";
//echo "<pre>\n";
//print_r($aDataToReplica);
//echo "</pre>\n";
}
//////////////////////////////////////////////////
//
// Summary of settings and results
@@ -455,6 +484,8 @@ try
if (($sOutput == "summary") || ($sOutput == 'details'))
{
$oP->add_comment("Data Source: ".$iDataSourceId);
$oP->add_comment("Synchronize: ".($bSynchronize ? '1' : '0'));
$oP->add_comment("Class: ".$sClass);
$oP->add_comment("Separator: ".$sSep);
$oP->add_comment("Qualifier: ".$sQualifier);

View File

@@ -45,23 +45,30 @@ class SynchroDataSource extends cmdbAbstractObject
MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('implementation,production,obsolete'), "sql"=>"status", "default_value"=>"implementation", "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeExternalKey("user_id", array("targetclass"=>"User", "jointype"=>null, "allowed_values"=>null, "sql"=>"user_id", "is_null_allowed"=>true, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeClass("scope_class", array("allowed_values"=>null, "sql"=>"scope_class", "default_value"=>null, "class_category" => '', "more_values" => array(), "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeClass("scope_class", array("class_category"=>"bizmodel", "more_values"=>"", "sql"=>"scope_class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeString("scope_restriction", array("allowed_values"=>null, "sql"=>"scope_restriction", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
//MetaModel::Init_AddAttribute(new AttributeDateTime("last_synchro_date", array("allowed_values"=>null, "sql"=>"last_synchro_date", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
// Format: '1 hour', '2 weeks', '3 hoursABCDEF'... Cf DateTime->Modify()
MetaModel::Init_AddAttribute(new AttributeString("full_load_periodicity", array("allowed_values"=>null, "sql"=>"full_load_periodicity", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeString("reconciliation_list", array("allowed_values"=>null, "sql"=>"reconciliation_list", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
// MetaModel::Init_AddAttribute(new AttributeString("reconciliation_list", array("allowed_values"=>null, "sql"=>"reconciliation_list", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeEnum("reconciliation_policy", array("allowed_values"=>new ValueSetEnum('use_primary_key,use_attributes'), "sql"=>"reconciliation_policy", "default_value"=>"use_attributes", "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeEnum("action_on_zero", array("allowed_values"=>new ValueSetEnum('create,error'), "sql"=>"action_on_zero", "default_value"=>"create", "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeEnum("action_on_one", array("allowed_values"=>new ValueSetEnum('update,error,delete'), "sql"=>"action_on_one", "default_value"=>"update", "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeEnum("action_on_multiple", array("allowed_values"=>new ValueSetEnum('take_first,create,error'), "sql"=>"action_on_multiple", "default_value"=>"error", "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeEnum("delete_policy", array("allowed_values"=>new ValueSetEnum('ignore,delete,update,update_then_delete'), "sql"=>"delete_policy", "default_value"=>"ignore", "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeString("delete_policy_update", array("allowed_values"=>null, "sql"=>"delete_policy_update", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
// Format: '1 hour', '2 weeks', '3 hoursABCDEF'... Cf DateTime->Modify()
MetaModel::Init_AddAttribute(new AttributeString("delete_policy_retention", array("allowed_values"=>null, "sql"=>"delete_policy_retention", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeLinkedSet("attribute_list", array("linked_class"=>"SynchroAttribute", "ext_key_to_me"=>"sync_source_id", "allowed_values"=>null, "count_min"=>0, "count_max"=>0, "depends_on"=>array())));
// Display lists
MetaModel::Init_SetZListItems('details', array('name', 'description', 'status', 'user_id', 'scope_class', 'scope_restriction', 'full_load_periodicity', 'reconciliation_list', 'action_on_zero', 'action_on_one', 'action_on_multiple', 'delete_policy', 'delete_policy_update', 'delete_policy_retention')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('details', array('name', 'description', 'status', 'user_id', 'scope_class', 'scope_restriction', 'full_load_periodicity', 'reconciliation_policy', 'action_on_zero', 'action_on_one', 'action_on_multiple', 'delete_policy', 'delete_policy_update', 'delete_policy_retention', 'attribute_list')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('name', 'status', 'scope_class', 'user_id')); // Attributes to be displayed for a list
// Search criteria
MetaModel::Init_SetZListItems('standard_search', array('name', 'status', 'scope_class', 'user_id')); // Criteria of the std search form
@@ -136,8 +143,26 @@ class SynchroDataSource extends cmdbAbstractObject
$sTriggerUpdate .= " END IF;";
$sTriggerUpdate .= " END;";
CMDBSource::Query($sTriggerUpdate);
$sTriggerInsert = "CREATE TRIGGER `{$sTable}_ad` AFTER DELETE ON $sTable";
$sTriggerInsert .= " FOR EACH ROW";
$sTriggerInsert .= " BEGIN";
$sTriggerInsert .= " DELETE FROM priv_sync_replica WHERE id = OLD.id;";
$sTriggerInsert .= " END;";
CMDBSource::Query($sTriggerInsert);
}
protected function AfterDelete()
{
parent::AfterInsert();
$sTable = $this->GetDataTable();
$sDropTable = "DROP TABLE `$sTable`";
CMDBSource::Query($sDropTable);
// TO DO - check that triggers get dropped with the table
}
/**
* Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table)
* and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity
@@ -161,64 +186,96 @@ class SynchroDataSource extends cmdbAbstractObject
// No previous import known, use the full_load_periodicity value... and the current date
$oLastFullLoadStartDate = new DateTime(); // Now
// TO DO: how do we support localization here ??
$oInterval = DateInterval::createFromDateString($this->Get('full_load_periodicity'));
$oLastFullLoadStartDate->sub($oInterval);
$sLoadPeriodicity = trim($this->Get('full_load_periodicity'));
if (strlen($sLoadPeriodicity) > 0)
{
$sInterval = '-'.$sLoadPeriodicity;
// Note: the PHP doc states that Modify return FALSE in case of error
// but, this is actually NOT the case
// Therefore, I do compare before and after, considering that the
// format is incorrect when the datetime remains unchanged
$sBefore = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
$oLastFullLoadStartDate->Modify($sInterval);
$sAfter = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
if ($sBefore == $sAfter)
{
throw new CoreException("Data exchange: Wrong interval specification", array('interval' => $sInterval, 'source_id' => $this->GetKey()));
}
}
}
$sLimitDate = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
// TO DO: remove trace
echo "<p>sLimitDate: $sLimitDate</p>\n";
$sOQL = "SELECT SynchroReplica WHERE status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
$oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array() /* order by*/, array('last_import' => $sLimitDate));
while($oReplica = $oSet->Fetch())
$sSelectToObsolete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
$oSetToObsolete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
while($oReplica = $oSetToObsolete->Fetch())
{
// TO DO: take the appropriate action based on the 'delete_policy' field
$sUpdateOnObsolete = $this->Get('delete_policy');
if ( ($sUpdateOnObsolete == 'update') || ($sUpdateOnObsolete == 'update_then_delete') )
{
// TO DO: remove trace
echo "<p>Destination object: (dest_id:".$oReplica->Get('dest_id').") to be updated.</p>";
// TO DO: update the dest object for real...
$aToUpdate = array();
$aToUpdate = explode(';', $this->Get('delete_policy_update')); //ex: 'status:obsolete;description:stopped',
foreach($aToUpdate as $sUpdateSpec)
{
$aUpdateSpec = explode(':', $sUpdateSpec);
if (count($aUpdateSpec) == 2)
{
$sAttCode = $aUpdateSpec[0];
$sValue = $aUpdateSpec[1];
$aToUpdate[$sAttCode] = $sValue;
}
}
$oReplica->UpdateDestObject($aToUpdate, $oMyChange);
}
// TO DO: remove trace
echo "<p>Replica id:".$oReplica->GetKey()." (dest_id:".$oReplica->Get('dest_id').") marked as obsolete</p>";
$oReplica->Set('status', 'obsolete');
$oReplica->DBUpdateTracked($oMyChange);
}
// Get all the replicas that are 'new' or modified
// Get the list of SQL columns: TO DO: retrieve this list from the SynchroAttributes
//
// Get the list of SQL columns
$sClass = $this->GetTargetClass();
// TO DO: remove trace
echo "<p>TargetClass: $sClass</p>";
$aAttCodes = array();
foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
$sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND update = 1";
$oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */);
while ($oSyncAtt = $oSetAtt->Fetch())
{
if ($sAttCode == 'finalclass') continue;
$aAttCodes[] = $sAttCode;
$aAttCodes[] = $oSyncAtt->Get('attcode');
}
$aColumns = $this->GetSQLColumns($aAttCodes);
$aExtDataFields = array_keys($aColumns);
$aExtDataFields[] = 'primary_key';
$aExtDataSpec = array(
'table' => $this->GetDataTable(),
'join_key' => 'id',
'fields' => array_keys($aColumns));
'table' => $this->GetDataTable(),
'join_key' => 'id',
'fields' => $aExtDataFields
);
$sOQL = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
$oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
// Get the list of reconciliation keys, make sure they are valid
// Get the list of reconciliation keys
$aReconciliationKeys = array();
foreach( explode(',', $this->Get('reconciliation_list')) as $sKey)
if ($this->Get('reconciliation_policy') == 'use_attributes')
{
$sFilterCode = trim($sKey);
if (MetaModel::IsValidFilterCode($this->GetTargetClass(), $sFilterCode))
$sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND reconcile = 1";
$oAttSet = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */);
while ($oSyncAtt = $oAttSet->Fetch())
{
$aReconciliationKeys[] = $sFilterCode;
}
else
{
throw(new Exception('Invalid reconciliation criteria: '.$sFilterCode));
$aReconciliationKeys[] = $oSyncAtt->Get('attcode');
}
}
elseif ($this->Get('reconciliation_policy') == 'use_primary_key')
{
$aReconciliationKeys[] = "primary_key";
}
// TO DO: remove trace
echo "Reconciliation on: {".implode(', ', $aReconciliationKeys)."}<br/>\n";
// TO DO: Get the "real" list of enabled attributes ! Not all of them !
// for now get all scalar & writable attributes
$aAttributes = array();
foreach($aAttCodes as $sAttCode)
{
@@ -229,37 +286,59 @@ class SynchroDataSource extends cmdbAbstractObject
}
}
while($oReplica = $oSet->Fetch())
$sSelectToSync = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
$oSetToSync = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
while($oReplica = $oSetToSync->Fetch())
{
$oReplica->Synchro($this, $aReconciliationKeys, $aAttributes, $oMyChange);
}
// Get all the replicas that are to be deleted
$oInterval = DateInterval::createFromDateString($this->Get('delete_policy_retention'));
$oDeletionDate = $oLastFullLoadStartDate->Sub($oInterval);
//
$oDeletionDate = $oLastFullLoadStartDate;
$sDeleteRetention = trim($this->Get('delete_policy_retention'));
if (strlen($sDeleteRetention) > 0)
{
$sInterval = '-'.$sDeleteRetention;
// Note: the PHP doc states that Modify return FALSE in case of error
// but, this is actually NOT the case
// Therefore, I do compare before and after, considering that the
// format is incorrect when the datetime remains unchanged
$sBefore = $oDeletionDate->Format('Y-m-d H:i:s');
$oDeletionDate->Modify($sInterval);
$sAfter = $oDeletionDate->Format('Y-m-d H:i:s');
if ($sBefore == $sAfter)
{
throw new CoreException("Data exchange: Wrong interval specification", array('interval' => $sInterval, 'source_id' => $this->GetKey()));
}
}
$sDeletionDate = $oDeletionDate->Format('Y-m-d H:i:s');
// TO DO: remove trace
echo "<p>sDeletionDate: $sDeletionDate</p>\n";
$sOQL = "SELECT SynchroReplica WHERE status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
$oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array() /* order by*/, array('last_import' => $sDeletionDate));
while($oReplica = $oSet->Fetch())
$sSelectToDelete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('obsolete') AND status_last_seen < :last_import";
$oSetToDelete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sDeletionDate));
while($oReplica = $oSetToDelete->Fetch())
{
$sUpdateOnObsolete = $this->Get('delete_policy');
if ( ($sUpdateOnObsolete == 'delete') || ($sUpdateOnObsolete == 'update_then_delete') )
{
// TO DO: remove trace
echo "<p>Destination object: (dest_id:".$oReplica->Get('dest_id').") to be DELETED.</p>";
// TO DO: delete the dest object for real...
$oReplica->DeleteDestObject($oMyChange);
}
echo "<p>Replica id:".$oReplica->GetKey()." (dest_id:".$oReplica->Get('dest_id').") marked as deleted</p>";
$oReplica->Set('status', 'deleted');
$oReplica->DBUpdateTracked($oMyChange);
// TO DO: remove trace
echo "<p>Replica id:".$oReplica->GetKey()." (dest_id:".$oReplica->Get('dest_id').") to be deleted</p>";
$oReplica->DBDeleteTracked($oMyChange);
}
return;
}
/**
* Get the list of SQL columns corresponding to a particular list of attribute codes
* Defaults to the whole list of columns for the current task
* Defaults to the whole list of columns for the current class
*/
public function GetSQLColumns($aAttributeCodes = null)
{
@@ -332,6 +411,7 @@ class SynchroDataSource extends cmdbAbstractObject
}
else
{
// TO DO: remove trace
echo "<p>No completed log found</p>\n";
}
return $date;
@@ -358,12 +438,13 @@ class SynchroAttribute extends cmdbAbstractObject
MetaModel::Init_InheritAttributes();
MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeBoolean("enabled", array("allowed_values"=>null, "sql"=>"enabled", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeBoolean("update", array("allowed_values"=>null, "sql"=>"update", "default_value"=>true, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeBoolean("reconcile", array("allowed_values"=>null, "sql"=>"reconcile", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeEnum("update_policy", array("allowed_values"=>new ValueSetEnum('master_locked,master_unlocked,write_once'), "sql"=>"update_policy", "default_value"=>"master_locked", "is_null_allowed"=>false, "depends_on"=>array())));
// Display lists
MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
// Search criteria
// MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
// MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
@@ -391,8 +472,8 @@ class SynchroAttExtKey extends SynchroAttribute
MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
// Display lists
MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
// Search criteria
// MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
@@ -423,8 +504,8 @@ class SynchroAttLinkSet extends SynchroAttribute
MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
// Display lists
MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy', 'row_separator', 'attribute_separator')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'row_separator', 'attribute_separator')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
// Search criteria
// MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
@@ -495,7 +576,7 @@ class SynchroReplica extends cmdbAbstractObject
MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeClass("dest_class", array("allowed_values"=>null, "sql"=>"dest_class", "default_value"=>null, "class_category" => '', "more_values" => array(), "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeClass("dest_class", array("class_category"=>"bizmodel", "more_values"=>"", "sql"=>"dest_class", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeDateTime("status_last_seen", array("allowed_values"=>null, "sql"=>"status_last_seen", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('new,synchronized,modified,orphan,obsolete'), "sql"=>"status", "default_value"=>"new", "is_null_allowed"=>false, "depends_on"=>array())));
@@ -513,6 +594,42 @@ class SynchroReplica extends cmdbAbstractObject
MetaModel::Init_SetZListItems('standard_search', array('sync_source_id', 'status_last_seen', 'status', 'status_dest_creator', 'dest_class', 'dest_id', 'status_last_error')); // Criteria of the std search form
// MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
}
public function DBInsert()
{
throw new CoreException('A synchronization replica must be created only by the mean of triggers');
}
// Overload the deletion -> the replica has been created by the mean of a trigger,
// it will be deleted by the mean of a trigger too
public function DBDeleteTracked_Internal()
{
$oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
$sTable = $oDataSource->GetDataTable();
$sSQL = "DELETE FROM `$sTable` WHERE id = '{$this->GetKey()}'";
CMDBSource::Query($sSQL);
$this->m_bIsInDB = false;
$this->m_iKey = null;
}
public function SetLastError($sMessage, $oException = null)
{
if ($oException)
{
$sText = $sMessage.$oException->getMessage();
}
else
{
$sText = $sMessage;
}
if (strlen($sText) > 255)
{
$sText = substr($sText, 0, 200).'...('.strlen($sText).' chars)...';
}
$this->Set('status_last_error', $sText);
}
public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange)
{
@@ -559,7 +676,7 @@ class SynchroReplica extends cmdbAbstractObject
$aConditions[] = $sCode.'='.$sValue;
}
$sCondition = implode(' AND ', $aConditions);
$this->Set('status_last_error', $iCount.' destination objects match the reconciliation criterias: '.$sCondition);
$this->SetLastError($iCount.' destination objects match the reconciliation criterias: '.$sCondition);
}
break;
@@ -568,7 +685,7 @@ class SynchroReplica extends cmdbAbstractObject
if ($oDestObj == null)
{
$this->Set('status', 'orphan'); // The destination object has been deleted !
$this->Set('status_last_error', 'Destination object deleted unexpectedly');
$this->SetLastError('Destination object deleted unexpectedly');
}
else
{
@@ -586,11 +703,13 @@ class SynchroReplica extends cmdbAbstractObject
*/
protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange)
{
echo "<p>Update object ".$oDestObj->GetName()."</p>";
// TO DO: remove trace
echo "<p>Update object ".$oDestObj->GetHyperLink()."</p>";
foreach($aAttributes as $sAttCode)
{
$value = $this->GetValueFromExtData($sAttCode);
$oDestObj->Set($sAttCode, $value);
// TO DO: remove trace
echo "<p>&nbsp;&nbsp;&nbsp;Setting $sAttCode to $value</p>";
}
try
@@ -601,7 +720,7 @@ class SynchroReplica extends cmdbAbstractObject
}
catch(Exception $e)
{
$this->Set('status_last_error', 'Unable to update destination object');
$this->SetLastError('Unable to update destination object: ', $e);
}
}
@@ -610,17 +729,22 @@ class SynchroReplica extends cmdbAbstractObject
*/
protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange)
{
// TO DO: remove trace
echo "<p>Creating new $sClass</p>";
$oDestObj = MetaModel::NewObject($sClass);
foreach($aAttributes as $sAttCode)
{
$value = $this->GetValueFromExtData($sAttCode);
$oDestObj->Set($sAttCode, $value);
// TO DO: remove trace
echo "<p>&nbsp;&nbsp;&nbsp;Setting $sAttCode to $value</p>";
}
try
{
$oDestObj->DBInsertTracked($oChange);
$iNew = $oDestObj->DBInsertTracked($oChange);
// TO DO: remove trace
echo "<p>Created: $iNew</p>";
$this->Set('dest_id', $oDestObj->GetKey());
$this->Set('dest_class', get_class($oDestObj));
$this->Set('status_dest_creator', true);
@@ -629,10 +753,49 @@ class SynchroReplica extends cmdbAbstractObject
}
catch(Exception $e)
{
$this->Set('status_last_error', 'Unable to update destination object');
$this->SetLastError('Unable to create destination object: ', $e);
}
}
/**
* Update the destination object with given values
*/
public function UpdateDestObject($aValues, $oChange)
{
try
{
$oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
foreach($aValues as $sAttCode => $value)
{
$oDestObj->Set($sAttCode, $value);
}
$oDestObj->DBUpdateTracked($oChange);
}
catch(Exception $e)
{
$this->SetLastError('Unable to update the destination object: ', $e);
}
}
/**
* Delete the destination object
*/
public function DeleteDestObject($oChange)
{
if($this->Get('status_dest_creator'))
{
$oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
try
{
$oDestObj->DBDeleteTracked($oChange);
}
catch(Exception $e)
{
$this->SetLastError('Unable to delete the destination object: ', $e);
}
}
}
/**
* Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
*/

View File

@@ -190,30 +190,6 @@ abstract class TestHandler
$this->m_sOutput = ob_get_clean();
return (count($this->GetErrors()) == 0);
}
}
/**
* Test to execute a piece of code (checks if an error occurs)
*
* @package iTopORM
*/
abstract class TestFunction extends TestHandler
{
// simply overload DoExecute (temporary)
}
/**
* Test to execute a piece of code (checks if an error occurs)
*
* @package iTopORM
*/
abstract class TestWebServices extends TestHandler
{
// simply overload DoExecute (temporary)
static protected function DoPostRequestAuth($sRelativeUrl, $aData, $sLogin = 'admin', $sPassword = 'admin', $sOptionnalHeaders = null)
{
@@ -272,6 +248,29 @@ abstract class TestWebServices extends TestHandler
}
}
/**
* Test to execute a piece of code (checks if an error occurs)
*
* @package iTopORM
*/
abstract class TestFunction extends TestHandler
{
// simply overload DoExecute (temporary)
}
/**
* Test to execute a piece of code (checks if an error occurs)
*
* @package iTopORM
*/
abstract class TestWebServices extends TestHandler
{
}
/**
* Test to execute a piece of code (checks if an error occurs)
*

View File

@@ -1783,6 +1783,446 @@ class TestImportRESTMassive extends TestImportREST
}
}
///////////////////////////////////////////////////////////////////////////
// Test data exchange
///////////////////////////////////////////////////////////////////////////
class TestDataExchange extends TestBizModel
{
static public function GetName()
{
return 'Data exchange';
}
static public function GetDescription()
{
return 'Test REST services: synchro_import and synchro_exec';
}
static public function GetConfigFile() {return '/config-itop.php';}
protected function DoExecScenario($aSingleScenario)
{
echo "<div style=\"padding: 10;\">\n";
echo "<h3 style=\"background-color: #ddddff; padding: 10;\">{$aSingleScenario['desc']}</h3>\n";
$sClass = $aSingleScenario['target_class'];
$aTargetData = $aSingleScenario['target_data'];
$aSourceData = $aSingleScenario['source_data'];
$aTargetAttributes = array_shift($aTargetData);
$aSourceAttributes = array_shift($aSourceData);
if (count($aSourceData) + 1 != count($aTargetData))
{
throw new Exception("Target data must contain exactly ".(count($aSourceData) + 1)." items, found ".count($aTargetData));
}
// Create the data source
//
$oDataSource = new SynchroDataSource();
$oDataSource->Set('name', 'Test data sync '.time());
$oDataSource->Set('description', 'unit test - created automatically');
$oDataSource->Set('status', 'production');
$oDataSource->Set('user_id', 0);
$oDataSource->Set('scope_class', $sClass);
$oDataSource->Set('scope_restriction', '');
$oDataSource->Set('full_load_periodicity', $aSingleScenario['full_load_periodicity']);
$oDataSource->Set('reconciliation_policy', $aSingleScenario['reconciliation_policy']);
$oDataSource->Set('action_on_zero', $aSingleScenario['action_on_zero']);
$oDataSource->Set('action_on_one', $aSingleScenario['action_on_one']);
$oDataSource->Set('action_on_multiple', $aSingleScenario['action_on_multiple']);
$oDataSource->Set('delete_policy', $aSingleScenario['delete_policy']);
$oDataSource->Set('delete_policy_update', $aSingleScenario['delete_policy_update']);
$oDataSource->Set('delete_policy_retention', $aSingleScenario['delete_policy_retention']);
$iDataSourceId = $this->ObjectToDB($oDataSource);
// Specify the attributes for the data source
foreach($aSingleScenario['attributes'] as $aAttribInfo)
{
$oSyncAtt = new SynchroAttribute();
$oSyncAtt->Set('sync_source_id', $iDataSourceId);
$oSyncAtt->Set('attcode', $aAttribInfo['attcode']);
$oSyncAtt->Set('update', $aAttribInfo['do_update']);
$oSyncAtt->Set('reconcile', $aAttribInfo['do_reconcile']);
$this->ObjectToDB($oSyncAtt);
}
// Prepare list of prefixes -> make sure objects are unique with regard to the reconciliation scheme
$aPrefixes = array(); // attcode => prefix
foreach($aSourceAttributes as $iDummy => $sAttCode)
{
$aPrefixes[$sAttCode] = ''; // init with something
}
foreach($aSingleScenario['attributes'] as $aAttribInfo)
{
if ($aAttribInfo['do_reconcile'])
{
$aPrefixes[$aAttribInfo['attcode']] = 'TEST_'.$iDataSourceId.'_';
}
}
// List existing objects (to be ignored in the analysis
//
$oAllObjects = new DBObjectSet(new DBObjectSearch($sClass));
$aExisting = $oAllObjects->ToArray(true);
$sExistingIds = implode(', ', array_keys($aExisting));
// Create the initial object list
//
$aInitialTarget = $aTargetData[0];
foreach($aInitialTarget as $aObjFields)
{
$oNewTarget = MetaModel::NewObject($sClass);
foreach($aTargetAttributes as $iAtt => $sAttCode)
{
$oNewTarget->Set($sAttCode, $aPrefixes[$sAttCode].$aObjFields[$iAtt]);
}
$this->ObjectToDB($oNewTarget);
}
foreach($aTargetData as $iRow => $aExpectedObjects)
{
// Check the status (while ignoring existing objects)
//
$oObjects = new DBObjectSet(DBObjectSearch::FromOQL("SELECT $sClass WHERE id NOT IN($sExistingIds)"));
$aFound = $oObjects->ToArray();
$aErrors_Unexpected = array();
foreach($aFound as $iObj => $oObj)
{
// Is this object in the expected objects list
$bFoundMatch = false;
foreach($aExpectedObjects as $iExp => $aValues)
{
$bDoesMatch = true;
foreach($aTargetAttributes as $iCol => $sAttCode)
{
if ($oObj->Get($sAttCode) != $aPrefixes[$sAttCode].$aValues[$iCol])
{
$bDoesMatch = false;
break;
}
}
if ($bDoesMatch)
{
$bFoundMatch = true;
unset($aExpectedObjects[$iExp]);
break;
}
}
if (!$bFoundMatch)
{
$aErrors_Unexpected[] = $oObj->GetKey();
}
}
// Display the current status
//
echo "<p>Status at step $iRow</p>\n";
$aCurrentDataSet = array();
foreach($aFound as $iObj => $oObj)
{
$aObjDesc = array(
'Status' => (in_array($iObj, $aErrors_Unexpected) ? 'unexpected' : 'ok'),
'Object' => $oObj->GetHyperLink()
);
foreach($aTargetAttributes as $iCol => $sAttCode)
{
$aObjDesc[$sAttCode] = $oObj->Get($sAttCode);
}
$aCurrentDataSet[] = $aObjDesc;
}
if (count($aExpectedObjects) > 0)
{
foreach($aExpectedObjects as $iExp => $aValues)
{
$aObjDesc = array(
'Status' => 'missing',
'Object' => 'n/a'
);
foreach($aTargetAttributes as $iCol => $sAttCode)
{
$aObjDesc[$sAttCode] = $aPrefixes[$sAttCode].$aValues[$iCol];
}
$aCurrentDataSet[] = $aObjDesc;
}
}
echo MyHelpers::make_table_from_assoc_array($aCurrentDataSet);
if ((count($aErrors_Unexpected) > 0) || (count($aExpectedObjects) > 0))
{
throw new UnitTestException("The current status in iTop does not match the expectations");
}
// If not on the final row, run a data exchange sequence
//
if (array_key_exists($iRow, $aSourceData))
{
$aToBeLoaded = $aSourceData[$iRow];
$sCsvData = implode(';', $aSourceAttributes)."\n";
foreach($aToBeLoaded as $aDataRow)
{
$aFinalData = array();
foreach($aDataRow as $iCol => $value)
{
$sAttCode = $aSourceAttributes[$iCol];
$aFinalData[] = $aPrefixes[$sAttCode].$value;
}
$sCsvData .= implode(';', $aFinalData)."\n";
}
$aPostData = array('csvdata' => $sCsvData);
$aImportArgs = array(
'data_source_id' => $iDataSourceId,
'separator' => ';',
'simulate' => 0,
'output' => 'details',
);
$aGetParams = array();
$aGetParamReport = array();
foreach($aImportArgs as $sArg => $sValue)
{
$aGetParams[] = $sArg.'='.urlencode($sValue);
$aGetParamReport[] = $sArg.'='.$sValue;
}
$sGetParams = implode('&', $aGetParams);
$sLogin = isset($aSingleScenario['login']) ? $aSingleScenario['login'] : 'admin';
$sPassword = isset($aSingleScenario['password']) ? $aSingleScenario['password'] : 'admin';
$sRes = self::DoPostRequestAuth('../synchro/synchro_import.php?'.$sGetParams, $aPostData, $sLogin, $sPassword);
// Report the load results
//
if (strlen($sCsvData) > 5000)
{
$sCsvDataViewable = 'INPUT TOO LONG TO BE DISPLAYED ('.strlen($sCsvData).")\n".substr($sCsvData, 0, 500)."\n... TO BE CONTINUED";
}
else
{
$sCsvDataViewable = $sCsvData;
}
echo "<div style=\"\">\n";
echo " <pre class=\"vardump\">$sCsvDataViewable</pre>\n";
echo "</div>\n";
echo "<pre class=\"vardump\" style=\"clear: both; padding: 15; background-color: black; color: green;\">$sRes</pre>\n";
if (stripos($sRes, 'exception') !== false)
{
throw new UnitTestException('Encountered an Exception during the last import/synchro');
}
if (stripos($sRes, 'error') !== false)
{
throw new UnitTestException('Encountered an Error during the last import/synchro');
}
}
}
return;
echo "</div>\n";
}
protected function DoExecute()
{
$aScenarios = array(
array(
'desc' => 'Simple scenario with delete option',
'login' => 'admin',
'password' => 'admin',
'target_class' => 'ApplicationSolution',
'full_load_periodicity' => '1 hour',
'reconciliation_policy' => 'use_attributes',
'action_on_zero' => 'create',
'action_on_one' => 'update',
'action_on_multiple' => 'error',
'delete_policy' => 'update_then_delete',
'delete_policy_update' => 'status:obsolete',
'delete_policy_retention' => '',
'source_data' => array(
array('primary_key', 'org_id', 'name', 'status'),
array(
array('obj_A', 2, 'obj_A', 'production'),
array('obj_B', 2, 'obj_B', 'production'),
),
array(
),
),
'target_data' => array(
array('org_id', 'name', 'status'),
array(
// Initial state
array(2, 'obj_A', 'production'),
),
array(
array(2, 'obj_A', 'production'),
array(2, 'obj_B', 'production'),
),
array(
array(2, 'obj_A', 'obsolete'),
// deleted !
),
),
'attributes' => array(
array(
'attcode' => 'org_id',
'do_reconcile' => false,
'do_update' => true,
),
array(
'attcode' => 'name',
'do_reconcile' => true,
'do_update' => true,
),
array(
'attcode' => 'status',
'do_reconcile' => false,
'do_update' => true,
),
),
),
//);
//$aScenarios = array(
array(
'desc' => 'Update then delete with retention (to complete with manual testing)',
'login' => 'admin',
'password' => 'admin',
'target_class' => 'ApplicationSolution',
'full_load_periodicity' => '1 hour',
'reconciliation_policy' => 'use_attributes',
'action_on_zero' => 'create',
'action_on_one' => 'update',
'action_on_multiple' => 'error',
'delete_policy' => 'update_then_delete',
'delete_policy_update' => 'status:obsolete',
'delete_policy_retention' => '1 hour',
'source_data' => array(
array('primary_key', 'org_id', 'name', 'status'),
array(
array('obj_A', 2, 'obj_A', 'production'),
),
array(
),
),
'target_data' => array(
array('org_id', 'name', 'status'),
array(
// Initial state
),
array(
array(2, 'obj_A', 'production'),
),
array(
array(2, 'obj_A', 'obsolete'),
// deleted !
),
),
'attributes' => array(
array(
'attcode' => 'org_id',
'do_reconcile' => false,
'do_update' => true,
),
array(
'attcode' => 'name',
'do_reconcile' => true,
'do_update' => true,
),
array(
'attcode' => 'status',
'do_reconcile' => false,
'do_update' => true,
),
),
),
//);
//$aScenarios = array(
array(
'desc' => 'Simple scenario loading a few ApplicationSolution',
'login' => 'admin',
'password' => 'admin',
'target_class' => 'ApplicationSolution',
'full_load_periodicity' => '1 hour',
'reconciliation_policy' => 'use_attributes',
'action_on_zero' => 'create',
'action_on_one' => 'update',
'action_on_multiple' => 'error',
'delete_policy' => 'update',
'delete_policy_update' => 'status:obsolete',
'delete_policy_retention' => '',
'source_data' => array(
array('primary_key', 'org_id', 'name', 'status'),
array(
array('obj_A', 2, 'obj_A', 'production'),
array('obj_B', 2, 'obj_B', 'implementation'),
array('obj_C', 2, 'obj_C', 'implementation'),
),
array(
array('obj_A', 2, 'obj_A', 'production'),
array('obj_C', 2, 'obj_C', 'implementation'),
array('obj_D', 2, 'obj_D', 'implementation'),
),
array(
array('obj_C', 2, 'obj_C', 'production'),
),
),
'target_data' => array(
array('org_id', 'name', 'status'),
array(
// Initial state
array(2, 'obj_A', 'implementation'),
array(2, 'obj_B', 'production'),
array(2, 'obj_B', 'implementation'),
),
array(
array(2, 'obj_A', 'production'),
array(2, 'obj_B', 'production'),
array(2, 'obj_B', 'implementation'),
array(2, 'obj_C', 'implementation'),
),
array(
array(2, 'obj_A', 'production'),
array(2, 'obj_B', 'production'),
array(2, 'obj_B', 'implementation'),
array(2, 'obj_C', 'implementation'),
array(2, 'obj_D', 'implementation'),
),
array(
array(2, 'obj_A', 'obsolete'),
array(2, 'obj_B', 'production'),
array(2, 'obj_B', 'implementation'),
array(2, 'obj_C', 'production'),
array(2, 'obj_D', 'obsolete'),
),
),
'attributes' => array(
array(
'attcode' => 'org_id',
'do_reconcile' => false,
'do_update' => true,
),
array(
'attcode' => 'name',
'do_reconcile' => true,
'do_update' => true,
),
array(
'attcode' => 'status',
'do_reconcile' => false,
'do_update' => true,
),
),
),
);
foreach ($aScenarios as $aSingleScenario)
{
$this->DoExecScenario($aSingleScenario);
}
}
}
///////////////////////////////////////////////////////////////////////////
// Test SOAP services