public function postEvent(array $eventData, $environment = false, $params = []) { $uri = $environment === false ? self::getAccountApiUrl('/events') : self::getUserApiUrl('/events', $environment); $response = $this->request($uri, Request::METHOD_POST, $params, $eventData); $body = $response->getBody(); if ($response->status == 201 && isset($body->data->id)) { $criteria = $this->getCriteria($environment === null); $criteria[] = ['name' => $body->data->id]; $this->eventToDelete(EventDefinition::findOne($criteria)->id); } return $response; }
/** * Creates new event in current scope * * @return \Scalr\Api\DataType\ResultEnvelope * @throws ApiErrorException * @throws \Scalr\Exception\ModelException */ public function createAction() { $this->checkPermissions(Acl::RESOURCE_GENERAL_CUSTOM_EVENTS, Acl::PERM_GENERAL_CUSTOM_EVENTS_MANAGE); $object = $this->request->getJsonBody(); $eventAdapter = $this->adapter('event'); //Pre validates the request object $eventAdapter->validateObject($object, Request::METHOD_POST); if (empty($object->id)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Required field 'id' is missing."); } $object->scope = $this->getScope(); $criteria = [['name' => $object->id]]; switch ($this->getScope()) { case ScopeInterface::SCOPE_ACCOUNT: $criteria[] = ['$or' => [['$and' => [['envId' => null], ['accountId' => null]]], ['accountId' => $this->getUser()->getAccountId()]]]; break; case ScopeInterface::SCOPE_ENVIRONMENT: $criteria[] = ['$and' => [['envId' => $this->getEnvironment()->id], ['accountId' => $this->getUser()->getAccountId()]]]; break; default: throw new ApiErrorException(500, ErrorMessage::ERR_NOT_IMPLEMENTED, sprintf("The Scope '%s' has not been implemented yet", $this->getScope())); } /* @var $oldEvent Entity\EventDefinition */ $oldEvent = Entity\EventDefinition::findOne($criteria); if (!empty($oldEvent)) { if ($this->getScope() == ScopeInterface::SCOPE_ACCOUNT && $this->request->get('replace', false)) { $replacements = Entity\EventDefinition::find([['name' => $object->id], ['accountId' => $this->getUser()->getAccountId()], ['envId' => ['$ne' => null]]]); if ($replacements->count()) { foreach ($replacements as $lowerEvent) { $lowerEvent->delete(); } } else { throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('Event with id %s already exists', $object->id)); } } else { throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('Event with id %s already exists', $object->id)); } } /* @var $event Entity\EventDefinition */ //Converts object into EventDefinition entity $event = $eventAdapter->toEntity($object); $event->id = null; $eventAdapter->validateEntity($event); //Saves entity $event->save(); //Responds with 201 Created status $this->response->setStatus(201); return $this->result($eventAdapter->toData($event)); }
public function xSaveAction() { $this->request->restrictAccess(Acl::RESOURCE_GENERAL_SCHEDULERTASKS, Acl::PERM_GENERAL_SCHEDULERTASKS_MANAGE); $this->request->defineParams(array('id' => array('type' => 'integer'), 'name' => array('type' => 'string', 'validator' => array(Scalr_Validator::REQUIRED => true, Scalr_Validator::NOHTML => true)), 'type' => array('type' => 'string', 'validator' => array(Scalr_Validator::RANGE => array(Scalr_SchedulerTask::SCRIPT_EXEC, Scalr_SchedulerTask::LAUNCH_FARM, Scalr_SchedulerTask::TERMINATE_FARM, Scalr_SchedulerTask::FIRE_EVENT), Scalr_Validator::REQUIRED => true)), 'startTime', 'startTimeDate', 'restartEvery', 'timezone' => array('type' => 'string', 'validator' => array(Scalr_Validator::REQUIRED => true)), 'farmId' => array('type' => 'integer'), 'farmRoleId' => array('type' => 'integer'), 'serverId' => array('type' => 'string'), 'scriptOptions' => array('type' => 'array'), 'eventParams' => array('type' => 'array'), 'eventName' => array('type' => 'string'))); $task = Scalr_SchedulerTask::init(); if ($this->getParam('id')) { $task->loadById($this->getParam('id')); $this->user->getPermissions()->validate($task); } else { $task->accountId = $this->user->getAccountId(); $task->envId = $this->getEnvironmentId(); $task->status = Scalr_SchedulerTask::STATUS_ACTIVE; } $this->request->validate(); $params = array(); $timezone = new DateTimeZone($this->getParam('timezone')); $startTm = $this->getParam('startTime') ? new DateTime($this->getParam('startTimeDate') . " " . $this->getParam('startTime'), $timezone) : NULL; if ($startTm) { Scalr_Util_DateTime::convertTimeZone($startTm, NULL); } $curTm = new DateTime(); if ($startTm && $startTm < $curTm && !$task->id) { $this->request->addValidationErrors('startTimeDate', array('Start time must be greater then current time')); } switch ($this->getParam('type')) { case Scalr_SchedulerTask::FIRE_EVENT: case Scalr_SchedulerTask::SCRIPT_EXEC: if ($this->getParam('serverId')) { $dbServer = DBServer::LoadByID($this->getParam('serverId')); $this->user->getPermissions()->validate($dbServer); $task->targetId = $dbServer->GetFarmRoleObject()->ID; $task->targetServerIndex = $dbServer->index; $task->targetType = Scalr_SchedulerTask::TARGET_INSTANCE; } else { if ($this->getParam('farmRoleId')) { $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId')); $this->user->getPermissions()->validate($dbFarmRole); $task->targetId = $dbFarmRole->ID; $task->targetType = Scalr_SchedulerTask::TARGET_ROLE; } else { if ($this->getParam('farmId')) { $dbFarm = DBFarm::LoadByID($this->getParam('farmId')); $this->user->getPermissions()->validate($dbFarm); $task->targetId = $dbFarm->ID; $task->targetType = Scalr_SchedulerTask::TARGET_FARM; } else { $this->request->addValidationErrors('farmId', array('Farm ID is required')); } } } if ($this->getParam('type') == Scalr_SchedulerTask::SCRIPT_EXEC) { /* @var $script Script */ $script = Script::findPk($this->getParam('scriptId')); try { if ($script) { $script->checkPermission($this->user, $this->getEnvironmentId()); $task->scriptId = $this->getParam('scriptId'); $params['scriptId'] = $this->getParam('scriptId'); $params['scriptIsSync'] = $this->getParam('scriptIsSync'); $params['scriptTimeout'] = $this->getParam('scriptTimeout'); $params['scriptVersion'] = $this->getParam('scriptVersion'); $params['scriptOptions'] = $this->getParam('scriptOptions'); } else { throw new Exception(); } } catch (Exception $e) { $this->request->addValidationErrors('scriptId', array('Script ID is required')); } } elseif ($this->getParam('type') == Scalr_SchedulerTask::FIRE_EVENT) { if (!EventDefinition::findOne([['name' => $this->getParam('eventName')], ['$or' => [['accountId' => null], ['accountId' => $this->user->getAccountId()]]], ['$or' => [['envId' => null], ['envId' => $this->getEnvironmentId()]]]])) { throw new Exception("Event definition not found"); } $params['eventName'] = $this->getParam('eventName'); $params['eventParams'] = $this->getParam('eventParams'); } break; case Scalr_SchedulerTask::LAUNCH_FARM: if ($this->getParam('farmId')) { $dbFarm = DBFarm::LoadByID($this->getParam('farmId')); $this->user->getPermissions()->validate($dbFarm); $task->targetId = $dbFarm->ID; $task->targetType = Scalr_SchedulerTask::TARGET_FARM; } else { $this->request->addValidationErrors('farmId', array('Farm ID is required')); } break; case Scalr_SchedulerTask::TERMINATE_FARM: if ($this->getParam('farmId')) { $dbFarm = DBFarm::LoadByID($this->getParam('farmId')); $this->user->getPermissions()->validate($dbFarm); $task->targetId = $dbFarm->ID; $task->targetType = Scalr_SchedulerTask::TARGET_FARM; } else { $this->request->addValidationErrors('farmId', array('Farm ID is required')); } $params['deleteDNSZones'] = $this->getParam('deleteDNSZones'); $params['deleteCloudObjects'] = $this->getParam('deleteCloudObjects'); break; } if (!$this->request->isValid()) { $this->response->failure(); $this->response->data($this->request->getValidationErrors()); return; } $task->name = $this->getParam('name'); $task->type = $this->getParam('type'); $task->comments = $this->getParam('comments'); $task->timezone = $this->getParam('timezone'); $task->startTime = $startTm ? $startTm->format('Y-m-d H:i:s') : NULL; //$task->endTime = $endTm ? $endTm->format('Y-m-d H:i:s') : NULL; $task->restartEvery = $this->getParam('restartEvery'); $task->config = $params; $task->save(); $this->response->success(); }
/** * @param string $eventName * @param int $farmId * @param int $farmRoleId * @param string $serverId * @param JsonData $eventParams * @throws Exception * @throws Scalr_Exception_InsufficientPermissions */ public function xFireAction($eventName, $farmId = 0, $farmRoleId = 0, $serverId = '', JsonData $eventParams) { $this->request->restrictAccess(Acl::RESOURCE_GENERAL_CUSTOM_EVENTS, Acl::PERM_GENERAL_CUSTOM_EVENTS_FIRE); if (!EventDefinition::findOne([['name' => $eventName], ['$or' => [['accountId' => null], ['accountId' => $this->user->getAccountId()]]], ['$or' => [['envId' => null], ['envId' => $this->getEnvironmentId()]]]])) { throw new Exception("Event definition not found"); } if ($serverId) { $dbServer = DBServer::LoadByID($serverId); $this->user->getPermissions()->validate($dbServer); $servers = array($dbServer); } else { if ($farmRoleId) { $dbFarmRole = DBFarmRole::LoadByID($farmRoleId); $this->user->getPermissions()->validate($dbFarmRole); $servers = $dbFarmRole->GetServersByFilter(array('status' => SERVER_STATUS::RUNNING)); } else { $dbFarm = DBFarm::LoadByID($farmId); $this->user->getPermissions()->validate($dbFarm); $servers = $dbFarm->GetServersByFilter(array('status' => SERVER_STATUS::RUNNING)); } } if (count($servers) == 0) { throw new Exception("No running Servers found. Event was not fired."); } foreach ($servers as $dbServer) { /* @var $dbServer DBServer */ $event = new CustomEvent($dbServer, $eventName, (array) $eventParams); Scalr::FireEvent($dbServer->farmId, $event); } $this->response->success(sprintf("Event successfully fired on behalf of %s Server(s)", count($servers))); }
/** * Creates new event in current scope * * @return \Scalr\Api\DataType\ResultEnvelope * @throws ApiErrorException * @throws \Scalr\Exception\ModelException */ public function createAction() { $this->checkPermissions(Acl::RESOURCE_GENERAL_CUSTOM_EVENTS); $object = $this->request->getJsonBody(); $eventAdapter = $this->adapter('event'); //Pre validates the request object $eventAdapter->validateObject($object, Request::METHOD_POST); if (empty($object->id)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Required field 'id' is missing."); } $object->scope = $this->getScope(); $criteria = $this->getDefaultCriteria(); $criteria[] = ['name' => $object->id]; /* @var $oldEvent Entity\EventDefinition */ $oldEvent = Entity\EventDefinition::findOne($criteria); if (!empty($oldEvent)) { throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('Event with id %s already exists', $object->id)); } /* @var $event Entity\EventDefinition */ //Converts object into EventDefinition entity $event = $eventAdapter->toEntity($object); $event->id = null; $eventAdapter->validateEntity($event); //Saves entity $event->save(); //Responds with 201 Created status $this->response->setStatus(201); return $this->result($eventAdapter->toData($event)); }