@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add almanac.queryservices Conduit API method

Summary:
Ref T5833. Just building one query for now which returns the whole binding + interface + network + device tree. Maybe this will get split up in the future.

This will allow web hosts to call the central Almanac and pull instance configuration, authenticating with SSH.

Test Plan: {F234443}

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: chad, epriestley

Maniphest Tasks: T5833

Differential Revision: https://secure.phabricator.com/D10862

+168 -3
+4
src/__phutil_library_map__.php
··· 19 19 'AlmanacBindingTransaction' => 'applications/almanac/storage/AlmanacBindingTransaction.php', 20 20 'AlmanacBindingTransactionQuery' => 'applications/almanac/query/AlmanacBindingTransactionQuery.php', 21 21 'AlmanacBindingViewController' => 'applications/almanac/controller/AlmanacBindingViewController.php', 22 + 'AlmanacConduitAPIMethod' => 'applications/almanac/conduit/AlmanacConduitAPIMethod.php', 22 23 'AlmanacConduitUtil' => 'applications/almanac/util/AlmanacConduitUtil.php', 23 24 'AlmanacConsoleController' => 'applications/almanac/controller/AlmanacConsoleController.php', 24 25 'AlmanacController' => 'applications/almanac/controller/AlmanacController.php', ··· 66 67 'AlmanacPropertyInterface' => 'applications/almanac/property/AlmanacPropertyInterface.php', 67 68 'AlmanacPropertyQuery' => 'applications/almanac/query/AlmanacPropertyQuery.php', 68 69 'AlmanacQuery' => 'applications/almanac/query/AlmanacQuery.php', 70 + 'AlmanacQueryServicesConduitAPIMethod' => 'applications/almanac/conduit/AlmanacQueryServicesConduitAPIMethod.php', 69 71 'AlmanacSchemaSpec' => 'applications/almanac/storage/AlmanacSchemaSpec.php', 70 72 'AlmanacService' => 'applications/almanac/storage/AlmanacService.php', 71 73 'AlmanacServiceController' => 'applications/almanac/controller/AlmanacServiceController.php', ··· 2996 2998 'AlmanacBindingTransaction' => 'PhabricatorApplicationTransaction', 2997 2999 'AlmanacBindingTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 2998 3000 'AlmanacBindingViewController' => 'AlmanacServiceController', 3001 + 'AlmanacConduitAPIMethod' => 'ConduitAPIMethod', 2999 3002 'AlmanacConduitUtil' => 'Phobject', 3000 3003 'AlmanacConsoleController' => 'AlmanacController', 3001 3004 'AlmanacController' => 'PhabricatorController', ··· 3062 3065 'AlmanacPropertyEditController' => 'AlmanacDeviceController', 3063 3066 'AlmanacPropertyQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 3064 3067 'AlmanacQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 3068 + 'AlmanacQueryServicesConduitAPIMethod' => 'AlmanacConduitAPIMethod', 3065 3069 'AlmanacSchemaSpec' => 'PhabricatorConfigSchemaSpec', 3066 3070 'AlmanacService' => array( 3067 3071 'AlmanacDAO',
+20
src/applications/almanac/conduit/AlmanacConduitAPIMethod.php
··· 1 + <?php 2 + 3 + abstract class AlmanacConduitAPIMethod extends ConduitAPIMethod { 4 + 5 + final public function getApplication() { 6 + return PhabricatorApplication::getByClass( 7 + 'PhabricatorAlmanacApplication'); 8 + } 9 + 10 + public function getMethodStatus() { 11 + return self::METHOD_STATUS_UNSTABLE; 12 + } 13 + 14 + public function getMethodStatusDescription() { 15 + return pht( 16 + 'Almanac is a prototype application and its APIs are '. 17 + 'subject to change.'); 18 + } 19 + 20 + }
+138
src/applications/almanac/conduit/AlmanacQueryServicesConduitAPIMethod.php
··· 1 + <?php 2 + 3 + final class AlmanacQueryServicesConduitAPIMethod 4 + extends AlmanacConduitAPIMethod { 5 + 6 + public function getAPIMethodName() { 7 + return 'almanac.queryservices'; 8 + } 9 + 10 + public function getMethodDescription() { 11 + return pht('Query Almanac services.'); 12 + } 13 + 14 + public function defineParamTypes() { 15 + return array( 16 + 'ids' => 'optional list<id>', 17 + 'phids' => 'optional list<phid>', 18 + 'names' => 'optional list<phid>', 19 + ) + self::getPagerParamTypes(); 20 + } 21 + 22 + public function defineReturnType() { 23 + return 'list<wild>'; 24 + } 25 + 26 + public function defineErrorTypes() { 27 + return array(); 28 + } 29 + 30 + protected function execute(ConduitAPIRequest $request) { 31 + $viewer = $request->getUser(); 32 + 33 + $query = id(new AlmanacServiceQuery()) 34 + ->setViewer($viewer); 35 + 36 + $ids = $request->getValue('ids'); 37 + if ($ids !== null) { 38 + $query->withIDs($ids); 39 + } 40 + 41 + $phids = $request->getValue('phids'); 42 + if ($phids !== null) { 43 + $query->withPHIDs($phids); 44 + } 45 + 46 + $names = $request->getValue('names'); 47 + if ($names !== null) { 48 + $query->withNames($names); 49 + } 50 + 51 + $pager = $this->newPager($request); 52 + 53 + $services = $query->executeWithCursorPager($pager); 54 + 55 + $bindings = id(new AlmanacBindingQuery()) 56 + ->setViewer($viewer) 57 + ->withServicePHIDs(mpull($services, 'getPHID')) 58 + ->execute(); 59 + $bindings = mgroup($bindings, 'getServicePHID'); 60 + 61 + $data = array(); 62 + foreach ($services as $service) { 63 + $phid = $service->getPHID(); 64 + 65 + $properties = $service->getAlmanacProperties(); 66 + $properties = mpull($properties, 'getFieldValue', 'getFieldName'); 67 + 68 + $service_bindings = idx($bindings, $phid, array()); 69 + $service_bindings = array_values($service_bindings); 70 + foreach ($service_bindings as $key => $service_binding) { 71 + $service_bindings[$key] = $this->getBindingDictionary($service_binding); 72 + } 73 + 74 + $data[] = $this->getServiceDictionary($service) + array( 75 + 'bindings' => $service_bindings, 76 + ); 77 + } 78 + 79 + $results = array( 80 + 'data' => $data, 81 + ); 82 + 83 + return $this->addPagerResults($results, $pager); 84 + } 85 + 86 + private function getServiceDictionary(AlmanacService $service) { 87 + return array( 88 + 'id' => (int)$service->getID(), 89 + 'phid' => $service->getPHID(), 90 + 'name' => $service->getName(), 91 + 'uri' => PhabricatorEnv::getProductionURI($service->getURI()), 92 + 'properties' => $this->getPropertiesDictionary($service), 93 + ); 94 + } 95 + 96 + private function getBindingDictionary(AlmanacBinding $binding) { 97 + return array( 98 + 'id' => (int)$binding->getID(), 99 + 'phid' => $binding->getPHID(), 100 + 'properties' => $this->getPropertiesDictionary($binding), 101 + 'interface' => $this->getInterfaceDictionary($binding->getInterface()), 102 + ); 103 + } 104 + 105 + private function getPropertiesDictionary(AlmanacPropertyInterface $obj) { 106 + $properties = $obj->getAlmanacProperties(); 107 + return (object)mpull($properties, 'getFieldValue', 'getFieldName'); 108 + } 109 + 110 + private function getInterfaceDictionary(AlmanacInterface $interface) { 111 + return array( 112 + 'id' => (int)$interface->getID(), 113 + 'phid' => $interface->getPHID(), 114 + 'address' => $interface->getAddress(), 115 + 'port' => (int)$interface->getPort(), 116 + 'device' => $this->getDeviceDictionary($interface->getDevice()), 117 + 'network' => $this->getNetworkDictionary($interface->getNetwork()), 118 + ); 119 + } 120 + 121 + private function getDeviceDictionary(AlmanacDevice $device) { 122 + return array( 123 + 'id' => (int)$device->getID(), 124 + 'phid' => $device->getPHID(), 125 + 'name' => $device->getName(), 126 + 'properties' => $this->getPropertiesDictionary($device), 127 + ); 128 + } 129 + 130 + private function getNetworkDictionary(AlmanacNetwork $network) { 131 + return array( 132 + 'id' => (int)$network->getID(), 133 + 'phid' => $network->getPHID(), 134 + 'name' => $network->getName(), 135 + ); 136 + } 137 + 138 + }
+2 -1
src/applications/almanac/storage/AlmanacBinding.php
··· 21 21 22 22 public static function initializeNewBinding(AlmanacService $service) { 23 23 return id(new AlmanacBinding()) 24 - ->setServicePHID($service->getPHID()); 24 + ->setServicePHID($service->getPHID()) 25 + ->attachAlmanacProperties(array()); 25 26 } 26 27 27 28 public function getConfiguration() {
+2 -1
src/applications/almanac/storage/AlmanacDevice.php
··· 22 22 public static function initializeNewDevice() { 23 23 return id(new AlmanacDevice()) 24 24 ->setViewPolicy(PhabricatorPolicies::POLICY_USER) 25 - ->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN); 25 + ->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN) 26 + ->attachAlmanacProperties(array()); 26 27 } 27 28 28 29 public function getConfiguration() {
+2 -1
src/applications/almanac/storage/AlmanacService.php
··· 21 21 public static function initializeNewService() { 22 22 return id(new AlmanacService()) 23 23 ->setViewPolicy(PhabricatorPolicies::POLICY_USER) 24 - ->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN); 24 + ->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN) 25 + ->attachAlmanacProperties(array()); 25 26 } 26 27 27 28 public function getConfiguration() {