Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/drydock/storage/DrydockBlueprint.php
12256 views
1
<?php
2
3
/**
4
* @task resource Allocating Resources
5
* @task lease Acquiring Leases
6
*/
7
final class DrydockBlueprint extends DrydockDAO
8
implements
9
PhabricatorApplicationTransactionInterface,
10
PhabricatorPolicyInterface,
11
PhabricatorCustomFieldInterface,
12
PhabricatorNgramsInterface,
13
PhabricatorProjectInterface,
14
PhabricatorConduitResultInterface {
15
16
protected $className;
17
protected $blueprintName;
18
protected $viewPolicy;
19
protected $editPolicy;
20
protected $details = array();
21
protected $isDisabled;
22
23
private $implementation = self::ATTACHABLE;
24
private $customFields = self::ATTACHABLE;
25
private $fields = null;
26
27
public static function initializeNewBlueprint(PhabricatorUser $actor) {
28
$app = id(new PhabricatorApplicationQuery())
29
->setViewer($actor)
30
->withClasses(array('PhabricatorDrydockApplication'))
31
->executeOne();
32
33
$view_policy = $app->getPolicy(
34
DrydockDefaultViewCapability::CAPABILITY);
35
$edit_policy = $app->getPolicy(
36
DrydockDefaultEditCapability::CAPABILITY);
37
38
return id(new DrydockBlueprint())
39
->setViewPolicy($view_policy)
40
->setEditPolicy($edit_policy)
41
->setBlueprintName('')
42
->setIsDisabled(0);
43
}
44
45
protected function getConfiguration() {
46
return array(
47
self::CONFIG_AUX_PHID => true,
48
self::CONFIG_SERIALIZATION => array(
49
'details' => self::SERIALIZATION_JSON,
50
),
51
self::CONFIG_COLUMN_SCHEMA => array(
52
'className' => 'text255',
53
'blueprintName' => 'sort255',
54
'isDisabled' => 'bool',
55
),
56
) + parent::getConfiguration();
57
}
58
59
public function generatePHID() {
60
return PhabricatorPHID::generateNewPHID(
61
DrydockBlueprintPHIDType::TYPECONST);
62
}
63
64
public function getImplementation() {
65
return $this->assertAttached($this->implementation);
66
}
67
68
public function attachImplementation(DrydockBlueprintImplementation $impl) {
69
$this->implementation = $impl;
70
return $this;
71
}
72
73
public function hasImplementation() {
74
return ($this->implementation !== self::ATTACHABLE);
75
}
76
77
public function getDetail($key, $default = null) {
78
return idx($this->details, $key, $default);
79
}
80
81
public function setDetail($key, $value) {
82
$this->details[$key] = $value;
83
return $this;
84
}
85
86
public function getFieldValue($key) {
87
$key = "std:drydock:core:{$key}";
88
$fields = $this->loadCustomFields();
89
90
$field = idx($fields, $key);
91
if (!$field) {
92
throw new Exception(
93
pht(
94
'Unknown blueprint field "%s"!',
95
$key));
96
}
97
98
return $field->getBlueprintFieldValue();
99
}
100
101
private function loadCustomFields() {
102
if ($this->fields === null) {
103
$field_list = PhabricatorCustomField::getObjectFields(
104
$this,
105
PhabricatorCustomField::ROLE_VIEW);
106
$field_list->readFieldsFromStorage($this);
107
108
$this->fields = $field_list->getFields();
109
}
110
return $this->fields;
111
}
112
113
public function logEvent($type, array $data = array()) {
114
$log = id(new DrydockLog())
115
->setEpoch(PhabricatorTime::getNow())
116
->setType($type)
117
->setData($data);
118
119
$log->setBlueprintPHID($this->getPHID());
120
121
return $log->save();
122
}
123
124
public function getURI() {
125
$id = $this->getID();
126
return "/drydock/blueprint/{$id}/";
127
}
128
129
130
/* -( Allocating Resources )----------------------------------------------- */
131
132
133
/**
134
* @task resource
135
*/
136
public function canEverAllocateResourceForLease(DrydockLease $lease) {
137
return $this->getImplementation()->canEverAllocateResourceForLease(
138
$this,
139
$lease);
140
}
141
142
143
/**
144
* @task resource
145
*/
146
public function canAllocateResourceForLease(DrydockLease $lease) {
147
return $this->getImplementation()->canAllocateResourceForLease(
148
$this,
149
$lease);
150
}
151
152
153
/**
154
* @task resource
155
*/
156
public function allocateResource(DrydockLease $lease) {
157
return $this->getImplementation()->allocateResource(
158
$this,
159
$lease);
160
}
161
162
163
/**
164
* @task resource
165
*/
166
public function activateResource(DrydockResource $resource) {
167
return $this->getImplementation()->activateResource(
168
$this,
169
$resource);
170
}
171
172
173
/**
174
* @task resource
175
*/
176
public function destroyResource(DrydockResource $resource) {
177
$this->getImplementation()->destroyResource(
178
$this,
179
$resource);
180
return $this;
181
}
182
183
184
/**
185
* @task resource
186
*/
187
public function getResourceName(DrydockResource $resource) {
188
return $this->getImplementation()->getResourceName(
189
$this,
190
$resource);
191
}
192
193
194
/* -( Acquiring Leases )--------------------------------------------------- */
195
196
197
/**
198
* @task lease
199
*/
200
public function canAcquireLeaseOnResource(
201
DrydockResource $resource,
202
DrydockLease $lease) {
203
return $this->getImplementation()->canAcquireLeaseOnResource(
204
$this,
205
$resource,
206
$lease);
207
}
208
209
210
/**
211
* @task lease
212
*/
213
public function acquireLease(
214
DrydockResource $resource,
215
DrydockLease $lease) {
216
return $this->getImplementation()->acquireLease(
217
$this,
218
$resource,
219
$lease);
220
}
221
222
223
/**
224
* @task lease
225
*/
226
public function activateLease(
227
DrydockResource $resource,
228
DrydockLease $lease) {
229
return $this->getImplementation()->activateLease(
230
$this,
231
$resource,
232
$lease);
233
}
234
235
236
/**
237
* @task lease
238
*/
239
public function didReleaseLease(
240
DrydockResource $resource,
241
DrydockLease $lease) {
242
$this->getImplementation()->didReleaseLease(
243
$this,
244
$resource,
245
$lease);
246
return $this;
247
}
248
249
250
/**
251
* @task lease
252
*/
253
public function destroyLease(
254
DrydockResource $resource,
255
DrydockLease $lease) {
256
$this->getImplementation()->destroyLease(
257
$this,
258
$resource,
259
$lease);
260
return $this;
261
}
262
263
public function getInterface(
264
DrydockResource $resource,
265
DrydockLease $lease,
266
$type) {
267
268
$interface = $this->getImplementation()
269
->getInterface($this, $resource, $lease, $type);
270
271
if (!$interface) {
272
throw new Exception(
273
pht(
274
'Unable to build resource interface of type "%s".',
275
$type));
276
}
277
278
return $interface;
279
}
280
281
public function shouldAllocateSupplementalResource(
282
DrydockResource $resource,
283
DrydockLease $lease) {
284
return $this->getImplementation()->shouldAllocateSupplementalResource(
285
$this,
286
$resource,
287
$lease);
288
}
289
290
291
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
292
293
294
public function getApplicationTransactionEditor() {
295
return new DrydockBlueprintEditor();
296
}
297
298
public function getApplicationTransactionTemplate() {
299
return new DrydockBlueprintTransaction();
300
}
301
302
303
/* -( PhabricatorPolicyInterface )----------------------------------------- */
304
305
306
public function getCapabilities() {
307
return array(
308
PhabricatorPolicyCapability::CAN_VIEW,
309
PhabricatorPolicyCapability::CAN_EDIT,
310
);
311
}
312
313
public function getPolicy($capability) {
314
switch ($capability) {
315
case PhabricatorPolicyCapability::CAN_VIEW:
316
return $this->getViewPolicy();
317
case PhabricatorPolicyCapability::CAN_EDIT:
318
return $this->getEditPolicy();
319
}
320
}
321
322
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
323
return false;
324
}
325
326
327
/* -( PhabricatorCustomFieldInterface )------------------------------------ */
328
329
330
public function getCustomFieldSpecificationForRole($role) {
331
return array();
332
}
333
334
public function getCustomFieldBaseClass() {
335
return 'DrydockBlueprintCustomField';
336
}
337
338
public function getCustomFields() {
339
return $this->assertAttached($this->customFields);
340
}
341
342
public function attachCustomFields(PhabricatorCustomFieldAttachment $fields) {
343
$this->customFields = $fields;
344
return $this;
345
}
346
347
348
/* -( PhabricatorNgramsInterface )----------------------------------------- */
349
350
351
public function newNgrams() {
352
return array(
353
id(new DrydockBlueprintNameNgrams())
354
->setValue($this->getBlueprintName()),
355
);
356
}
357
358
359
/* -( PhabricatorConduitResultInterface )---------------------------------- */
360
361
362
public function getFieldSpecificationsForConduit() {
363
return array(
364
id(new PhabricatorConduitSearchFieldSpecification())
365
->setKey('name')
366
->setType('string')
367
->setDescription(pht('The name of this blueprint.')),
368
id(new PhabricatorConduitSearchFieldSpecification())
369
->setKey('type')
370
->setType('string')
371
->setDescription(pht('The type of resource this blueprint provides.')),
372
);
373
}
374
375
public function getFieldValuesForConduit() {
376
return array(
377
'name' => $this->getBlueprintName(),
378
'type' => $this->getImplementation()->getType(),
379
);
380
}
381
382
public function getConduitSearchAttachments() {
383
return array(
384
);
385
}
386
387
}
388
389