diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 1bb318eea9..693ccd1d3f 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -77,6 +77,7 @@ phutil_register_library_map(array( 'AlmanacPropertyInterface' => 'applications/almanac/property/AlmanacPropertyInterface.php', 'AlmanacPropertyQuery' => 'applications/almanac/query/AlmanacPropertyQuery.php', 'AlmanacQuery' => 'applications/almanac/query/AlmanacQuery.php', + 'AlmanacQueryDevicesConduitAPIMethod' => 'applications/almanac/conduit/AlmanacQueryDevicesConduitAPIMethod.php', 'AlmanacQueryServicesConduitAPIMethod' => 'applications/almanac/conduit/AlmanacQueryServicesConduitAPIMethod.php', 'AlmanacSchemaSpec' => 'applications/almanac/storage/AlmanacSchemaSpec.php', 'AlmanacService' => 'applications/almanac/storage/AlmanacService.php', @@ -3218,6 +3219,7 @@ phutil_register_library_map(array( 'AlmanacPropertyEditController' => 'AlmanacDeviceController', 'AlmanacPropertyQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'AlmanacQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', + 'AlmanacQueryDevicesConduitAPIMethod' => 'AlmanacConduitAPIMethod', 'AlmanacQueryServicesConduitAPIMethod' => 'AlmanacConduitAPIMethod', 'AlmanacSchemaSpec' => 'PhabricatorConfigSchemaSpec', 'AlmanacService' => array( diff --git a/src/applications/almanac/conduit/AlmanacConduitAPIMethod.php b/src/applications/almanac/conduit/AlmanacConduitAPIMethod.php index fc3e8811c4..974d888917 100644 --- a/src/applications/almanac/conduit/AlmanacConduitAPIMethod.php +++ b/src/applications/almanac/conduit/AlmanacConduitAPIMethod.php @@ -17,4 +17,57 @@ abstract class AlmanacConduitAPIMethod extends ConduitAPIMethod { 'subject to change.'); } + protected function getServiceDictionary(AlmanacService $service) { + return array( + 'id' => (int)$service->getID(), + 'phid' => $service->getPHID(), + 'name' => $service->getName(), + 'uri' => PhabricatorEnv::getProductionURI($service->getURI()), + 'serviceClass' => $service->getServiceClass(), + 'properties' => $this->getPropertiesDictionary($service), + ); + } + + protected function getBindingDictionary(AlmanacBinding $binding) { + return array( + 'id' => (int)$binding->getID(), + 'phid' => $binding->getPHID(), + 'properties' => $this->getPropertiesDictionary($binding), + 'interface' => $this->getInterfaceDictionary($binding->getInterface()), + ); + } + + protected function getPropertiesDictionary(AlmanacPropertyInterface $obj) { + $properties = $obj->getAlmanacProperties(); + return (object)mpull($properties, 'getFieldValue', 'getFieldName'); + } + + protected function getInterfaceDictionary(AlmanacInterface $interface) { + return array( + 'id' => (int)$interface->getID(), + 'phid' => $interface->getPHID(), + 'address' => $interface->getAddress(), + 'port' => (int)$interface->getPort(), + 'device' => $this->getDeviceDictionary($interface->getDevice()), + 'network' => $this->getNetworkDictionary($interface->getNetwork()), + ); + } + + protected function getDeviceDictionary(AlmanacDevice $device) { + return array( + 'id' => (int)$device->getID(), + 'phid' => $device->getPHID(), + 'name' => $device->getName(), + 'properties' => $this->getPropertiesDictionary($device), + ); + } + + protected function getNetworkDictionary(AlmanacNetwork $network) { + return array( + 'id' => (int)$network->getID(), + 'phid' => $network->getPHID(), + 'name' => $network->getName(), + ); + } + } diff --git a/src/applications/almanac/conduit/AlmanacQueryDevicesConduitAPIMethod.php b/src/applications/almanac/conduit/AlmanacQueryDevicesConduitAPIMethod.php new file mode 100644 index 0000000000..8225ddd1b0 --- /dev/null +++ b/src/applications/almanac/conduit/AlmanacQueryDevicesConduitAPIMethod.php @@ -0,0 +1,67 @@ + 'optional list', + 'phids' => 'optional list', + 'names' => 'optional list', + ) + self::getPagerParamTypes(); + } + + public function defineReturnType() { + return 'list'; + } + + public function defineErrorTypes() { + return array(); + } + + protected function execute(ConduitAPIRequest $request) { + $viewer = $request->getUser(); + + $query = id(new AlmanacDeviceQuery()) + ->setViewer($viewer); + + $ids = $request->getValue('ids'); + if ($ids !== null) { + $query->withIDs($ids); + } + + $phids = $request->getValue('phids'); + if ($phids !== null) { + $query->withPHIDs($phids); + } + + $names = $request->getValue('names'); + if ($names !== null) { + $query->withNames($names); + } + + $pager = $this->newPager($request); + + $devices = $query->executeWithCursorPager($pager); + + $data = array(); + foreach ($devices as $device) { + $data[] = $this->getDeviceDictionary($device); + } + + $results = array( + 'data' => $data, + ); + + return $this->addPagerResults($results, $pager); + } + +} diff --git a/src/applications/almanac/conduit/AlmanacQueryServicesConduitAPIMethod.php b/src/applications/almanac/conduit/AlmanacQueryServicesConduitAPIMethod.php index ffabfbbc45..c25d6ba6e8 100644 --- a/src/applications/almanac/conduit/AlmanacQueryServicesConduitAPIMethod.php +++ b/src/applications/almanac/conduit/AlmanacQueryServicesConduitAPIMethod.php @@ -69,9 +69,6 @@ final class AlmanacQueryServicesConduitAPIMethod foreach ($services as $service) { $phid = $service->getPHID(); - $properties = $service->getAlmanacProperties(); - $properties = mpull($properties, 'getFieldValue', 'getFieldName'); - $service_bindings = $service->getBindings(); $service_bindings = array_values($service_bindings); foreach ($service_bindings as $key => $service_binding) { @@ -90,57 +87,4 @@ final class AlmanacQueryServicesConduitAPIMethod return $this->addPagerResults($results, $pager); } - private function getServiceDictionary(AlmanacService $service) { - return array( - 'id' => (int)$service->getID(), - 'phid' => $service->getPHID(), - 'name' => $service->getName(), - 'uri' => PhabricatorEnv::getProductionURI($service->getURI()), - 'serviceClass' => $service->getServiceClass(), - 'properties' => $this->getPropertiesDictionary($service), - ); - } - - private function getBindingDictionary(AlmanacBinding $binding) { - return array( - 'id' => (int)$binding->getID(), - 'phid' => $binding->getPHID(), - 'properties' => $this->getPropertiesDictionary($binding), - 'interface' => $this->getInterfaceDictionary($binding->getInterface()), - ); - } - - private function getPropertiesDictionary(AlmanacPropertyInterface $obj) { - $properties = $obj->getAlmanacProperties(); - return (object)mpull($properties, 'getFieldValue', 'getFieldName'); - } - - private function getInterfaceDictionary(AlmanacInterface $interface) { - return array( - 'id' => (int)$interface->getID(), - 'phid' => $interface->getPHID(), - 'address' => $interface->getAddress(), - 'port' => (int)$interface->getPort(), - 'device' => $this->getDeviceDictionary($interface->getDevice()), - 'network' => $this->getNetworkDictionary($interface->getNetwork()), - ); - } - - private function getDeviceDictionary(AlmanacDevice $device) { - return array( - 'id' => (int)$device->getID(), - 'phid' => $device->getPHID(), - 'name' => $device->getName(), - 'properties' => $this->getPropertiesDictionary($device), - ); - } - - private function getNetworkDictionary(AlmanacNetwork $network) { - return array( - 'id' => (int)$network->getID(), - 'phid' => $network->getPHID(), - 'name' => $network->getName(), - ); - } - }