Skip to content

Commit 9b0fa57

Browse files
author
epriestley
committed
Make Drydock more broadly aware of policies
Summary: Ref T2015. Moves a bunch of raw object loads into modern policy-aware queries. Also straightens out the Log and Lease policies a little bit: there are legitimate states where these objects are not attached to a resource (particularly, while a lease is being acquired). Handle these more gracefully. Test Plan: Lint / browsed stuff. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T2015 Differential Revision: https://secure.phabricator.com/D7836
1 parent 1650874 commit 9b0fa57

17 files changed

+192
-86
lines changed

src/applications/drydock/blueprint/DrydockBlueprintImplementation.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ final public function allocateLease(
122122
$resource->beginReadLocking();
123123
$resource->reload();
124124

125+
// TODO: Policy stuff.
125126
$other_leases = id(new DrydockLease())->loadAllWhere(
126127
'status IN (%Ld) AND resourceID = %d',
127128
array(
@@ -388,13 +389,13 @@ public static function getAllBlueprintImplementationsForResource($type) {
388389
}
389390

390391
protected function newResourceTemplate($name) {
391-
$resource = new DrydockResource();
392-
$resource->setBlueprintPHID($this->getInstance()->getPHID());
393-
$resource->setBlueprintClass($this->getBlueprintClass());
394-
$resource->setType($this->getType());
395-
$resource->setStatus(DrydockResourceStatus::STATUS_PENDING);
396-
$resource->setName($name);
397-
$resource->save();
392+
$resource = id(new DrydockResource())
393+
->setBlueprintPHID($this->getInstance()->getPHID())
394+
->setBlueprintClass($this->getBlueprintClass())
395+
->setType($this->getType())
396+
->setStatus(DrydockResourceStatus::STATUS_PENDING)
397+
->setName($name)
398+
->save();
398399

399400
$this->activeResource = $resource;
400401

src/applications/drydock/blueprint/DrydockWorkingCopyBlueprintImplementation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected function executeAllocateResource(DrydockLease $lease) {
5252
throw new Exception("Unsupported VCS!");
5353
}
5454

55+
// TODO: Policy stuff here too.
5556
$host_lease = id(new DrydockLease())
5657
->setResourceType('host')
5758
->waitUntilActive();

src/applications/drydock/controller/DrydockBlueprintCreateController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public function processRequest() {
1616
return $this->createDialog($implementations);
1717
}
1818

19-
$blueprint = new DrydockBlueprint();
20-
$blueprint->setClassName($class);
21-
$blueprint->setDetails(array());
22-
$blueprint->setViewPolicy(PhabricatorPolicies::POLICY_ADMIN);
23-
$blueprint->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN);
24-
$blueprint->save();
19+
$blueprint = id(new DrydockBlueprint())
20+
->setClassName($class)
21+
->setDetails(array())
22+
->setViewPolicy(PhabricatorPolicies::POLICY_ADMIN)
23+
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN)
24+
->save();
2525

2626
$edit_uri = $this->getApplicationURI(
2727
"blueprint/edit/".$blueprint->getID()."/");

src/applications/drydock/controller/DrydockBlueprintViewController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ public function willProcessRequest(array $data) {
1010

1111
public function processRequest() {
1212
$request = $this->getRequest();
13-
$user = $request->getUser();
13+
$viewer = $request->getUser();
1414

15-
$blueprint = id(new DrydockBlueprint())->load($this->id);
15+
$blueprint = id(new DrydockBlueprintQuery())
16+
->setViewer($viewer)
17+
->withIDs(array($this->id))
18+
->executeOne();
1619
if (!$blueprint) {
1720
return new Aphront404Response();
1821
}
@@ -30,7 +33,7 @@ public function processRequest() {
3033

3134
$resources = id(new DrydockResourceQuery())
3235
->withBlueprintPHIDs(array($blueprint->getPHID()))
33-
->setViewer($user)
36+
->setViewer($viewer)
3437
->execute();
3538

3639
$resource_list = $this->buildResourceListView($resources);

src/applications/drydock/controller/DrydockLeaseViewController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ public function willProcessRequest(array $data) {
1010

1111
public function processRequest() {
1212
$request = $this->getRequest();
13-
$user = $request->getUser();
13+
$viewer = $request->getUser();
1414

15-
$lease = id(new DrydockLease())->load($this->id);
15+
$lease = id(new DrydockLeaseQuery())
16+
->setViewer($viewer)
17+
->withIDs(array($this->id))
18+
->executeOne();
1619
if (!$lease) {
1720
return new Aphront404Response();
1821
}
@@ -32,7 +35,7 @@ public function processRequest() {
3235
$pager->setOffset($request->getInt('offset'));
3336

3437
$logs = id(new DrydockLogQuery())
35-
->setViewer($user)
38+
->setViewer($viewer)
3639
->withLeaseIDs(array($lease->getID()))
3740
->executeWithOffsetPager($pager);
3841

src/applications/drydock/controller/DrydockResourceCloseController.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ public function willProcessRequest(array $data) {
1010

1111
public function processRequest() {
1212
$request = $this->getRequest();
13-
$user = $request->getUser();
13+
$viewer = $request->getUser();
1414

15-
$resource = id(new DrydockResource())->load($this->id);
15+
$resource = id(new DrydockResourceQuery())
16+
->setViewer($viewer)
17+
->withIDs(array($this->id))
18+
->executeOne();
1619
if (!$resource) {
1720
return new Aphront404Response();
1821
}
@@ -22,7 +25,7 @@ public function processRequest() {
2225

2326
if ($resource->getStatus() != DrydockResourceStatus::STATUS_OPEN) {
2427
$dialog = id(new AphrontDialogView())
25-
->setUser($user)
28+
->setUser($viewer)
2629
->setTitle(pht('Resource Not Open'))
2730
->appendChild(phutil_tag('p', array(), pht(
2831
'You can only close "open" resources.')))
@@ -31,22 +34,22 @@ public function processRequest() {
3134
return id(new AphrontDialogResponse())->setDialog($dialog);
3235
}
3336

34-
if (!$request->isDialogFormPost()) {
35-
$dialog = id(new AphrontDialogView())
36-
->setUser($user)
37-
->setTitle(pht('Really close resource?'))
38-
->appendChild(phutil_tag('p', array(), pht(
39-
'Closing a resource releases all leases and destroys the '.
40-
'resource. It can not be undone. Continue?')))
41-
->addSubmitButton(pht('Close Resource'))
42-
->addCancelButton($resource_uri);
43-
44-
return id(new AphrontDialogResponse())->setDialog($dialog);
37+
if ($request->isFormPost()) {
38+
$resource->closeResource();
39+
return id(new AphrontReloadResponse())->setURI($resource_uri);
4540
}
4641

47-
$resource->closeResource();
42+
$dialog = id(new AphrontDialogView())
43+
->setUser($viewer)
44+
->setTitle(pht('Really close resource?'))
45+
->appendChild(
46+
pht(
47+
'Closing a resource releases all leases and destroys the '.
48+
'resource. It can not be undone. Continue?'))
49+
->addSubmitButton(pht('Close Resource'))
50+
->addCancelButton($resource_uri);
4851

49-
return id(new AphrontReloadResponse())->setURI($resource_uri);
52+
return id(new AphrontDialogResponse())->setDialog($dialog);
5053
}
5154

5255
}

src/applications/drydock/controller/DrydockResourceViewController.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ public function willProcessRequest(array $data) {
1010

1111
public function processRequest() {
1212
$request = $this->getRequest();
13-
$user = $request->getUser();
13+
$viewer = $request->getUser();
1414

15-
$resource = id(new DrydockResource())->load($this->id);
15+
$resource = id(new DrydockResourceQuery())
16+
->setViewer($viewer)
17+
->withIDs(array($this->id))
18+
->executeOne();
1619
if (!$resource) {
1720
return new Aphront404Response();
1821
}
@@ -29,7 +32,7 @@ public function processRequest() {
2932
$resource_uri = $this->getApplicationURI($resource_uri);
3033

3134
$leases = id(new DrydockLeaseQuery())
32-
->setViewer($user)
35+
->setViewer($viewer)
3336
->withResourceIDs(array($resource->getID()))
3437
->execute();
3538

@@ -41,7 +44,7 @@ public function processRequest() {
4144
$pager->setOffset($request->getInt('offset'));
4245

4346
$logs = id(new DrydockLogQuery())
44-
->setViewer($user)
47+
->setViewer($viewer)
4548
->withResourceIDs(array($resource->getID()))
4649
->executeWithOffsetPager($pager);
4750

src/applications/drydock/management/DrydockManagementCloseWorkflow.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ public function execute(PhutilArgumentParser $args) {
2525
"Specify one or more resource IDs to close.");
2626
}
2727

28+
$viewer = PhabricatorUser::getOmnipotentUser();
29+
30+
$resources = id(new DrydockResourceQuery())
31+
->setViewer($viewer)
32+
->withIDs($ids)
33+
->execute();
34+
2835
foreach ($ids as $id) {
29-
$resource = id(new DrydockResource())->load($id);
36+
$resource = idx($resources, $id);
3037
if (!$resource) {
3138
$console->writeErr("Resource %d does not exist!\n", $id);
3239
} else if ($resource->getStatus() != DrydockResourceStatus::STATUS_OPEN) {

src/applications/drydock/management/DrydockManagementCreateResourceWorkflow.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,22 @@ public function execute(PhutilArgumentParser $args) {
4949
$attributes = $options->parse($attributes);
5050
}
5151

52-
$blueprint = id(new DrydockBlueprint())->load((int)$blueprint_id);
52+
$viewer = PhabricatorUser::getOmnipotentUser();
53+
54+
$blueprint = id(new DrydockBlueprintQuery())
55+
->setViewer($viewer)
56+
->withIDs(array($blueprint_id))
57+
->executeOne();
5358
if (!$blueprint) {
5459
throw new PhutilArgumentUsageException(
5560
"Specified blueprint does not exist.");
5661
}
5762

58-
$resource = new DrydockResource();
59-
$resource->setBlueprintPHID($blueprint->getPHID());
60-
$resource->setType($blueprint->getImplementation()->getType());
61-
$resource->setName($resource_name);
62-
$resource->setStatus(DrydockResourceStatus::STATUS_OPEN);
63+
$resource = id(new DrydockResource())
64+
->setBlueprintPHID($blueprint->getPHID())
65+
->setType($blueprint->getImplementation()->getType())
66+
->setName($resource_name)
67+
->setStatus(DrydockResourceStatus::STATUS_OPEN);
6368
if ($attributes) {
6469
$resource->setAttributes($attributes);
6570
}

src/applications/drydock/query/DrydockLeaseQuery.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,25 @@ public function loadPage() {
4444
}
4545

4646
public function willFilterPage(array $leases) {
47-
$resources = id(new DrydockResourceQuery())
48-
->setParentQuery($this)
49-
->setViewer($this->getViewer())
50-
->withIDs(mpull($leases, 'getResourceID'))
51-
->execute();
47+
$resource_ids = array_filter(mpull($leases, 'getResourceID'));
48+
if ($resource_ids) {
49+
$resources = id(new DrydockResourceQuery())
50+
->setParentQuery($this)
51+
->setViewer($this->getViewer())
52+
->withIDs($resource_ids)
53+
->execute();
54+
} else {
55+
$resources = array();
56+
}
5257

5358
foreach ($leases as $key => $lease) {
54-
$resource = idx($resources, $lease->getResourceID());
55-
if (!$resource) {
56-
unset($leases[$key]);
57-
continue;
59+
$resource = null;
60+
if ($lease->getResourceID()) {
61+
$resource = idx($resources, $lease->getResourceID());
62+
if (!$resource) {
63+
unset($leases[$key]);
64+
continue;
65+
}
5866
}
5967
$lease->attachResource($resource);
6068
}

0 commit comments

Comments
 (0)