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

Feat: ‘Virtualisation system’ criterion in the rules #18932

Closed
Closed
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
268 changes: 268 additions & 0 deletions phpunit/functional/Glpi/Inventory/InventoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8821,4 +8821,272 @@ public function testRuleRecursivityNo(): void
}
}
}

public function testRuleRefuseImportComputerVirtualMachines()
{
//change config to import vms as computers
$this->login();
$conf = new \Glpi\Inventory\Conf();
$this->assertTrue($conf->saveConf(['vm_as_computer' => 1]));
$this->logout();
$nb_vm = countElementsInTable(\VirtualMachineType::getTable());
$docker_vm = $this->createItems(
\VirtualMachineType::class,
[
['name' => 'docker'],
]
);
$this->assertGreaterThan($nb_vm, countElementsInTable(\VirtualMachineType::getTable()));
// Import rule to refuse "docker" virtual machine
$criteria = [
[
'condition' => 0,
'criteria' => 'itemtype',
'pattern' => 'Computer',
], [
'condition' => \Rule::PATTERN_IS,
'criteria' => 'virtualmachinetypes_id',
'pattern' => $docker_vm[0]->fields['id']
]
];
$action = [
'action_type' => 'assign',
'field' => '_ignore_import',
'value' => \RuleImportAsset::RULE_ACTION_LINK_OR_NO_IMPORT
];
$rule = new \RuleImportAsset();
$collection = new \RuleImportAssetCollection();
$rulecriteria = new \RuleCriteria();
$input = [
'is_active' => 1,
'name' => 'Import virtualisation',
'match' => 'AND',
'sub_type' => 'RuleImportAsset',
];
$rules_id = $rule->add($input);
$this->assertGreaterThan(0, $rules_id);
$this->assertTrue($collection->moveRule($rules_id, 0, $collection::MOVE_BEFORE));
$nb_computers = countElementsInTable(\Computer::getTable());
// Add criteria
foreach ($criteria as $crit) {
$input = [
'rules_id' => $rules_id,
'criteria' => $crit['criteria'],
'pattern' => $crit['pattern'],
'condition' => $crit['condition'],
];
$this->assertGreaterThan(0, (int)$rulecriteria->add($input));
}
// Add action
$ruleaction = new \RuleAction();
$input = [
'rules_id' => $rules_id,
'action_type' => $action['action_type'],
'field' => $action['field'],
'value' => $action['value'],
];
$this->assertGreaterThan(0, (int)$ruleaction->add($input));
$json_str = <<<JSON
{
"action": "inventory",
"content": {
"bios": {
"bdate": "2023-07-07",
"bmanufacturer": "Dell Inc.",
"bversion": "1.12.0",
"mmanufacturer": "Dell Inc.",
"mmodel": "0RWPXY",
"msn": ".CF301Z3.CNCMC003AT0582.",
"skunumber": "0B9F",
"smanufacturer": "Dell Inc.",
"smodel": "Inspiron 15 3525",
"ssn": "CF301Z3"
},
"hardware": {
"chassis_type": "Notebook",
"datelastloggeduser": "Tue Jul 30 09:55",
"defaultgateway": "192.168.1.1",
"dns": "127.0.0.53",
"lastloggeduser": "samuel",
"memory": 15326,
"name": "samuel-Inspiron-15-3525",
"swap": 1951,
"uuid": "4c4c4544-0046-3310-8030-c3c04f315a33",
"vmsystem": "Physical",
"workgroup": "home"
},
"virtualmachines": [
{
"image": "axllent\/mailpit",
"name": "mailpit",
"status": "running",
"uuid": "0b66f80dde33",
"vmtype": "docker"
},
{
"image": "hello-world",
"name": "elastic_tu",
"status": "off",
"uuid": "cdc8df147abb",
"vmtype": "docker"
}
],
"versionclient": "GLPI-Agent_v1.10-dev"
},
"deviceid": "test-2024-09-04-13-57-38",
"itemtype": "Computer"
}
JSON;
$json = json_decode($json_str);
$this->doInventory($json);
$count_vms = count($json->content->virtualmachines);
//check created vms
$this->assertSame($count_vms, countElementsInTable(\ComputerVirtualMachine::getTable()));
$this->assertSame($nb_computers + 1, countElementsInTable(\Computer::getTable()));
}

