Skip to content

Proof of concept for licensing #3008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add licenseXService
  • Loading branch information
presleyp committed Jul 15, 2022
commit ead0ab44619bfff1d968916b67d44fc6ede9fbe5
14 changes: 14 additions & 0 deletions site/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ export interface ReconnectingPTYRequest {
export type WorkspaceBuildTransition = "start" | "stop" | "delete"

export type Message = { message: string }

export type LicensePermission = "audit" | "createUser" | "createOrg"

export type LicenseFeatures = Record<LicensePermission, {
entitled: boolean
enabled: boolean
limit?: number
actual?: number
}>

export type LicenseData = {
features: LicenseFeatures
warnings: string[]
}
3 changes: 3 additions & 0 deletions site/src/xServices/StateContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useNavigate } from "react-router"
import { ActorRefFrom } from "xstate"
import { authMachine } from "./auth/authXService"
import { buildInfoMachine } from "./buildInfo/buildInfoXService"
import { licenseMachine } from "./license/licenseXService"
import { siteRolesMachine } from "./roles/siteRolesXService"
import { usersMachine } from "./users/usersXService"

Expand All @@ -12,6 +13,7 @@ interface XServiceContextType {
buildInfoXService: ActorRefFrom<typeof buildInfoMachine>
usersXService: ActorRefFrom<typeof usersMachine>
siteRolesXService: ActorRefFrom<typeof siteRolesMachine>
licenseXService: ActorRefFrom<typeof licenseMachine>
}

/**
Expand Down Expand Up @@ -39,6 +41,7 @@ export const XServiceProvider: React.FC = ({ children }) => {
usersMachine.withConfig({ actions: { redirectToUsersPage } }),
),
siteRolesXService: useInterpret(siteRolesMachine),
licenseXService: useInterpret(licenseMachine)
}}
>
{children}
Expand Down
24 changes: 24 additions & 0 deletions site/src/xServices/license/licenseSelectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { State } from "xstate"
import { LicensePermission } from "../../api/types"
import { LicenseContext, LicenseEvent } from "./licenseXService"
type LicenseState = State<LicenseContext, LicenseEvent>

export const selectLicenseVisibility = (state: LicenseState): Record<LicensePermission, boolean> => {
const features = state.context.licenseData.features
const featureNames = Object.keys(features) as LicensePermission[]
const visibilityPairs = featureNames.map((feature: LicensePermission) => {
return [feature, features[feature].enabled]
})
return Object.fromEntries(visibilityPairs)
}

export const selectLicenseEntitlement = (state: LicenseState): Record<LicensePermission, boolean> => {
const features = state.context.licenseData.features
const featureNames = Object.keys(features) as LicensePermission[]
const permissionPairs = featureNames.map((feature: LicensePermission) => {
const { entitled, limit, actual } = features[feature]
const limitCompliant = limit && actual && limit >= actual
return [feature, entitled && limitCompliant]
})
return Object.fromEntries(permissionPairs)
}
94 changes: 94 additions & 0 deletions site/src/xServices/license/licenseXService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { assign, createMachine } from "xstate"
import * as API from "../../api/api"
import { LicenseData } from "../../api/types"

export const Language = {
getLicenseError: "Error getting license information.",
}

/* deserves more thought but this is one way to handle unlicensed cases */
const defaultLicenseData = {
warnings: [],
features: {
audit: {
enabled: false,
entitled: false
},
createUser: {
enabled: true,
entitled: true,
limit: 0
},
createOrg: {
enabled: false,
entitled: false
}
}
}

export type LicenseContext = {
licenseData: LicenseData
getLicenseError?: Error | unknown
}

export type LicenseEvent = {
type: "GET_LICENSE_DATA"
}

export const licenseMachine = createMachine(
{
id: "licenseMachine",
initial: "idle",
schema: {
context: {} as LicenseContext,
events: {} as LicenseEvent,
services: {
getLicenseData: {
data: {} as LicenseData,
},
},
},
tsTypes: {} as import("./licenseXService.typegen").Typegen0,
context: {
licenseData: defaultLicenseData
},
states: {
idle: {
on: {
GET_LICENSE_DATA: "gettingLicenseData",
},
},
gettingLicenseData: {
entry: "clearGetLicenseError",
invoke: {
id: "getLicenseData",
src: "getLicenseData",
onDone: {
target: "idle",
actions: ["assignLicenseData"],
},
onError: {
target: "idle",
actions: ["assignGetLicenseError"],
},
},
},
},
},
{
actions: {
assignLicenseData: assign({
licenseData: (_, event) => event.data,
}),
assignGetLicenseError: assign({
getLicenseError: (_, event) => event.data,
}),
clearGetLicenseError: assign({
getLicenseError: (_) => undefined,
}),
},
services: {
getLicenseData: () => API.getLicenseData(),
},
},
)