public function validateEntity($entity)
{
/* @var $entity OrchestrationRule */
$entityClass = $this->entityClass;
if (!$entity instanceof $entityClass) {
throw new InvalidArgumentException(sprintf("First argument must be instance of {$entityClass} class"));
}
if ($entity->id !== null) {
if (!$entityClass::findPk($entity->id)) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the rule with ID: %d", $entity->id));
}
}
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);
}
/* @var $found ScriptVersion */
if (empty($found) || !$found->hasAccessPermissions($this->controller->getUser(), $this->controller->getEnvironment())) {
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 (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->getScope() === ScopeInterface::SCOPE_ENVIRONMENT ? $this->controller->getEnvironment()->id : null))) === 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 ($entity->eventName == EVENT_TYPE::BEFORE_INSTANCE_LAUNCH && $entity->target == Script::TARGET_INSTANCE) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Event '{$entity->eventName}' will never be handled by the triggering server");
}
}
}
if (!$this->controller->hasPermissions($entity, true)) {
//Checks entity level write access permissions
throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
}
}