|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; |
| 8 | + |
| 9 | +export type WorkspaceClassesConfig = [WorkspaceClassConfig]; |
| 10 | + |
| 11 | +export interface WorkspaceClassConfig { |
| 12 | + // The technical string we use to identify the class with internally |
| 13 | + id: string; |
| 14 | + |
| 15 | + // Is the "default" class. The config is validated to only every have exactly _one_ default class. |
| 16 | + isDefault: boolean; |
| 17 | + |
| 18 | + // The string we display to users in the UI |
| 19 | + displayName: string; |
| 20 | + |
| 21 | + // Whether or not to: |
| 22 | + // - offer users this Workspace class for selection |
| 23 | + // - use this class to start workspaces with. If a user has a class marked like this configured and starts a workspace they get the default class instead. |
| 24 | + deprecated: boolean; |
| 25 | + |
| 26 | + // Marks this class to have special semantics |
| 27 | + marker?: { |
| 28 | + // Marks this class as the one that users marked with "GetMoreResources" receive |
| 29 | + moreResources: boolean; |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +export namespace WorkspaceClasses { |
| 34 | + /** |
| 35 | + * @param workspaceClasses |
| 36 | + * @return The WorkspaceClass ID of the first class that is marked with "moreResources" (and not deprecated). Falls back to "getDefaultId()". |
| 37 | + */ |
| 38 | + export function getMoreResourcesIdOrDefault(workspaceClasses: WorkspaceClassesConfig): string { |
| 39 | + const moreResources = workspaceClasses.filter((c) => !c.deprecated).find((c) => !!c.marker?.moreResources); |
| 40 | + if (moreResources) { |
| 41 | + return moreResources.id; |
| 42 | + } |
| 43 | + |
| 44 | + // fallback: default |
| 45 | + return getDefaultId(workspaceClasses); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param workspaceClasses |
| 50 | + * @return The WorkspaceClass ID of the "default" class |
| 51 | + */ |
| 52 | + export function getDefaultId(workspaceClasses: WorkspaceClassesConfig): string { |
| 53 | + validate(workspaceClasses); |
| 54 | + |
| 55 | + return workspaceClasses.filter((c) => !c.deprecated).find((c) => c.isDefault)!.id; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Checks that the given workspaceClass is: |
| 60 | + * - still configured |
| 61 | + * - not deprecated |
| 62 | + * If any of that is the case, it returns the default class |
| 63 | + * |
| 64 | + * @param workspaceClasses |
| 65 | + * @param previousWorkspaceClass |
| 66 | + */ |
| 67 | + export function getPreviousOrDefault( |
| 68 | + workspaceClasses: WorkspaceClassesConfig, |
| 69 | + previousWorkspaceClass: string | undefined, |
| 70 | + ): string { |
| 71 | + if (!previousWorkspaceClass) { |
| 72 | + return getDefaultId(workspaceClasses); |
| 73 | + } |
| 74 | + |
| 75 | + const config = workspaceClasses.find((c) => c.id === previousWorkspaceClass); |
| 76 | + if (!config) { |
| 77 | + log.error( |
| 78 | + `Found previous instance with workspace class '${previousWorkspaceClass}' which is no longer configured! Falling back to default class.`, |
| 79 | + { workspaceClasses }, |
| 80 | + ); |
| 81 | + return getDefaultId(workspaceClasses); |
| 82 | + } |
| 83 | + if (config.deprecated) { |
| 84 | + log.info( |
| 85 | + `Found previous instance with workspace class '${previousWorkspaceClass}' which is deprecated. Falling back to default class.`, |
| 86 | + { workspaceClasses }, |
| 87 | + ); |
| 88 | + return getDefaultId(workspaceClasses); |
| 89 | + } |
| 90 | + return config.id; |
| 91 | + } |
| 92 | + |
| 93 | + export function validate(workspaceClasses: WorkspaceClassesConfig): void { |
| 94 | + const defaultClasses = workspaceClasses |
| 95 | + .filter((c) => !c.deprecated) |
| 96 | + .map((c) => (c.isDefault ? 1 : 0)) |
| 97 | + .reduce((acc: number, isDefault: number) => (acc + isDefault) as number, 0); |
| 98 | + |
| 99 | + if (defaultClasses !== 1) { |
| 100 | + throw new Error( |
| 101 | + "Exactly one default workspace class needs to be configured:" + JSON.stringify(defaultClasses), |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments