Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command manage:describe-organization-workspaces #60

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
);
}
}
Loading