Skip to content
Open
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
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ parameters:
bootstrapFiles:
- ../../stubs/glpi_constants.php
- ../../vendor/autoload.php
- stubs/OptionalClass.php
paths:
- src
- front
Expand All @@ -17,5 +18,6 @@ parameters:
- ../../src
stubFiles:
- ../../stubs/glpi_constants.php
- ./stubs/OptionalClass.php
rules:
- GlpiProject\Tools\PHPStan\Rules\GlobalVarTypeRule
3 changes: 3 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Config as GlpiConfig;
use CronTask as GlpiCronTask;
use Glpi\Plugin\Hooks;
use GlpiPlugin\Carbon\CloudInventoryConnector;
use GlpiPlugin\Carbon\Config;
use GlpiPlugin\Carbon\CronTask;
use GlpiPlugin\Carbon\Dashboard\Grid;
Expand Down Expand Up @@ -114,6 +115,8 @@ function plugin_carbon_setupHooks()
LcaClientFactory::getSecuredConfigs()
);

$PLUGIN_HOOKS[Hooks::POST_INIT]['carbon'] = [CloudInventoryConnector::class, 'checkPluginAvailability'];

// add new cards to the dashboard
$PLUGIN_HOOKS[Hooks::DASHBOARD_CARDS]['carbon'] = [Grid::class, 'getDashboardCards'];
$PLUGIN_HOOKS[Hooks::DASHBOARD_TYPES]['carbon'] = [Widget::class, 'WidgetTypes'];
Expand Down
57 changes: 57 additions & 0 deletions src/CloudInventoryConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* -------------------------------------------------------------------------
* Carbon plugin for GLPI
*
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
* @link https://github.com/pluginsGLPI/carbon
*
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Carbon plugin for GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Carbon;

use Plugin;

/**
* Establish the functional bridge between Carbon and the plugin CloudInventory
*/
class CloudInventoryConnector
{
public static function checkPluginAvailability(): void
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

$CFG_GLPI['plugin:carbon']['use_cloudinventory'] = Plugin::isPluginActive('cloudinventory');
}

public function pluginAvailable(): bool
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

return $CFG_GLPI['plugin:carbon']['use_cloudinventory'] ?? false;
}
}
2 changes: 2 additions & 0 deletions src/ComputerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ComputerType extends AbstractChildDropdown
public const CATEGORY_LAPTOP = 3;
public const CATEGORY_TABLET = 4;
public const CATEGORY_SMARTPHONE = 5;
public const CATEGORY_CLOUD = 6;

public static function getCategories(): array
{
Expand All @@ -60,6 +61,7 @@ public static function getCategories(): array
self::CATEGORY_LAPTOP => __('Laptop', 'carbon'),
self::CATEGORY_TABLET => __('Tablet', 'carbon'),
self::CATEGORY_SMARTPHONE => __('Smartphone', 'carbon'),
self::CATEGORY_CLOUD => __('Cloud server', 'carbon'),
];
}

Expand Down
91 changes: 90 additions & 1 deletion src/Impact/Embodied/Boavizta/Computer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@

use CommonDBTM;
use Computer as GlpiComputer;
use ComputerModel as GlpiComputerModel;
use ComputerType as GlpiComputerType;
use GlpiPlugin\Carbon\CloudInventoryConnector;
use GlpiPlugin\Carbon\ComputerType;
use GlpiPlugin\Carbon\DataSource\Lca\Boaviztapi\ComputerModelizationAdapterTrait;
use GlpiPlugin\Cloudinventory\Amazon;
use GlpiPlugin\Cloudinventory\Azure;
use GlpiPlugin\Cloudinventory\CloudInstance;
use GlpiPlugin\Cloudinventory\Google;
use GlpiPlugin\Cloudinventory\Ovh;
use GlpiPlugin\Cloudinventory\Scaleway;
use Override;

class Computer extends AbstractAsset
Expand All @@ -48,6 +56,12 @@ class Computer extends AbstractAsset

protected string $endpoint = 'server';

/**
* If the plugin CloudInventory is available, this is an oblect from that
* plugin representing the cloud related data of the computer
*/
protected ?CloudInstance $cloud_instance = null;

#[Override]
protected function doEvaluation(): ?array
{
Expand All @@ -62,10 +76,25 @@ protected function doEvaluation(): ?array
ComputerType::CATEGORY_DESKTOP,
ComputerType::CATEGORY_UNDEFINED,
]);
if ($handle_hardware) {
return $this->getHarwareBasedEvaluation();
}

if ($type === ComputerType::CATEGORY_CLOUD) {
return $this->getCloudBasedEvaluation();
}

return null;
}

