Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow importing from an external Server #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
101 changes: 58 additions & 43 deletions library/Fileshipper/ProvidedHook/Director/ImportSource.php
Expand Up @@ -31,15 +31,20 @@ public function getName()
*/
public function fetchData()
{
$basedir = $this->getSetting('basedir');
$basedir = $this->getSetting('basedir');
$filename = $this->getSetting('file_name');
$format = $this->getSetting('file_format');
$format = $this->getSetting('file_format');

$url = $this->getSetting('url');
if ($url !== null) {
return (array)$this->fetchFile(null, null, $url, $format);
}

if ($filename === '*') {
return $this->fetchFiles($basedir, $format);
}

return (array) $this->fetchFile($basedir, $filename, $format);
return (array)$this->fetchFile($basedir, $filename, null, $format);
}

/**
Expand All @@ -49,7 +54,7 @@ public function fetchData()
*/
public function listColumns()
{
return array_keys((array) current($this->fetchData()));
return array_keys((array)current($this->fetchData()));
}

/**
Expand All @@ -60,15 +65,15 @@ public function listColumns()
public static function addSettingsFormFields(QuickForm $form)
{
$form->addElement('select', 'file_format', array(
'label' => $form->translate('File format'),
'description' => $form->translate(
'label' => $form->translate('File format'),
'description' => $form->translate(
'Available file formats, usually CSV, JSON, YAML and XML. Whether'
. ' all of those are available eventually depends on various'
. ' libraries installed on your system. Please have a look at'
. ' the documentation in case your list is not complete.'
),
'required' => true,
'class' => 'autosubmit',
'required' => true,
'class' => 'autosubmit',
'multiOptions' => $form->optionalEnum(
static::listAvailableFormats($form)
),
Expand All @@ -77,34 +82,40 @@ public static function addSettingsFormFields(QuickForm $form)
/** @var \Icinga\Module\Director\Forms\ImportSourceForm $form */
$format = $form->getSentOrObjectSetting('file_format');

$form->addElement('text', 'url', array(
'label' => $form->translate('URL'),
'description' => $form->translate('Enter the URL from where the Config should be imported configuration in'),
'required' => false,
));

