Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

enh(API): Add API to retrieve all modules and widgets version #8469

Merged
merged 4 commits into from
Apr 3, 2020
Merged
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
4 changes: 4 additions & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ security:
contact_provider:
id: contact.provider
firewalls:
free:
stateless: true
pattern: ^/centreon/api/(?:latest|beta|v[0-9]+|v[0-9]+\.[0-9]+)/platform/.*$
security: false
api:
stateless: true
pattern: ^/(?!centreon/api/(?:latest|beta|v[0-9]+|v[0-9]+\.[0-9]+)/login).*
Expand Down
4 changes: 4 additions & 0 deletions config/routes/Centreon/platform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
centreon_application_platform_getversion:
methods: GET
path: /platform/versions
controller: 'Centreon\Application\Controller\PlatformController::getVersions'
94 changes: 94 additions & 0 deletions src/Centreon/Application/Controller/PlatformController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Centreon\Application\Controller;

use Centreon\Domain\Platform\PlatformException;
use Centreon\Domain\Platform\Interfaces\PlatformServiceInterface;
use FOS\RestBundle\View\View;

/**
* This controller is designed to manage API requests concerning the versions of the different modules, widgets on the
* Centreon platform.
*
* @package Centreon\Application\Controller
*/
class PlatformController extends AbstractController
{
/**
* @var PlatformServiceInterface
*/
private $informationService;

public function __construct(PlatformServiceInterface $informationService)
{
$this->informationService = $informationService;
}

/**
* Retrieves the version of modules, widgets, remote pollers from the Centreon Platform.
*
* @return View
* @throws PlatformException
*/
public function getVersions(): View
{
$webVersion = $this->informationService->getWebVersion();
$modulesVersion = $this->informationService->getModulesVersion();
$widgetsVersion = $this->informationService->getWidgetsVersion();

return $this->view(
[
'web' => $this->extractVersion($webVersion),
'modules' => array_map(
function ($version) {
return $this->extractVersion($version);
},
$modulesVersion
),
'widgets' => array_map(
function ($version) {
return $this->extractVersion($version);
},
$widgetsVersion
)
]
);
}

/**
* Extract the major, minor and fix number from the version.
*
* @param string $version Version to analyse (ex: 1.2.09)
* @return array<string, string> (ex: [ 'major' => '1', 'minor' => '2', 'fix' => '09'])
*/
private function extractVersion(string $version): array
{
list($major, $minor, $fix) = explode('.', $version, 3);
return [
'version' => $version,
'major' => $major,
'minor' => $minor,
'fix' => !empty($fix) ? $fix : '0'
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Centreon\Domain\Platform\Interfaces;

interface PlatformRepositoryInterface
{
/**
* Retrieves the web version of the Centreon platform.
*
* @return string|null Version of the Centreon platform
* @throws \Exception
*/
public function getWebVersion(): ?string;

/**
* Retrieves the version of each modules installed on the Centreon platform.
*
* @return array<string, string> Version of the modules on the Centreon platform
* @throws \Exception
*/
public function getModulesVersion(): array;

/**
* Retrieves the version of each widget installed on the Centreon platform.
*
* @return array<string, string> Version of the widgets on the Centreon platform
* @throws \Exception
*/
public function getWidgetsVersion(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Centreon\Domain\Platform\Interfaces;

use Centreon\Domain\Platform\PlatformException;

interface PlatformServiceInterface
{
/**
* Retrieves the web version of the Centreon platform.
*
* @return string Version of the Centreon platform
* @throws PlatformException
*/
public function getWebVersion(): string;

/**
* Retrieves the version of each modules installed on the Centreon platform.
*
* @return array<string, string> Version of the modules on the Centreon platform
* @throws PlatformException
*/
public function getModulesVersion(): array;

/**
* Retrieves the version of each widget installed on the Centreon platform.
*
* @return array<string, string> Version of the widgets on the Centreon platform
* @throws PlatformException
*/
public function getWidgetsVersion(): array;
}
27 changes: 27 additions & 0 deletions src/Centreon/Domain/Platform/PlatformException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Centreon\Domain\Platform;

class PlatformException extends \Exception
{
}
85 changes: 85 additions & 0 deletions src/Centreon/Domain/Platform/PlatformService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Centreon\Domain\Platform;

use Centreon\Domain\Platform\Interfaces\PlatformRepositoryInterface;
use Centreon\Domain\Platform\Interfaces\PlatformServiceInterface;

/**
* This class is designed to retrieve the version of modules, widgets, remote pollers from the Centreon Platform.
*
* @package Centreon\Domain\Platform
*/
class PlatformService implements PlatformServiceInterface
{

/**
* @var PlatformRepositoryInterface
*/
private $platformRepository;

/**
* @param PlatformRepositoryInterface $informationRepository
*/
public function __construct(PlatformRepositoryInterface $informationRepository)
{
$this->platformRepository = $informationRepository;
}

/**
* @inheritDoc
*/
public function getWebVersion(): string
{
try {
$webVersion = $this->platformRepository->getWebVersion();
return ($webVersion !== null) ? $webVersion : '0.0.0';
} catch (\Exception $ex) {
throw new PlatformException('Error while searching for the web version of the Centreon platform');
}
}

/**
* @inheritDoc
*/
public function getModulesVersion(): array
{
try {
return $this->platformRepository->getModulesVersion();
} catch (\Exception $ex) {
throw new PlatformException('Error while searching for the modules version of the Centreon platform');
}
}

/**
* @inheritDoc
*/
public function getWidgetsVersion(): array
{
try {
return $this->platformRepository->getWidgetsVersion();
} catch (\Exception $ex) {
throw new PlatformException('Error while searching for the widgets version of the Centreon platform');
}
}
}
Loading