1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
<?php
namespace Icinga\Module\Director\Web;
use gipfl\Web\Widget\Hint;
use ipl\Html\Text;
use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Web\Request;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Widget\ControlsAndContent;
class ObjectPreview
{
use TranslationHelper;
/** @var IcingaObject */
protected $object;
/** @var Request */
protected $request;
public function __construct(IcingaObject $object, Request $request)
{
$this->object = $object;
$this->request = $request;
}
/**
* @param ControlsAndContent $cc
* @throws \Icinga\Exception\NotFoundError
*/
public function renderTo(ControlsAndContent $cc)
{
$object = $this->object;
$url = $this->request->getUrl();
$params = $url->getParams();
$cc->addTitle(
$this->translate('Config preview: %s'),
$object->getObjectName()
);
if ($params->shift('resolved')) {
$object = $object::fromPlainObject(
$object->toPlainObject(true),
$object->getConnection()
);
$cc->actions()->add(Link::create(
$this->translate('Show normal'),
$url->without('resolved'),
null,
['class' => 'icon-resize-small state-warning']
));
} else {
try {
if ($object->supportsImports() && $object->imports()->count() > 0) {
$cc->actions()->add(Link::create(
$this->translate('Show resolved'),
$url->with('resolved', true),
null,
['class' => 'icon-resize-full']
));
}
} catch (NestingError $e) {
// No resolve link with nesting errors
}
}
$content = $cc->content();
if ($object->isDisabled()) {
$content->add(Hint::error(
$this->translate('This object will not be deployed as it has been disabled')
));
}
if ($object->isExternal()) {
$content->add(Html::tag('p', null, $this->translate((
'This is an external object. It has been imported from Icinga 2 through the'
. ' Core API and cannot be managed with the Icinga Director. It is however'
. ' perfectly valid to create objects using this or referring to this object.'
. ' You might also want to define related Fields to make work based on this'
. ' object more enjoyable.'
))));
}
$config = $object->toSingleIcingaConfig();
foreach ($config->getFiles() as $filename => $file) {
if (! $object->isExternal()) {
$content->add(Html::tag('h2', null, $filename));
}
$classes = array();
if ($object->isDisabled()) {
$classes[] = 'disabled';
} elseif ($object->isExternal()) {
$classes[] = 'logfile';
}
$type = $object->getShortTableName();
$plain = Html::wantHtml($file->getContent())->render();
$plain = preg_replace_callback(
'/^(\s+import\s+\"\;)(.+)(\"\;)/m',
[$this, 'linkImport'],
$plain
);
if ($type !== 'command') {
$plain = preg_replace_callback(
'/^(\s+(?:check_|event_)?command\s+=\s+\"\;)(.+)(\"\;)/m',
[$this, 'linkCommand'],
$plain
);
}
$plain = preg_replace_callback(
'/^(\s+host_name\s+=\s+\"\;)(.+)(\"\;)/m',
[$this, 'linkHost'],
$plain
);
$text = Text::create($plain)->setEscaped();
$content->add(Html::tag('pre', ['class' => $classes], $text));
}
}
/**
* @api internal
* @param $match
* @return string
*/
public function linkImport($match)
{
$blacklist = [
'plugin-notification-command',
'plugin-check-command',
];
if (in_array($match[2], $blacklist)) {
return $match[1] . $match[2] . $match[3];
}
$urlObjectType = $this->object->getShortTableName();
if ($urlObjectType === 'service_set') {
$urlObjectType = 'service';
}
return $match[1] . Link::create(
$match[2],
sprintf("director/$urlObjectType"),
['name' => $match[2]]
)->render() . $match[3];
}
/**
* @api internal
* @param $match
* @return string
*/
public function linkCommand($match)
{
return $match[1] . Link::create(
$match[2],
sprintf('director/command'),
['name' => $match[2]]
)->render() . $match[3];
}
/**
* @api internal
* @param $match
* @return string
*/
public function linkHost($match)
{
return $match[1] . Link::create(
$match[2],
sprintf('director/host'),
['name' => $match[2]]
)->render() . $match[3];
}
}
|