Skip to content

Commit

Permalink
Add refused equipments when ignored from entities rules; closes #17
Browse files Browse the repository at this point in the history
Test refused from entities, reword
Add tests
  • Loading branch information
trasher committed Jan 13, 2021
1 parent 3eb8c5f commit 990571d
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 88 deletions.
51 changes: 28 additions & 23 deletions inc/inventory/asset/mainasset.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ public function handle() {
$dataEntity = $ruleEntity->processAllRules($entity_input, []);

if (isset($dataEntity['_ignore_import'])) {
Toolbox::logWarning('Import ignored from entities rules');
$input['rules_id'] = $dataEntity['rules_id'];
$this->addRefused($input);
return;
}

Expand All @@ -455,31 +456,35 @@ public function handle() {
//no rule matched, this is a new one
$this->rulepassed(0, $this->item->getType(), null);
} else if (!isset($datarules['found_inventories'])) {
$refused_input = [
'name' => $input['name'],
'itemtype' => $input['itemtype'],
'serial' => $input['serial'] ?? '',
'ip' => $input['ip'] ?? '',
'mac' => $input['mac'] ?? '',
'uuid' => $input['uuid'] ?? '',
'rules_id' => $datarules['rules_id'],
'entities_id' => $input['entities_id'],
//TODO: plugin had inventory, networkinventory or networkdiscovery possible methods.
//'method' => ??

];

foreach (['ip', 'mac'] as $array) {
if (is_array($refused_input[$array])) {
$refused_input[$array] = exportArrayToDB($refused_input[$array]);
}
}
$input['rules_id'] = $datarules['rules_id'];
$this->addRefused($input);
}
}
}

protected function addRefused(array $input) {
$refused_input = [
'name' => $input['name'],
'itemtype' => $input['itemtype'],
'serial' => $input['serial'] ?? '',
'ip' => $input['ip'] ?? '',
'mac' => $input['mac'] ?? '',
'uuid' => $input['uuid'] ?? '',
'rules_id' => $input['rules_id'],
'entities_id' => $input['entities_id'],
//TODO: plugin had inventory, networkinventory or networkdiscovery possible methods.
//'method' => ??
];

$refused = new \RefusedEquipment();
$refused->add($refused_input);
$this->setItem($refused);
foreach (['ip', 'mac'] as $array) {
if (is_array($refused_input[$array])) {
$refused_input[$array] = exportArrayToDB($refused_input[$array]);
}
}

$refused = new \RefusedEquipment();
$refused->add($refused_input);
$this->setItem($refused);
}

public function checkConf(Conf $conf): bool {
Expand Down
2 changes: 1 addition & 1 deletion inc/ruleimportasset.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function getActions() {
'type' => 'inventory_type'
],
'_ignore_import' => [
'name' => __('To be unaware of import'),
'name' => __('Refuse import'),
'type' => 'yesonly'
]
];
Expand Down
137 changes: 74 additions & 63 deletions inc/ruleimportentity.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,44 +91,48 @@ function executeActions($output, $params, array $input = []) {
}


/**
* @see Rule::getCriterias()
**/
function getCriterias() {

static $criterias = [];

if (count($criterias)) {
return $criterias;
}

$criterias['tag']['field'] = 'name';
$criterias['tag']['name'] = __('Inventory tag');

$criterias['domain']['field'] = 'name';
$criterias['domain']['name'] = Domain::getTypeName(1);

$criterias['subnet']['field'] = 'name';
$criterias['subnet']['name'] = __('Subnet');

$criterias['ip']['field'] = 'name';
$criterias['ip']['name'] = __('IP Address');

$criterias['name']['field'] = 'name';
$criterias['name']['name'] = __("Equipment name");

$criterias['serial']['field'] = 'name';
$criterias['serial']['name'] = __('Serial number');

$criterias['oscomment']['field'] = 'name';
$criterias['oscomment']['name'] = OperatingSystem::getTypeName(1).'/'.__('Comments');

$criterias['_source']['table'] = '';
$criterias['_source']['field'] = '_source';
$criterias['_source']['name'] = __('Source');
$criterias['_source']['allow_condition'] = [Rule::PATTERN_IS, Rule::PATTERN_IS_NOT];

return $criterias;
return [
'tag' => [
'field' => 'name',
'name' => __('Inventory tag')
],
'domain' => [
'field' => 'name',
'name' => Domain::getTypeName(1)
],
'subnet' => [
'field' => 'name',
'name' => __('Subnet')
],
'ip' => [
'field' => 'name',
'name' => IPAddress::getTypeName(1)
],
'name' => [
'field' => 'name',
'name' => __("Equipment name")
],
'serial' => [
'field' => 'name',
'name' => __('Serial number')
],
'oscomment' => [
'field' => 'name',
'name' => sprintf(
'%s / %s',
OperatingSystem::getTypeName(1),
__('Comments')
)
],
'_source' => [
'table' => '',
'field' => '_source',
'name' => __('Source'),
'allow_condition' => [Rule::PATTERN_IS, Rule::PATTERN_IS_NOT]
]
];
}