private function getHarwareBasedEvaluation(): ?array
{
$configuration = $this->analyzeHardware();
if ($handle_hardware && count($configuration) === 0) {
if (count($configuration) === 0) {
return null;
}

// Ask for embodied impact only
$description = [
'configuration' => $configuration,
'usage' => [
Expand All @@ -78,13 +107,71 @@ protected function doEvaluation(): ?array
return $impacts;
}

private function getCloudBasedEvaluation(): ?array
{
// Ask for embodied impact only
$description = [
'usage' => [
'avg_power' => 0,
],
];

// Find provider
switch ($this->cloud_instance->fields['itemtype']) {
case Amazon::class:
$description['provider'] = 'aws';
case Azure::class:
$description['provider'] = 'azure';
case Google::class:
$description['provider'] = 'gcp';
case Ovh::class:
$description['provider'] = 'ovhcloud';
case Scaleway::class:
$description['provider'] = 'scaleway';
}

$glpi_computer_model = GlpiComputerModel::getById($this->cloud_instance->fields['computermodels_id']);

// Find model
$model = $glpi_computer_model ? $glpi_computer_model->fields['name'] : '';
if ($model !== '') {
$description['instance_type'] = $model;
} else {
$configuration = $this->analyzeHardware();
if (count($configuration) === 0) {
// No model nor hardware configuration
return null;
}
$description['configuration'] = $configuration;
}
$response = $this->query($description);
$impacts = $this->client->parseResponse($response, 'embedded');

return $impacts;
}

/**
* Get the type of the computer
* @param CommonDBTM $item
* @return int The type of the computer
*/
protected function getType(CommonDBTM $item): int
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

$cloudInventory_connector = new CloudInventoryConnector();
if ($cloudInventory_connector->pluginAvailable()) {
$cloud_instance = new CloudInstance();
$cloud_instance->getFromDBByCrit([
'computers_id' => $item->getID(),
]);
if (!$cloud_instance->isNewItem()) {
$this->cloud_instance = $cloud_instance;
return ComputerType::CATEGORY_CLOUD;
}
}

$computer_table = GlpiComputer::getTable();
$computer_type_table = ComputerType::getTable();
$glpi_computer_type_table = GlpiComputerType::getTable();
Expand Down Expand Up @@ -129,6 +216,8 @@ protected function getEndpoint(int $type)
return 'terminal/tablet';
case ComputerType::CATEGORY_SMARTPHONE:
return 'terminal/smartphone';
case ComputerType::CATEGORY_CLOUD:
return 'cloud/instance';
}

// ComputerType::CATEGORY_UNDEFINED
Expand Down
18 changes: 18 additions & 0 deletions stubs/OptionalClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace GlpiPlugin\Cloudinventory;

class CloudInstance {
/** @var array<string,mixed> */
public $fields = [];

public function getFromDBByCrit(array $criteria): bool {}

Check failure on line 9 in stubs/OptionalClass.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.2 - mariadb:10.6 / Continuous integration

Method GlpiPlugin\Cloudinventory\CloudInstance::getFromDBByCrit() has parameter $criteria with no value type specified in iterable type array.

Check failure on line 9 in stubs/OptionalClass.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.5 - mariadb:11.8 / Continuous integration

Method GlpiPlugin\Cloudinventory\CloudInstance::getFromDBByCrit() has parameter $criteria with no value type specified in iterable type array.

public function isNewItem(): bool {}
}

class Amazon {}
class Azure {}
class Google {}
class Ovh {}
class Scaleway {}
6 changes: 3 additions & 3 deletions tests/src/CommonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CommonTestCase extends TestCase
/** @var int $debugMode save state of GLPI debug mode */
private $debugMode = null;

protected $str = null;
protected ?string $str = null;

protected function disableDebug()
{
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function setupGLPIFramework(): void
return;
}

protected function login($name, $password, $noauto = false)
protected function login(string $name, string $password, $noauto = false)
{
Session::start();
$auth = new Auth();
Expand Down Expand Up @@ -539,7 +539,7 @@ protected function isolateInEntity(): int
*
* @return mixed
*/
protected function callPrivateMethod($instance, string $methodName, ...$args)
protected function callPrivateMethod($instance, string $methodName, mixed ...$args)
{
$method = new ReflectionMethod($instance, $methodName);
if (version_compare(PHP_VERSION, '8.1.0') < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ public function testConfigUpdate(array $input, array $expected)
/** @var array $CFG_GLPI */
global $CFG_GLPI;

$CFG_GLPI['plugi:carbon']['lca_datasources'] = [
Client::class,
];

$instance = new Config();
$result = $instance->configUpdate($input);
$this->assertEquals($expected, $result);
Expand Down
4 changes: 0 additions & 4 deletions tests/units/DataSource/Lca/Boaviztapi/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ public function testConfigUpdate(array $input, array $expected)
/** @var array $CFG_GLPI */
global $CFG_GLPI;

$CFG_GLPI['plugi:carbon']['lca_datasources'] = [
Client::class,
];

$result = (new Config())->configUpdate($input);
$this->assertEquals($expected, $result);
}
Expand Down
Loading