-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server] Separate EntitlementServiceLicense from EntitlementServiceCh…
…argebee
- Loading branch information
Showing
8 changed files
with
123 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
components/server/ee/src/billing/entitlement-service-license.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { UserDB } from "@gitpod/gitpod-db/lib"; | ||
import { | ||
User, | ||
WorkspaceInstance, | ||
WorkspaceTimeoutDuration, | ||
WORKSPACE_TIMEOUT_DEFAULT_LONG, | ||
WORKSPACE_TIMEOUT_DEFAULT_SHORT, | ||
} from "@gitpod/gitpod-protocol"; | ||
import { LicenseEvaluator } from "@gitpod/licensor/lib"; | ||
import { Feature } from "@gitpod/licensor/lib/api"; | ||
import { inject, injectable } from "inversify"; | ||
import { EntitlementService } from "../../../src/billing/entitlement-service"; | ||
import { Config } from "../../../src/config"; | ||
import { MayStartWorkspaceResult } from "../user/eligibility-service"; | ||
|
||
@injectable() | ||
export class EntitlementServiceLicense implements EntitlementService { | ||
@inject(Config) protected readonly config: Config; | ||
@inject(UserDB) protected readonly userDb: UserDB; | ||
@inject(LicenseEvaluator) protected readonly licenseEvaluator: LicenseEvaluator; | ||
|
||
async mayStartWorkspace( | ||
user: User, | ||
date: Date, | ||
runningInstances: Promise<WorkspaceInstance[]>, | ||
): Promise<MayStartWorkspaceResult> { | ||
// if payment is not enabled users can start as many parallel workspaces as they want | ||
return { enoughCredits: true }; | ||
} | ||
|
||
async maySetTimeout(user: User, date: Date): Promise<boolean> { | ||
// when payment is disabled users can do everything | ||
return true; | ||
} | ||
|
||
async getDefaultWorkspaceTimeout(user: User, date: Date): Promise<WorkspaceTimeoutDuration> { | ||
const userCount = await this.userDb.getUserCount(true); | ||
|
||
// the self-hosted case | ||
if (!this.licenseEvaluator.isEnabled(Feature.FeatureSetTimeout, userCount)) { | ||
return WORKSPACE_TIMEOUT_DEFAULT_SHORT; | ||
} | ||
|
||
return WORKSPACE_TIMEOUT_DEFAULT_LONG; | ||
} | ||
|
||
async userGetsMoreResources(user: User): Promise<boolean> { | ||
// TODO(gpl) Not sure this makes sense, but it's the way it was before | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { User, WorkspaceInstance, WorkspaceTimeoutDuration } from "@gitpod/gitpod-protocol"; | ||
import { inject, injectable } from "inversify"; | ||
import { EntitlementService } from "../../../src/billing/entitlement-service"; | ||
import { Config } from "../../../src/config"; | ||
import { MayStartWorkspaceResult } from "../user/eligibility-service"; | ||
import { EntitlementServiceChargebee } from "./entitlement-service-chargebee"; | ||
import { EntitlementServiceLicense } from "./entitlement-service-license"; | ||
|
||
/** | ||
* The default implementation for the Enterprise Edition (EE). It decides based on config which ruleset to choose for each call. | ||
*/ | ||
@injectable() | ||
export class EntitlementServiceImpl implements EntitlementService { | ||
@inject(Config) protected readonly config: Config; | ||
@inject(EntitlementServiceChargebee) protected readonly etsChargebee: EntitlementServiceChargebee; | ||
@inject(EntitlementServiceLicense) protected readonly etsLicense: EntitlementServiceLicense; | ||
|
||
async mayStartWorkspace( | ||
user: User, | ||
date: Date, | ||
runningInstances: Promise<WorkspaceInstance[]>, | ||
): Promise<MayStartWorkspaceResult> { | ||
if (!this.config.enablePayment) { | ||
return await this.etsLicense.mayStartWorkspace(user, date, runningInstances); | ||
} | ||
return await this.etsChargebee.mayStartWorkspace(user, date, runningInstances); | ||
} | ||
|
||
async maySetTimeout(user: User, date: Date): Promise<boolean> { | ||
if (!this.config.enablePayment) { | ||
return await this.etsLicense.maySetTimeout(user, date); | ||
} | ||
return await this.etsChargebee.maySetTimeout(user, date); | ||
} | ||
|
||
async getDefaultWorkspaceTimeout(user: User, date: Date): Promise<WorkspaceTimeoutDuration> { | ||
if (!this.config.enablePayment) { | ||
return await this.etsLicense.getDefaultWorkspaceTimeout(user, date); | ||
} | ||
return await this.etsChargebee.getDefaultWorkspaceTimeout(user, date); | ||
} | ||
|
||
async userGetsMoreResources(user: User): Promise<boolean> { | ||
if (!this.config.enablePayment) { | ||
return await this.etsLicense.userGetsMoreResources(user); | ||
} | ||
return await this.etsChargebee.userGetsMoreResources(user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters