diff --git a/application/controllers/ImportsourceController.php b/application/controllers/ImportsourceController.php index d259281e..c7d373ba 100644 --- a/application/controllers/ImportsourceController.php +++ b/application/controllers/ImportsourceController.php @@ -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 diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index b1342309..7af620c2 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -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)