forked from ssrini/scalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoleScriptAdapter.php
346 lines (291 loc) · 12.7 KB
/
RoleScriptAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
namespace Scalr\Api\Service\User\V1beta0\Adapter;
use EVENT_TYPE;
use InvalidArgumentException;
use Scalr\Api\DataType\ApiEntityAdapter;
use Scalr\Api\DataType\ErrorMessage;
use Scalr\Api\Rest\Controller\ApiController;
use Scalr\Api\Rest\Exception\ApiErrorException;
use Scalr\Api\Service\User\V1beta0\Controller\OrchestrationRules;
use Scalr\Model\Entity\EventDefinition;
use Scalr\Model\Entity\OrchestrationRule;
use Scalr\Model\Entity\RoleScript;
use Scalr\Model\Entity\Script;
use Scalr\Model\Entity\ScriptVersion;
/**
* ScriptAdapter V1beta0
*
* @author N.V.
*
* @method \Scalr\Model\Entity\RoleScript toEntity($data) Converts data to entity
*
* @property OrchestrationRules $controller;
*/
class RoleScriptAdapter extends ApiEntityAdapter
{
/**
* Actions types
*/
const ACTION_SCRIPT = 'ScriptAction';
const ACTION_URI = 'UriAction';
const ACTION_CHEF = 'ChefAction';
/**
* Triggers types
*/
const TRIGGER_ALL_EVENTS = 'AllEventsTrigger';
const TRIGGER_SINGLE_EVENT = 'SpecificEventTrigger';
/**
* Targets types
*/
const TARGET_NULL = 'NullTarget';
const TARGET_FARM = 'FarmTarget';
const TARGET_TRIGGERING_SERVER = 'TriggeringServerTarget';
const TARGET_TRIGGERING_FARM_ROLE = 'TriggeringFarmRoleTarget';
const TARGET_SPECIFIED_FARM_ROLE = 'SelectedFarmRolesTarget';
protected static $targetConversationRules = [
self::TARGET_NULL => '',
self::TARGET_FARM => 'farm',
self::TARGET_TRIGGERING_SERVER => 'instance',
self::TARGET_TRIGGERING_FARM_ROLE => 'role',
//TODO: this target mentioned, but not described
self::TARGET_SPECIFIED_FARM_ROLE => 'role_spec'
];
/**
* Converter rules
*
* @var array
*/
protected $rules = [
//Allows all entity properties to be converted from entity into data result object.
//[entityProperty1 => resultProperty1, ... or entityProperty1, entityProperty2, ...]
self::RULE_TYPE_TO_DATA => [
'id', '_trigger' => 'trigger', '_target' => 'target', '_action' => 'action', 'timeout', 'runAs',
'orderIndex' => 'order', 'issync' => 'blocking',
'_scope' => 'scope'
],
//The alterable properties
self::RULE_TYPE_ALTERABLE => [],
self::RULE_TYPE_FILTERABLE => ['id'],
self::RULE_TYPE_SORTING => [self::RULE_TYPE_PROP_DEFAULT => ['id' => true]]
];
/**
* {@inheritdoc}
*/
protected $entityClass = 'Scalr\Model\Entity\RoleScript';
public static function targetToData(RoleScript $entity, $object)
{
$rules = array_flip(static::$targetConversationRules);
$object->target = (object) [
'targetType' => $rules[$entity->target]
];
}
public static function targetToEntity($object, RoleScript $entity)
{
if (!isset(static::$targetConversationRules[$object->target->targetType])) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unexpected target type!");
}
$entity->target = static::$targetConversationRules[$object->target->targetType];
}
public function _scope($from, $to, $action)
{
switch ($action) {
case static::ACT_CONVERT_TO_OBJECT:
$to->scope = $from->getScope();
break;
case static::ACT_CONVERT_TO_ENTITY:
break;
case static::ACT_GET_FILTER_CRITERIA:
break;
}
}
public function _action($from, $to, $action)
{
switch ($action) {
case static::ACT_CONVERT_TO_OBJECT:
/* @var $from RoleScript */
switch ($from->scriptType) {
case OrchestrationRule::ORCHESTRATION_RULE_TYPE_SCALR:
$to->action = [
'actionType' => static::ACTION_SCRIPT,
'scriptVersion' => [
'script' => [
'id' => $from->scriptId
]
]
];
if(!empty($from->version)) {
$to->action['scriptVersion']['version'] = $from->version;
}
break;
case OrchestrationRule::ORCHESTRATION_RULE_TYPE_LOCAL:
$to->action = [
'actionType' => static::ACTION_URI,
'path' => $from->scriptPath
];
break;
case OrchestrationRule::ORCHESTRATION_RULE_TYPE_CHEF:
$to->action = ['actionType' => static::ACTION_CHEF];
break;
}
break;
case static::ACT_CONVERT_TO_ENTITY:
/* @var $to RoleScript */
$action = $from->action;
if (empty($action->actionType)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed action type");
}
switch ($action->actionType) {
case static::ACTION_SCRIPT:
$scriptId = ApiController::getBareId($from->action->scriptVersion, 'script');
if (empty($scriptId)){
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property script.id");
}
$to->scriptId = $scriptId;
$from->action->scriptVersion->version = $from->action->scriptVersion->version ?: ScriptVersion::LATEST_SCRIPT_VERSION;
$to->version = $from->action->scriptVersion->version;
$to->scriptType = OrchestrationRule::ORCHESTRATION_RULE_TYPE_SCALR;
break;
case static::ACTION_URI:
if (empty($action->path)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property path");
}
$to->scriptPath = $action->path;
$to->scriptType = OrchestrationRule::ORCHESTRATION_RULE_TYPE_LOCAL;
break;
case static::ACTION_CHEF:
$to->scriptType = OrchestrationRule::ORCHESTRATION_RULE_TYPE_CHEF;
break;
default:
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unexpected actionType");
}
break;
case static::ACT_GET_FILTER_CRITERIA:
break;
}
}
public function _trigger($from, $to, $action)
{
switch ($action) {
case static::ACT_CONVERT_TO_OBJECT:
/* @var $from RoleScript */
switch ($from->eventName) {
case '*':
$to->trigger = [
'triggerType' => static::TRIGGER_ALL_EVENTS
];
break;
default:
$to->trigger = [
'triggerType' => static::TRIGGER_SINGLE_EVENT,
'event' => [
'id' => $from->eventName
]
];
break;
}
break;
case static::ACT_CONVERT_TO_ENTITY:
/* @var $to RoleScript */
if (empty($from->trigger->triggerType)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed trigger type");
}
switch($from->trigger->triggerType) {
case static::TRIGGER_ALL_EVENTS:
$to->eventName = '*';
break;
case static::TRIGGER_SINGLE_EVENT:
if (!($event = ApiController::getBareId($from->trigger, 'event'))) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property trigger:id");
}
$to->eventName = $event;
break;
default:
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unexpected trigger type");
}
break;
case static::ACT_GET_FILTER_CRITERIA:
return [[ 'eventName' => $from->trigger ]];
}
}
public function _target($from, $to, $action)
{
switch ($action) {
case static::ACT_CONVERT_TO_OBJECT:
/* @var $from RoleScript */
static::targetToData($from, $to);
break;
case static::ACT_CONVERT_TO_ENTITY:
/* @var $to RoleScript */
if (empty($from->target->targetType)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed target type");
}
static::targetToEntity($from, $to);
break;
case static::ACT_GET_FILTER_CRITERIA:
if (empty($from->target->targetType)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed target type");
}
if (!isset(static::$targetConversationRules[$from->target->targetType])) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unexpected target type!");
}
return [[ 'target' => static::$targetConversationRules[$from->target->targetType]]];
}
}
/**
* {@inheritdoc}
* @see \Scalr\Api\DataType\ApiEntityAdapter::validateEntity()
*/
public function validateEntity($entity)
{
if (!$entity instanceof RoleScript) {
throw new InvalidArgumentException(sprintf(
"First argument must be instance of Scalr\\Model\\Entity\\RoleScript class"
));
}
if ($entity->id !== null) {
if (!RoleScript::findPk($entity->id)) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf(
"Could not find out the rule with ID: %d", $entity->id
));
}
}
//Getting the role initiates check permissions
$role = $this->controller->getRole($entity->roleId);
if (!empty($entity->scriptId)) {
if ($entity->version == ScriptVersion::LATEST_SCRIPT_VERSION) {
$found = ScriptVersion::findOne([['scriptId' => $entity->scriptId]], null, ['version' => false]);
} else {
$found = ScriptVersion::findPk($entity->scriptId, $entity->version);
}
if (empty($found)) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf(
"Could not find version %d of the script with ID: %d", $entity->version, $entity->scriptId
));
}
if (Script::findPk($entity->scriptId)->os == 'windows' && $role->getOs()->family != 'windows') {
throw new ApiErrorException(409, ErrorMessage::ERR_OS_MISMATCH, "Script OS family does not match role OS family");
}
}
if (empty($entity->eventName)) {
$entity->eventName = '*';
} else {
if ($entity->eventName !== '*') {
if (array_key_exists($entity->eventName, array_merge(
EVENT_TYPE::getScriptingEventsWithScope(),
EventDefinition::getList(
$this->controller->getUser()->id,
$this->controller->getEnvironment()->id
))) === false) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Could not find out the event '{$entity->eventName}'");
}
if ($entity->scriptType == OrchestrationRule::ORCHESTRATION_RULE_TYPE_CHEF && in_array($entity->eventName, EVENT_TYPE::getChefRestrictedEvents())) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Chef can't be used with {$entity->eventName}");
}
}
}
if (!$this->controller->hasPermissions($entity, true)) {
//Checks entity level write access permissions
throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
}
}
}