Skip to content

Commit

Permalink
Fix(Inventory) add history on Item_Devices add / update / delete when…
Browse files Browse the repository at this point in the history
… Mainasset already exist (#16750)

* Fix(inventory): add history on Item_Devices add / update / delete when Mainasset already exist

* add TU

* fix TU

* fix TU
  • Loading branch information
stonebuzz committed Mar 12, 2024
1 parent fd2f456 commit da62dcc
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/Inventory/Asset/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function handle()
'items_id' => $this->item->fields['id'],
'is_dynamic' => 1
] + $this->handleInput($val, $itemdevice);
$itemdevice->add(Sanitizer::sanitize($itemdevice_data), [], false);
$itemdevice->add(Sanitizer::sanitize($itemdevice_data), [], !$this->item->isNewItem()); //log only if mainitem is not new
$this->itemdeviceAdded($itemdevice, $val);
}

Expand All @@ -188,12 +188,7 @@ public function handle()
foreach ($existing as $data) {
foreach ($data as $itemdevice_data) {
if ($itemdevice_data['is_dynamic'] == 1) {
$DB->delete(
$itemdevice->getTable(),
[
'id' => $itemdevice_data['id']
]
);
$itemdevice->delete(['id' => $itemdevice_data['id']], true, !$this->item->isNewItem()); //log only if mainitem is not new
}
}
}
Expand Down
143 changes: 143 additions & 0 deletions tests/functional/Glpi/Inventory/Assets/Bios.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,147 @@ public function testInventoryUpdate()
//"original" firmware has been removed
$this->boolean($item_bios->getFromDB($item_bios_id))->isFalse();
}

