From 30623f91b0e6d6c08c6787a9175fe6347b75e3d3 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 13 Oct 2020 15:16:37 +0200 Subject: [PATCH] Adds support to RDS Proxies (#77) * wip * wip * Apply fixes from StyleCI (#76) --- src/Commands/DatabaseDeleteProxyCommand.php | 48 +++++++++++++++++++++ src/Commands/DatabaseListCommand.php | 3 +- src/Commands/DatabaseProxyCommand.php | 48 +++++++++++++++++++++ src/ConsoleVaporClient.php | 24 +++++++++++ vapor | 2 + 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/Commands/DatabaseDeleteProxyCommand.php create mode 100644 src/Commands/DatabaseProxyCommand.php diff --git a/src/Commands/DatabaseDeleteProxyCommand.php b/src/Commands/DatabaseDeleteProxyCommand.php new file mode 100644 index 00000000..d27da1c5 --- /dev/null +++ b/src/Commands/DatabaseDeleteProxyCommand.php @@ -0,0 +1,48 @@ +setName('database:delete-proxy') + ->addArgument('database', InputArgument::REQUIRED, 'The name of the database') + ->setDescription('Delete the proxy associated to the database.'); + } + + /** + * Execute the command. + * + * @return void + */ + public function handle() + { + Helpers::ensure_api_token_is_available(); + + $databases = $this->vapor->databases(); + + if (!is_numeric($databaseId = $this->argument('database'))) { + $databaseId = $this->findIdByName($databases, $databaseId); + } + + if (is_null($databaseId)) { + Helpers::abort('Unable to find a database with that name / ID.'); + } + + $response = $this->vapor->deleteDatabaseProxy( + $databaseId + ); + + Helpers::info('Database proxy deleted successfully.'); + } +} diff --git a/src/Commands/DatabaseListCommand.php b/src/Commands/DatabaseListCommand.php index cc71a1b9..d9dde75a 100644 --- a/src/Commands/DatabaseListCommand.php +++ b/src/Commands/DatabaseListCommand.php @@ -29,7 +29,7 @@ public function handle() Helpers::ensure_api_token_is_available(); $this->table([ - 'ID', 'Provider', 'Name', 'Region', 'Type', 'Class', 'Storage', 'Status', + 'ID', 'Provider', 'Name', 'Region', 'Type', 'Class', 'Storage', 'Status', 'Proxy', ], collect($this->vapor->databases())->map(function ($database) { return [ $database['id'], @@ -40,6 +40,7 @@ public function handle() $database['instance_class'], $database['storage'].'GB', Str::title(str_replace('_', ' ', $database['status'])), + $database['proxy'] ? Str::title(str_replace('_', ' ', $database['proxy']['status'])) : 'No', ]; })->all()); } diff --git a/src/Commands/DatabaseProxyCommand.php b/src/Commands/DatabaseProxyCommand.php new file mode 100644 index 00000000..0305a891 --- /dev/null +++ b/src/Commands/DatabaseProxyCommand.php @@ -0,0 +1,48 @@ +setName('database:proxy') + ->addArgument('database', InputArgument::REQUIRED, 'The name of the database') + ->setDescription('Create an proxy for a database'); + } + + /** + * Execute the command. + * + * @return void + */ + public function handle() + { + Helpers::ensure_api_token_is_available(); + + $databases = $this->vapor->databases(); + + if (!is_numeric($databaseId = $this->argument('database'))) { + $databaseId = $this->findIdByName($databases, $databaseId); + } + + if (is_null($databaseId)) { + Helpers::abort('Unable to find a database with that name / ID.'); + } + + $response = $this->vapor->createDatabaseProxy( + $databaseId + ); + + Helpers::info('Database proxy created successfully.'); + } +} diff --git a/src/ConsoleVaporClient.php b/src/ConsoleVaporClient.php index 8ad25125..e70a1e5b 100644 --- a/src/ConsoleVaporClient.php +++ b/src/ConsoleVaporClient.php @@ -482,6 +482,30 @@ public function dropDatabaseUser($databaseUserId) $this->requestWithErrorHandling('delete', '/api/database-users/'.$databaseUserId); } + /** + * Create a new database proxy. + * + * @param string $databaseId + * + * @return array + */ + public function createDatabaseProxy($databaseId) + { + return $this->requestWithErrorHandling('post', '/api/databases/'.$databaseId.'/proxy'); + } + + /** + * Delete the proxy associated to the given database. + * + * @param string $databaseId + * + * @return void + */ + public function deleteDatabaseProxy($databaseId) + { + $this->requestWithErrorHandling('delete', '/api/databases/'.$databaseId.'/proxy'); + } + /** * Rotate the given database's password. * diff --git a/vapor b/vapor index 65f147d8..5ab6c069 100755 --- a/vapor +++ b/vapor @@ -122,6 +122,8 @@ $app->add(new Commands\DatabaseCommand); $app->add(new Commands\DatabaseShowCommand); $app->add(new Commands\DatabaseScaleCommand); $app->add(new Commands\DatabasePasswordCommand); +$app->add(new Commands\DatabaseProxyCommand); +$app->add(new Commands\DatabaseDeleteProxyCommand); $app->add(new Commands\DatabaseRestoreCommand); $app->add(new Commands\DatabaseMetricsCommand); $app->add(new Commands\DatabaseUsersCommand);