2015-12-02 03:53:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Cli;
|
|
|
|
|
|
|
|
use Icinga\Cli\Command as CliCommand;
|
2016-11-01 18:28:36 +01:00
|
|
|
use Icinga\Module\Director\Core\CoreApi;
|
2015-12-02 03:53:08 +01:00
|
|
|
use Icinga\Module\Director\Db;
|
2016-02-17 16:43:29 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaEndpoint;
|
2016-09-05 17:55:26 +02:00
|
|
|
use Icinga\Application\Config;
|
2015-12-02 03:53:08 +01:00
|
|
|
|
|
|
|
class Command extends CliCommand
|
|
|
|
{
|
2016-11-01 18:28:36 +01:00
|
|
|
/** @var Db */
|
2015-12-02 03:53:08 +01:00
|
|
|
protected $db;
|
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
/** @var CoreApi */
|
2016-02-17 16:43:29 +01:00
|
|
|
private $api;
|
|
|
|
|
2016-03-16 20:28:52 +01:00
|
|
|
protected function renderJson($object, $pretty = true)
|
|
|
|
{
|
|
|
|
if ($pretty && version_compare(PHP_VERSION, '5.4.0') >= 0) {
|
|
|
|
return json_encode($object, JSON_PRETTY_PRINT) . "\n";
|
|
|
|
} else {
|
|
|
|
return json_encode($object) . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseJson($json)
|
|
|
|
{
|
|
|
|
$res = json_decode($json);
|
|
|
|
|
|
|
|
if ($res === null) {
|
|
|
|
$this->fail(sprintf(
|
|
|
|
'Invalid JSON',
|
|
|
|
$this->getLastJsonError()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: just return json_last_error_msg() for PHP >= 5.5.0
|
|
|
|
protected function getLastJsonError()
|
|
|
|
{
|
|
|
|
switch (json_last_error()) {
|
|
|
|
case JSON_ERROR_DEPTH:
|
|
|
|
return 'The maximum stack depth has been exceeded';
|
|
|
|
case JSON_ERROR_CTRL_CHAR:
|
|
|
|
return 'Control character error, possibly incorrectly encoded';
|
|
|
|
case JSON_ERROR_STATE_MISMATCH:
|
|
|
|
return 'Invalid or malformed JSON';
|
|
|
|
case JSON_ERROR_SYNTAX:
|
|
|
|
return 'Syntax error';
|
|
|
|
case JSON_ERROR_UTF8:
|
|
|
|
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
|
|
|
default:
|
|
|
|
return 'An error occured when parsing a JSON string';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 16:43:29 +01:00
|
|
|
protected function api($endpointName = null)
|
|
|
|
{
|
|
|
|
if ($this->api === null) {
|
|
|
|
if ($endpointName === null) {
|
|
|
|
$endpoint = $this->db()->getDeploymentEndpoint();
|
|
|
|
} else {
|
|
|
|
$endpoint = IcingaEndpoint::load($endpointName, $this->db());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->api = $endpoint->api();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->api;
|
|
|
|
}
|
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
/**
|
|
|
|
* @return Db
|
|
|
|
*/
|
2015-12-02 03:53:08 +01:00
|
|
|
protected function db()
|
|
|
|
{
|
|
|
|
if ($this->db === null) {
|
2016-09-05 17:55:26 +02:00
|
|
|
// Hint: not using $this->Config() intentionally. This allows
|
|
|
|
// CLI commands in other modules to use this as a base class.
|
|
|
|
$resourceName = Config::module('director')->get('db', 'resource');
|
2015-12-02 03:53:08 +01:00
|
|
|
if ($resourceName) {
|
|
|
|
$this->db = Db::fromResourceName($resourceName);
|
|
|
|
} else {
|
|
|
|
$this->fail('Director is not configured correctly');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db;
|
|
|
|
}
|
|
|
|
}
|