$form->addElement('select', 'basedir', array(
'label' => $form->translate('Base directory'),
'description' => sprintf(
'label' => $form->translate('Base directory'),
'description' => sprintf(
$form->translate(
'This import rule will only work with files relative to this'
. ' directory. The content of this list depends on your'
. ' configuration in "%s"'
),
Config::module('fileshipper', 'imports')->getConfigFile()
),
'required' => true,
'class' => 'autosubmit',
'required' => false,
'class' => 'autosubmit',
'multiOptions' => $form->optionalEnum(static::listBaseDirectories()),
));


if (! ($basedir = $form->getSentOrObjectSetting('basedir'))) {
if (!($basedir = $form->getSentOrObjectSetting('basedir'))) {
return $form;
}

$form->addElement('select', 'file_name', array(
'label' => $form->translate('File name'),
'description' => $form->translate(
'label' => $form->translate('File name'),
'description' => $form->translate(
'Choose a file from the above directory or * to import all files'
. ' from there at once'
),
'required' => true,
'class' => 'autosubmit',
'class' => 'autosubmit',
'multiOptions' => $form->optionalEnum(self::enumFiles($basedir, $form)),
));

Expand Down Expand Up @@ -135,23 +146,23 @@ public static function addSettingsFormFields(QuickForm $form)
protected static function addCsvElements(QuickForm $form)
{
$form->addElement('text', 'csv_delimiter', array(
'label' => $form->translate('Field delimiter'),
'label' => $form->translate('Field delimiter'),
'description' => $form->translate(
'This sets the field delimiter. One character only, defaults'
. ' to comma: ,'
),
'value' => ',',
'required' => true,
'value' => ',',
'required' => true,
));

$form->addElement('text', 'csv_enclosure', array(
'label' => $form->translate('Value enclosure'),
'label' => $form->translate('Value enclosure'),
'description' => $form->translate(
'This sets the field enclosure character. One character only,'
. ' defaults to double quote: "'
),
'value' => '"',
'required' => true,
'value' => '"',
'required' => true,
));

/*
Expand All @@ -178,14 +189,14 @@ protected static function addCsvElements(QuickForm $form)
protected static function addXslxElements(QuickForm $form, $filename)
{
$form->addElement('select', 'worksheet_addressing', array(
'label' => $form->translate('Choose worksheet'),
'description' => $form->translate('How to choose a worksheet'),
'label' => $form->translate('Choose worksheet'),
'description' => $form->translate('How to choose a worksheet'),
'multiOptions' => array(
'by_position' => $form->translate('by position'),
'by_name' => $form->translate('by name'),
'by_name' => $form->translate('by name'),
),
'value' => 'by_position',
'class' => 'autosubmit',
'value' => 'by_position',
'class' => 'autosubmit',
'required' => true,
));

Expand All @@ -197,19 +208,19 @@ protected static function addXslxElements(QuickForm $form, $filename)
$names = $file->getSheetNames();
$names = array_combine($names, $names);
$form->addElement('select', 'worksheet_name', array(
'label' => $form->translate('Name'),
'label' => $form->translate('Name'),
'required' => true,
'value' => $file->getFirstSheetName(),
'value' => $file->getFirstSheetName(),
'multiOptions' => $names,
));
break;

case 'by_position':
default:
$form->addElement('text', 'worksheet_position', array(
'label' => $form->translate('Position'),
'label' => $form->translate('Position'),
'required' => true,
'value' => '1',
'value' => '1',
));
break;
}
Expand All @@ -226,7 +237,7 @@ protected function fetchFiles($basedir, $format)
{
$result = array();
foreach (static::listFiles($basedir) as $file) {
$result[$file] = (object) $this->fetchFile($basedir, $file, $format);
$result[$file] = (object)$this->fetchFile($basedir, $file, null, $format);
}

return $result;
Expand All @@ -240,9 +251,13 @@ protected function fetchFiles($basedir, $format)
* @throws ConfigurationError
* @throws IcingaException
*/
protected function fetchFile($basedir, $file, $format)
protected function fetchFile($basedir, $file, $url, $format)
{
$filename = $basedir . '/' . $file;
if ($url !== null) {
$filename = $url;
} else {
$filename = $basedir . '/' . $file;
}

switch ($format) {
case 'yaml':
Expand Down Expand Up @@ -283,7 +298,7 @@ protected function readXslxFile($filename)
if ($this->getSetting('worksheet_addressing') === 'by_name') {
$sheet = $xlsx->getSheetByName($this->getSetting('worksheet_name'));
} else {
$sheet = $xlsx->getSheet((int) $this->getSetting('worksheet_position'));
$sheet = $xlsx->getSheet((int)$this->getSetting('worksheet_position'));
}

$data = $sheet->getData();
Expand Down Expand Up @@ -316,7 +331,7 @@ protected function readXslxFile($filename)
$row[$headers[$key]] = $val;
}

$result[] = (object) $row;
$result[] = (object)$row;
}

return $result;
Expand Down Expand Up @@ -353,9 +368,9 @@ protected function readCsvFile($filename)
$value = null;
}
}
$lines[] = (object) $line;
$lines[] = (object)$line;

$row ++;
$row++;
}
fclose($fh);

Expand Down Expand Up @@ -408,7 +423,7 @@ protected function normalizeSimpleXML($object)
{
$data = $object;
if (is_object($data)) {
$data = (object) get_object_vars($data);
$data = (object)get_object_vars($data);
}

if (is_object($data)) {
Expand Down Expand Up @@ -445,16 +460,16 @@ protected function fixYamlObjects($what)
{
if (is_array($what)) {
foreach (array_keys($what) as $key) {
if (! is_int($key)) {
$what = (object) $what;
if (!is_int($key)) {
$what = (object)$what;
break;
}
}
}

if (is_array($what) || is_object($what)) {
foreach ($what as $k => $v) {
if (! empty($v)) {
if (!empty($v)) {
if (is_object($what)) {
$what->$k = $this->fixYamlObjects($v);
} elseif (is_array($what)) {
Expand All @@ -474,7 +489,7 @@ protected function fixYamlObjects($what)
protected static function listAvailableFormats(QuickForm $form)
{
$formats = array(
'csv' => $form->translate('CSV (Comma Separated Value)'),
'csv' => $form->translate('CSV (Comma Separated Value)'),
'json' => $form->translate('JSON (JavaScript Object Notation)'),
);

Expand Down