Differential D16565 Diff 39942 src/applications/drydock/conduit/DrydockCreateLeaseConduitAPIMethod.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/conduit/DrydockCreateLeaseConduitAPIMethod.php
- This file was added.
<?php | |||||
final class DrydockCreateLeaseConduitAPIMethod | |||||
extends DrydockConduitAPIMethod { | |||||
public function getAPIMethodName() { | |||||
return 'drydock.createlease'; | |||||
} | |||||
public function getMethodSummary() { | |||||
return pht('Lease a Drydock resource.'); | |||||
} | |||||
public function getMethodDescription() { | |||||
$implementations = | |||||
DrydockBlueprintImplementation::getAllBlueprintImplementations(); | |||||
$implementations = mfilter($implementations, 'isEnabled'); | |||||
$head_class = pht('Class'); | |||||
$head_type = pht('Type'); | |||||
$head_name = pht('Name'); | |||||
$head_desc = pht('Summary'); | |||||
$out = array(); | |||||
$out[] = pht( | |||||
'This method leases a Drydock resource using an existing Drydock '. | |||||
'blueprint. Before using a blueprint here, you must obtain '. | |||||
'authorization for its use. You can request authorizations by calling '. | |||||
'`drydock.requestauthorization`.'); | |||||
$out[] = null; | |||||
$out[] = pht( | |||||
'When leasing a resource via a blueprint, the implementation class for '. | |||||
'that blueprint determines what type of resource is leased. The '. | |||||
'following blueprint implementation classes are supported:'); | |||||
$out[] = "| {$head_class} | {$head_type} | {$head_name} | {$head_desc} |"; | |||||
$out[] = '|---------------|--------------|--------------|--------------|'; | |||||
foreach ($implementations as $class => $implementation) { | |||||
$type = $implementation->getType(); | |||||
$name = $implementation->getBlueprintName(); | |||||
$desc = $implementation->getSummary(); | |||||
$out[] = "| `{$class}` | `{$type}` | **{$name}** | {$desc} |"; | |||||
} | |||||
$out[] = null; | |||||
$out[] = pht( | |||||
'Some blueprint implementations also define attributes which should '. | |||||
'be provided when leasing resources. You should provide these '. | |||||
'attributes using the `leaseAttributes` parameter when calling '. | |||||
'this method. The attributes you provide depend on the implementation '. | |||||
'class for the blueprint you are using.'); | |||||
$head_key = pht('Key'); | |||||
foreach ($implementations as $class => $implementation) { | |||||
$type = $implementation->getType(); | |||||
$name = $implementation->getBlueprintName(); | |||||
$out[] = "== {$name} =="; | |||||
$out[] = null; | |||||
$out[] = $implementation->getDescription(); | |||||
$out[] = null; | |||||
$out[] = pht( | |||||
'Blueprints with implementations of class `%s` will lease '. | |||||
'resources of the `%s` type.', | |||||
$class, | |||||
$type); | |||||
$out[] = null; | |||||
$spec = $implementation->getLeaseAttributesSpecification(); | |||||
if (!$spec) { | |||||
$out[] = pht( | |||||
'//(This implementation does not specify any lease attributes)//'); | |||||
$out[] = null; | |||||
continue; | |||||
} | |||||
$desc = $implementation->getLeaseAttributesDescriptions(); | |||||
$out[] = "| {$head_key} | {$head_type} | {$head_desc} |"; | |||||
$out[] = '|-------------|--------------|--------------|'; | |||||
foreach ($spec as $key => $key_type) { | |||||
$key_desc = idx($desc, $key); | |||||
$out[] = "| `{$key}` | //{$key_type}// | {$key_desc} |"; | |||||
} | |||||
$example = $implementation->getLeaseAttributesDataExample(); | |||||
if ($example !== null) { | |||||
$json = new PhutilJSON(); | |||||
$rendered = $json->encodeFormatted($example); | |||||
$out[] = pht('For example:'); | |||||
$out[] = '```lang=json'; | |||||
$out[] = $rendered; | |||||
$out[] = '```'; | |||||
} | |||||
} | |||||
return implode("\n", $out); | |||||
} | |||||
protected function defineParamTypes() { | |||||
return array( | |||||
'blueprintPHID' => 'required phid', | |||||
'leaseAttributes' => 'optional map<string, wild>', | |||||
); | |||||
} | |||||
protected function defineErrorTypes() { | |||||
return array( | |||||
'ERR_NO_BLUEPRINT' => pht( | |||||
'No active blueprint exists with the specified PHID.'), | |||||
'ERR_NOT_AUTHORIZED' => pht( | |||||
'You are not authorized to use this blueprint.'), | |||||
); | |||||
} | |||||
protected function defineReturnType() { | |||||
return 'wild'; | |||||
} | |||||
protected function execute(ConduitAPIRequest $request) { | |||||
$viewer = $request->getUser(); | |||||
$blueprint_phid = $request->getValue('blueprintPHID'); | |||||
$blueprint = id(new DrydockBlueprintQuery()) | |||||
->setViewer($viewer) | |||||
->withDisabled(false) | |||||
->withPHIDs(array($blueprint_phid)) | |||||
->executeOne(); | |||||
if (!$blueprint) { | |||||
throw new ConduitException('ERR_NO_BLUEPRINT'); | |||||
} | |||||
$authorization = id(new DrydockAuthorizationQuery()) | |||||
->setViewer($viewer) | |||||
->withBlueprintPHIDs(array($blueprint_phid)) | |||||
->withObjectPHIDs(array($viewer->getPHID())) | |||||
->withBlueprintStates(array( | |||||
DrydockAuthorization::BLUEPRINTAUTH_AUTHORIZED, | |||||
)) | |||||
->withObjectStates(array(DrydockAuthorization::OBJECTAUTH_ACTIVE)) | |||||
->executeOne(); | |||||
if (!$authorization) { | |||||
throw new ConduitException('ERR_NOT_AUTHORIZED'); | |||||
} | |||||
$implementation = $blueprint->getImplementation(); | |||||
$lease = id(new DrydockLease()) | |||||
->setOwnerPHID($viewer->getPHID()) | |||||
->setResourceType($implementation->getType()) | |||||
->setAuthorizingPHID($viewer->getPHID()) | |||||
->setAllowedBlueprintPHIDs(array($blueprint_phid)); | |||||
$lease_attributes = $request->getValue('leaseAttributes', array()); | |||||
$implementation->validateLeaseAttributes($lease_attributes); | |||||
foreach ($lease_attributes as $key => $value) { | |||||
$lease->setAttribute($key, $value); | |||||
} | |||||
$lease->queueForActivation(); | |||||
$search = id(new ConduitCall('drydock.lease.search', array( | |||||
'constraints' => array( | |||||
'phids' => array($lease->getPHID()), | |||||
), | |||||
))) | |||||
->setUser($viewer) | |||||
->execute(); | |||||
return idxv($search, array('data', 0)); | |||||
} | |||||
} |