2016-02-18 16:02:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director;
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
use Exception;
|
2016-02-18 16:02:51 +01:00
|
|
|
use Icinga\Application\Config;
|
2016-03-21 19:09:34 +01:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2016-02-18 20:46:07 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2017-03-13 22:06:10 +01:00
|
|
|
use Icinga\Module\Director\Exception\NestingError;
|
2016-02-18 16:02:51 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaApiUser;
|
2016-03-21 19:09:34 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaEndpoint;
|
2017-03-13 22:06:10 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
2016-03-21 19:09:34 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaZone;
|
2016-02-18 16:02:51 +01:00
|
|
|
use Icinga\Module\Director\Core\CoreApi;
|
|
|
|
use Icinga\Module\Director\Core\RestApiClient;
|
|
|
|
|
|
|
|
class KickstartHelper
|
|
|
|
{
|
2017-03-13 22:06:10 +01:00
|
|
|
/** @var Db */
|
2016-02-18 16:02:51 +01:00
|
|
|
protected $db;
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/** @var CoreApi */
|
2016-03-21 19:09:34 +01:00
|
|
|
protected $api;
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/** @var IcingaApiUser */
|
2016-02-18 20:46:07 +01:00
|
|
|
protected $apiUser;
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/** @var IcingaEndpoint */
|
2016-03-21 19:09:34 +01:00
|
|
|
protected $deploymentEndpoint;
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/** @var IcingaEndpoint[] */
|
2016-03-21 19:09:34 +01:00
|
|
|
protected $loadedEndpoints;
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/** @var IcingaEndpoint[] */
|
|
|
|
protected $removeEndpoints;
|
|
|
|
|
|
|
|
/** @var IcingaZone[] */
|
2016-03-21 19:09:34 +01:00
|
|
|
protected $loadedZones;
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/** @var IcingaZone[] */
|
2016-10-06 18:20:25 +02:00
|
|
|
protected $removeZones;
|
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
protected $config = array(
|
2016-02-26 11:58:37 +01:00
|
|
|
'endpoint' => null,
|
|
|
|
'host' => null,
|
|
|
|
'port' => null,
|
|
|
|
'username' => null,
|
|
|
|
'password' => null,
|
2016-02-18 20:46:07 +01:00
|
|
|
);
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* KickstartHelper constructor.
|
|
|
|
* @param Db $db
|
|
|
|
*/
|
2016-02-18 16:02:51 +01:00
|
|
|
public function __construct(Db $db)
|
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* Trigger a complete kickstart run
|
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->fetchEndpoints()
|
|
|
|
->reconnectToDeploymentEndpoint()
|
|
|
|
->fetchZones()
|
|
|
|
->storeZones()
|
|
|
|
->storeEndpoints()
|
|
|
|
->removeEndpoints()
|
|
|
|
->removeZones()
|
|
|
|
->importCommands();
|
|
|
|
|
|
|
|
$this->apiUser()->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-18 16:02:51 +01:00
|
|
|
public function isConfigured()
|
|
|
|
{
|
2016-02-18 20:46:07 +01:00
|
|
|
$config = $this->fetchConfigFileSection();
|
|
|
|
return array_key_exists('endpoint', $config)
|
|
|
|
&& array_key_exists('username', $config);
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return KickstartHelper
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws ProgrammingError
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
2016-02-18 20:46:07 +01:00
|
|
|
public function loadConfigFromFile()
|
|
|
|
{
|
|
|
|
return $this->setConfig($this->fetchConfigFileSection());
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-02-18 20:46:07 +01:00
|
|
|
protected function fetchConfigFileSection()
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
2016-02-18 20:46:07 +01:00
|
|
|
return Config::module('director', 'kickstart')
|
|
|
|
->getSection('config')
|
|
|
|
->toArray();
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @param array $config
|
|
|
|
* @return $this
|
|
|
|
* @throws ProgrammingError
|
|
|
|
*/
|
2016-02-18 16:02:51 +01:00
|
|
|
public function setConfig($config)
|
|
|
|
{
|
2016-02-18 20:46:07 +01:00
|
|
|
foreach ($config as $key => $value) {
|
2016-03-07 01:10:17 +01:00
|
|
|
if ($value === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
if (! array_key_exists($key, $this->config)) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'"%s" is not a valid config setting for the kickstart helper',
|
|
|
|
$key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->config[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-18 16:02:51 +01:00
|
|
|
public function isRequired()
|
|
|
|
{
|
|
|
|
$stats = $this->db->getObjectSummary();
|
|
|
|
return (int) $stats['apiuser']->cnt_total === 0;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @param mixed $default
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-02-18 20:46:07 +01:00
|
|
|
protected function getValue($key, $default = null)
|
|
|
|
{
|
|
|
|
if ($this->config[$key] === null) {
|
|
|
|
return $default;
|
|
|
|
} else {
|
|
|
|
return $this->config[$key];
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 16:02:51 +01:00
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return IcingaApiUser
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
2016-02-18 16:02:51 +01:00
|
|
|
protected function apiUser()
|
|
|
|
{
|
|
|
|
if ($this->apiUser === null) {
|
2016-03-21 19:09:34 +01:00
|
|
|
$name = $this->getValue('username');
|
|
|
|
|
|
|
|
$user = IcingaApiUser::create(array(
|
2016-02-18 16:02:51 +01:00
|
|
|
'object_name' => $this->getValue('username'),
|
|
|
|
'object_type' => 'external_object',
|
|
|
|
'password' => $this->getValue('password')
|
|
|
|
), $this->db);
|
2016-03-21 19:09:34 +01:00
|
|
|
|
|
|
|
if (IcingaApiUser::exists($name, $this->db)) {
|
|
|
|
$this->apiUser = IcingaApiUser::load($name, $this->db)->replaceWith($user);
|
|
|
|
} else {
|
|
|
|
$this->apiUser = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->apiUser->store();
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->apiUser;
|
|
|
|
}
|
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
/**
|
2017-03-13 22:06:10 +01:00
|
|
|
* @param IcingaObject[] $objects
|
|
|
|
* @return IcingaObject[]
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws NestingError
|
2016-11-01 18:28:36 +01:00
|
|
|
*/
|
2017-09-28 20:19:24 +02:00
|
|
|
protected function sortByParent(array $objects)
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
2017-03-13 22:06:10 +01:00
|
|
|
$sorted = array();
|
2016-03-21 19:09:34 +01:00
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
$cnt = 0;
|
|
|
|
while (! empty($objects)) {
|
|
|
|
$cnt++;
|
|
|
|
if ($cnt > 20) {
|
|
|
|
$this->throwObjectLoop($objects);
|
|
|
|
}
|
2017-09-28 20:19:24 +02:00
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
$unset = array();
|
2017-03-13 22:06:10 +01:00
|
|
|
foreach ($objects as $key => $object) {
|
2017-09-28 20:19:24 +02:00
|
|
|
$parentName = $object->get('parent');
|
|
|
|
if ($parentName === null || array_key_exists($parentName, $sorted)) {
|
|
|
|
$sorted[$object->getObjectName()] = $object;
|
|
|
|
$unset[] = $key;
|
2016-03-21 19:09:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
foreach ($unset as $key) {
|
|
|
|
unset($objects[$key]);
|
2016-03-21 19:09:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
return $sorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IcingaObject[] $objects
|
|
|
|
* @throws NestingError
|
|
|
|
*/
|
|
|
|
protected function throwObjectLoop(array $objects)
|
|
|
|
{
|
|
|
|
$names = array();
|
|
|
|
if (empty($objects)) {
|
|
|
|
$class = 'Nothing';
|
|
|
|
} else {
|
|
|
|
$class = preg_split('/\\\/', get_class(current($objects)));
|
|
|
|
$class = end($class);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
$names[] = $object->getObjectName();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NestingError(
|
|
|
|
'Loop detected while resolving %s: %s',
|
|
|
|
$class,
|
|
|
|
implode(', ', $names)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws NestingError
|
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
|
|
|
protected function fetchZones()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
2017-09-28 20:19:24 +02:00
|
|
|
$this->loadedZones = $this->sortByParent(
|
2017-03-13 22:06:10 +01:00
|
|
|
$this->api()->setDb($db)->getZoneObjects()
|
|
|
|
);
|
2016-03-21 19:09:34 +01:00
|
|
|
|
2016-02-18 16:02:51 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return $this
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function storeZones()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
2017-03-13 22:06:10 +01:00
|
|
|
$existing = IcingaObject::loadAllExternalObjectsByType('zone', $db);
|
2016-10-06 18:20:25 +02:00
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
foreach ($this->loadedZones as $name => $object) {
|
|
|
|
if (array_key_exists($name, $existing)) {
|
|
|
|
$object = $existing[$name]->replaceWith($object);
|
|
|
|
unset($existing[$name]);
|
2016-03-21 19:09:34 +01:00
|
|
|
}
|
2017-03-13 22:06:10 +01:00
|
|
|
|
|
|
|
$object->store();
|
2016-03-21 19:09:34 +01:00
|
|
|
}
|
2016-10-06 18:20:25 +02:00
|
|
|
|
|
|
|
$this->removeZones = $existing;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-10-06 18:20:25 +02:00
|
|
|
protected function removeZones()
|
2017-03-13 22:06:10 +01:00
|
|
|
{
|
|
|
|
foreach ($this->removeZones as $zone) {
|
|
|
|
$zone->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
|
|
|
protected function fetchEndpoints()
|
2016-10-06 18:20:25 +02:00
|
|
|
{
|
|
|
|
$db = $this->db;
|
2017-09-28 20:19:24 +02:00
|
|
|
$this->loadedEndpoints = $this->api()->setDb($db)->getEndpointObjects();
|
2016-10-06 18:20:25 +02:00
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
$master = $this->getValue('endpoint');
|
|
|
|
if (array_key_exists($master, $this->loadedEndpoints)) {
|
|
|
|
$apiuser = $this->apiUser();
|
|
|
|
$apiuser->store();
|
|
|
|
$object = $this->loadedEndpoints[$master];
|
|
|
|
$object->apiuser = $apiuser->object_name;
|
|
|
|
$this->deploymentEndpoint = $object;
|
2016-03-21 19:09:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return $this
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
|
|
|
protected function storeEndpoints()
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
|
|
|
$db = $this->db;
|
2017-03-13 22:06:10 +01:00
|
|
|
$existing = IcingaObject::loadAllExternalObjectsByType('endpoint', $db);
|
2016-02-18 16:02:51 +01:00
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
foreach ($this->loadedEndpoints as $name => $object) {
|
|
|
|
if (array_key_exists($name, $existing)) {
|
|
|
|
$object = $existing[$name]->replaceWith($object);
|
|
|
|
unset($existing[$name]);
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
$object->store();
|
2016-03-21 19:09:34 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
$this->removeEndpoints = $existing;
|
|
|
|
|
|
|
|
$db->settings()->master_zone = $this->deploymentEndpoint->zone;
|
2016-03-21 19:09:34 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
protected function removeEndpoints()
|
|
|
|
{
|
|
|
|
foreach ($this->removeEndpoints as $endpoint) {
|
|
|
|
$endpoint->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
* @throws ConfigurationError
|
|
|
|
*/
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function reconnectToDeploymentEndpoint()
|
|
|
|
{
|
|
|
|
$master = $this->getValue('endpoint');
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
if (! $this->deploymentEndpoint) {
|
2016-03-21 19:09:34 +01:00
|
|
|
throw new ConfigurationError(
|
|
|
|
'I found no Endpoint object called "%s" on %s:%d',
|
|
|
|
$master,
|
|
|
|
$this->getHost(),
|
|
|
|
$this->getPort()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-31 18:17:42 +02:00
|
|
|
$ep = $this->deploymentEndpoint;
|
|
|
|
|
|
|
|
$epHost = $ep->get('host');
|
2017-03-13 22:06:10 +01:00
|
|
|
if (! $epHost) {
|
|
|
|
$epHost = $ep->getObjectName();
|
2016-03-31 18:17:42 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
try {
|
|
|
|
$this->switchToDeploymentApi()->getStatus();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'I was unable to re-establish a connection to the Endpoint "%s" (%s:%d).'
|
|
|
|
. ' When reconnecting to the configured Endpoint (%s:%d) I get an error: %s'
|
|
|
|
. ' Please re-check your Icinga 2 endpoint configuration',
|
|
|
|
$master,
|
|
|
|
$this->getHost(),
|
|
|
|
$this->getPort(),
|
2016-03-31 18:17:42 +02:00
|
|
|
$epHost,
|
2016-03-24 14:09:14 +01:00
|
|
|
$ep->get('port'),
|
2016-03-21 19:09:34 +01:00
|
|
|
$e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* Import existing commands as external objects
|
|
|
|
*
|
|
|
|
* TODO: remove outdated ones
|
|
|
|
*
|
|
|
|
* @return $this
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
|
|
|
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
2016-02-18 16:02:51 +01:00
|
|
|
protected function importCommands()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
2017-03-13 22:06:10 +01:00
|
|
|
$zdb = $db->getDbAdapter();
|
|
|
|
$zdb->beginTransaction();
|
2017-09-28 20:19:24 +02:00
|
|
|
/** @var IcingaObject $object */
|
2018-02-20 12:33:43 +01:00
|
|
|
foreach (['Check', 'Notification', 'Event'] as $type) {
|
|
|
|
foreach ($this->api()->setDb($db)->getSpecificCommandObjects($type) as $object) {
|
|
|
|
if ($object::exists($object->object_name, $db)) {
|
|
|
|
$new = $object::load($object->getObjectName(), $db)->replaceWith($object);
|
|
|
|
} else {
|
|
|
|
$new = $object;
|
|
|
|
}
|
2016-02-18 16:02:51 +01:00
|
|
|
|
2018-02-20 12:33:43 +01:00
|
|
|
$new->store();
|
2016-10-30 13:29:04 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-13 22:06:10 +01:00
|
|
|
$zdb->commit();
|
2016-10-30 13:29:04 +01:00
|
|
|
|
2016-02-18 16:02:51 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @param Db $db
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setDb(Db $db)
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-18 20:46:07 +01:00
|
|
|
protected function getHost()
|
|
|
|
{
|
|
|
|
return $this->getValue('host', $this->getValue('endpoint'));
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-02-18 20:46:07 +01:00
|
|
|
protected function getPort()
|
|
|
|
{
|
|
|
|
return (int) $this->getValue('port', 5665);
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return CoreApi
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function getDeploymentApi()
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
2016-03-21 19:09:34 +01:00
|
|
|
unset($this->api);
|
|
|
|
$ep = $this->deploymentEndpoint;
|
|
|
|
|
2016-03-31 18:17:42 +02:00
|
|
|
$epHost = $ep->get('host');
|
|
|
|
if (!$epHost) {
|
|
|
|
$epHost = $ep->object_name;
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
$client = new RestApiClient(
|
2016-03-31 18:17:42 +02:00
|
|
|
$epHost,
|
2016-03-24 14:09:14 +01:00
|
|
|
$ep->get('port')
|
2016-03-21 19:09:34 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$apiuser = $this->apiUser();
|
|
|
|
$client->setCredentials($apiuser->object_name, $apiuser->password);
|
|
|
|
|
|
|
|
$api = new CoreApi($client);
|
2018-06-01 16:19:03 +02:00
|
|
|
$api->setDb($this->db);
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
return $api;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return CoreApi
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function getConfiguredApi()
|
|
|
|
{
|
|
|
|
unset($this->api);
|
2016-02-18 16:02:51 +01:00
|
|
|
$client = new RestApiClient(
|
2016-02-18 20:46:07 +01:00
|
|
|
$this->getHost(),
|
|
|
|
$this->getPort()
|
2016-02-18 16:02:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$apiuser = $this->apiUser();
|
|
|
|
$client->setCredentials($apiuser->object_name, $apiuser->password);
|
|
|
|
|
|
|
|
$api = new CoreApi($client);
|
2018-06-01 16:19:03 +02:00
|
|
|
$api->setDb($this->db);
|
|
|
|
|
2016-02-18 16:02:51 +01:00
|
|
|
return $api;
|
|
|
|
}
|
2016-03-21 19:09:34 +01:00
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return CoreApi
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function switchToDeploymentApi()
|
|
|
|
{
|
|
|
|
return $this->api = $this->getDeploymentApi();
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:06:10 +01:00
|
|
|
/**
|
|
|
|
* @return CoreApi
|
2018-06-01 16:19:03 +02:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2017-03-13 22:06:10 +01:00
|
|
|
*/
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function api()
|
|
|
|
{
|
|
|
|
if ($this->api === null) {
|
|
|
|
$this->api = $this->getConfiguredApi();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->api;
|
|
|
|
}
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|