diff --git a/README.md b/README.md index 7fa5fafa..7d6796a9 100644 --- a/README.md +++ b/README.md @@ -262,9 +262,13 @@ op read "op://vault/stripe/api-key" | cloud secret:create --name=STRIPE_KEY -n | `cloud cache:create` | Create a cache | | `cloud cache:update` | Update a cache | | `cloud cache:delete` | Delete a cache | +| `cloud cache:start` | Start a stopped cache | +| `cloud cache:stop` | Stop a cache | | `cloud cache:types` | List available cache types | | `cloud cache:metrics` | View cache metrics | +Environments cannot connect to a stopped cache until it is started again, so `cache:stop` asks for confirmation (or `--force`). + ### Object storage (buckets) | Command | Description | diff --git a/app/Client/README.md b/app/Client/README.md index 919d5670..a699df61 100644 --- a/app/Client/README.md +++ b/app/Client/README.md @@ -138,6 +138,8 @@ $data = $response->json(); - `CreateCacheRequest` - Create a new cache - `UpdateCacheRequest` - Update a cache - `DeleteCacheRequest` - Delete a cache +- `StartCacheRequest` - Start a stopped cache +- `StopCacheRequest` - Stop a cache - `ListCacheTypesRequest` - List available cache types #### WebSocket Clusters diff --git a/app/Client/Resources/Caches/StartCacheRequest.php b/app/Client/Resources/Caches/StartCacheRequest.php new file mode 100644 index 00000000..85e94ee1 --- /dev/null +++ b/app/Client/Resources/Caches/StartCacheRequest.php @@ -0,0 +1,29 @@ +cacheId}/start"; + } + + public function createDtoFromResponse(Response $response): Cache + { + return Cache::createFromResponse($response->json()); + } +} diff --git a/app/Client/Resources/Caches/StopCacheRequest.php b/app/Client/Resources/Caches/StopCacheRequest.php new file mode 100644 index 00000000..8dc7f840 --- /dev/null +++ b/app/Client/Resources/Caches/StopCacheRequest.php @@ -0,0 +1,29 @@ +cacheId}/stop"; + } + + public function createDtoFromResponse(Response $response): Cache + { + return Cache::createFromResponse($response->json()); + } +} diff --git a/app/Client/Resources/CachesResource.php b/app/Client/Resources/CachesResource.php index 9d0aa328..60ad8490 100644 --- a/app/Client/Resources/CachesResource.php +++ b/app/Client/Resources/CachesResource.php @@ -10,6 +10,8 @@ use App\Client\Resources\Caches\GetCacheRequest; use App\Client\Resources\Caches\ListCachesRequest; use App\Client\Resources\Caches\ListCacheTypesRequest; +use App\Client\Resources\Caches\StartCacheRequest; +use App\Client\Resources\Caches\StopCacheRequest; use App\Client\Resources\Caches\UpdateCacheRequest; use App\Dto\Cache; use App\Dto\CacheMetrics; @@ -57,6 +59,22 @@ public function update(UpdateCacheRequestData $data): Cache return $request->createDtoFromResponse($response); } + public function start(string $cacheId): Cache + { + $request = new StartCacheRequest($cacheId); + $response = $this->send($request); + + return $request->createDtoFromResponse($response); + } + + public function stop(string $cacheId): Cache + { + $request = new StopCacheRequest($cacheId); + $response = $this->send($request); + + return $request->createDtoFromResponse($response); + } + public function delete(string $cacheId): void { $this->send(new DeleteCacheRequest($cacheId)); diff --git a/app/Commands/CacheStart.php b/app/Commands/CacheStart.php new file mode 100644 index 00000000..9f42b655 --- /dev/null +++ b/app/Commands/CacheStart.php @@ -0,0 +1,35 @@ +ensureClient(); + + intro('Starting Cache'); + + $cache = $this->resolvers()->cache()->from($this->argument('cache')); + + $started = spin( + fn () => $this->client->caches()->start($cache->id), + 'Starting cache...', + ); + + $this->outputJsonIfWanted($started); + + success("Cache '{$started->name}' started."); + } +} diff --git a/app/Commands/CacheStop.php b/app/Commands/CacheStop.php new file mode 100644 index 00000000..6a48ef62 --- /dev/null +++ b/app/Commands/CacheStop.php @@ -0,0 +1,39 @@ +ensureClient(); + + intro('Stopping Cache'); + + $cache = $this->resolvers()->cache()->from($this->argument('cache')); + + $this->confirmDestructive("Stop cache '{$cache->name}'?"); + + $stopped = spin( + fn () => $this->client->caches()->stop($cache->id), + 'Stopping cache...', + ); + + $this->outputJsonIfWanted($stopped); + + success("Cache '{$stopped->name}' stopped."); + } +} diff --git a/tests/Feature/CacheStartStopTest.php b/tests/Feature/CacheStartStopTest.php new file mode 100644 index 00000000..39abeb63 --- /dev/null +++ b/tests/Feature/CacheStartStopTest.php @@ -0,0 +1,88 @@ +shouldReceive('apiTokens')->andReturn(collect(['test-api-token'])); + $this->app->instance(ConfigRepository::class, $config); +}); + +afterEach(function () { + MockClient::destroyGlobal(); +}); + +function setupCacheStartStopMocks(): void +{ + MockClient::global([ + GetCacheRequest::class => MockResponse::make(['data' => cacheResponse(['attributes' => ['name' => 'sessions']])], 200), + StartCacheRequest::class => MockResponse::make(['data' => cacheResponse(['attributes' => ['name' => 'sessions', 'status' => 'available']])], 200), + StopCacheRequest::class => MockResponse::make(['data' => cacheResponse(['attributes' => ['name' => 'sessions', 'status' => 'stopped']])], 200), + ]); +} + +it('starts a cache', function () { + setupCacheStartStopMocks(); + + $this->artisan('cache:start', ['cache' => 'cache-1', '--no-interaction' => true]) + ->assertSuccessful(); + + MockClient::global()->assertSent(fn ($request) => $request instanceof StartCacheRequest + && $request->getMethod()->value === 'POST' + && $request->resolveEndpoint() === '/caches/cache-1/start'); + MockClient::global()->assertNotSent(StopCacheRequest::class); +}); + +it('outputs the started cache as json', function () { + setupCacheStartStopMocks(); + + Artisan::call('cache:start', ['cache' => 'cache-1', '--json' => true]); + + $output = json_decode(Artisan::output(), true); + + expect($output)->toMatchArray([ + 'id' => 'cache-1', + 'name' => 'sessions', + 'status' => 'available', + ]); +}); + +it('stops a cache with --force', function () { + setupCacheStartStopMocks(); + + $this->artisan('cache:stop', ['cache' => 'cache-1', '--force' => true, '--no-interaction' => true]) + ->assertSuccessful(); + + MockClient::global()->assertSent(fn ($request) => $request instanceof StopCacheRequest + && $request->getMethod()->value === 'POST' + && $request->resolveEndpoint() === '/caches/cache-1/stop'); +}); + +it('outputs the stopped cache as json', function () { + setupCacheStartStopMocks(); + + Artisan::call('cache:stop', ['cache' => 'cache-1', '--force' => true, '--json' => true]); + + $output = json_decode(Artisan::output(), true); + + expect($output)->toMatchArray([ + 'id' => 'cache-1', + 'name' => 'sessions', + 'status' => 'stopped', + ]); +}); + +it('requires --force to stop a cache non-interactively', function () { + setupCacheStartStopMocks(); + + $this->artisan('cache:stop', ['cache' => 'cache-1', '--no-interaction' => true]) + ->assertFailed(); + + MockClient::global()->assertNotSent(StopCacheRequest::class); +});