'mount', 'attributes' => [ ...MountController::MOUNT_EXAMPLE['attributes'], 'mounted' => true, ], ]; private const array SERVER_MOUNT_LIST_EXAMPLE = [ 'object' => 'list', 'data' => [ self::SERVER_MOUNT_EXAMPLE, ], ]; private const array INSTALL_STATE_ERROR = [ 'errors' => [ [ 'code' => 'HttpException', 'status' => '403', 'detail' => 'Access to this resource is not allowed due to the current installation state.', ], ], ]; /** * List server mounts. * * @return array{object: string, data: list}>} */ #[Endpoint('List server mounts', 'Returns mount definitions that are eligible for a server, including whether each mount is attached.')] #[ScribeResponse(self::SERVER_MOUNT_LIST_EXAMPLE, description: 'Eligible server mounts returned.')] #[ScribeResponse(self::INSTALL_STATE_ERROR, status: 403, description: 'The server is not installed.')] public function index(GetServerMountsRequest $request, MountListService $listing, Server $server): array { $this->assertServerInstalled($server); $mounts = $listing->handle($server); $mountedIds = $server->mounts->pluck('id')->all(); $transformer = $this->getTransformer(MountTransformer::class); $data = array_values($mounts->map(fn (Mount $mount): array => array_merge($transformer->transform($mount), [ 'mounted' => in_array($mount->id, $mountedIds, true), ]))->all()); return [ 'object' => 'list', 'data' => array_map(fn (array $attributes): array => [ 'object' => Mount::RESOURCE_NAME, 'attributes' => $attributes, ], $data), ]; } /** * Create server mount. */ #[Endpoint('Attach server mount', 'Attaches an eligible mount definition to a server.')] #[ScribeResponse(status: 204, description: 'Mount attached.')] #[ScribeResponse(self::INSTALL_STATE_ERROR, status: 403, description: 'The server is not installed.')] public function store(StoreServerMountRequest $request, AttachesMountsToServers $attach, Server $server): Response { $this->assertServerInstalled($server); $mount = $attach->attach($server, JsonValueGuard::integer($request->validated('mount_id'))); Activity::event('admin:server.mount') ->subject($server, $mount) ->property('name', $mount->name) ->log(); return $this->returnNoContent(); } /** * Delete server mount. */ #[Endpoint('Detach server mount', 'Detaches a mount definition from a server.')] #[ScribeResponse(status: 204, description: 'Mount detached.')] #[ScribeResponse(self::INSTALL_STATE_ERROR, status: 403, description: 'The server is not installed.')] public function destroy(DeleteServerMountRequest $request, DetachesMountsFromServers $detach, Server $server, Mount $mount): Response { $this->assertServerInstalled($server); $detach->detach($server, $mount); Activity::event('admin:server.unmount') ->subject($server, $mount) ->property('name', $mount->name) ->log(); return $this->returnNoContent(); } private function assertServerInstalled(Server $server): void { throw_unless($server->isInstalled(), HttpException::class, Response::HTTP_FORBIDDEN, 'Access to this resource is not allowed due to the current installation state.'); } }