Add AlmanacBinding, to bind a service to an interface
Summary: Ref T5833. Allows you to bind a service (like `db.example.com`) to one or more interfaces (for example, to specify a pool with one read/write host and two read-only hosts). You can't configure which hosts have which properties yet, but you can add all the relevant interfaces to the service. Next diff will start supporting service, binding, and device properties like "is writable", "is active", etc., so that Almanac will be able to express operations like "change which database is writable", "disable writes", "bring a device down", etc.
Test Plan: See screenshots.
Reviewers: btrahan
Reviewed By: btrahan
Subscribers: epriestley
Maniphest Tasks: T5833
Differential Revision: https://secure.phabricator.com/D10745
2014-10-27 21:39:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class AlmanacBindingEditor
|
2016-02-22 15:35:30 +01:00
|
|
|
extends AlmanacEditor {
|
Add AlmanacBinding, to bind a service to an interface
Summary: Ref T5833. Allows you to bind a service (like `db.example.com`) to one or more interfaces (for example, to specify a pool with one read/write host and two read-only hosts). You can't configure which hosts have which properties yet, but you can add all the relevant interfaces to the service. Next diff will start supporting service, binding, and device properties like "is writable", "is active", etc., so that Almanac will be able to express operations like "change which database is writable", "disable writes", "bring a device down", etc.
Test Plan: See screenshots.
Reviewers: btrahan
Reviewed By: btrahan
Subscribers: epriestley
Maniphest Tasks: T5833
Differential Revision: https://secure.phabricator.com/D10745
2014-10-27 21:39:36 +01:00
|
|
|
|
|
|
|
public function getEditorObjectsDescription() {
|
|
|
|
return pht('Almanac Binding');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTransactionTypes() {
|
|
|
|
$types = parent::getTransactionTypes();
|
|
|
|
|
|
|
|
$types[] = AlmanacBindingTransaction::TYPE_INTERFACE;
|
|
|
|
|
|
|
|
return $types;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getCustomTransactionOldValue(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case AlmanacBindingTransaction::TYPE_INTERFACE:
|
|
|
|
return $object->getInterfacePHID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getCustomTransactionOldValue($object, $xaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getCustomTransactionNewValue(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
|
|
|
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case AlmanacBindingTransaction::TYPE_INTERFACE:
|
|
|
|
return $xaction->getNewValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getCustomTransactionNewValue($object, $xaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function applyCustomInternalTransaction(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
|
|
|
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case AlmanacBindingTransaction::TYPE_INTERFACE:
|
|
|
|
$interface = id(new AlmanacInterfaceQuery())
|
|
|
|
->setViewer($this->requireActor())
|
|
|
|
->withPHIDs(array($xaction->getNewValue()))
|
|
|
|
->executeOne();
|
|
|
|
$object->setDevicePHID($interface->getDevicePHID());
|
|
|
|
$object->setInterfacePHID($interface->getPHID());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::applyCustomInternalTransaction($object, $xaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function applyCustomExternalTransaction(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
|
|
|
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case AlmanacBindingTransaction::TYPE_INTERFACE:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::applyCustomExternalTransaction($object, $xaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function validateTransaction(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
$type,
|
|
|
|
array $xactions) {
|
|
|
|
|
|
|
|
$errors = parent::validateTransaction($object, $type, $xactions);
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case AlmanacBindingTransaction::TYPE_INTERFACE:
|
|
|
|
$missing = $this->validateIsEmptyTextField(
|
|
|
|
$object->getInterfacePHID(),
|
|
|
|
$xactions);
|
|
|
|
if ($missing) {
|
|
|
|
$error = new PhabricatorApplicationTransactionValidationError(
|
|
|
|
$type,
|
|
|
|
pht('Required'),
|
|
|
|
pht('Bindings must specify an interface.'),
|
|
|
|
nonempty(last($xactions), null));
|
|
|
|
$error->setIsMissingFieldError(true);
|
|
|
|
$errors[] = $error;
|
|
|
|
} else if ($xactions) {
|
|
|
|
foreach ($xactions as $xaction) {
|
|
|
|
$interfaces = id(new AlmanacInterfaceQuery())
|
|
|
|
->setViewer($this->requireActor())
|
|
|
|
->withPHIDs(array($xaction->getNewValue()))
|
|
|
|
->execute();
|
|
|
|
if (!$interfaces) {
|
|
|
|
$error = new PhabricatorApplicationTransactionValidationError(
|
|
|
|
$type,
|
|
|
|
pht('Invalid'),
|
|
|
|
pht(
|
|
|
|
'You can not bind a service to an invalid or restricted '.
|
|
|
|
'interface.'),
|
|
|
|
$xaction);
|
|
|
|
$errors[] = $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$final_value = last($xactions)->getNewValue();
|
|
|
|
|
|
|
|
$binding = id(new AlmanacBindingQuery())
|
|
|
|
->setViewer(PhabricatorUser::getOmnipotentUser())
|
|
|
|
->withServicePHIDs(array($object->getServicePHID()))
|
|
|
|
->withInterfacePHIDs(array($final_value))
|
|
|
|
->executeOne();
|
|
|
|
if ($binding && ($binding->getID() != $object->getID())) {
|
|
|
|
$error = new PhabricatorApplicationTransactionValidationError(
|
|
|
|
$type,
|
|
|
|
pht('Already Bound'),
|
|
|
|
pht(
|
|
|
|
'You can not bind a service to the same interface multiple '.
|
|
|
|
'times.'),
|
|
|
|
last($xactions));
|
|
|
|
$errors[] = $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|