Skip to content

Commit

Permalink
Merge pull request #1378 from keboola/KAB-179-CRUD-conversations-inne…
Browse files Browse the repository at this point in the history
…r-workings

KAB-179 queueCreateWorkspace
  • Loading branch information
zajca authored Jul 25, 2024
2 parents 08a2d4b + 958366e commit d4b5f42
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Keboola/StorageApi/Workspaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ public function __construct(Client $client)
*/
public function createWorkspace(array $options = [], bool $async = false)
{
$url = 'workspaces';
$requestOptions = [Client::REQUEST_OPTION_EXTENDED_TIMEOUT => true];
if ($async) {
$url .= '?' . http_build_query(['async' => $async]);
$requestOptions = [];
}

$workspaceResponse = $this->client->apiPostJson($url, $options, true, $requestOptions);
$workspaceResponse = $this->internalCreateWorkspace($async, $options, true);
assert(is_array($workspaceResponse));

$resetPasswordResponse = $this->resetWorkspacePassword($workspaceResponse['id']);
return Workspaces::addCredentialsToWorkspaceResponse($workspaceResponse, $resetPasswordResponse);
}

public function queueCreateWorkspace(array $options = []): int
{
$job = $this->internalCreateWorkspace(true, $options, false);
return (int) $job['id'];
}

/**
* @return array
*/
Expand Down Expand Up @@ -150,4 +149,16 @@ public static function addCredentialsToWorkspaceResponse(array $workspaceRespons
],
]);
}

private function internalCreateWorkspace(bool $async, array $options, bool $handleAsyncTask): array
{
$url = 'workspaces';
$requestOptions = [Client::REQUEST_OPTION_EXTENDED_TIMEOUT => true];
if ($async) {
$url .= '?' . http_build_query(['async' => $async]);
$requestOptions = [];
}

return $this->client->apiPostJson($url, $options, $handleAsyncTask, $requestOptions);
}
}
38 changes: 38 additions & 0 deletions tests/Backend/Workspaces/WorkspacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,44 @@ public function testWorkspaceCreate(bool $async): void
$this->assertCredentialsShouldNotWork($connection);
}

public function testQueueWorkspaceCreate(): void
{
$this->initEvents($this->workspaceSapiClient);

$workspaces = new Workspaces($this->workspaceSapiClient);

foreach ($this->listTestWorkspaces($this->_client) as $workspace) {
$workspaces->deleteWorkspace($workspace['id'], [], true);
}

$runId = $this->_client->generateRunId();
$this->_client->setRunId($runId);
$this->workspaceSapiClient->setRunId($runId);

$jobId = $workspaces->queueCreateWorkspace([]);

$job = $this->_client->waitForJob($jobId);
$this->assertNotNull($job);
$workspace = $job['results'];
$this->assertIsArray($workspace);
$this->assertArrayHasKey('id', $workspace);
$this->assertIsInt($workspace['id']);
$resetPasswordResponse = $workspaces->resetWorkspacePassword($workspace['id']);
$workspace = Workspaces::addCredentialsToWorkspaceResponse($workspace, $resetPasswordResponse);

/** @var array $connection */
$connection = $workspace['connection'];
$this->assertArrayHasKey('region', $connection);
$this->assertNotEmpty($connection['region']);

$backend = WorkspaceBackendFactory::createWorkspaceBackend($workspace);
$backend->createTable('mytable', ['amount' => $this->getColumnAmountType($connection['backend'])]);
$tableNames = $backend->getTables();
$backend = null; // force odbc disconnect
$this->assertArrayHasKey('mytable', array_flip($tableNames));
$workspaces->deleteWorkspace($workspace['id'], [], true);
}

public function testWorkspacePasswordReset(): void
{
$workspaces = new Workspaces($this->workspaceSapiClient);
Expand Down

0 comments on commit d4b5f42

Please sign in to comment.