/**
  * @param Orchestration $orchestration
  * @param Client $storageApi
  * @return bool
  * @throws StorageApi\InvalidStateException
  */
 private function validateConfiguration(Orchestration $orchestration, Client $storageApi)
 {
     $components = new Components($storageApi);
     $configuration = $components->getConfiguration(KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME, $orchestration->getId());
     // same name
     if ($configuration['name'] !== $orchestration->getName()) {
         throw new StorageApi\InvalidStateException('Orchestration names are different');
     }
     if (count($configuration['configuration']['tasks']) !== count($orchestration->getTasks())) {
         throw new StorageApi\InvalidStateException('Orchestration has same tasks count');
     }
     $configurationTasks = $configuration['configuration']['tasks'];
     foreach ($orchestration->getTasks() as $i => $task) {
         $task = $task->toApiArray();
         foreach ($task as $key => $value) {
             if ($configurationTasks[$i][$key] != $value) {
                 throw new StorageApi\InvalidStateException(sprintf('Task %s is different: %s||%s', $task['id'], json_encode($task), $configurationTasks[$i]));
             }
         }
     }
     return true;
 }
 /**
  * @param Orchestration $orchestration
  * @param int $offset
  * @param int $limit
  * @return  Metadata\Job[]
  */
 private function loadOrchestrationSuccessJobs(Orchestration $orchestration, $offset = 0, $limit = 25)
 {
     /**
      * @var ComponentIndex $config
      */
     $config = $this->getContainer()->get('syrup.elasticsearch.current_component_index');
     $orchestrations = $this->loadActiveOrchestrationIds();
     $filter = [];
     $filter[] = ['term' => ['status' => Metadata\Job::STATUS_SUCCESS]];
     $filter[] = ['terms' => ['params.orchestration.id' => array($orchestration->getId())]];
     $query = ['match_all' => []];
     $params = [];
     $params['index'] = $config->getIndexPrefix() . '_syrup_' . KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME;
     $params['body'] = ['from' => (int) $offset, 'size' => (int) $limit, 'query' => ['filtered' => ['filter' => ['bool' => ['must' => $filter]], 'query' => $query]], 'sort' => ['id' => ['order' => 'desc']]];
     $results = [];
     $hits = $this->elasticSearch->search($params);
     foreach ($hits['hits']['hits'] as $hit) {
         $res = $hit['_source'];
         $res['_index'] = $hit['_index'];
         $res['_type'] = $hit['_type'];
         $res['id'] = (int) $res['id'];
         $results[] = $res;
     }
     $objectEncryptor = $this->objectEncryptor;
     return array_map(function ($line) use($objectEncryptor) {
         return new Metadata\Job($objectEncryptor, $line, $line['_index'], $line['_type']);
     }, $results);
 }
 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;
         }
     }
 }
 /**
  * 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']);
 }