public function validate($value, Constraint $constraint)
 {
     if (!$constraint instanceof TokenEntity) {
         throw new \InvalidArgumentException('Given constraint must ne instance of TokenEntity class');
     }
     if ($value) {
         $configValidation = false;
         try {
             /** @var StorageApi $storageApi */
             $storageApi = $constraint->getStorageApi();
             $token = $storageApi->getToken($value);
             if (!$token) {
                 throw new \Exception('Token does not exists');
             }
             $userToken = new Token($storageApi);
             $configValidation = true;
             $components = new Components(new StorageApi(array('token' => $token['token'], 'url' => $storageApi->getApiUrl(), 'userAgent' => $storageApi->getUserAgent())));
             $components->listComponents(new ListConfigurationsOptions());
         } catch (\Exception $e) {
             if ($e instanceof \Keboola\StorageApi\ClientException && $e->getCode() === 403) {
                 //@FIXME jak zmenit api exception code
                 if ($configValidation) {
                     $this->context->addViolation($constraint->configMessage, array('%string%' => $value), null, null, 'TOKEN_PERMISSION');
                 } else {
                     $this->context->addViolation($constraint->permissionsMessage, array(), null, null, 'TOKEN_PERMISSION');
                 }
             } else {
                 $this->context->addViolation($constraint->message, array('%string%' => $value));
             }
             return;
         }
         //@FIXME doplnit validaci na master token!!!
     }
 }
 /**
  * @param Client $storageApi
  * @return bool
  * @throws StorageApi\InvalidStateException
  */
 private function validateToken(Client $storageApi)
 {
     try {
         $components = new Components($storageApi);
         $components->listComponents(new ListConfigurationsOptions());
     } catch (ClientException $e) {
         if ($e->getCode() == 403) {
             throw new StorageApi\InvalidStateException('Token has no permission to read configuration');
         } else {
             throw new StorageApi\InvalidStateException($e->getMessage());
         }
     }
     return true;
 }
 public function beforeOrchestrationDelete(Entity\Orchestration $orchestration)
 {
     try {
         $this->components->deleteConfiguration(KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME, $orchestration->getId());
     } catch (ClientException $e) {
         if ($e->getCode() !== 404) {
             throw $e;
         }
     }
 }
 public function testConfigId()
 {
     $token1 = $this->createNewToken($this->storageApi, 'manage');
     $orchestrationId = $this->createOrchestrationTest($token1);
     $jobId = $this->runOrchestrationTest($orchestrationId);
     $job = $this->syrupJobMapper->get($jobId);
     $params = $job->getRawParams();
     $this->assertArrayHasKey('config', $params);
     $this->assertEquals($orchestrationId, $params['config']);
     $components = new Components($this->storageApi);
     $configuration = $components->getConfiguration(KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME, $orchestrationId);
     $this->assertArrayHasKey('name', $configuration);
     $this->assertArrayHasKey('id', $configuration);
 }
 /**
  * Save tasks to configuration
  *
  * @param $id orchestration ID
  */
 private function putOrchestrationTaskTest($id)
 {
     $url = '/orchestrator/orchestrations/' . $id . '/tasks';
     $errorMessage = sprintf("Response for call '%s %s' should return two created tasks.", 'PUT', $url);
     $actionParams = array('fake' => 1);
     $data = array(array("some-extra-field" => "dummy value", "component" => "ex-google-drive", "action" => "run", "active" => true), array("some-extra-field" => "dummy value", "componentUrl" => "https://syrup.keboola.com/ex-google-drive/run", "actionParameters" => $actionParams, "active" => false));
     $response = $this->callApiPut($url, $data);
     $this->assertCount(2, $response, $errorMessage);
     $this->assertArrayNotHasKey('some-extra-field', $response[0], $errorMessage);
     $this->assertArrayHasKey('actionParameters', $response[0], $errorMessage);
     $this->assertArrayHasKey('action', $response[0], $errorMessage);
     $this->assertArrayHasKey('component', $response[0], $errorMessage);
     $this->assertArrayNotHasKey('componentUrl', $response[0], $errorMessage);
     $this->assertEquals($data[0]['component'], $response[0]['component'], $errorMessage);
     $this->assertEquals($data[0]['action'], $response[0]['action'], $errorMessage);
     $this->assertArrayHasKey('actionParameters', $response[1], $errorMessage);
     $this->assertArrayNotHasKey('action', $response[1], $errorMessage);
     $this->assertArrayHasKey('componentUrl', $response[1], $errorMessage);
     $this->assertArrayNotHasKey('component', $response[1], $errorMessage);
     $this->assertEquals($actionParams, $response[1]['actionParameters'], $errorMessage);
     $this->assertEquals($data[1]['componentUrl'], $response[1]['componentUrl'], $errorMessage);
     $this->assertEquals($data[1]['active'], $response[1]['active'], $errorMessage);
     // Validation of tasks copy in sapi configuration
     $orchestration = new Orchestration();
     $orchestration->fromArray(array('id' => $id));
     $components = new Components($this->storageApi);
     $configuration = $components->getConfiguration(KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME, $orchestration->getId());
     $this->assertArrayHasKey('configuration', $configuration);
     $this->assertArrayHasKey('tasks', $configuration['configuration']);
     $configurationTasks = $configuration['configuration']['tasks'];
     $this->assertCount(2, $configurationTasks);
     $this->assertArrayHasKey('actionParameters', $configurationTasks[0]);
     $this->assertArrayHasKey('action', $configurationTasks[0]);
     $this->assertArrayHasKey('componentUrl', $configurationTasks[0]);
     $this->assertArrayHasKey('component', $configurationTasks[0]);
     $this->assertEmpty($configurationTasks[0]['actionParameters']);
     $this->assertEquals($data[0]['action'], $configurationTasks[0]['action']);
     $this->assertEquals($data[0]['component'], $configurationTasks[0]['component']);
     $this->assertEquals($data[0]['active'], $configurationTasks[0]['active']);
     $this->assertEmpty($configurationTasks[0]['componentUrl']);
     $this->assertArrayHasKey('actionParameters', $configurationTasks[1]);
     $this->assertArrayHasKey('action', $configurationTasks[1]);
     $this->assertArrayHasKey('componentUrl', $configurationTasks[1]);
     $this->assertArrayHasKey('component', $configurationTasks[1]);
     $this->assertEquals($actionParams, $configurationTasks[1]['actionParameters']);
     $this->assertEquals($data[1]['componentUrl'], $configurationTasks[1]['componentUrl']);
     $this->assertEquals($data[1]['active'], $configurationTasks[1]['active']);
     $this->assertEmpty($configurationTasks[1]['action']);
     $this->assertEmpty($configurationTasks[1]['component']);
 }
 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;
     }
 }