/**
  * @param Job $job
  * @param StorageApi\Token $token
  * @return StorageApi\OrchestrationTask[]
  * @throws UserException
  */
 protected final function loadTasks(Job $job, StorageApi\Token $token)
 {
     try {
         $taskManager = new StorageApi\OrchestrationTaskManager($this->storageApi);
         return $taskManager->findTasksByJob($job, $token);
     } catch (\Exception $e) {
         throw new UserException('Error on configuration read.', $e, array());
     }
 }
 /**
  * Modify orchestration tasks
  *
  * @param $orchestrationId
  * @param Request $request
  * @return Response
  * @throws \Keboola\StorageApi\TableException
  */
 public function putTasksAction($orchestrationId, Request $request)
 {
     $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 {
         $form = $this->createForm(new CollectionType(new OrchestrationTaskType($this->storageApi)));
         $handler = $this->createTasksFormHandler($form, $request);
         $taskObjectList = array();
         if ($handler->process()) {
             $taskObjectList = $handler->fillEntities(new StorageApi\OrchestrationTask($this->token));
         }
         //@FIXME remove taskId
         $orchestration->removeTasks();
         array_map(function (StorageApi\OrchestrationTask $task) use($orchestration) {
             $task->setId($this->storageApi->generateId());
             $orchestration->addTask($task);
         }, $taskObjectList);
         $orchestrationTaskManager = new StorageApi\OrchestrationTaskManager($this->storageApi);
         $orchestrationTaskManager->updateTasks($orchestration);
         // return task list in response
     } catch (ClientException $e) {
         if ($e->getCode() === 403) {
             $exception = new OrchestratorException($e->getCode(), "Token has no permissions for update configuration");
             $exception->setExceptionCode('TOKEN_VALIDATION');
             throw $exception;
         } else {
             throw $e;
         }
     } catch (AccessDeniedHttpException $e) {
         $exception = new OrchestratorException($e->getStatusCode(), $e->getMessage());
         $exception->setExceptionCode('TOKEN_VALIDATION');
         throw $exception;
     } catch (HttpException $e) {
         $exception = new OrchestratorException(400, $e->getMessage());
         $exception->setExceptionCode('TASK_VALIDATION');
         throw $exception;
     }
     $orchestrationTaskManager = new StorageApi\OrchestrationTaskManager($this->storageApi);
     $tasks = $orchestrationTaskManager->findTasks($orchestration, $this->token);
     $tasks = array_map(function ($row) {
         return $row->toApiArray();
     }, $tasks);
     return $this->createJsonResponse($tasks, 202);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     parent::execute($input, $output);
     $this->dbOrchestrationManager = $this->getContainer()->get('orchestrator.doctrine.orchestration_manager');
     $skippedCount = 0;
     $successCount = 0;
     $errorCount = 0;
     $force = $input->getOption('force');
     $projectId = $input->getArgument('projectId');
     $this->logger->info('configuration-migration.start ' . $projectId, array('test' => $force ? true : false));
     foreach ($this->loadProjectOrchestrations($projectId) as $orchestration) {
         try {
             $jobClient = $this->createProjectSapi($orchestration);
             $token = new StorageApi\Token($jobClient);
             if ($token->hasEnabledFeature(StorageApi\Token::ORCHESTRATOR_NEW_CONFIGURATION)) {
                 throw new DisabledException('Migration skipped: new configuration already enabled');
             }
         } catch (DisabledException $e) {
             $message = $e->getMessage();
             if ($e->getPrevious()) {
                 $message = sprintf('%s: %s', $message, $e->getMessage());
             }
             $output->writeln(sprintf('<error>' . $message . '</error>'));
             $this->logger->info('configuration-migration.failed ' . $projectId, array('test' => $force ? true : false, 'reason' => $message));
             $output->writeln(sprintf('<error>Finished with errors</error>'));
             return JobCommand::STATUS_ERROR;
         }
         $orchestration = $this->dbOrchestrationManager->findOrchestrationById($orchestration->getId(), $token, true);
         $components = new Components($jobClient);
         // remove old configuration
         $oldConfiguration = null;
         try {
             $oldConfiguration = $components->getConfiguration(KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME, sprintf("%s-%s", KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME, $orchestration->getId()));
         } catch (ClientException $e) {
             if ($e->getCode() !== 404) {
                 throw $e;
             }
         }
         if ($force) {
             // create or update configuration
             $orchestrationTaskManager = new StorageApi\OrchestrationTaskManager($jobClient);
             $orchestrationTaskManager->updateTasks($orchestration);
             // validate configuration
             try {
                 $this->validateConfiguration($orchestration, $jobClient);
                 $output->writeln(sprintf('<info>(%s) %s - Migrated</info>', $orchestration->getId(), $orchestration->getName()));
                 if ($oldConfiguration) {
                     $components->deleteConfiguration(KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME, $oldConfiguration['id']);
                     $output->writeln(sprintf('<comment>(%s) - Old configuration deleted</comment>', $oldConfiguration['id']));
                 }
                 $successCount++;
             } catch (StorageApi\InvalidStateException $e) {
                 $output->writeln(sprintf('<error>' . $e->getMessage() . '</error>'));
                 $errorCount++;
             }
         } else {
             $output->writeln(sprintf('<comment>(%s) %s - Will be migrated</comment>', $orchestration->getId(), $orchestration->getName()));
             if ($oldConfiguration) {
                 $output->writeln(sprintf('<comment>(%s) - Old configuration will be deleted</comment>', $oldConfiguration['id']));
             }
             $skippedCount++;
         }
     }
     if ($errorCount) {
         $output->writeln(sprintf('<error>Finished with errors</error>'));
     } else {
         $output->writeln(sprintf('<info>Finished</info>'));
     }
     $this->logger->info('configuration-migration.migrated ' . $projectId, array('test' => $force ? true : false, 'successCount' => $successCount, 'errorCount' => $errorCount, 'skippedCount' => $skippedCount));
     if ($errorCount) {
         return JobCommand::STATUS_ERROR;
     }
 }