-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathVCenterShipMetricsForm.php
More file actions
140 lines (116 loc) · 4 KB
/
Copy pathVCenterShipMetricsForm.php
File metadata and controls
140 lines (116 loc) · 4 KB
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
<?php
namespace Icinga\Module\Vspheredb\Web\Form;
use gipfl\IcingaWeb2\Link;
use ipl\I18n\Translation;
use gipfl\ZfDbStore\ZfDbStore;
use Icinga\Module\Vspheredb\Daemon\RemoteClient;
use Icinga\Module\Vspheredb\DbObject\VCenter;
use Icinga\Module\Vspheredb\Hook\PerfDataConsumerHook;
use Icinga\Module\Vspheredb\Storable\PerfdataConsumer;
use Icinga\Module\Vspheredb\Storable\PerfdataSubscription;
use ipl\Html\Html;
use Ramsey\Uuid\Uuid;
use React\EventLoop\LoopInterface;
class VCenterShipMetricsForm extends ObjectForm
{
use FormElementStealer;
use Translation;
public const ON_DELETE = 'delete';
protected $class = PerfdataSubscription::class;
/** @var VCenter */
protected $vCenter;
/** @var PerfdataConsumer[] */
protected $consumers;
/** @var RemoteClient */
protected $remoteClient;
/** @var LoopInterface */
protected $loop;
public function __construct(ZfDbStore $store, VCenter $vCenter, RemoteClient $client, LoopInterface $loop)
{
parent::__construct($store);
$this->vCenter = $vCenter;
$this->remoteClient = $client;
$this->loop = $loop;
$this->populate($vCenter->getProperties());
}
protected function fetchConsumers()
{
$db = $this->vCenter->getConnection()->getDbAdapter();
/** @var PerfdataConsumer $consumers */
$consumers = [];
foreach ($db->fetchAll($db->select()->from('perfdata_consumer')) as $row) {
$consumers[Uuid::fromBytes($row->uuid)->toString()] = PerfdataConsumer::create((array) $row);
}
return $consumers;
}
protected function enumConsumers($consumers)
{
$result = [];
foreach ($consumers as $uuid => $consumer) {
$result[$uuid] = $consumer->get('name');
}
return $result;
}
public function assemble()
{
$this->add(Html::tag('h3', $this->translate('Ship Performance Data')));
if ($this->object instanceof PerfdataSubscription && !$this->hasBeenSent()) {
$this->populate((array) $this->object->settings());
}
if ($consumer = $this->selectConsumer()) {
$this->addConsumerConfig($consumer);
}
$this->addButtons(isset($consumer), 'consumer');
}
/**
* @return PerfdataConsumer|null
*/
protected function selectConsumer()
{
$consumers = $this->fetchConsumers();
if ($this->object) {
$consumer = Uuid::fromBytes($this->object->get('consumer_uuid'))->toString();
} else {
$consumer = null;
}
$this->addElement('select', 'consumer', [
'label' => $this->translate('Consumer'),
'options' => ['' => $this->translate('- please choose -')] + $this->enumConsumers($consumers),
'description' => Html::sprintf(
$this->translate('Choose one of your configured %s'),
Link::create($this->translate('Performance Data Consumers'), 'vspheredb/perfdata/consumers')
),
'required' => true,
'value' => $consumer,
'class' => 'autosubmit',
]);
$this->addHidden('enabled', [
'value' => 'y'
]);
$value = $this->getValue('consumer') ?? '';
if (isset($consumers[$value])) {
return $consumers[$value];
}
return null;
}
protected function addConsumerConfig($consumer)
{
$instance = PerfDataConsumerHook::createConsumerInstance($consumer, $this->loop);
if ($form = $instance->getSubscriptionForm($this->remoteClient)) {
$this->addFormElementsFrom($form);
}
}
public function isValidEvent($event): bool
{
if ($event === self::ON_DELETE) {
return true;
}
return parent::isValidEvent($event);
}
public function createObject()
{
$object = parent::createObject();
$object->set('vcenter_uuid', $this->vCenter->get('instance_uuid'));
return $object;
}
}