Skip to content

Commit 7f9c6c6

Browse files
Mirkktrasherstonebuzz
authored
Update NetworkEquipment.php - Stacked switch Name with ID (#17091)
* Update NetworkEquipment.php - Stacked switch Name with ID Stacked switches got a name with the "description" (model) field, all switches the same, which gives several issues in managing them. It seems that this was just a wrong copy-paste from the previous code line. Corrected with _$SwitchID as a common naming scheme. * Keep name, add number after Co-authored-by: Stanislas <[email protected]> * Another adaptation * Add test --------- Co-authored-by: Johan Cwiklinski <[email protected]> Co-authored-by: Stanislas <[email protected]> Co-authored-by: Johan Cwiklinski <[email protected]>
1 parent 116d525 commit 7f9c6c6

File tree

3 files changed

+282
-0
lines changed

3 files changed

+282
-0
lines changed

src/Inventory/Asset/NetworkEquipment.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public function prepare(): array
138138
$stack->$model_field = $switch->model;
139139
$stack->description = $stack->name . ' - ' . ($switch->name ?? $switch->description);
140140
$stack->name = $stack->name . ' - ' . ($switch->name ?? $switch->description);
141+
if (($switch->name ?? $switch->description) != $switch->stack_number ?? '') {
142+
$stack->name .= ' - ' . $switch->stack_number;
143+
}
141144
$stack->stack_number = $switch->stack_number ?? null;
142145
$this->data[] = $stack;
143146
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<REQUEST>
3+
<CONTENT>
4+
<DEVICE>
5+
<COMPONENTS>
6+
<COMPONENT>
7+
<CONTAINEDININDEX>0</CONTAINEDININDEX>
8+
<DESCRIPTION>HPE</DESCRIPTION>
9+
<FRU>2</FRU>
10+
<INDEX>1</INDEX>
11+
<MANUFACTURER>HPE</MANUFACTURER>
12+
<TYPE>stack</TYPE>
13+
</COMPONENT>
14+
<COMPONENT>
15+
<CONTAINEDININDEX>1</CONTAINEDININDEX>
16+
<DESCRIPTION>HPE 5130 48G PoE+ 4SFP+ EI JG937A Software Version 7.1.070</DESCRIPTION>
17+
<FIRMWARE>147</FIRMWARE>
18+
<FRU>1</FRU>
19+
<INDEX>2</INDEX>
20+
<MANUFACTURER>HPE</MANUFACTURER>
21+
<MODEL>JG937A</MODEL>
22+
<NAME>5130EI</NAME>
23+
<REVISION>Ver.B</REVISION>
24+
<SERIAL>CX15GXXX99</SERIAL>
25+
<TYPE>chassis</TYPE>
26+
<VERSION>7.1.070 Release 3208P16</VERSION>
27+
</COMPONENT>
28+
<COMPONENT>
29+
<CONTAINEDININDEX>1</CONTAINEDININDEX>
30+
<DESCRIPTION>HPE 5130 48G PoE+ 4SFP+ EI JG937A Software Version 7.1.070</DESCRIPTION>
31+
<FIRMWARE>147</FIRMWARE>
32+
<FRU>1</FRU>
33+
<INDEX>3</INDEX>
34+
<MANUFACTURER>HPE</MANUFACTURER>
35+
<MODEL>JG937A</MODEL>
36+
<NAME>5130EI</NAME>
37+
<REVISION>Ver.B</REVISION>
38+
<SERIAL>CN15CN123456</SERIAL>
39+
<TYPE>chassis</TYPE>
40+
<VERSION>7.1.070 Release 3208P16</VERSION>
41+
</COMPONENT>
42+
<COMPONENT>
43+
<CONTAINEDININDEX>2</CONTAINEDININDEX>
44+
<DESCRIPTION>CONTAINER LEVEL1</DESCRIPTION>
45+
<FRU>2</FRU>
46+
<INDEX>12</INDEX>
47+
<NAME>Level 1 Container #1</NAME>
48+
<TYPE>container</TYPE>
49+
</COMPONENT>
50+
<COMPONENT>
51+
<CONTAINEDININDEX>12</CONTAINEDININDEX>
52+
<DESCRIPTION>MODULE LEVEL1</DESCRIPTION>
53+
<FIRMWARE>147</FIRMWARE>
54+
<FRU>2</FRU>
55+
<INDEX>192</INDEX>
56+
<MANUFACTURER>HPE</MANUFACTURER>
57+
<MODEL>JG937A</MODEL>
58+
<NAME>Board</NAME>
59+
<REVISION>Ver.B</REVISION>
60+
<SERIAL>CN123456</SERIAL>
61+
<TYPE>module</TYPE>
62+
<VERSION>Release 3208P16</VERSION>
63+
</COMPONENT>
64+
<COMPONENT>
65+
<CONTAINEDININDEX>383</CONTAINEDININDEX>
66+
<DESCRIPTION>Ten-GigabitEthernet2/0/52</DESCRIPTION>
67+
<FRU>1</FRU>
68+
<INDEX>495</INDEX>
69+
<MANUFACTURER>HPE</MANUFACTURER>
70+
<NAME>Ten-GigabitEthernet2/0/52</NAME>
71+
<SERIAL>CN0CN123456</SERIAL>
72+
<TYPE>port</TYPE>
73+
</COMPONENT>
74+
</COMPONENTS>
75+
<FIRMWARES>
76+
<DESCRIPTION>device firmware</DESCRIPTION>
77+
<MANUFACTURER>Hewlett-Packard</MANUFACTURER>
78+
<NAME>5130-48G-PoE+-4SFP+(370W) EI</NAME>
79+
<TYPE>device</TYPE>
80+
<VERSION>7.1.070 Release 3208P16</VERSION>
81+
</FIRMWARES>
82+
<INFO>
83+
<CONTACT>[email protected]</CONTACT>
84+
<FIRMWARE>7.1.070 Release 3208P16</FIRMWARE>
85+
<ID>797</ID>
86+
<IPS>
87+
<IP>10.2.7.32</IP>
88+
</IPS>
89+
<LOCATION>office</LOCATION>
90+
<MAC>dc:68:00:00:00:ae</MAC>
91+
<MANUFACTURER>Hewlett-Packard</MANUFACTURER>
92+
<MODEL>5130-48G-PoE+-4SFP+(370W) EI</MODEL>
93+
<NAME>SW00Test</NAME>
94+
<SERIAL>CX15GXXX06</SERIAL>
95+
<TYPE>NETWORKING</TYPE>
96+
<UPTIME>22 days, 00:08:54.24</UPTIME>
97+
</INFO>
98+
<PORTS>
99+
<PORT>
100+
<IFALIAS>GigabitEthernet1/0/2 Interface</IFALIAS>
101+
<IFDESCR>GigabitEthernet1/0/2</IFDESCR>
102+
<IFINERRORS>0</IFINERRORS>
103+
<IFINOCTETS>0</IFINOCTETS>
104+
<IFINTERNALSTATUS>2</IFINTERNALSTATUS>
105+
<IFLASTCHANGE>1 minute, 12.58</IFLASTCHANGE>
106+
<IFMTU>12288</IFMTU>
107+
<IFNAME>GigabitEthernet1/0/2</IFNAME>
108+
<IFNUMBER>2</IFNUMBER>
109+
<IFOUTERRORS>0</IFOUTERRORS>
110+
<IFOUTOCTETS>0</IFOUTOCTETS>
111+
<IFPORTDUPLEX>1</IFPORTDUPLEX>
112+
<IFSPEED>1000000000</IFSPEED>
113+
<IFSTATUS>2</IFSTATUS>
114+
<IFTYPE>6</IFTYPE>
115+
<MAC>dc:00:0c:00:00:d8</MAC>
116+
<VLANS>
117+
<VLAN>
118+
<NAME>VLAN 0001</NAME>
119+
<NUMBER>1</NUMBER>
120+
<TAGGED>0</TAGGED>
121+
</VLAN>
122+
</VLANS>
123+
</PORT>
124+
</PORTS>
125+
</DEVICE>
126+
<MODULEVERSION>6.1</MODULEVERSION>
127+
<PROCESSNUMBER>442</PROCESSNUMBER>
128+
</CONTENT>
129+
<DEVICEID>it-2024-05-11-14-34-10</DEVICEID>
130+
<QUERY>SNMPQUERY</QUERY>
131+
</REQUEST>

tests/functional/Glpi/Inventory/Inventory.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,154 @@ public function testImportStackedNetworkEquipment()
26732673
$this->integer(count($iterator))->isIdenticalTo(43);
26742674
}
26752675