public function testRuleRefuseUpdateComputerVirtualMachines()
{
// Helper function to create JSON string
$json_str =
<<<JSON
{
"action": "inventory",
"content": {
"bios": {
"bdate": "2023-07-07",
"bmanufacturer": "Dell Inc.",
"bversion": "1.12.0",
"mmanufacturer": "Dell Inc.",
"mmodel": "0RWPXY",
"msn": ".CF301Z3.CNCMC003AT0582.",
"skunumber": "0B9F",
"smanufacturer": "Dell Inc.",
"smodel": "Inspiron 15 3525",
"ssn": "CF301Z3"
},
"hardware": {
"chassis_type": "Notebook",
"datelastloggeduser": "Tue Jul 30 09:55",
"defaultgateway": "192.168.1.1",
"dns": "127.0.0.53",
"lastloggeduser": "samuel",
"memory": 15326,
"name": "samuel-Inspiron-15-3525",
"swap": 1951,
"uuid": "qsrdgfd",
"vmsystem": "Physical",
"workgroup": "home"
},
"virtualmachines": [
{
"image": "axllent\/mailpit",
"name": "mailpit_update",
"status": "running",
"uuid": "zrerythegfzed",
"vmtype": "docker"
}
],
"versionclient": "GLPI-Test_v1.10-dev"
},
"deviceid": "test-2024-09-04-16-49-35",
"itemtype": "Computer"
}
JSON;
// Change config to import VMs as computers
$this->login();
$conf = new \Glpi\Inventory\Conf();
$this->assertTrue($conf->saveConf(['vm_as_computer' => 1]));
$this->logout();
// Initial inventory
$json = json_decode($json_str);
$count_vms = count($json->content->virtualmachines);
$nb_computers = countElementsInTable(\Computer::getTable());
$this->doInventory($json);
// Check created VMs
$this->assertSame($count_vms, countElementsInTable(\ComputerVirtualMachine::getTable()));
$this->assertSame($nb_computers + 2, countElementsInTable(\Computer::getTable()));
$computer = new \Computer();
$computer->getFromDBByCrit(['uuid' => 'zrerythegfzed']);
// First update inventory
$json = json_decode($json_str);
$count_vms = count($json->content->virtualmachines);
$nb_computers = countElementsInTable(\Computer::getTable());
$this->doInventory($json);
// Check created VMs
$computeru = new \Computer();
$computeru->getFromDBByCrit(['uuid' => 'zrerythegfzed']);
$this->assertSame($computeru->fields['name'], 'mailpit_update');
// Create Docker VM type
$nb_vm = countElementsInTable(\VirtualMachineType::getTable());
$vm_types = new \VirtualMachineType();
if (!$vm_types->getFromDBByCrit(['name' => 'docker'])) {
$docker_vm = $this->createItems(
\VirtualMachineType::class,
[
['name' => 'docker'],
]
);
$this->assertGreaterThan($nb_vm, countElementsInTable(\VirtualMachineType::getTable()));
$docker_type_id = $docker_vm[0]->fields['id'];
} else {
$docker_type_id = $vm_types->fields['id'];
}
// Import rule to refuse "docker" virtual machine
$criteria = [
[
'condition' => 0,
'criteria' => 'itemtype',
'pattern' => 'Computer',
], [
'condition' => \Rule::PATTERN_IS,
'criteria' => 'virtualmachinetypes_id',
'pattern' => $docker_type_id
]
];
$action = [
'action_type' => 'assign',
'field' => '_ignore_import',
'value' => \RuleImportAsset::RULE_ACTION_LINK_OR_NO_IMPORT
];
$rule = new \RuleImportAsset();
$collection = new \RuleImportAssetCollection();
$rulecriteria = new \RuleCriteria();
$input = [
'is_active' => 1,
'name' => 'Update virtualisation',
'match' => 'AND',
'sub_type' => 'RuleImportAsset',
];
$rules_id = $rule->add($input);
$this->assertGreaterThan(0, $rules_id);
$this->assertTrue($collection->moveRule($rules_id, 0, $collection::MOVE_BEFORE));
// Add criteria
foreach ($criteria as $crit) {
$input = [
'rules_id' => $rules_id,
'criteria' => $crit['criteria'],
'pattern' => $crit['pattern'],
'condition' => $crit['condition'],
];
$this->assertGreaterThan(0, (int)$rulecriteria->add($input));
}
// Add action
$ruleaction = new \RuleAction();
$input = [
'rules_id' => $rules_id,
'action_type' => $action['action_type'],
'field' => $action['field'],
'value' => $action['value'],
];
$this->assertGreaterThan(0, (int)$ruleaction->add($input));
// Second update inventory (Must be refused because of the rule)
$json_str = str_replace('"mailpit_update"', '"mailpit_update2"', $json_str);
$json = json_decode($json_str);
$this->doInventory($json);
// Check that VM computers are not updated
$c_update = new \Computer();
$c_update->getFromDBByCrit(['uuid' => 'zrerythegfzed']);
$this->assertSame("mailpit_update", $c_update->fields['name']);
}
}
4 changes: 3 additions & 1 deletion src/Inventory/Asset/InventoryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,9 @@ protected function handleInput(\stdClass $value, ?CommonDBTM $item = null): arra
}
$known_key = md5($key . $val);
if (in_array($key, $locks)) {
$input[$key] = $this->raw_links[$known_key];
if (isset($this->raw_links[$known_key])) {
$input[$key] = $this->raw_links[$known_key];
}
} elseif (isset($this->known_links[$known_key])) {
$input[$key] = $this->known_links[$known_key];
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/RuleImportAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ public function getCriterias()
Rule::REGEX_NOT_MATCH
],
],
'virtualmachinetypes_id' => [
'table' => 'glpi_virtualmachinetypes',
'field' => 'name',
'name' => VirtualMachineType::getTypeName(0),
'type' => 'dropdown',
'allow_condition' => [
Rule::PATTERN_IS,
Rule::PATTERN_IS_NOT,
Rule::PATTERN_CONTAIN,
Rule::PATTERN_NOT_CONTAIN,
Rule::PATTERN_BEGIN,
Rule::PATTERN_END,
Rule::REGEX_MATCH,
Rule::REGEX_NOT_MATCH
],
],
'states_id' => [
'table' => 'glpi_states',
'field' => 'name',
Expand Down
Loading