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
|
<?php
namespace Icinga\Module\Director\Web\Widget;
use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use ipl\Html\Html;
use ipl\Html\Table;
class InspectPackages
{
use TranslationHelper;
/** @var Db */
protected $db;
/** @var string */
protected $baseUrl;
public function __construct(Db $db, $baseUrl)
{
$this->db = $db;
$this->baseUrl = $baseUrl;
}
public function getContent(IcingaEndpoint $endpoint = null, $package = null, $stage = null, $file = null)
{
if ($endpoint === null) {
return $this->getRootEndpoints();
} elseif ($package === null) {
return $this->getPackages($endpoint);
} elseif ($stage === null) {
return $this->getStages($endpoint, $package);
} elseif ($file === null) {
return $this->getFiles($endpoint, $package, $stage);
} else {
return $this->getFile($endpoint, $package, $stage, $file);
}
}
public function getTitle(IcingaEndpoint $endpoint = null, $package = null, $stage = null, $file = null)
{
if ($endpoint === null) {
return $this->translate('Endpoint in your Root Zone');
} elseif ($package === null) {
return \sprintf($this->translate('Packages on Endpoint: %s'), $endpoint->getObjectName());
} elseif ($stage === null) {
return \sprintf($this->translate('Stages in Package: %s'), $package);
} elseif ($file === null) {
return \sprintf($this->translate('Files in Stage: %s'), $stage);
} else {
return \sprintf($this->translate('File Content: %s'), $file);
}
}
public function getBreadCrumb(IcingaEndpoint $endpoint = null, $package = null, $stage = null)
{
$parts = [
'endpoint' => $endpoint === null ? null : $endpoint->getObjectName(),
'package' => $package,
'stage' => $stage,
];
$params = [];
// No root zone link for now:
// $result = [Link::create($this->translate('Root Zone'), $this->baseUrl)];
$result = [Html::tag('a', ['href' => '#'], $this->translate('Root Zone'))];
foreach ($parts as $name => $value) {
if ($value === null) {
break;
}
$params[$name] = $value;
$result[] = Link::create($value, $this->baseUrl, $params);
}
return Html::tag('ul', ['class' => 'breadcrumb'], Html::wrapEach($result, 'li'));
}
protected function getRootEndpoints()
{
$table = $this->prepareTable();
foreach ($this->db->getEndpointNamesInDeploymentZone() as $name) {
$table->add(Table::row([
Link::create($name, $this->baseUrl, [
'endpoint' => $name,
])
]));
}
return $table;
}
protected function getPackages(IcingaEndpoint $endpoint)
{
$table = $this->prepareTable();
$api = $endpoint->api();
foreach ($api->getPackages() as $package) {
$table->add(Table::row([
Link::create($package->name, $this->baseUrl, [
'endpoint' => $endpoint->getObjectName(),
'package' => $package->name,
])
]));
}
return $table;
}
protected function getStages(IcingaEndpoint $endpoint, $packageName)
{
$table = $this->prepareTable();
$api = $endpoint->api();
foreach ($api->getPackages() as $package) {
if ($package->name !== $packageName) {
continue;
}
foreach ($package->stages as $stage) {
$label = [$stage];
if ($stage === $package->{'active-stage'}) {
$label[] = Html::tag('small', [' (', $this->translate('active'), ')']);
}
$table->add(Table::row([
Link::create($label, $this->baseUrl, [
'endpoint' => $endpoint->getObjectName(),
'package' => $package->name,
'stage' => $stage
])
]));
}
}
return $table;
}
protected function getFiles(IcingaEndpoint $endpoint, $package, $stage)
{
$table = $this->prepareTable();
$table->getAttributes()->set('data-base-target', '_next');
foreach ($endpoint->api()->listStageFiles($stage, $package) as $filename) {
$table->add($table->row([
Link::create($filename, $this->baseUrl, [
'endpoint' => $endpoint->getObjectName(),
'package' => $package,
'stage' => $stage,
'file' => $filename
])
]));
}
return $table;
}
protected function getFile(IcingaEndpoint $endpoint, $package, $stage, $file)
{
return Html::tag('pre', $endpoint->api()->getStagedFile($stage, $file, $package));
}
protected function prepareTable($headerCols = [])
{
$table = new Table();
$table->addAttributes([
'class' => ['common-table', 'table-row-selectable'],
'data-base-target' => '_self'
]);
if (! empty($headerCols)) {
$table->add($table::row($headerCols, null, 'th'));
}
return $table;
}
}
|