Skip to content

Commit

Permalink
Merge pull request #60 from keboola/ES-156-describe-workspaces
Browse files Browse the repository at this point in the history
Add command manage:describe-organization-workspaces
  • Loading branch information
jirkasemmler committed May 2, 2024
2 parents 5e9c68c + bc2564e commit 382c358
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ It will perform a dry run unleass the `--force/-f` option is applied.

Arguments:
- Storage Token *required*
- Hostname sUffix *optional* (default: keboola.com)
- Hostname suffix *optional* (default: keboola.com)

Options:
- `--force/-f`
Expand All @@ -288,6 +288,31 @@ Options:
php ./cli.php storage:delete-ownerless-workspaces [--force/-f] [--includeShared] <storage-token> <hostname-suffix>
```

## Describe Connection Workspaces for an organization
This command takes an output file argument and writes out a csv describing all connection workspaces in an organisation.
The output file has header:
'projectId',
'projectName',
'branchId',
'branchName',
'componentId',
'configurationId',
'creatorEmail',
'createdDate',
'snowflakeSchema',
'readOnlyStorageAccess'

Arguments:
- Manage Token *required*
- Organisation Id *required*
- Output File *required*
- Hostname suffix *optional* (default: keboola.com)

- Run the command
```
php ./cli.php manage:describe-organization-workspaces <manage-token> <organization-id> <output-file> <hostname-suffix>
```

## License

MIT licensed, see [LICENSE](./LICENSE) file.
2 changes: 2 additions & 0 deletions cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Keboola\Console\Command\DeleteOrganizationOrphanedWorkspaces;
use Keboola\Console\Command\DeleteOrphanedWorkspaces;
use Keboola\Console\Command\DeleteOwnerlessWorkspaces;
use Keboola\Console\Command\DescribeOrganizationWorkspaces;
use Keboola\Console\Command\LineageEventsExport;
use Keboola\Console\Command\MassDedup;
use Keboola\Console\Command\MassProjectEnableDynamicBackends;
Expand Down Expand Up @@ -57,4 +58,5 @@
$application->add(new DeleteProjectSandboxes());
$application->add(new RemoveUserFromOrganizationProjects());
$application->add(new ReactivateSchedules());
$application->add(new DescribeOrganizationWorkspaces());
$application->run();
146 changes: 146 additions & 0 deletions src/Keboola/Console/Command/DescribeOrganizationWorkspaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
namespace Keboola\Console\Command;

use Keboola\Csv\CsvFile;
use Keboola\ManageApi\Client;
use Keboola\StorageApi\BranchAwareClient;
use Keboola\StorageApi\Client as StorageApiClient;
use Keboola\StorageApi\DevBranches;
use Keboola\StorageApi\Tokens;
use Keboola\StorageApi\Workspaces;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DescribeOrganizationWorkspaces extends Command
{
protected function configure()
{
$this
->setName('manage:describe-organization-workspaces')
->setDescription('Describe workspaces of this organization.')
->addArgument(
'manageToken',
InputArgument::REQUIRED,
'Keboola Storage API token to use'
)
->addArgument(
'organizationId',
InputArgument::REQUIRED,
'ID of the organization to clean'
)
->addArgument(
'outputFile',
InputArgument::REQUIRED,
'file to output the csv results'
)
->addArgument(
'hostnameSuffix',
InputArgument::OPTIONAL,
'Keboola Connection Hostname Suffix',
'keboola.com'
);
}

protected function execute(InputInterface $input, OutputInterface $output): void
{
$manageToken = $input->getArgument('manageToken');
$organizationId = $input->getArgument('organizationId');
$outputFile = $input->getArgument('outputFile');
$kbcUrl = sprintf('https://connection.%s', $input->getArgument('hostnameSuffix'));
$manageClient = new Client(['token' => $manageToken, 'url' => $kbcUrl]);
$organization = $manageClient->getOrganization($organizationId);
$projects = $organization['projects'];
$output->writeln(
sprintf(
'Checking workspaces for "%d" projects',
count($projects)
)
);

$storageUrl = 'https://connection.' . $input->getArgument('hostnameSuffix');

$totalWorkspaces = 0;
$totalDeletedWorkspaces = 0;

$csvFile = new CsvFile($outputFile);
$csvFile->writeRow([
'projectId',
'projectName',
'branchId',
'branchName',
'componentId',
'configurationId',
'creatorEmail',
'createdDate',
'snowflakeSchema',
'readOnlyStorageAccess'
]);

foreach ($projects as $project) {
$storageToken = $manageClient->createProjectStorageToken(
$project['id'],
['description' => 'Fetching Workspace Details']
);
$storageClient = new StorageApiClient([
'token' => $storageToken['token'],
'url' => $storageUrl,
]);
$devBranches = new DevBranches($storageClient);
$branchesList = $devBranches->listBranches();
$output->writeln(
sprintf(
'Retrieving workspaces for project %s : %s ',
$project['id'],
$project['name']
)
);
$totalProjectWorkspaces = 0;
foreach ($branchesList as $branch) {
$branchId = $branch['id'];
$branchStorageClient = new BranchAwareClient($branchId, [
'token' => $storageToken['token'],
'url' => $storageUrl,
]);
$workspacesClient = new Workspaces($branchStorageClient);
$workspaceList = $workspacesClient->listWorkspaces();
$output->writeln('Found ' . count($workspaceList) . ' workspaces in branch ' . $branch['name']);
$totalProjectWorkspaces += count($workspaceList);
foreach ($workspaceList as $workspace) {
$row = [
$project['id'],
$project['name'],
$branch['id'],
$branch['name'],
$workspace['component'],
$workspace['configurationId'],
$workspace['creatorToken']['description'],
$workspace['created'],
$workspace['name'],
$workspace['readOnlyStorageAccess']
];
$csvFile->writeRow($row);
$totalProjectWorkspaces ++;
}
}
$output->writeln(
sprintf(
'Project %s has a total of %d workspaces.',
$project['id'],
$totalProjectWorkspaces
)
);
$tokensClient = new Tokens($storageClient);
$tokensClient->dropToken($storageToken['id']);

$totalWorkspaces += $totalProjectWorkspaces;
}
$output->writeln(
sprintf(
'A grand total of %d workspaces in this organisation',
$totalWorkspaces
)
);
}
}

0 comments on commit 382c358

Please sign in to comment.