Skip to content

Commit

Permalink
IcingaServiceSet: Refactor config rendering for sets
Browse files Browse the repository at this point in the history
* A header is now always rendered and includes assign and description
* Diff is now visible for sets
  • Loading branch information
lazyfrosch committed Apr 10, 2019
1 parent db00f1b commit 5cac698
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions library/Director/Objects/IcingaServiceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ protected function setKey($key)
*/
public function getServiceObjects()
{
// don't try to resolve services for unstored objects - as in getServiceObjectsForSet()
// also for diff in activity log
if ($this->get('id') === null) {
return [];
}

if ($this->get('host_id')) {
$imports = $this->imports()->getObjects();
if (empty($imports)) {
Expand Down Expand Up @@ -280,6 +286,9 @@ public function onDelete()
*/
public function renderToConfig(IcingaConfig $config)
{
// always print the header, so you have minimal info present
$file = $this->getConfigFileWithHeader($config);

if ($this->get('assign_filter') === null && $this->isTemplate()) {
return;
}
Expand All @@ -293,7 +302,6 @@ public function renderToConfig(IcingaConfig $config)
if (empty($services)) {
return;
}
$file = $this->getConfigFileWithHeader($config);

// Loop over all services belonging to this set
// add our assign rules and then add the service to the config
Expand Down Expand Up @@ -350,17 +358,38 @@ protected function getConfigFileWithHeader(IcingaConfig $config)

protected function getConfigHeaderComment(IcingaConfig $config)
{
$name = $this->getObjectName();
$assign = $this->get('assign_filter');

if ($config->isLegacy()) {
if ($this->get('assign_filter')) {
$comment = "## applied Service Set '%s'\n\n";
if ($assign !== null) {
return "## applied Service Set '${name}'\n\n";
} else {
$comment = "## Service Set '%s' on this host\n\n";
return "## Service Set '${name}' on this host\n\n";
}
} else {
$comment = "/** Service Set '%s' **/\n\n";
}
$comment = [
"Service Set: ${name}",
];

return sprintf($comment, $this->getObjectName());
if (($host = $this->get('host')) !== null) {
$comment[] = 'on host ' . $host;
}

if (($description = $this->get('description')) !== null){
$comment[] = '';
foreach (preg_split('~\\n~', $description) as $line) {
$comment[] = $line;
}
}

if ($assign !== null) {
$comment[] = '';
$comment[] = trim($this->renderAssign_Filter());
}

return "/**\n * " . join("\n * ", $comment) . "\n */\n\n";
}
}

public function copyVarsToService(IcingaService $service)
Expand Down

0 comments on commit 5cac698

Please sign in to comment.