2676+
public function testImportStackedNetworkEquipment2()
2677+
{
2678+
$xml = file_get_contents(GLPI_ROOT . '/tests/fixtures/inventories/stacked_switch_name.xml');
2679+
2680+
$date_now = date('Y-m-d H:i:s');
2681+
$_SESSION['glpi_currenttime'] = $date_now;
2682+
$inventory = $this->doInventory($xml, true);
2683+
2684+
//check inventory metadata
2685+
$metadata = $inventory->getMetadata();
2686+
2687+
$this->array($metadata)->hasSize(5)
2688+
->string['deviceid']->isIdenticalTo('it-2024-05-11-14-34-10')
2689+
->string['version']->isIdenticalTo('6.1')
2690+
->string['itemtype']->isIdenticalTo('NetworkEquipment')
2691+
->variable['port']->isIdenticalTo(null)
2692+
->string['action']->isIdenticalTo('netinventory');
2693+
2694+
global $DB;
2695+
//check created agent
2696+
$agenttype = $DB->request(['FROM' => \AgentType::getTable(), 'WHERE' => ['name' => 'Core']])->current();
2697+
$this->array($inventory->getAgent()->fields)
2698+
->string['deviceid']->isIdenticalTo('it-2024-05-11-14-34-10')
2699+
->string['name']->isIdenticalTo('it-2024-05-11-14-34-10')
2700+
->string['itemtype']->isIdenticalTo('NetworkEquipment')
2701+
->integer['agenttypes_id']->isIdenticalTo($agenttype['id']);
2702+
2703+
//get model, manufacturer, ...
2704+
$autoupdatesystems = $DB->request(['FROM' => \AutoupdateSystem::getTable(), 'WHERE' => ['name' => 'GLPI Native Inventory']])->current();
2705+
$this->array($autoupdatesystems);
2706+
$autoupdatesystems_id = $autoupdatesystems['id'];
2707+
2708+
$cmodels = $DB->request(['FROM' => \NetworkEquipmentModel::getTable(), 'WHERE' => ['name' => 'JG937A']])->current();
2709+
$this->array($cmodels);
2710+
$models_id = $cmodels['id'];
2711+
2712+
$ctypes = $DB->request(['FROM' => \NetworkEquipmentType::getTable(), 'WHERE' => ['name' => 'Networking']])->current();
2713+
$this->array($ctypes);
2714+
$types_id = $ctypes['id'];
2715+
2716+
$cmanuf = $DB->request(['FROM' => \Manufacturer::getTable(), 'WHERE' => ['name' => 'Hewlett-Packard']])->current();
2717+
$this->array($cmanuf);
2718+
$manufacturers_id = $cmanuf['id'];
2719+
2720+
$cloc = $DB->request(['FROM' => \Location::getTable(), 'WHERE' => ['name' => 'office']])->current();
2721+
$this->array($cloc);
2722+
$locations_id = $cloc['id'];
2723+
2724+
//check created equipments
2725+
$expected_eq_count = 2;
2726+
$iterator = $DB->request([
2727+
'FROM' => \NetworkEquipment::getTable(),
2728+
'WHERE' => ['is_dynamic' => 1]
2729+
]);
2730+
$this->integer(count($iterator))->isIdenticalTo($expected_eq_count);
2731+
2732+
$main_expected = [
2733+
'id' => null,
2734+
'entities_id' => 0,
2735+
'is_recursive' => 0,
2736+
'name' => 'SW00Test - 5130EI',
2737+
'ram' => null,
2738+
'serial' => 'CX15GXXX99',
2739+
'otherserial' => null,
2740+
'contact' => '[email protected]',
2741+
'contact_num' => null,
2742+
'users_id_tech' => 0,
2743+
'groups_id_tech' => 0,
2744+
'date_mod' => null,
2745+
'comment' => null,
2746+
'locations_id' => $locations_id,
2747+
'networks_id' => 0,
2748+
'networkequipmenttypes_id' => $types_id,
2749+
'networkequipmentmodels_id' => $models_id,
2750+
'manufacturers_id' => $manufacturers_id,
2751+
'is_deleted' => 0,
2752+
'is_template' => 0,
2753+
'template_name' => null,
2754+
'users_id' => 0,
2755+
'groups_id' => 0,
2756+
'states_id' => 0,
2757+
'ticket_tco' => '0.0000',
2758+
'is_dynamic' => 1,
2759+
'uuid' => null,
2760+
'date_creation' => null,
2761+
'autoupdatesystems_id' => $autoupdatesystems_id,
2762+
'sysdescr' => null,
2763+
'cpu' => 0,
2764+
'uptime' => '22 days, 00:08:54.24',
2765+
'last_inventory_update' => $date_now,
2766+
'snmpcredentials_id' => 0,
2767+
];
2768+
2769+
$stacks = [
2770+
1 => [
2771+
'name' => 'SW00Test - 5130EI - 1',
2772+
'serial' => 'CX15GXXX99',
2773+
'connections' => 0
2774+
],
2775+
2 => [
2776+
'name' => 'SW00Test - 5130EI - 2',
2777+
'serial' => 'CN15CN123456',
2778+
'connections' => 1
2779+
]
2780+
];
2781+
2782+
foreach ($iterator as $row) {
2783+
$expected = $main_expected;
2784+
$equipments_id = $row['id'];
2785+
$expected['id'] = $equipments_id;
2786+
$equipment = new \NetworkEquipment();
2787+
$this->boolean($equipment->getFromDB($equipments_id))->isTrue();
2788+
$expected['date_mod'] = $row['date_mod'];
2789+
$expected['date_creation'] = $row['date_creation'];
2790+
$stack_id = preg_replace('/.+ - (\d)/', '$1', $row['name']);
2791+
$this->array($stacks)->hasKey($stack_id);
2792+
$expected['name'] .= ' - ' . $stack_id;
2793+
$expected['serial'] = $stacks[$stack_id]['serial'];
2794+
$this->array($row)->isIdenticalTo($expected);
2795+
}
2796+
2797+
//check matchedlogs
2798+
$mlogs = new \RuleMatchedLog();
2799+
$found = $mlogs->find();
2800+
$this->array($found)->hasSize(2);
2801+
2802+
$mrules_criteria = [
2803+
'FROM' => \RuleMatchedLog::getTable(),
2804+
'LEFT JOIN' => [
2805+
\Rule::getTable() => [
2806+
'ON' => [
2807+
\RuleMatchedLog::getTable() => 'rules_id',
2808+
\Rule::getTable() => 'id'
2809+
]
2810+
]
2811+
],
2812+
'WHERE' => []
2813+
];
2814+
2815+
$neteq_criteria = $mrules_criteria;
2816+
$neteq_criteria['WHERE'][] = ['itemtype' => \NetworkEquipment::getType()];
2817+
$iterator = $DB->request($neteq_criteria);
2818+
$this->integer(count($iterator))->isIdenticalTo($expected_eq_count);
2819+
foreach ($iterator as $neteq) {
2820+
$this->string($neteq['name'])->isIdenticalTo('NetworkEquipment import (by serial)');
2821+
$this->string($neteq['method'])->isIdenticalTo(\Glpi\Inventory\Request::INVENT_QUERY);
2822+
}
2823+
}
26762824
public function testImportNetworkEquipmentMultiConnections()
26772825
{
26782826
$json = json_decode(file_get_contents(self::INV_FIXTURES . 'networkequipment_3.json'));

0 commit comments

Comments
 (0)