private function initClient() { $handlerStack = HandlerStack::create(); $handlerStack->push(MiddlewareBuilder::factoryForPing($this->logger, self::MAX_RETRIES)); $handlerStack->push(Middleware::log($this->logger, new MessageFormatter('{hostname} {req_header_User-Agent} - [{ts}] \\"{method} {resource} {protocol}/{version}\\" {code} {res_header_Content-Length}'))); $this->client = new \GuzzleHttp\Client(['base_uri' => $this->storageApi->getApiUrl(), 'handler' => $handlerStack]); }
/** * Get uri template for component * * @param $componentId * @return string */ public function getJobUriTemplate($componentId) { if ($componentId !== KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME) { throw new \InvalidArgumentException(sprintf('Cannot get uri for "%s" component', $componentId)); } $jobUrl = '#'; $this->loadData(); // only for orchestrator if (!empty($this->urlTemplates['orchestrationJob'])) { $jobUrl = sprintf("%s%s", preg_replace('/(\\/+)$/', '', $this->storageApi->getApiUrl()), $this->urlTemplates['orchestrationJob']); } return $jobUrl; }
/** * @param Orchestration $entity * @param \Keboola\StorageApi\Client $storageApi * @return Orchestration */ public function fillOrchestrationEntity(Orchestration $entity, Client $storageApi) { $oldTokenId = $entity->getTokenId(); $oldName = $entity->getName(); /** @var $entity Orchestration */ $entity = parent::fillEntity($entity); $name = $entity->getName(); if (empty($name)) { $entity->setName($oldName); } // token change if ($oldTokenId !== $entity->getTokenId()) { $token = $storageApi->getToken($entity->getTokenId()); $token = new Token(new Client(array('token' => $token['token'], 'url' => $storageApi->getApiUrl()))); $entity->setToken($token)->setTokenId($token->getId())->setTokenDesc($token->getDescription())->setTokenOwnerName($token->getOwnerName())->setProjectId($token->getProjectId()); } // notifications $data = $this->getPostJsonData(); if (!empty($data['notifications'])) { $data['notifications'] = array_map(function ($row) { $notification = new Notification(); $notification->fromArray($row); return $notification; }, $data['notifications']); $entity->setNotifications($data['notifications']); } // tasks $data = $this->getPostJsonData(); if (array_key_exists('tasks', $data)) { $entity->removeTasks(); $data['tasks'] = array_map(function ($row) { $notification = new OrchestrationTask(); $notification->fromArray($row); return $notification; }, $data['tasks']); foreach ($data['tasks'] as $task) { $entity->addTask($task); } } //@FIXME catch and throw exception return $entity; }
/** * @param Orchestration $entity * @param \Keboola\StorageApi\Client $storageApi * @return Orchestration */ public function fillOrchestrationEntity(Orchestration $entity, Client $storageApi) { /** @var $entity Orchestration */ $entity = parent::fillEntity($entity); if (!$entity->getTokenId()) { $entity->setTokenId($storageApi->createToken('manage', sprintf('Orchestrator %s', $entity->getName()), null, true)); } // fill token data $token = $storageApi->getToken($entity->getTokenId()); $token = new Token(new Client(array('token' => $token['token'], 'url' => $storageApi->getApiUrl()))); $entity->setToken($token)->setTokenId($token->getId())->setTokenDesc($token->getDescription())->setTokenOwnerName($token->getOwnerName())->setProjectId($token->getProjectId()); // notifications $data = $this->getPostJsonData(); if (!empty($data['notifications'])) { $data['notifications'] = array_map(function ($row) { $notification = new Notification(); $notification->fromArray($row); return $notification; }, $data['notifications']); $entity->setNotifications($data['notifications']); } // tasks $data = $this->getPostJsonData(); if (!empty($data['tasks'])) { $data['tasks'] = array_map(function ($row) { $notification = new OrchestrationTask(); $notification->fromArray($row); return $notification; }, $data['tasks']); foreach ($data['tasks'] as $task) { $entity->addTask($task); } } //@FIXME catch and throw exception return $entity; }
/** * @param StorageApiClient $storageApi * @param array $permissions hash bucketId => permission (read/write) or "manage" for all buckets permissions * @return StorageApiToken */ protected function createNewToken(StorageApiClient $storageApi, $permissions) { $tokenId = $storageApi->createToken($permissions, sprintf('Orchestrator %s', self::TESTING_NAME), 3600 * 2, true); $errorMessage = 'Could not create testing token'; $this->assertNotEmpty($tokenId, $errorMessage); $token = $storageApi->getToken($tokenId); $this->assertArrayHasKey('id', $token, $errorMessage); $this->assertArrayHasKey('description', $token, $errorMessage); $this->assertArrayHasKey('canManageBuckets', $token, $errorMessage); $this->assertEquals(sprintf('Orchestrator %s', self::TESTING_NAME), $token['description'], $errorMessage); $this->assertEquals($permissions == 'manage' ? 1 : 0, $token['canManageBuckets'], $errorMessage); return new StorageApiToken(new StorageApiClient(array('token' => $token['token'], 'url' => $storageApi->getApiUrl()))); }