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'); +});