-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from keboola/ES-156-describe-workspaces
Add command manage:describe-organization-workspaces
- Loading branch information
Showing
3 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/Keboola/Console/Command/DescribeOrganizationWorkspaces.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
); | ||
} | ||
} |