ImportsourceController: allow to fetch preview

fixes #2096
This commit is contained in:
Thomas Gelf 2020-03-05 13:06:51 +01:00
parent 3bffe6a0d9
commit 865ed3f0d0
2 changed files with 42 additions and 1 deletions

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Forms\ImportRowModifierForm;
use Icinga\Module\Director\Forms\ImportSourceForm;
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Web\ActionBar\AutomationObjectActionBar;
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Objects\ImportSource;
@ -143,12 +144,51 @@ class ImportsourceController extends ActionController
$source->get('source_name')
);
$this->actions()->add(Link::create('[..]', '#', null, [
$this->actions()->add(Link::create(
$this->translate('Download JSON'),
$this->url()->setBasePath('director/importsource/fetch'),
null,
[
'target' => '_blank',
'class' => 'icon-download',
]
))->add(Link::create('[..]', '#', null, [
'onclick' => 'javascript:$("table.raw-data-table").toggleClass("collapsed");'
]));
(new ImportsourceHookTable())->setImportSource($source)->renderTo($this);
}
/**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\NotFoundError
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
*/
public function fetchAction()
{
$source = $this->getImportSource();
$source->checkForChanges();
$hook = ImportSourceHook::forImportSource($source);
$data = $hook->fetchData();
$source->applyModifiers($data);
$filename = sprintf(
"director-importsource-%d_%s.json",
$this->getParam('id'),
date('YmdHis')
);
$response = $this->getResponse();
$response->setHeader('Content-Type', 'application/json', true);
$response->setHeader('Content-disposition', "attachment; filename=$filename", true);
$response->sendHeaders();
$this->sendJson($this->getResponse(), $data);
// TODO: this is not clean
if (\ob_get_level()) {
\ob_end_flush();
}
exit;
}
/**
* @return ImportSource
* @throws \Icinga\Exception\NotFoundError

View File

@ -21,6 +21,7 @@ next (will be 1.8.0)
### Import and Sync
* FEATURE: allow to define update-only Sync Rules (#2059)
* FEATURE: New Property Modifier: ListToObject (#2062)
* FEATURE: Import Sources now allow to download previewed data as JSON (#2096)
### REST API
* FEATURE: Self Service API ignores empty/missing properties (e.g. no address)