public function testHistory()
{
global $DB;
$item_bios = new \Item_DeviceFirmware();

$xml_source = '<?xml version="1.0" encoding="UTF-8"?>
<REQUEST>
<CONTENT>
<DEVICE>
<FIRMWARES>
<DESCRIPTION>device firmware</DESCRIPTION>
<MANUFACTURER>Cisco</MANUFACTURER>
<NAME>SG300-10MP</NAME>
<TYPE>device</TYPE>
<VERSION>1.4.12</VERSION>
</FIRMWARES>
<INFO>
<COMMENTS>SG300-10MPP 10-Port Gigabit PoE+ Managed Switch</COMMENTS>
<CONTACT>TECLIB</CONTACT>
<FIRMWARE>1.4.12</FIRMWARE>
<ID>2304</ID>
<IPS>
<IP>10.10.10.10</IP>
<IP>127.0.0.1</IP>
</IPS>
<LOCATION>Teclib Caaen</LOCATION>
<MAC>cc:8e:71:fb:42:1b</MAC>
<MANUFACTURER>Cisco</MANUFACTURER>
<MODEL>SG300-10MP</MODEL>
<NAME>TECLIB678</NAME>
<SERIAL>SDGFSDF51687SDF</SERIAL>
<TYPE>NETWORKING</TYPE>
<UPTIME>5 hours, 35:46.00</UPTIME>
</INFO>
</DEVICE>
<MODULEVERSION>5.1</MODULEVERSION>
<PROCESSNUMBER>6465205</PROCESSNUMBER>
</CONTENT>
<DEVICEID>teclib-home-2022-09-14-14-57-39</DEVICEID>
<QUERY>SNMPQUERY</QUERY>
</REQUEST>';

//computer inventory knows bios
$this->doInventory($xml_source, true);

$networkequipement = new \NetworkEquipment();
$networkequipement->getFromDBByCrit(['name' => 'TECLIB678']);
$this->integer($networkequipement->getID())->isGreaterThan(0);

//bios present in the inventory source is dynamic
$firmwares = $item_bios->find(['itemtype' => \NetworkEquipment::class, 'items_id' => $networkequipement->getID(), 'is_dynamic' => 1]);
$this->integer(count($firmwares))->isIdenticalTo(1);

//no log from first import (Computer or Monitor)
$nblogsnow = countElementsInTable(\Log::getTable());
$logs = $DB->request([
'FROM' => \Log::getTable(),
'LIMIT' => $nblogsnow,
'OFFSET' => $this->nblogs,
'WHERE' => [
'itemtype' => \NetworkEquipment::class,
'items_id' => $networkequipement->getID(),
'itemtype_link' => \DeviceFirmware::class
]
]);
$this->integer(count($logs))->isIdenticalTo(0);

// change version
// As the version is one of the reconciliation keys, we should see a deletion and then an addition.
$xml_source = '<?xml version="1.0" encoding="UTF-8"?>
<REQUEST>
<CONTENT>
<DEVICE>
<FIRMWARES>
<DESCRIPTION>device firmware</DESCRIPTION>
<MANUFACTURER>Cisco</MANUFACTURER>
<NAME>SG300-10MP</NAME>
<TYPE>device</TYPE>
<VERSION>1.4.13</VERSION>
</FIRMWARES>
<INFO>
<COMMENTS>SG300-10MPP 10-Port Gigabit PoE+ Managed Switch</COMMENTS>
<CONTACT>TECLIB</CONTACT>
<FIRMWARE>1.4.13</FIRMWARE>
<ID>2304</ID>
<IPS>
<IP>10.10.10.10</IP>
<IP>127.0.0.1</IP>
</IPS>
<LOCATION>Teclib Caaen</LOCATION>
<MAC>cc:8e:71:fb:42:1b</MAC>
<MANUFACTURER>Cisco</MANUFACTURER>
<MODEL>SG300-10MP</MODEL>
<NAME>TECLIB678</NAME>
<SERIAL>SDGFSDF51687SDF</SERIAL>
<TYPE>NETWORKING</TYPE>
<UPTIME>5 hours, 35:46.00</UPTIME>
</INFO>
</DEVICE>
<MODULEVERSION>5.1</MODULEVERSION>
<PROCESSNUMBER>6465205</PROCESSNUMBER>
</CONTENT>
<DEVICEID>teclib-home-2022-09-14-14-57-39</DEVICEID>
<QUERY>SNMPQUERY</QUERY>
</REQUEST>';
$this->doInventory($xml_source, true);


$networkequipement = new \NetworkEquipment();
$networkequipement->getFromDBByCrit(['name' => 'TECLIB678']);
$this->integer($networkequipement->getID())->isGreaterThan(0);

//bios present in the inventory source is dynamic
$firmwares = $item_bios->find(['itemtype' => \NetworkEquipment::class, 'items_id' => $networkequipement->getID(), 'is_dynamic' => 1]);
$this->integer(count($firmwares))->isIdenticalTo(1);

//one log for delete old firmware
$logs = $DB->request([
'FROM' => \Log::getTable(),
'WHERE' => [
'itemtype' => \NetworkEquipment::class,
'items_id' => $networkequipement->getID(),
'itemtype_link' => \DeviceFirmware::class,
'linked_action' => \Log::HISTORY_DELETE_DEVICE
]
]);

$this->integer(count($logs))->isIdenticalTo(1);

//one log for add new firmware
$logs = $DB->request([
'FROM' => \Log::getTable(),
'WHERE' => [
'itemtype' => \NetworkEquipment::class,
'items_id' => $networkequipement->getID(),
'itemtype_link' => \DeviceFirmware::class,
'linked_action' => \Log::HISTORY_ADD_DEVICE
]
]);

$this->integer(count($logs))->isIdenticalTo(1);
}
}
6 changes: 4 additions & 2 deletions tests/functional/Glpi/Inventory/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -1783,16 +1783,18 @@ public function testUpdateComputer()
'OFFSET' => $nblogsnow,
]);

$this->integer(count($logs))->isIdenticalTo(4412);
$this->integer(count($logs))->isIdenticalTo(4418);

$expected_types_count = [
0 => 3, //Agent version, disks usage
\Log::HISTORY_ADD_DEVICE => 2, //new item_device...
\Log::HISTORY_DELETE_DEVICE => 2, //delete item_device...
\Log::HISTORY_ADD_RELATION => 1, //new IPNetwork/IPAddress
\Log::HISTORY_DEL_RELATION => 2,//monitor-computer relation
\Log::HISTORY_ADD_SUBITEM => 3247,//network port/name, ip address, VMs, Software
\Log::HISTORY_UPDATE_SUBITEM => 828,//disks usage, software updates
\Log::HISTORY_DELETE_SUBITEM => 99,//networkport and networkname, Software?
\Log::HISTORY_CREATE_ITEM => 230, //virtual machines, os, manufacturer, net ports, net names, software category ...
\Log::HISTORY_CREATE_ITEM => 232, //virtual machines, os, manufacturer, net ports, net names, software category / item_device...
\Log::HISTORY_UPDATE_RELATION => 2,//kernel version
];

Expand Down

0 comments on commit da62dcc

Please sign in to comment.