Skip to content

Commit 6333296

Browse files
committed
feat: add support for creating aurora serverless database clusters
1 parent de1981b commit 6333296

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

src/ApiClient.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,25 @@ public function createDatabase(int $databaseServerId, string $name): Collection
161161
/**
162162
* Create a new database on the given network.
163163
*/
164-
public function createDatabaseServer(string $name, int $networkId, string $type, int $storage = 100, bool $public = false): Collection
164+
public function createDatabaseServer(string $name, int $networkId, string $type, ?int $storage = 50, bool $public = false): Collection
165165
{
166-
return $this->request('post', "/networks/{$networkId}/database-servers", [
166+
if ('aurora-mysql' === $type && null !== $storage) {
167+
throw new \InvalidArgumentException('Cannot specify a "storage" value for "aurora-mysql" database server');
168+
} elseif ('aurora-mysql' === $type && $public) {
169+
throw new \InvalidArgumentException('An "aurora-mysql" database server cannot be public');
170+
}
171+
172+
$databaseServer = [
167173
'name' => $name,
168-
'publicly_accessible' => $public,
169-
'storage' => $storage,
170174
'type' => $type,
171-
]);
175+
];
176+
177+
if ('aurora-mysql' !== $type) {
178+
$databaseServer['publicly_accessible'] = $public;
179+
$databaseServer['storage'] = $storage;
180+
}
181+
182+
return $this->request('post', "/networks/{$networkId}/database-servers", $databaseServer);
172183
}
173184