Expand Down Expand Up @@ -328,33 +332,40 @@ function process(&$input, &$output, &$params, &$options = []) {


function getActions() {

$actions = [];

$actions['entities_id']['name'] = Entity::getTypeName(1);
$actions['entities_id']['type'] = 'dropdown';
$actions['entities_id']['table'] = 'glpi_entities';

$actions['locations_id']['name'] = Location::getTypeName(1);
$actions['locations_id']['type'] = 'dropdown';
$actions['locations_id']['table'] = 'glpi_locations';

$actions['_affect_entity_by_tag']['name'] = __('Entity from TAG');
$actions['_affect_entity_by_tag']['type'] = 'text';
$actions['_affect_entity_by_tag']['force_actions'] = ['regex_result'];

$actions['_ignore_import']['name'] = __('To be unaware of import');
$actions['_ignore_import']['type'] = 'yesonly';

$actions['is_recursive']['name'] = __('Child entities');
$actions['is_recursive']['type'] = 'yesno';

$actions['groups_id_tech']['name'] = __('Group in charge of the hardware');
$actions['groups_id_tech']['type'] = 'dropdown';
$actions['groups_id_tech']['table'] = 'glpi_groups';

$actions['users_id_tech']['name'] = __('Technician in charge of the hardware');
$actions['users_id_tech']['type'] = 'dropdown_users';
$actions = [
'entities_id' => [
'name' => Entity::getTypeName(1),
'type' => 'dropdown',
'table' => Entity::getTable()
],
'locations_id' => [
'name' => Location::getTypeName(1),
'type' => 'dropdown',
'table' => Location::getTable(),
],
'_affect_entity_by_tag' => [
'name' => __('Entity from TAG'),
'type' => 'text',
'force_actions' => 'regex_result',
],
'_ignore_import' => [
'name' => __('Refuse import'),
'type' => 'yesonly'
],
'is_recursive' => [
'name' => __('Child entities'),
'type' => 'yesno'
],
'groups_id_tech' => [
'name' => __('Group in charge of the hardware'),
'type' => 'dropdown',
'table' => Group::getTable()
],
'users_id_tech' => [
'name' => __('Technician in charge of the hardware'),
'type' => 'dropdown_users'
]
];

return $actions;
}
Expand Down
105 changes: 104 additions & 1 deletion tests/functionnal/Glpi/Inventory/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3111,7 +3111,110 @@ public function testImportNetworkEquipmentWAggregatedPorts() {
$this->integer(count($unmanageds))->isIdenticalTo(36);
}

public function testImportIgnoredComputer() {
public function testImportRefusedFromAssetRules() {
$json = file_get_contents(GLPI_ROOT . '/tests/fixtures/inventory/computer_1.json');
$data = json_decode($json);
unset($data->content->bios);
unset($data->content->hardware->name);
$json = json_encode($data);

$inventory = new \Glpi\Inventory\Inventory($json);

if ($inventory->inError()) {
foreach ($inventory->getErrors() as $error) {
var_dump($error);
}
}
$this->boolean($inventory->inError())->isFalse();
$this->array($inventory->getErrors())->isEmpty();

//check inventory metadata
$metadata = $inventory->getMetadata();
$this->array($metadata)->hasSize(5)
->string['deviceid']->isIdenticalTo('glpixps-2018-07-09-09-07-13')
->string['version']->isIdenticalTo('FusionInventory-Agent_v2.5.2-1.fc31')
->string['itemtype']->isIdenticalTo('Computer')
->string['tag']->isIdenticalTo('000005');
$this->array($metadata['provider'])->hasSize(10);

global $DB;
//check created agent
$agenttype = $DB->request(['FROM' => \AgentType::getTable(), 'WHERE' => ['name' => 'Core']])->next();
$agents = $DB->request(['FROM' => \Agent::getTable()]);
$this->integer(count($agents))->isIdenticalTo(1);
$agent = $agents->next();
$this->array($agent)
->string['deviceid']->isIdenticalTo('glpixps-2018-07-09-09-07-13')
->string['name']->isIdenticalTo('glpixps-2018-07-09-09-07-13')
->string['version']->isIdenticalTo('2.5.2-1.fc31')
->string['itemtype']->isIdenticalTo('Computer')
->integer['agenttypes_id']->isIdenticalTo($agenttype['id']);

$computers_id = $agent['items_id'];
$this->integer($computers_id)->isIdenticalTo(0);

$iterator = $DB->request([
'FROM' => \RefusedEquipment::getTable(),
]);
$this->integer(count($iterator))->isIdenticalTo(1);

$result = $iterator->next();
$expected = [
'id' => $result['id'],
'name' => '',
'itemtype' => 'Computer',
'entities_id' => 0,
'ip' => '["192.168.1.142","fe80::b283:4fa3:d3f2:96b1","192.168.1.118","fe80::92a4:26c6:99dd:2d60"]',
'mac' => '["00:e0:4c:68:01:db","44:85:00:2b:90:bc"]',
'rules_id' => $result['rules_id'],
'method' => null,
'serial' => '',
'uuid' => '4c4c4544-0034-3010-8048-b6c04f503732',
'agents_id' => 0,
'date_creation' => $result['date_creation'],
'date_mod' => $result['date_mod']

];

$this->array($result)->isEqualTo($expected);
}

public function testImportRefusedFromEntitiesRules() {
$this->login();

$rule = new \Rule();
$input = [
'is_active' => 1,
'name' => 'entity refuse rule',
'match' => 'AND',
'sub_type' => \RuleImportEntity::class,
'ranking' => 1
];
$rules_id = $rule->add($input);
$this->integer($rules_id)->isGreaterThan(0);

// Add criteria
$rulecriteria = new \RuleCriteria();
$this->integer(
$rulecriteria->add([
'rules_id' => $rules_id,
'criteria' => "name",
'pattern' => "/^glpixps.*/",
'condition' => \RuleImportEntity::REGEX_MATCH
])
)->isGreaterThan(0);

// Add action
$ruleaction = new \RuleAction();
$this->integer(
$ruleaction->add([
'rules_id' => $rules_id,
'action_type' => 'assign',
'field' => '_ignore_import',
'value' => 1
])
)->isGreaterThan(0);

$json = file_get_contents(GLPI_ROOT . '/tests/fixtures/inventory/computer_1.json');
$data = json_decode($json);
unset($data->content->bios);
Expand Down
53 changes: 53 additions & 0 deletions tests/functionnal/RuleImportEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,57 @@ public function testTwoRegexpEntitiesTest() {
];
$this->array($ent)->isEqualTo($expected);
}

public function testRefusedByEntity() {
global $DB;

$this->login();

$rule = new \Rule();
$input = [
'is_active' => 1,
'name' => 'entity refuse rule',
'match' => 'AND',
'sub_type' => \RuleImportEntity::class,
'ranking' => 1
];
$rules_id = $rule->add($input);
$this->integer($rules_id)->isGreaterThan(0);

// Add criteria
$rulecriteria = new \RuleCriteria();
$this->integer(
$rulecriteria->add([
'rules_id' => $rules_id,
'criteria' => "name",
'pattern' => "/^([A-Za-z0-9]*) - (.*)$/",
'condition' => \RuleImportEntity::REGEX_MATCH
])
)->isGreaterThan(0);

// Add action
$ruleaction = new \RuleAction();
$this->integer(
$ruleaction->add([
'rules_id' => $rules_id,
'action_type' => 'assign',
'field' => '_ignore_import',
'value' => 1
])
)->isGreaterThan(0);

$input = [
'name' => 'computer01 - entD'
];

$ruleEntity = new \RuleImportEntityCollection();
$ruleEntity->getCollectionPart();
$ent = $ruleEntity->processAllRules($input, []);

$expected = [
'_ignore_import' => 1,
'_ruleid' => $rules_id
];
$this->array($ent)->isEqualTo($expected);
}
}

0 comments on commit 990571d

Please sign in to comment.