Commit 06199f7f authored by Johan Cwiklinski's avatar Johan Cwiklinski Committed by Johan Cwiklinski
Browse files

Fix inventory file for stacked and wireless with AP equipements

Fix refused stacked equipments and their inventory file
Do not store each access point on refused equipments
parent fa18beb7
......@@ -66,6 +66,8 @@ abstract class MainAsset extends InventoryAsset
protected $assets = [];
/** @var Conf */
protected $conf;
/** @var array */
protected $refused = [];
public function __construct(CommonDBTM $item, $data) {
$namespaced = explode('\\', static::class);
......@@ -414,7 +416,9 @@ abstract class MainAsset extends InventoryAsset
$this->current_key = $key;
$input = $this->prepareAllRulesInput($data);
if (!property_exists($data, 'is_ap') || $data->is_ap != true) {
$is_ap = property_exists($data, 'is_ap') && $data->is_ap == true;
if (!$is_ap) {
$entity_input = $this->prepareEntitiesRulesInput($data, $input);
$ruleEntity = new RuleImportEntityCollection();
......@@ -445,8 +449,13 @@ abstract class MainAsset extends InventoryAsset
//no rule matched, this is a new one
$this->rulepassed(0, $this->item->getType(), null);
} else if (!isset($datarules['found_inventories'])) {
$input['rules_id'] = $datarules['rules_id'];
$this->addRefused($input);
if ($is_ap) {
//Only main item is stored as refused, not all APs
unset($this->data[$key]);
} else {
$input['rules_id'] = $datarules['rules_id'];
$this->addRefused($input);
}
}
}
}
......@@ -471,7 +480,7 @@ abstract class MainAsset extends InventoryAsset
$refused = new \RefusedEquipment();
$refused->add($refused_input);
$this->setItem($refused);
$this->refused[] = $refused;
}
public function checkConf(Conf $conf): bool {
......@@ -670,4 +679,13 @@ abstract class MainAsset extends InventoryAsset
public function getItem(): CommonDBTM {
return $this->item;
}
/**
* Get refused entries
*
* @return RefusedEquipment[]
*/
public function getRefused(): array {
return $this->refused;
}
}
......@@ -45,18 +45,18 @@ use Toolbox;
*/
class Inventory
{
const FULL_MODE = 0;
const INCR_MODE = 1;
const FULL_MODE = 0;
const INCR_MODE = 1;
/** @var integer */
/** @var integer */
protected $mode;
/** @var \stdClass */
/** @var \stdClass */
protected $raw_data = null;
/** @var array */
protected $data = [];
/** @var array */
/** @var array */
private $metadata;
/** @var array */
/** @var array */
private $errors = [];
/** @var CommonDBTM */
protected $item;
......@@ -75,19 +75,19 @@ class Inventory
/** @var InventoryAsset */
private $mainasset;
/**
* @param mixed $data Inventory data, optionnal
* @param integer $mode One of self::*_MODE
* @param integer $format One of Request::*_MODE
*/
/**
* @param mixed $data Inventory data, optionnal
* @param integer $mode One of self::*_MODE
* @param integer $format One of Request::*_MODE
*/
public function __construct($data = null, $mode = self::FULL_MODE, $format = Request::JSON_MODE) {
$this->mode = $mode;
$this->conf = new Conf();
$this->inventory_id = Toolbox::getRandomString(30);
$this->mode = $mode;
$this->conf = new Conf();
$this->inventory_id = Toolbox::getRandomString(30);
if (null !== $data) {
$this->setData($data, $format);
$this->doInventory();
$this->setData($data, $format);
$this->doInventory();
}
}
......@@ -96,14 +96,14 @@ class Inventory
return $this;
}
/**
* Set data, and convert them if we're using legacy format
*
* @param mixed $data Inventory data, optionnal
* @param integer $format One of self::*_FORMAT
*
* @return boolean
*/
/**
* Set data, and convert them if we're using legacy format
*
* @param mixed $data Inventory data, optionnal
* @param integer $format One of self::*_FORMAT
*
* @return boolean
*/
public function setData($data, $format = Request::JSON_MODE) :bool {
// Write inventory file
......@@ -134,45 +134,45 @@ class Inventory
return true;
}
/**
* Prepare inventory data
*
* @return array
*/
/**
* Prepare inventory data
*
* @return array
*/
public function extractMetadata() :array {
//check
//check
if ($this->inError()) {
throw new \RuntimeException(print_r($this->getErrors(), true));
throw new \RuntimeException(print_r($this->getErrors(), true));
}
$this->metadata = [
$this->metadata = [
'deviceid' => $this->raw_data->deviceid,
'version' => $this->raw_data->content->versionclient,
'itemtype' => $this->raw_data->itemtype ?? 'Computer'
];
// Get tag if defined
if (property_exists($this->raw_data->content, 'accountinfo')) {
$ainfos = $this->raw_data->content->accountinfo;
if (property_exists($ainfos, 'keyname')
&& $ainfos->keyname == 'TAG'
&& property_exists($ainfos, 'keyvalue')
&& $ainfos->keyvalue != ''
) {
$this->metadata['tag'] = $ainfos->keyvalue;
}
}
return $this->metadata;
// Get tag if defined
if (property_exists($this->raw_data->content, 'accountinfo')) {
$ainfos = $this->raw_data->content->accountinfo;
if (property_exists($ainfos, 'keyname')
&& $ainfos->keyname == 'TAG'
&& property_exists($ainfos, 'keyvalue')
&& $ainfos->keyvalue != ''
) {
$this->metadata['tag'] = $ainfos->keyvalue;
}
}
return $this->metadata;
}
/**
* Do inventory
*
* @param boolean $test_rules Only to test rules, do not store anything
*
* @return array
*/
/**
* Do inventory
*
* @param boolean $test_rules Only to test rules, do not store anything
*
* @return array
*/
public function doInventory($test_rules = false) {
global $DB;
......@@ -185,7 +185,7 @@ class Inventory
//bench
$main_start = microtime(true);
if (!$DB->inTransaction()) {
$DB->beginTransaction();
$DB->beginTransaction();
}
$converter = new Converter;
......@@ -285,8 +285,8 @@ class Inventory
$this->mainasset = $main;
if (isset($this->data['hardware'])) {
//hardware is handled in inventoried item, but may be used outside
$this->data['hardware'] = $main->getHardware();
//hardware is handled in inventoried item, but may be used outside
$this->data['hardware'] = $main->getHardware();
}
if ($test_rules === false) {
......@@ -320,12 +320,19 @@ class Inventory
private function handleInventoryFile() {
$ext = (Request::XML_MODE === $this->inventory_format ? 'xml' : 'json');
$tmpfile = sprintf('%s/%s.%s', GLPI_INVENTORY_DIR, $this->inventory_id, $ext);
$success = $this->item !== null && !$this->item->isNewItem();
if ($success) {
$item = $this->mainasset->getItem();
$itemtype = $item->getType();
if (!isset($item->fields['id'])) {
$items = [];
if ($this->item !== null && !$this->item->isNewItem()) {
$items[] = $this->item;
}
foreach ($this->mainasset->getRefused() as $refused) {
$items[] = $refused;
}
foreach ($items as $item) {
$itemtype = $item->getType();
if (!isset($item->fields['id']) || empty($item->fields['id'])) {
throw new \RuntimeException('Item ID is missing :(');
}
$id = $item->fields['id'];
......@@ -335,7 +342,7 @@ class Inventory
mkdir($dir);
}
$filename = sprintf('%s/%s.%s', $dir, $id, $ext);
rename($tmpfile, $filename);
copy($tmpfile, $filename);
}
if (file_exists($tmpfile)) {
......@@ -344,22 +351,22 @@ class Inventory
}
}
/**
* Get error
*
* @return array
*/
/**
* Get error
*
* @return array
*/
public function getErrors() :array {
return $this->errors;
return $this->errors;
}
/**
* Check if erorrs has been throwed
*
* @return boolean
*/
/**
* Check if erorrs has been throwed
*
* @return boolean
*/
public function inError() :bool {
return (bool)count($this->errors);
return (bool)count($this->errors);
}
static function getMenuContent() {
......@@ -371,7 +378,7 @@ class Inventory
$links = [];
foreach ($classes as $class) {
$entry = "<i class=\"". $class::getIcon() . " pointer\" title=\"" . $class::getTypeName(Session::getPluralNumber()) .
"\"></i><span class=\"sr-only\">" . $class::getTypeName(Session::getPluralNumber()). "</span>";
"\"></i><span class=\"sr-only\">" . $class::getTypeName(Session::getPluralNumber()). "</span>";
$links[$entry] = $class::getSearchURL(false);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment