Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(Inventory): set default instantiation type #18746

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions phpunit/functional/Glpi/Inventory/Assets/NetworkPortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1531,4 +1531,126 @@ public function testNetworkEquipmentsConnectionsConverted(): void
//8 connections on port, but one related to existing equipment
$this->assertCount(7, $unmanaged->find());
}

public function testDefaultInstantiationType()
{
global $DB;
$json_str = '
{
"action": "inventory",
"content": {
"bios": {
trasher marked this conversation as resolved.
Show resolved Hide resolved
"assettag": "Asset-1234567890",
"bdate": "2013-10-29",
"bmanufacturer": "American Megatrends Inc.",
"bversion": "1602",
"mmanufacturer": "ASUSTeK COMPUTER INC.",
"mmodel": "Z87-A",
"msn": "131219362301208",
"skunumber": "All",
"smanufacturer": "ASUS",
"smodel": "All Series"
},
"hardware": {
"chassis_type": "Desktop",
"datelastloggeduser": "Mon Dec 11 09:34",
"defaultgateway": "192.168.1.1",
"dns": "127.0.0.53",
"lastloggeduser": "teclib",
"memory": 32030,
"name": "teclib-asus-desktop",
"swap": 2047,
"uuid": "31042c80-d7da-11dd-93d0-bcee7b8de946",
"vmsystem": "Physical",
"workgroup": "home"
},
"networks": [
stonebuzz marked this conversation as resolved.
Show resolved Hide resolved
{
"description": "vmk1",
"ipaddress": "10.15.55.81",
"ipmask": "255.255.254.0",
"mac": "00:50:56:6b:fa:c6",
"mtu": 1500,
"status": "up",
"virtualdev": true
},
{
"description": "vmk2",
"ipaddress": "10.15.55.81",
"ipmask": "255.255.254.0",
"mtu": 1500,
"status": "up",
"virtualdev": true
}
],
"versionclient": "GLPI-Agent_v1.4-1"
},
"deviceid": "teclib-asus-desktop-2022-09-20-16-43-09",
"itemtype": "Computer"
}';

$json = json_decode($json_str);
$this->doInventory($json);

//check created agent
$agenttype = $DB->request(['FROM' => \AgentType::getTable(), 'WHERE' => ['name' => 'Core']])->current();
$agents = $DB->request(['FROM' => \Agent::getTable()]);
$this->assertCount(1, $agents);
$agent = $agents->current();
$this->assertIsArray($agent);
$this->assertSame('teclib-asus-desktop-2022-09-20-16-43-09', $agent['deviceid']);
$this->assertSame('teclib-asus-desktop-2022-09-20-16-43-09', $agent['name']);
$this->assertSame('Computer', $agent['itemtype']);
$this->assertSame($agenttype['id'], $agent['agenttypes_id']);
$this->assertGreaterThan(0, $agent['items_id']);

//check created computer
$computers_id = $agent['items_id'];
$this->assertGreaterThan(0, $computers_id);
$computer = new \Computer();
$this->assertTrue($computer->getFromDB($computers_id));

//check created networkport
$networkport1 = new \NetworkPort();

// vmk1 -> ethernet -> NetworkPortEthernet
$this->assertTrue($networkport1->getFromDbByCrit(
[
'itemtype' => 'Computer',
'items_id' => $computers_id,
'name' => 'vmk1',
'instantiation_type' => 'NetworkPortEthernet'
]
));

// vmk2 -> no mac -> no instantiation type
$this->assertTrue($networkport1->getFromDbByCrit(
[
'itemtype' => 'Computer',
'items_id' => $computers_id,
'name' => 'vmk2',
'instantiation_type' => null
]
));
// update vmk1 to set instantiation_type to null to test update case
$this->assertTrue($networkport1->update([
'id' => $networkport1->fields['id'],
'instantiation_type' => null,
'is_dynamic' => 1 // prevent lock
]));

//redo inventory
$json = json_decode($json_str);
$this->doInventory($json);

// vmk1 -> ethernet -> NetworkPortEthernet
$this->assertTrue($networkport1->getFromDbByCrit(
[
'itemtype' => 'Computer',
'items_id' => $computers_id,
'name' => 'vmk1',
'instantiation_type' => 'NetworkPortEthernet'
]
));
}
}
2 changes: 1 addition & 1 deletion phpunit/functional/Glpi/Inventory/InventoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private function checkComputer1($computers_id)
], [
'logical_number' => 0,
'name' => 'virbr0-nic',
'instantiation_type' => null,
'instantiation_type' => 'NetworkPortEthernet',
'mac' => '52:54:00:fa:20:0e',
'ifstatus' => '2',
'ifinternalstatus' => '2',
Expand Down
11 changes: 10 additions & 1 deletion src/Inventory/Asset/InventoryNetworkPort.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ private function handleUpdates()
$networkport->update(Sanitizer::sanitize($criteria));
}

// force NetworkPortEthernet type if no instantiation_type and mac is set
if (!property_exists($data, 'instantiation_type') && property_exists($data, 'mac') && !empty($data->mac)) {
$data->instantiation_type = 'NetworkPortEthernet';
}

//check for instantiation_type switch for NetworkPort
if (
property_exists($data, 'instantiation_type')
Expand All @@ -422,7 +427,6 @@ private function handleUpdates()
//handle instantiation type
if (property_exists($data, 'instantiation_type')) {
$type = $data->instantiation_type;

//handle only ethernet and fiberchannel
$this->handleInstantiation($type, $data, $keydb, true);
}
Expand Down Expand Up @@ -623,6 +627,11 @@ private function handleCreates()
$ports += $this->getManagementPorts();
}
foreach ($ports as $port) {
// force NetworkPortEthernet type if no instantiation_type and mac is set
if (!property_exists($port, 'instantiation_type') && property_exists($port, 'mac') && !empty($port->mac)) {
$port->instantiation_type = 'NetworkPortEthernet';
}

$netports_id = $this->addNetworkPort($port);
if (count(($port->ipaddress ?? []))) {
if (property_exists($port, 'name')) {
Expand Down
Loading