Skip to content

Commit 2ac9877

Browse files
author
epriestley
committedNov 16, 2014
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
1 parent 1edcabe commit 2ac9877

6 files changed

+168
-3
lines changed
 

‎src/__phutil_library_map__.php

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'AlmanacBindingTransaction' => 'applications/almanac/storage/AlmanacBindingTransaction.php',
2020
'AlmanacBindingTransactionQuery' => 'applications/almanac/query/AlmanacBindingTransactionQuery.php',
2121
'AlmanacBindingViewController' => 'applications/almanac/controller/AlmanacBindingViewController.php',
22+
'AlmanacConduitAPIMethod' => 'applications/almanac/conduit/AlmanacConduitAPIMethod.php',
2223
'AlmanacConduitUtil' => 'applications/almanac/util/AlmanacConduitUtil.php',
2324
'AlmanacConsoleController' => 'applications/almanac/controller/AlmanacConsoleController.php',
2425
'AlmanacController' => 'applications/almanac/controller/AlmanacController.php',
@@ -66,6 +67,7 @@
6667
'AlmanacPropertyInterface' => 'applications/almanac/property/AlmanacPropertyInterface.php',
6768
'AlmanacPropertyQuery' => 'applications/almanac/query/AlmanacPropertyQuery.php',
6869
'AlmanacQuery' => 'applications/almanac/query/AlmanacQuery.php',
70+
'AlmanacQueryServicesConduitAPIMethod' => 'applications/almanac/conduit/AlmanacQueryServicesConduitAPIMethod.php',
6971
'AlmanacSchemaSpec' => 'applications/almanac/storage/AlmanacSchemaSpec.php',
7072
'AlmanacService' => 'applications/almanac/storage/AlmanacService.php',
7173
'AlmanacServiceController' => 'applications/almanac/controller/AlmanacServiceController.php',
@@ -2996,6 +2998,7 @@
29962998
'AlmanacBindingTransaction' => 'PhabricatorApplicationTransaction',
29972999
'AlmanacBindingTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
29983000
'AlmanacBindingViewController' => 'AlmanacServiceController',
3001+
'AlmanacConduitAPIMethod' => 'ConduitAPIMethod',
29993002
'AlmanacConduitUtil' => 'Phobject',
30003003
'AlmanacConsoleController' => 'AlmanacController',
30013004
'AlmanacController' => 'PhabricatorController',
@@ -3062,6 +3065,7 @@
30623065
'AlmanacPropertyEditController' => 'AlmanacDeviceController',
30633066
'AlmanacPropertyQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
30643067
'AlmanacQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
3068+
'AlmanacQueryServicesConduitAPIMethod' => 'AlmanacConduitAPIMethod',
30653069
'AlmanacSchemaSpec' => 'PhabricatorConfigSchemaSpec',
30663070
'AlmanacService' => array(
30673071
'AlmanacDAO',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}

‎src/applications/almanac/storage/AlmanacBinding.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ final class AlmanacBinding
2121

2222
public static function initializeNewBinding(AlmanacService $service) {
2323
return id(new AlmanacBinding())
24-
->setServicePHID($service->getPHID());
24+
->setServicePHID($service->getPHID())
25+
->attachAlmanacProperties(array());
2526
}
2627

2728
public function getConfiguration() {

‎src/applications/almanac/storage/AlmanacDevice.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ final class AlmanacDevice
2222
public static function initializeNewDevice() {
2323
return id(new AlmanacDevice())
2424
->setViewPolicy(PhabricatorPolicies::POLICY_USER)
25-
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN);
25+
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN)
26+
->attachAlmanacProperties(array());
2627
}
2728

2829
public function getConfiguration() {

‎src/applications/almanac/storage/AlmanacService.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ final class AlmanacService
2121
public static function initializeNewService() {
2222
return id(new AlmanacService())
2323
->setViewPolicy(PhabricatorPolicies::POLICY_USER)
24-
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN);
24+
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN)
25+
->attachAlmanacProperties(array());
2526
}
2627

2728
public function getConfiguration() {

0 commit comments

Comments
 (0)
Failed to load comments.