Skip to content

Commit

Permalink
Add VM OS and IP Inventory (#18265)
Browse files Browse the repository at this point in the history
* Add VM OS and IP Inventory

* Add units tests

* Update Test
  • Loading branch information
Lainow authored Nov 13, 2024
1 parent 9a9afd5 commit 5067f3b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
39 changes: 39 additions & 0 deletions phpunit/functional/Glpi/Inventory/Assets/VirtualMachineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,15 @@ public function testImportVirtualMachine()
<VERSIONCLIENT>GLPI-Agent_v1.4-1</VERSIONCLIENT>
<VIRTUALMACHINES>
<COMMENT>Computer VM</COMMENT>
<IPADDRESS>10.100.240.11</IPADDRESS>
<MAC>00:50:56:90:43:42</MAC>
<MEMORY>1024</MEMORY>
<NAME>SRV-DMZ-EZ</NAME>
<OPERATINGSYSTEM>
<BOOT_TIME>2024-01-24T16:00:00Z</BOOT_TIME>
<FQDN>test.test.test</FQDN>
<FULL_NAME>OS Test</FULL_NAME>
</OPERATINGSYSTEM>
<STATUS>running</STATUS>
<UUID>420904FE-6a92-95e8-13f9-a37fc3607c14</UUID>
<VCPU>1</VCPU>
Expand Down Expand Up @@ -270,6 +276,39 @@ public function testImportVirtualMachine()
'uuid' => '420904fe-6a92-95e8-13f9-a37fc3607c14',
]));

//get NetworkPort Computer
$netport_computer_linked_first = new \NetworkPort();
$this->assertTrue($netport_computer_linked_first->getFromDBByCrit([
'items_id' => $computer_linked_first->fields['id'],
'itemtype' => 'Computer',
]));

$netpname_computer_linked_first = new \NetworkName();
$this->assertTrue($netpname_computer_linked_first->getFromDBByCrit([
'items_id' => $netport_computer_linked_first->fields['id'],
'itemtype' => 'NetworkPort',
]));

$ip_computer_linked_first = new \IPAddress();
$this->assertTrue($ip_computer_linked_first->getFromDBByCrit([
'items_id' => $netpname_computer_linked_first->fields['id'],
'itemtype' => 'NetworkName',
'name' => '10.100.240.11'
]));

//get Operating system Computer
$itemos_computer_linked_first = new \Item_OperatingSystem();
$this->assertTrue($itemos_computer_linked_first->getFromDBByCrit([
'items_id' => $computer_linked_first->fields['id'],
'itemtype' => 'Computer',
]));

$os_computer_linked_first = new \OperatingSystem();
$this->assertTrue($os_computer_linked_first->getFromDBByCrit([
'id' => $itemos_computer_linked_first->fields['operatingsystems_id'],
'name' => 'OS Test'
]));

$xml_source = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<REQUEST>
<CONTENT>
Expand Down
23 changes: 23 additions & 0 deletions src/Glpi/Inventory/Asset/VirtualMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,29 @@ protected function createVmComputer()
if (isset($this->allports[$vm->uuid])) {
$this->ports = $this->allports[$vm->uuid];
$this->handlePorts('Computer', $computers_vm_id);
} elseif (property_exists($vm, 'ipaddress')) {
$net_val = new \stdClass();
if (property_exists($vm, 'ipaddress')) {
$net_val->ip = [$vm->ipaddress];
}

if (property_exists($vm, 'mac')) {
$net_val->instantiation_type = 'NetworkPortEthernet';
$net_val->mac = strtolower($vm->mac);
if (isset($this->allports[$vm->uuid][$vm->name])) {
if (property_exists($net_val, 'ip') && $net_val->ip != '') {
$net_val->ipaddress = $net_val->ip;
$this->allports[$vm->uuid][$vm->name] = $net_val;
}
} else {
if (property_exists($net_val, 'ip') && $net_val->ip != '') {
$net_val->ipaddress = $net_val->ip;
$this->allports[$vm->uuid][$vm->name] = $net_val;
}
}
$this->ports = $this->allports[$vm->uuid];
$this->handlePorts('Computer', $computers_vm_id);
}
}

//manage operating system
Expand Down

0 comments on commit 5067f3b

Please sign in to comment.