57 lines
1.9 KiB
PHP
57 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace GlpiPlugin\Urbackup\Command;
|
||
|
|
|
||
|
|
use GlpiPlugin\Urbackup\Server;
|
||
|
|
use GlpiPlugin\Urbackup\UrbackupApiClient;
|
||
|
|
use Symfony\Component\Console\Command\Command;
|
||
|
|
use Symfony\Component\Console\Input\InputArgument;
|
||
|
|
use Symfony\Component\Console\Input\InputInterface;
|
||
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
||
|
|
|
||
|
|
class TestApiCommand extends Command
|
||
|
|
{
|
||
|
|
protected function configure(): void
|
||
|
|
{
|
||
|
|
$this
|
||
|
|
->setName('urbackup:test-api')
|
||
|
|
->setDescription('Test UrBackup server API connection')
|
||
|
|
->addArgument('server_id', InputArgument::REQUIRED, 'Server ID');
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||
|
|
{
|
||
|
|
$server_id = (int) $input->getArgument('server_id');
|
||
|
|
|
||
|
|
$server = new Server();
|
||
|
|
if (!$server->getFromDB($server_id)) {
|
||
|
|
$output->writeln("<error>Server not found: $server_id</error>");
|
||
|
|
return Command::FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
$output->writeln("Testing API for: " . $server->fields['name']);
|
||
|
|
$output->writeln("URL: " . $server->getWebInterfaceUrl());
|
||
|
|
$output->writeln("Username: " . $server->fields['api_username']);
|
||
|
|
|
||
|
|
$client = new UrbackupApiClient($server);
|
||
|
|
|
||
|
|
try {
|
||
|
|
$result = $client->testConnection();
|
||
|
|
|
||
|
|
if ($result['success']) {
|
||
|
|
$output->writeln("<info>SUCCESS: " . $result['message'] . "</info>");
|
||
|
|
$output->writeln("Identity: " . $result['identity']);
|
||
|
|
} else {
|
||
|
|
$output->writeln("<error>FAILED: " . $result['message'] . "</error>");
|
||
|
|
}
|
||
|
|
|
||
|
|
return $result['success'] ? Command::SUCCESS : Command::FAILURE;
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
$output->writeln("<error>Exception: " . $e->getMessage() . "</error>");
|
||
|
|
return Command::FAILURE;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|