From db034db2ec876d41aba6124dd4b68b1654ebb8e5 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 21:38:12 +0000 Subject: [PATCH 1/3] Sync environments with the current Cloud API spec Publish the environment's post-deploy command, HTTP basic auth state and internal networking settings; let environment:update set the post-deploy command and HTTP basic auth; keep a metric interval with no sample as null instead of zero. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01T1Sq4Xyfgr8GKyqxCwmuSc --- .../Requests/UpdateEnvironmentRequestData.php | 7 +++ app/Commands/EnvironmentUpdate.php | 50 ++++++++++++++- app/Concerns/RendersMetrics.php | 9 +-- app/Dto/Environment.php | 6 ++ app/Dto/EnvironmentInternalNetworking.php | 28 +++++++++ app/Dto/MetricPoint.php | 5 +- app/Dto/MetricSeries.php | 2 +- tests/Feature/EnvironmentUpdateTest.php | 62 +++++++++++++++++++ tests/Unit/MetricSeriesTest.php | 15 +++++ tests/Unit/RendersMetricsTest.php | 27 ++++++++ 10 files changed, 203 insertions(+), 8 deletions(-) create mode 100644 app/Dto/EnvironmentInternalNetworking.php diff --git a/app/Client/Requests/UpdateEnvironmentRequestData.php b/app/Client/Requests/UpdateEnvironmentRequestData.php index e89b6dfa..6917eed7 100644 --- a/app/Client/Requests/UpdateEnvironmentRequestData.php +++ b/app/Client/Requests/UpdateEnvironmentRequestData.php @@ -19,6 +19,7 @@ public function __construct( public readonly ?string $buildCommand = null, public readonly ?string $nodeVersion = null, public readonly ?string $deployCommand = null, + public readonly ?string $postDeployCommand = null, /** @var string|null '' clears it */ public readonly ?string $startCommand = null, public readonly ?bool $usesVanityDomain = null, @@ -46,6 +47,9 @@ public function __construct( public readonly ?bool $firewallBrowserIntegrityCheck = null, /** @var array{mode: 'process', root: string|null}|null */ public readonly ?array $serving = null, + public readonly ?bool $usesHttpBasicAuth = null, + #[SensitiveParameter] + public readonly ?string $httpBasicAuthPassword = null, ) { // } @@ -64,6 +68,7 @@ public function toRequestData(): array 'build_command' => $this->buildCommand, 'node_version' => $this->nodeVersion, 'deploy_command' => $this->deployCommand, + 'post_deploy_command' => $this->postDeployCommand, 'start_command' => $this->startCommand, 'uses_vanity_domain' => $this->usesVanityDomain, 'database_schema_id' => $this->databaseSchemaId, @@ -84,6 +89,8 @@ public function toRequestData(): array 'firewall_block_path' => $this->firewallBlockPath, 'firewall_browser_integrity_check' => $this->firewallBrowserIntegrityCheck, 'serving' => $this->serving, + 'uses_http_basic_auth' => $this->usesHttpBasicAuth, + 'http_basic_auth_password' => $this->httpBasicAuthPassword, ]); // filter() drops nulls, so the CLI's '' stands in for the explicit null the API needs to clear these. diff --git a/app/Commands/EnvironmentUpdate.php b/app/Commands/EnvironmentUpdate.php index b40d67c9..d4d624c7 100644 --- a/app/Commands/EnvironmentUpdate.php +++ b/app/Commands/EnvironmentUpdate.php @@ -34,6 +34,7 @@ class EnvironmentUpdate extends BaseCommand {--branch= : Git branch} {--build-command= : Build command} {--deploy-command= : Deploy command} + {--post-deploy-command= : Commands run after the deployment goes live} {--start-command= : Start command for non-PHP dynamic apps (empty string to clear)} {--php-version= : PHP version (8.2:1, 8.3:1, 8.4:1, 8.5:1)} {--node-version= : Node version (20, 22, 24, 26)} @@ -41,6 +42,8 @@ class EnvironmentUpdate extends BaseCommand {--uses-deploy-hook= : Enable the deploy hook} {--uses-vanity-domain= : Serve the Laravel Cloud domain} {--uses-octane= : Uses Octane} + {--uses-http-basic-auth= : Require visitors to sign in as `admin` with --http-basic-auth-password} + {--http-basic-auth-password= : HTTP basic auth password (required when turning basic auth on)} {--uses-purge-edge-cache-on-deploy= : Purge the edge cache on deploy} {--timeout= : Request timeout in seconds (5-60)} {--shutdown-timeout= : Seconds to keep the previous deployment running (1-600)} @@ -110,6 +113,7 @@ protected function updateEnvironment(Environment $environment): Environment buildCommand: $this->form()->get('build_command'), nodeVersion: $this->form()->get('node_version'), deployCommand: $this->form()->get('deploy_command'), + postDeployCommand: $this->form()->get('post_deploy_command'), startCommand: $this->form()->get('start_command'), usesVanityDomain: $this->form()->boolean('uses_vanity_domain'), databaseSchemaId: $this->form()->get('database_id'), @@ -128,6 +132,8 @@ protected function updateEnvironment(Environment $environment): Environment responseHeadersHsts: $this->hsts($environment), firewallBlockPath: $this->form()->boolean('firewall_block_path'), firewallBrowserIntegrityCheck: $this->form()->boolean('firewall_browser_integrity_check'), + usesHttpBasicAuth: $this->form()->boolean('uses_http_basic_auth'), + httpBasicAuthPassword: $this->httpBasicAuthPassword(), ), ), 'Updating environment...', @@ -136,6 +142,24 @@ protected function updateEnvironment(Environment $environment): Environment return $this->client->environments()->get($environment->id); } + /** + * The API wants a password exactly when basic auth is being turned on, so turning it on asks for one. + */ + protected function httpBasicAuthPassword(): ?string + { + $password = $this->form()->get('http_basic_auth_password'); + + if (filled($password) || $this->form()->boolean('uses_http_basic_auth') !== true) { + return $password; + } + + if (! $this->isInteractive()) { + $this->failAndExit('Provide --http-basic-auth-password to turn on HTTP basic auth.'); + } + + return $this->form()->prompt('http_basic_auth_password')->value(); + } + protected function clearableInteger(string $key): int|string|null { $value = $this->form()->get($key); @@ -227,6 +251,17 @@ protected function defineFields(Environment $environment): void 'deploy-command', )->setPreviousValue($environment->deployCommand ?? ''); + $this->form()->define( + 'post_deploy_command', + fn ($resolver) => $resolver->fromInput( + fn ($value) => textarea( + label: 'Post-deploy command', + default: $value ?? $environment->postDeployCommand ?? '', + ), + ), + 'post-deploy-command', + )->setPreviousValue($environment->postDeployCommand ?? ''); + $this->form()->define( 'start_command', fn ($resolver) => $resolver->fromInput( @@ -256,6 +291,18 @@ protected function defineFields(Environment $environment): void $this->defineBoolean('uses_deploy_hook', 'Deploy hook', $environment->usesDeployHook); $this->defineBoolean('uses_vanity_domain', 'Vanity domain', null); $this->defineBoolean('uses_octane', 'Octane', $environment->usesOctane); + $this->defineBoolean('uses_http_basic_auth', 'HTTP basic auth', $environment->usesHttpBasicAuth); + + $this->form()->define( + 'http_basic_auth_password', + fn ($resolver) => $resolver->fromInput( + fn ($value) => password( + label: 'HTTP basic auth password', + required: true, + hint: 'Visitors sign in as `admin`', + ), + ), + )->setLabel('HTTP basic auth password'); $this->defineBoolean('uses_purge_edge_cache_on_deploy', 'Purge edge cache on deploy', null); $this->defineNumber('timeout', 'Timeout (seconds)', 5, 60, $environment->timeout); @@ -538,7 +585,8 @@ protected function collectDataAndUpdate(Environment $environment): Environment { $selection = multiselect( label: 'What do you want to update?', - options: collect($this->form()->defined())->mapWithKeys(fn ($field, $key) => [ + // The password is asked for when basic auth is turned on, never on its own. + options: collect($this->form()->defined())->reject(fn ($field) => $field->key === 'http_basic_auth_password')->mapWithKeys(fn ($field, $key) => [ $field->key => $field->label(), ])->toArray(), scroll: 15, diff --git a/app/Concerns/RendersMetrics.php b/app/Concerns/RendersMetrics.php index af4bb9dc..d947b798 100644 --- a/app/Concerns/RendersMetrics.php +++ b/app/Concerns/RendersMetrics.php @@ -65,7 +65,10 @@ protected function seriesListRows(string $label, array $series, Closure $format, */ protected function seriesLines(MetricSeries $series, Closure $format, array $stats = []): array { - if (! $series->hasData()) { + $values = $series->values(); + $sampled = array_values(array_filter($values, fn (?float $value) => $value !== null)); + + if ($sampled === []) { return [['No data']]; } @@ -74,13 +77,11 @@ protected function seriesLines(MetricSeries $series, Closure $format, array $sta ->map(fn (string $label, string $stat) => $format($series->{$stat}).' '.$label) ->values(); - $values = $series->values(); - return array_values(array_filter([ $summary->isEmpty() ? null : [$summary->first(), $summary->slice(1)->implode(' ยท ') ?: null], [ Sparkline::make($values, $this->sparklineWidth()), - $format($values[0]).' โ†’ '.$format($values[count($values) - 1]), + $format($sampled[0]).' โ†’ '.$format($sampled[count($sampled) - 1]), ], ])); } diff --git a/app/Dto/Environment.php b/app/Dto/Environment.php index 65c48482..9c3cf552 100644 --- a/app/Dto/Environment.php +++ b/app/Dto/Environment.php @@ -61,6 +61,9 @@ public function __construct( public readonly ?int $hibernationWakeUpInterval = null, public readonly ?int $timeout = null, public readonly ?int $shutdownTimeout = null, + public readonly ?string $postDeployCommand = null, + public readonly bool $usesHttpBasicAuth = false, + public readonly ?EnvironmentInternalNetworking $internalNetworking = null, ) { // } @@ -103,6 +106,9 @@ public static function createFromResponse(array $response): self 'hibernationWakeUpInterval' => $attributes['hibernation_wake_up_interval'] ?? null, 'timeout' => $attributes['timeout'] ?? null, 'shutdownTimeout' => $attributes['shutdown_timeout'] ?? null, + 'postDeployCommand' => $attributes['post_deploy_command'] ?? null, + 'usesHttpBasicAuth' => $attributes['uses_http_basic_auth'] ?? false, + 'internalNetworking' => isset($attributes['internal_networking']) ? EnvironmentInternalNetworking::createFromResponse($attributes['internal_networking']) : null, 'resend' => isset($attributes['resend']) ? EnvironmentResend::createFromResponse($attributes['resend']) : null, ]; diff --git a/app/Dto/EnvironmentInternalNetworking.php b/app/Dto/EnvironmentInternalNetworking.php new file mode 100644 index 00000000..cd786cac --- /dev/null +++ b/app/Dto/EnvironmentInternalNetworking.php @@ -0,0 +1,28 @@ + */ + public readonly array $environmentIds = [], + ) { + // + } + + public static function createFromResponse(array $internalNetworking): self + { + return new self( + enabled: (bool) ($internalNetworking['enabled'] ?? false), + hostname: $internalNetworking['hostname'] ?? null, + accessLevel: $internalNetworking['access_level'] ?? null, + environmentIds: $internalNetworking['environment_ids'] ?? [], + ); + } +} diff --git a/app/Dto/MetricPoint.php b/app/Dto/MetricPoint.php index 14ca54c4..698b4c15 100644 --- a/app/Dto/MetricPoint.php +++ b/app/Dto/MetricPoint.php @@ -12,7 +12,8 @@ class MetricPoint extends Data public function __construct( #[WithCast(DateTimeInterfaceCast::class, type: CarbonImmutable::class)] public readonly ?CarbonImmutable $at, - public readonly float $value, + /** Null when the instance reported no sample for the interval, which is not zero usage. */ + public readonly ?float $value, ) { // } @@ -27,7 +28,7 @@ public static function fromApiResponse(array $point, int $index = 0): self return new self( at: isset($point['x']) ? CarbonImmutable::parse($point['x']) : null, - value: (float) $value, + value: is_numeric($value) ? (float) $value : null, ); } } diff --git a/app/Dto/MetricSeries.php b/app/Dto/MetricSeries.php index d03e9b12..5052994d 100644 --- a/app/Dto/MetricSeries.php +++ b/app/Dto/MetricSeries.php @@ -74,7 +74,7 @@ public static function listFromApiResponse(mixed $series): array } /** - * @return list + * @return list */ public function values(): array { diff --git a/tests/Feature/EnvironmentUpdateTest.php b/tests/Feature/EnvironmentUpdateTest.php index fb18290f..212c1e22 100644 --- a/tests/Feature/EnvironmentUpdateTest.php +++ b/tests/Feature/EnvironmentUpdateTest.php @@ -27,6 +27,14 @@ 'hibernation_wake_up_interval' => 60, 'timeout' => 30, 'shutdown_timeout' => 45, + 'post_deploy_command' => 'php artisan cache:clear', + 'uses_http_basic_auth' => true, + 'internal_networking' => [ + 'enabled' => true, + 'hostname' => 'production.internal', + 'access_level' => 'specific', + 'environment_ids' => ['env-2'], + ], 'network_settings' => [ 'response_headers' => [ 'frame' => 'deny', @@ -194,3 +202,57 @@ function updateEnvironment(array $options): void 'shutdownTimeout' => 45, ]); }); + +it('sends the post-deploy command', function () { + updateEnvironment(['--post-deploy-command' => 'php artisan queue:restart']); + + expect($this->sent->body)->toBe(['post_deploy_command' => 'php artisan queue:restart']); +}); + +it('sends HTTP basic auth with its password', function () { + updateEnvironment([ + '--uses-http-basic-auth' => 'true', + '--http-basic-auth-password' => 'hunter2', + ]); + + expect($this->sent->body)->toBe([ + 'uses_http_basic_auth' => true, + 'http_basic_auth_password' => 'hunter2', + ]); +}); + +it('turns HTTP basic auth off without a password', function () { + updateEnvironment(['--uses-http-basic-auth' => 'false']); + + expect($this->sent->body)->toBe(['uses_http_basic_auth' => false]); +}); + +it('needs a password to turn HTTP basic auth on non-interactively', function () { + $this->artisan('environment:update', [ + 'environment' => 'env-1', + '--uses-http-basic-auth' => 'true', + '--no-interaction' => true, + ])->assertFailed(); + + expect($this->sent->body)->toBeNull(); +}); + +it('exposes the post-deploy command, HTTP basic auth and internal networking', function () { + Artisan::call('environment:get', [ + 'environment' => 'env-1', + '--json' => true, + '--fields' => 'postDeployCommand,usesHttpBasicAuth,internalNetworking', + '--no-interaction' => true, + ]); + + expect(json_decode(Artisan::output(), true))->toBe([ + 'postDeployCommand' => 'php artisan cache:clear', + 'usesHttpBasicAuth' => true, + 'internalNetworking' => [ + 'enabled' => true, + 'hostname' => 'production.internal', + 'accessLevel' => 'specific', + 'environmentIds' => ['env-2'], + ], + ]); +}); diff --git a/tests/Unit/MetricSeriesTest.php b/tests/Unit/MetricSeriesTest.php index 7c21e4d6..89f1bcc0 100644 --- a/tests/Unit/MetricSeriesTest.php +++ b/tests/Unit/MetricSeriesTest.php @@ -57,3 +57,18 @@ expect(MetricSeries::listFromApiResponse(null))->toBe([]); expect(MetricSeries::listFromApiResponse(['error' => true]))->toBe([]); }); + +it('keeps an interval with no sample as null rather than zero', function () { + $series = MetricSeries::listFromApiResponse([ + 'labels' => ['App', 'Worker'], + 'average' => [0.5, null], + 'data' => [ + ['x' => '2026-08-14T12:00:00.000000Z', 'y' => [0.5, null]], + ['x' => '2026-08-14T12:05:00.000000Z', 'y' => [null, null]], + ], + ]); + + expect($series[0]->values())->toBe([0.5, null]); + expect($series[1]->values())->toBe([null, null]); + expect($series[1]->average)->toBeNull(); +}); diff --git a/tests/Unit/RendersMetricsTest.php b/tests/Unit/RendersMetricsTest.php index 6a6b619b..c0ea79b5 100644 --- a/tests/Unit/RendersMetricsTest.php +++ b/tests/Unit/RendersMetricsTest.php @@ -75,3 +75,30 @@ function labelledSeries(): array it('formats bytes', function () { expect((metricsRenderer()->formatBytes())(2097152))->toBe('2 MB'); }); + +it('starts and ends the trend at the first and last intervals with a sample', function () { + $renderer = metricsRenderer(); + + $series = MetricSeries::fromApiResponse([ + 'data' => [ + ['x' => '2026-08-14T12:00:00.000000Z', 'y' => null], + ['x' => '2026-08-14T12:05:00.000000Z', 'y' => 2], + ['x' => '2026-08-14T12:10:00.000000Z', 'y' => 5], + ['x' => '2026-08-14T12:15:00.000000Z', 'y' => null], + ], + ]); + + [$trend] = $renderer->seriesLines($series, $renderer->formatCount()); + + expect($trend[1])->toBe('2 โ†’ 5'); +}); + +it('shows no data for a series with no samples at all', function () { + $renderer = metricsRenderer(); + + $series = MetricSeries::fromApiResponse([ + 'data' => [['x' => '2026-08-14T12:00:00.000000Z', 'y' => null]], + ]); + + expect($renderer->seriesLines($series, $renderer->formatCount()))->toBe([['No data']]); +}); From d53831d68dd59e011ec7df2c38056fe7809970a6 Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Fri, 20 Nov 2026 16:48:51 -0500 Subject: [PATCH 2/3] no need for http auth to have two flags --- app/Commands/EnvironmentUpdate.php | 44 +++++------------ tests/Feature/EnvironmentUpdateTest.php | 48 +++++++++++++------ .../InteractiveEnvironmentUpdateCommand.php | 32 +++++++++++++ 3 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 tests/Fixtures/InteractiveEnvironmentUpdateCommand.php diff --git a/app/Commands/EnvironmentUpdate.php b/app/Commands/EnvironmentUpdate.php index d4d624c7..cc2758da 100644 --- a/app/Commands/EnvironmentUpdate.php +++ b/app/Commands/EnvironmentUpdate.php @@ -42,8 +42,7 @@ class EnvironmentUpdate extends BaseCommand {--uses-deploy-hook= : Enable the deploy hook} {--uses-vanity-domain= : Serve the Laravel Cloud domain} {--uses-octane= : Uses Octane} - {--uses-http-basic-auth= : Require visitors to sign in as `admin` with --http-basic-auth-password} - {--http-basic-auth-password= : HTTP basic auth password (required when turning basic auth on)} + {--http-basic-auth-password= : Require visitors to sign in as `admin` with this password; pass an empty value to turn basic auth off} {--uses-purge-edge-cache-on-deploy= : Purge the edge cache on deploy} {--timeout= : Request timeout in seconds (5-60)} {--shutdown-timeout= : Seconds to keep the previous deployment running (1-600)} @@ -98,6 +97,9 @@ public function handle() protected function updateEnvironment(Environment $environment): Environment { + // The API turns basic auth on only alongside a password, so one option covers both: a password turns it on, empty turns it off. + $httpBasicAuthPassword = $this->form()->get('http_basic_auth_password'); + spin( fn () => $this->client->environments()->update( new UpdateEnvironmentRequestData( @@ -132,8 +134,8 @@ protected function updateEnvironment(Environment $environment): Environment responseHeadersHsts: $this->hsts($environment), firewallBlockPath: $this->form()->boolean('firewall_block_path'), firewallBrowserIntegrityCheck: $this->form()->boolean('firewall_browser_integrity_check'), - usesHttpBasicAuth: $this->form()->boolean('uses_http_basic_auth'), - httpBasicAuthPassword: $this->httpBasicAuthPassword(), + usesHttpBasicAuth: $httpBasicAuthPassword === null ? null : filled($httpBasicAuthPassword), + httpBasicAuthPassword: filled($httpBasicAuthPassword) ? $httpBasicAuthPassword : null, ), ), 'Updating environment...', @@ -142,24 +144,6 @@ protected function updateEnvironment(Environment $environment): Environment return $this->client->environments()->get($environment->id); } - /** - * The API wants a password exactly when basic auth is being turned on, so turning it on asks for one. - */ - protected function httpBasicAuthPassword(): ?string - { - $password = $this->form()->get('http_basic_auth_password'); - - if (filled($password) || $this->form()->boolean('uses_http_basic_auth') !== true) { - return $password; - } - - if (! $this->isInteractive()) { - $this->failAndExit('Provide --http-basic-auth-password to turn on HTTP basic auth.'); - } - - return $this->form()->prompt('http_basic_auth_password')->value(); - } - protected function clearableInteger(string $key): int|string|null { $value = $this->form()->get($key); @@ -291,18 +275,15 @@ protected function defineFields(Environment $environment): void $this->defineBoolean('uses_deploy_hook', 'Deploy hook', $environment->usesDeployHook); $this->defineBoolean('uses_vanity_domain', 'Vanity domain', null); $this->defineBoolean('uses_octane', 'Octane', $environment->usesOctane); - $this->defineBoolean('uses_http_basic_auth', 'HTTP basic auth', $environment->usesHttpBasicAuth); - $this->form()->define( 'http_basic_auth_password', fn ($resolver) => $resolver->fromInput( - fn ($value) => password( - label: 'HTTP basic auth password', - required: true, - hint: 'Visitors sign in as `admin`', - ), + fn ($value) => confirm(label: 'HTTP basic auth?', default: $environment->usesHttpBasicAuth) + ? password(label: 'HTTP basic auth password', required: true, hint: 'Visitors sign in as `admin`') + : '', ), - )->setLabel('HTTP basic auth password'); + )->setPreviousValue(var_export($environment->usesHttpBasicAuth, true))->setLabel('HTTP basic auth'); + $this->defineBoolean('uses_purge_edge_cache_on_deploy', 'Purge edge cache on deploy', null); $this->defineNumber('timeout', 'Timeout (seconds)', 5, 60, $environment->timeout); @@ -585,8 +566,7 @@ protected function collectDataAndUpdate(Environment $environment): Environment { $selection = multiselect( label: 'What do you want to update?', - // The password is asked for when basic auth is turned on, never on its own. - options: collect($this->form()->defined())->reject(fn ($field) => $field->key === 'http_basic_auth_password')->mapWithKeys(fn ($field, $key) => [ + options: collect($this->form()->defined())->mapWithKeys(fn ($field, $key) => [ $field->key => $field->label(), ])->toArray(), scroll: 15, diff --git a/tests/Feature/EnvironmentUpdateTest.php b/tests/Feature/EnvironmentUpdateTest.php index 212c1e22..dddfd19d 100644 --- a/tests/Feature/EnvironmentUpdateTest.php +++ b/tests/Feature/EnvironmentUpdateTest.php @@ -7,9 +7,12 @@ use App\Client\Resources\Meta\GetOrganizationRequest; use App\ConfigRepository; use Illuminate\Support\Facades\Artisan; +use Laravel\Prompts\Key; +use Laravel\Prompts\Prompt; use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; use Saloon\Http\PendingRequest; +use Tests\Fixtures\InteractiveEnvironmentUpdateCommand; beforeEach(function () { $this->mockConfig = Mockery::mock(ConfigRepository::class); @@ -209,11 +212,8 @@ function updateEnvironment(array $options): void expect($this->sent->body)->toBe(['post_deploy_command' => 'php artisan queue:restart']); }); -it('sends HTTP basic auth with its password', function () { - updateEnvironment([ - '--uses-http-basic-auth' => 'true', - '--http-basic-auth-password' => 'hunter2', - ]); +it('turns HTTP basic auth on with a password', function () { + updateEnvironment(['--http-basic-auth-password' => 'hunter2']); expect($this->sent->body)->toBe([ 'uses_http_basic_auth' => true, @@ -221,20 +221,40 @@ function updateEnvironment(array $options): void ]); }); -it('turns HTTP basic auth off without a password', function () { - updateEnvironment(['--uses-http-basic-auth' => 'false']); +it('turns HTTP basic auth off with an empty password', function () { + updateEnvironment(['--http-basic-auth-password' => '']); expect($this->sent->body)->toBe(['uses_http_basic_auth' => false]); }); -it('needs a password to turn HTTP basic auth on non-interactively', function () { - $this->artisan('environment:update', [ - 'environment' => 'env-1', - '--uses-http-basic-auth' => 'true', - '--no-interaction' => true, - ])->assertFailed(); +it('asks for a password when HTTP basic auth is turned on interactively', function () { + Artisan::registerCommand(new InteractiveEnvironmentUpdateCommand); - expect($this->sent->body)->toBeNull(); + Prompt::fake([ + ...array_fill(0, 14, Key::DOWN), Key::SPACE, Key::ENTER, + Key::ENTER, + 'h', 'u', 'n', 't', 'e', 'r', '2', Key::ENTER, + ]); + + $this->artisan('test:interactive-environment-update', ['environment' => 'env-1'])->assertSuccessful(); + + expect($this->sent->body)->toBe([ + 'uses_http_basic_auth' => true, + 'http_basic_auth_password' => 'hunter2', + ]); +}); + +it('turns HTTP basic auth off interactively without asking for a password', function () { + Artisan::registerCommand(new InteractiveEnvironmentUpdateCommand); + + Prompt::fake([ + ...array_fill(0, 14, Key::DOWN), Key::SPACE, Key::ENTER, + 'n', Key::ENTER, + ]); + + $this->artisan('test:interactive-environment-update', ['environment' => 'env-1'])->assertSuccessful(); + + expect($this->sent->body)->toBe(['uses_http_basic_auth' => false]); }); it('exposes the post-deploy command, HTTP basic auth and internal networking', function () { diff --git a/tests/Fixtures/InteractiveEnvironmentUpdateCommand.php b/tests/Fixtures/InteractiveEnvironmentUpdateCommand.php new file mode 100644 index 00000000..6432352e --- /dev/null +++ b/tests/Fixtures/InteractiveEnvironmentUpdateCommand.php @@ -0,0 +1,32 @@ +signature = str_replace('environment:update', 'test:interactive-environment-update', $this->signature); + + parent::__construct(); + } + + protected function isInteractive(): bool + { + return true; + } + + protected function configurePrompts(InputInterface $input): void + { + parent::configurePrompts($input); + + Prompt::interactive(true); + } +} From d05f61731a58bf629237cd1286c442935c656935 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 11:14:09 +0000 Subject: [PATCH 3/3] Attach a model gateway key to an environment environment:update takes --model-gateway-key-id (empty string to detach), and the Environment DTO exposes modelGatewayKeyId from the modelGatewayKey relationship, which environment fetches now include. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01T1Sq4XemtA7LBFDSeyGK7H --- .../Requests/UpdateEnvironmentRequestData.php | 3 ++ app/Client/Resources/EnvironmentsResource.php | 1 + app/Commands/EnvironmentUpdate.php | 15 +++++++++ app/Dto/Environment.php | 5 +++ tests/Feature/EnvironmentUpdateTest.php | 31 ++++++++++++++++++- 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/app/Client/Requests/UpdateEnvironmentRequestData.php b/app/Client/Requests/UpdateEnvironmentRequestData.php index 6917eed7..5b6b09d2 100644 --- a/app/Client/Requests/UpdateEnvironmentRequestData.php +++ b/app/Client/Requests/UpdateEnvironmentRequestData.php @@ -26,6 +26,8 @@ public function __construct( public readonly ?string $databaseSchemaId = null, public readonly ?string $cacheId = null, public readonly ?string $websocketApplicationId = null, + /** @var string|null '' detaches it */ + public readonly ?string $modelGatewayKeyId = null, public readonly ?bool $usesOctane = null, public readonly ?int $sleepTimeout = null, /** @var int|string|null '' disables it */ @@ -74,6 +76,7 @@ public function toRequestData(): array 'database_schema_id' => $this->databaseSchemaId, 'cache_id' => $this->cacheId, 'websocket_application_id' => $this->websocketApplicationId, + 'model_gateway_key_id' => $this->modelGatewayKeyId, 'uses_octane' => $this->usesOctane, 'sleep_timeout' => $this->sleepTimeout, 'hibernation_wake_up_interval' => $this->hibernationWakeUpInterval, diff --git a/app/Client/Resources/EnvironmentsResource.php b/app/Client/Resources/EnvironmentsResource.php index b41dc6f4..731ba0c5 100644 --- a/app/Client/Resources/EnvironmentsResource.php +++ b/app/Client/Resources/EnvironmentsResource.php @@ -172,6 +172,7 @@ public function withDefaultIncludes(): static 'database', 'cache', 'websocketApplication', + 'modelGatewayKey', 'buckets', ); } diff --git a/app/Commands/EnvironmentUpdate.php b/app/Commands/EnvironmentUpdate.php index bd5b6d80..7c43b9af 100644 --- a/app/Commands/EnvironmentUpdate.php +++ b/app/Commands/EnvironmentUpdate.php @@ -63,6 +63,7 @@ class EnvironmentUpdate extends BaseCommand {--database-id= : Database ID to attach (empty string to detach)} {--cache-id= : Cache ID to attach (empty string to detach)} {--websocket-application-id= : WebSocket application ID to attach (empty string to detach)} + {--model-gateway-key-id= : Model gateway key ID to attach, exposed as LARAVEL_CLOUD_MODEL_GATEWAY_API_KEY from the next deployment (empty string to detach)} {--bucket-keys= : Bucket keys to attach as comma-separated KEY_ID:DISK entries, with :default on at most one (fk-1:local:default,fk-2:public). Replaces every attached bucket; empty string to detach all} {--force : Force update without confirmation}'; @@ -126,6 +127,7 @@ protected function updateEnvironment(Environment $environment): Environment databaseSchemaId: $this->form()->get('database_id'), cacheId: $this->form()->get('cache_id'), websocketApplicationId: $this->form()->get('websocket_application_id'), + modelGatewayKeyId: $this->form()->get('model_gateway_key_id'), usesOctane: $this->form()->boolean('uses_octane'), sleepTimeout: $this->form()->integer('sleep_timeout'), hibernationWakeUpInterval: $this->clearableInteger('hibernation_wake_up_interval'), @@ -433,6 +435,19 @@ protected function defineFields(Environment $environment): void 'websocket-application-id', )->setPreviousValue($environment->websocketApplicationId ?? ''); + // The API has no endpoint that lists model gateway keys, so there is nothing to pick from. + $this->form()->define( + 'model_gateway_key_id', + fn ($resolver) => $resolver->fromInput( + fn ($value) => text( + label: 'Model gateway key ID', + default: $value ?? $environment->modelGatewayKeyId ?? '', + hint: 'Leave empty to detach', + ), + ), + 'model-gateway-key-id', + )->setPreviousValue($environment->modelGatewayKeyId ?? '')->setLabel('Model gateway key'); + // Named after the API field so a validation error on filesystem_keys.* asks for the buckets again. $this->form()->define( 'filesystem_keys', diff --git a/app/Dto/Environment.php b/app/Dto/Environment.php index fa4ee0b3..0a71809b 100644 --- a/app/Dto/Environment.php +++ b/app/Dto/Environment.php @@ -51,6 +51,7 @@ public function __construct( public readonly ?string $databaseSchemaId = null, public readonly ?string $cacheId = null, public readonly ?string $websocketApplicationId = null, + public readonly ?string $modelGatewayKeyId = null, /** IDs of the attached buckets. The API does not say which key or disk each one uses. */ public readonly array $bucketIds = [], #[DataCollectionOf(Secret::class)] @@ -163,6 +164,10 @@ public static function createFromResponse(array $response): self $transformed['websocketApplicationId'] = $relationships['websocketApplication']['data']['id']; } + if (isset($relationships['modelGatewayKey']['data']['id'])) { + $transformed['modelGatewayKeyId'] = $relationships['modelGatewayKey']['data']['id']; + } + if (isset($relationships['buckets']['data'])) { $transformed['bucketIds'] = array_column($relationships['buckets']['data'], 'id'); } diff --git a/tests/Feature/EnvironmentUpdateTest.php b/tests/Feature/EnvironmentUpdateTest.php index dddfd19d..3c99defb 100644 --- a/tests/Feature/EnvironmentUpdateTest.php +++ b/tests/Feature/EnvironmentUpdateTest.php @@ -47,12 +47,17 @@ ], ], 'relationships' => [ 'application' => ['data' => ['id' => 'app-123', 'type' => 'applications']], + 'modelGatewayKey' => ['data' => ['id' => 'mgk-1', 'type' => 'model_gateway_keys']], ]]), 'included' => [createApplicationResponse()]]; MockClient::global([ GetOrganizationRequest::class => MockResponse::make(organizationResponse(), 200), GetApplicationRequest::class => MockResponse::make(['data' => createApplicationResponse()], 200), - GetEnvironmentRequest::class => MockResponse::make($environment, 200), + GetEnvironmentRequest::class => function (PendingRequest $request) use ($environment) { + $this->sent->include = $request->query()->get('include'); + + return MockResponse::make($environment, 200); + }, UpdateEnvironmentRequest::class => function (PendingRequest $request) use ($environment) { $this->sent->body = $request->body()->all(); @@ -276,3 +281,27 @@ function updateEnvironment(array $options): void ], ]); }); + +it('attaches a model gateway key', function () { + updateEnvironment(['--model-gateway-key-id' => 'mgk-2']); + + expect($this->sent->body)->toBe(['model_gateway_key_id' => 'mgk-2']); +}); + +it('detaches the model gateway key with an empty string', function () { + updateEnvironment(['--model-gateway-key-id' => '']); + + expect($this->sent->body)->toBe(['model_gateway_key_id' => '']); +}); + +it('exposes the attached model gateway key', function () { + Artisan::call('environment:get', [ + 'environment' => 'env-1', + '--json' => true, + '--fields' => 'modelGatewayKeyId', + '--no-interaction' => true, + ]); + + expect(json_decode(Artisan::output(), true))->toBe(['modelGatewayKeyId' => 'mgk-1']) + ->and(explode(',', $this->sent->include))->toContain('modelGatewayKey'); +});