/**
  * Cancel waiting job
  *
  * @param $jobId
  * @throws \Keboola\OrchestratorBundle\Exception\OrchestratorException
  * @return Response
  */
 public function deleteAction($jobId)
 {
     $job = $this->jobEsManager->findJobById($jobId);
     if (!$job) {
         $exception = new OrchestratorException(404, sprintf('Job %s not found', $jobId));
         $exception->setExceptionCode('JOB_NOT_FOUND');
         throw $exception;
     }
     if (!$job->isWaiting()) {
         $exception = new OrchestratorException(403, 'Only waiting job can be cancelled');
         $exception->setExceptionCode('INVALID_JOB_STATUS');
         throw $exception;
     }
     $client = new GuzzleHttp\Client([]);
     try {
         $response = $client->post(sprintf($this->killUrl, $job->getId()), array('config' => array('curl' => array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0)), 'headers' => array('X-StorageApi-Token' => $this->token, 'X-KBC-RunId' => $this->storageApi->getRunId(), 'X-User-Agent', KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME . " - API cancel"), 'timeout' => 60));
         if ($response->getStatusCode() == 202) {
             $this->logger->info(sprintf('Orchestration job %s cancelled', $job->getId()));
             $this->logger->debug(sprintf('Orchestration job %s cancelled - old api', $job->getId()), array('orchestrationId' => $job->getOrchestrationId(), 'orchestration' => $job->getOrchestrationName(), 'projectId' => $job->getProjectId(), 'project' => $job->getTokenOwnerName(), 'tokenId' => $this->token->getId(), 'token' => $this->token->getDescription()));
         } else {
             throw new ApplicationException('Job cancel via syrup api failed', null, array('statusCode' => $response->getStatusCode(), 'body' => ResponseDecoder::decode($response)));
         }
     } catch (GuzzleHttp\Exception\RequestException $e) {
         throw new ApplicationException('Job cancel via syrup api failed', $e);
     }
     $jsonResponse = $this->createJsonResponse(array(), 204);
     return $jsonResponse;
 }
 public function postCreateTokenAction($orchestrationId)
 {
     $orchestration = $this->dbOrchestrationManager->findOrchestrationById($orchestrationId, $this->token);
     if (!$orchestration) {
         $exception = new OrchestratorException(404, sprintf('Orchestration %s not found', $orchestrationId));
         $exception->setExceptionCode('ORCHESTRATION_NOT_FOUND');
         throw $exception;
     }
     try {
         $tokenId = $this->storageApi->createToken('manage', sprintf('Orchestrator %s', $orchestration->getName()), null, true);
         // fill token data
         $token = $this->storageApi->getToken($tokenId);
         $token = new StorageApi\Token(new Client(array('token' => $token['token'], 'url' => $this->storageApi->getApiUrl())));
         $orchestration->setToken((string) $token)->setTokenId($token->getId())->setTokenDesc($token->getDescription())->setTokenOwnerName($token->getOwnerName())->setProjectId($token->getProjectId());
     } catch (\Exception $e) {
         $exception = new OrchestratorException(500, sprintf('Could not create new token'), $e);
         $exception->setExceptionCode('TOKEN_VALIDATION');
         throw $exception;
     }
     $orchestration = $this->dbOrchestrationManager->updateOrchestration($orchestration);
     $this->logger->info('Orchestration token changed');
     $data = $orchestration->toApiArray();
     return $this->createJsonResponse($data['token'], 201);
 }
 /**
  * Create new orchestration with full params
  *
  * @param StorageApiToken $token
  * @return int orchestration ID
  */
 private function createOrchestrationTest(StorageApiToken $token)
 {
     $url = '/orchestrator/orchestrations';
     $errorMessage = sprintf("Response for call '%s %s' should return new orchestration.", 'POST', $url);
     $data = array('name' => self::TESTING_NAME, 'crontabRecord' => '*/5 * * * *', 'tokenId' => $token->getId(), 'active' => false, 'notifications' => array(array('email' => TEST_ERROR_NOTIFICATION_EMAIL_1, 'channel' => 'error', 'parameters' => array()), array('email' => TEST_ERROR_NOTIFICATION_EMAIL_1, 'channel' => 'warning', 'parameters' => array())));
     $response = $this->callApiPost($url, $data);
     $this->assertArrayHasKey('id', $response, $errorMessage);
     $this->assertArrayHasKey('crontabRecord', $response, $errorMessage);
     return $response['id'];
 }
 /**
  * Create new orchestration with full params
  *
  * @param StorageApiToken $token
  * @return int orchestration ID
  */
 private function createOrchestrationTest(StorageApiToken $token)
 {
     $url = '/orchestrator/orchestrations';
     $errorMessage = sprintf("Response for call '%s %s' should return new orchestration.", 'POST', $url);
     $data = array('name' => self::TESTING_NAME, 'crontabRecord' => '*/1 * * * *', 'tokenId' => $token->getId(), 'active' => true);
     $response = $this->callApiPost($url, $data);
     $this->assertArrayHasKey('id', $response, $errorMessage);
     $this->assertArrayHasKey('crontabRecord', $response, $errorMessage);
     return $response['id'];
 }
 /**
  * Create new orchestration with full params
  *
  * @param StorageApiToken $token
  * @param StorageApi\Notification[] $notifications
  * @return int orchestration ID
  */
 private function createCrontabOrchestrationTest(StorageApiToken $token, $notifications = array())
 {
     $url = '/orchestrator/orchestrations';
     $errorMessage = sprintf("Response for call '%s %s' should return new orchestration.", 'POST', $url);
     $notifications = array_map(function ($row) {
         /**
          * @var StorageApi\Notification $row
          */
         return $row->toApiArray();
     }, $notifications);
     $data = array('name' => self::TESTING_NAME, 'crontabRecord' => '*/1 * * * *', 'tokenId' => $token->getId(), 'active' => true, 'notifications' => $notifications);
     $response = $this->callApiPost($url, $data);
     $this->assertArrayHasKey('id', $response, $errorMessage);
     $this->assertArrayHasKey('crontabRecord', $response, $errorMessage);
     return $response['id'];
 }
 /**
  * Change orchestration
  *
  * @param $id orchestration ID
  * @param StorageApiToken $token
  * @return int orchestration ID
  */
 private function putOrchestrationBadTokenTest($id, StorageApiToken $token)
 {
     $url = '/orchestrator/orchestrations/' . $id;
     $errorMessage = sprintf("Response for call '%s %s' should return error for new orchestration token.", 'PUT', $url);
     $data = array('tokenId' => $token->getId(), 'active' => true);
     $response = $this->callApiPut($url, $data, 400);
     $this->assertArrayHasKey('code', $response, $errorMessage);
     $this->assertArrayHasKey('message', $response, $errorMessage);
     $this->assertArrayHasKey('status', $response, $errorMessage);
     $this->assertEquals('ORCHESTRATION_VALIDATION', $response['code'], $errorMessage);
     $this->assertEquals('warning', $response['status'], $errorMessage);
     $this->assertContains('token', $response['message'], $errorMessage, true);
     $this->assertContains('permission', $response['message'], $errorMessage, true);
     return (int) $id;
 }