174185
/**

src/Command/Database/CreateDatabaseServerCommand.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Ymir\Cli\Command\Database;
1515

16-
use Illuminate\Support\Collection;
1716
use Symfony\Component\Console\Exception\InvalidArgumentException;
1817
use Symfony\Component\Console\Exception\RuntimeException;
1918
use Symfony\Component\Console\Helper\TableSeparator;
@@ -22,6 +21,7 @@
2221
use Symfony\Component\Console\Input\InputOption;
2322
use Ymir\Cli\Command\AbstractCommand;
2423
use Ymir\Cli\Console\OutputInterface;
24+
use Ymir\Cli\Exception\CommandCancelledException;
2525

2626
class CreateDatabaseServerCommand extends AbstractCommand
2727
{
@@ -44,6 +44,7 @@ protected function configure()
4444
->addOption('network', null, InputOption::VALUE_REQUIRED, 'The ID or name of the network on which the database will be created')
4545
->addOption('private', null, InputOption::VALUE_NONE, 'The created database server won\'t be publicly accessible')
4646
->addOption('public', null, InputOption::VALUE_NONE, 'The created database server will be publicly accessible')
47+
->addOption('serverless', null, InputOption::VALUE_NONE, 'Create an Aurora serverless database cluster (overrides all other options)')
4748
->addOption('storage', null, InputOption::VALUE_REQUIRED, 'The maximum amount of storage (in GB) allocated to the database server')
4849
->addOption('type', null, InputOption::VALUE_REQUIRED, 'The database server type to create on the cloud provider');
4950
}
@@ -60,12 +61,19 @@ protected function perform(InputInterface $input, OutputInterface $output)
6061
}
6162

6263
$network = $this->apiClient->getNetwork($this->determineOrCreateNetwork('On what network should the database server be created?', $input, $output));
63-
$type = $this->determineType($network, $input, $output);
64-
$storage = $this->determineStorage($input, $output);
65-
$public = $this->determinePublic($input, $output);
6664

67-
if (!$public && !$network->get('has_nat_gateway') && !$output->confirm('A private database server will require Ymir to add a NAT gateway to your network (~$32/month). Would you like to proceed? (Answering "no" will make the database server publicly accessible.)')) {
65+
if (!isset($network['provider']['id'])) {
66+
throw new RuntimeException('The Ymir API failed to return information on the cloud provider');
67+
}
68+
69+
$type = $this->determineType($input, $output, (int) $network['provider']['id']);
70+
$storage = 'aurora-mysql' !== $type ? $this->determineStorage($input, $output) : null;
71+
$public = 'aurora-mysql' !== $type && $this->determinePublic($input, $output);
72+
73+
if (!$public && !$network->get('has_nat_gateway') && !$output->confirm('A private database server requires that Ymir add a NAT gateway (~$32/month) to your network. Would you like to proceed? <fg=default>(Answering "<comment>no</comment>" will make the database server publicly accessible.)</>')) {
6874
$public = true;
75+
} elseif ('aurora-mysql' !== $type && !$network->get('has_nat_gateway') && !$output->confirm('An Aurora serverless database cluster requires that Ymir add a NAT gateway (~$32/month) to your network. Would you like to proceed? <fg=default>(Answering "<comment>no</comment>" will cancel the command.)</>')) {
76+
throw new CommandCancelledException();
6977
}
7078

7179
$database = $this->apiClient->createDatabaseServer($name, (int) $network['id'], $type, $storage, $public);
@@ -75,7 +83,7 @@ protected function perform(InputInterface $input, OutputInterface $output)
7583

7684
$output->horizontalTable(
7785
['Database Sever', new TableSeparator(), 'Username', 'Password', new TableSeparator(), 'Type', 'Public', 'Storage (in GB)'],
78-
[[$database['name'], new TableSeparator(), $database['username'], $database['password'], new TableSeparator(), $database['type'], $output->formatBoolean($database['publicly_accessible']), $database['storage']]]
86+
[[$database['name'], new TableSeparator(), $database['username'], $database['password'], new TableSeparator(), $database['type'], $output->formatBoolean($database['publicly_accessible']), $database['storage'] ?? 'N/A']]
7987
);
8088

8189
$output->infoWithDelayWarning('Database server created');
@@ -118,16 +126,14 @@ private function determineStorage(InputInterface $input, OutputInterface $output
118126
/**
119127
* Determine the database server type to create.
120128
*/
121-
private function determineType(Collection $network, InputInterface $input, OutputInterface $output): string
129+
private function determineType(InputInterface $input, OutputInterface $output, int $providerId): string
122130
{
123-
if (!isset($network['provider']['id'])) {
124-
throw new RuntimeException('The Ymir API failed to return information on the cloud provider');
125-
}
126-
127-
$type = $this->getStringOption($input, 'type');
128-
$types = $this->apiClient->getDatabaseServerTypes((int) $network['provider']['id']);
131+
$type = !$this->getBooleanOption($input, 'serverless') ? $this->getStringOption($input, 'type') : 'aurora-mysql';
132+
$types = $this->apiClient->getDatabaseServerTypes($providerId);
129133

130-
if (null !== $type && !$types->has($type)) {
134+
if ($types->isEmpty()) {
135+
throw new RuntimeException('The Ymir API failed to return database server types');
136+
} elseif (null !== $type && !$types->has($type)) {
131137
throw new InvalidArgumentException(sprintf('The type "%s" isn\'t a valid database type', $type));
132138
} elseif (null === $type) {
133139
$type = (string) $output->choice('What should the database server type be?', $types);

src/Command/Database/GetDatabaseServerInfoCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function perform(InputInterface $input, OutputInterface $output)
5858
$databaseServer['network']['name'],
5959
$databaseServer['region'],
6060
$databaseServer['type'],
61-
$databaseServer['storage'].'GB',
61+
$databaseServer['storage'] ? $databaseServer['storage'].'GB' : 'N/A',
6262
$databaseServer['endpoint'],
6363
]]
6464
);

src/Command/Database/ListDatabaseServersCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function perform(InputInterface $input, OutputInterface $output)
5454
$output->formatBoolean($database['locked']),
5555
$output->formatBoolean($database['publicly_accessible']),
5656
$database['type'],
57-
$database['storage'].'GB',
57+
$database['storage'] ? $database['storage'].'GB' : 'N/A',
5858
];
5959
})->all()
6060
);

0 commit comments

Comments
 (0)