Update license endpoints to provide consistent pattern for offline license and license key (create, read, delete)

This commit is contained in:
Rory Powell 2023-07-06 20:46:25 +01:00
parent 6aeb31c355
commit 0e80766125
4 changed files with 89 additions and 21 deletions

View File

@ -3,3 +3,4 @@ export * from "./auditLogs"
export * from "./events"
export * from "./configs"
export * from "./scim"
export * from "./license"

View File

@ -0,0 +1,19 @@
// LICENSE KEY
export interface ActivateLicenseKeyRequest {
licenseKey: string
}
export interface GetLicenseKeyResponse {
licenseKey: string,
}
// OFFLINE LICENSE
export interface ActivateOfflineLicenseRequest {
offlineLicense: string
}
export interface GetOfflineLicenseResponse {
offlineLicense: string
}

View File

@ -1,33 +1,66 @@
import { licensing, quotas } from "@budibase/pro"
import {
ActivateLicenseKeyRequest,
ActivateOfflineLicenseRequest,
GetLicenseKeyResponse,
GetOfflineLicenseResponse,
UserCtx,
} from "@budibase/types"
export const activate = async (ctx: any) => {
// LICENSE KEY
export async function activateLicenseKey(ctx: UserCtx<ActivateLicenseKeyRequest>) {
const { licenseKey } = ctx.request.body
if (!licenseKey) {
ctx.throw(400, "licenseKey is required")
}
await licensing.activateLicenseKey(licenseKey)
ctx.status = 200
}
export async function getLicenseKey(ctx: UserCtx<void, GetLicenseKeyResponse>) {
const licenseKey = await licensing.getLicenseKey()
if (licenseKey) {
ctx.body = { licenseKey: "*" }
ctx.status = 200
} else {
ctx.status = 404
}
}
export async function deleteLicenseKey(ctx: UserCtx<void, void>) {
await licensing.deleteLicenseKey()
ctx.status = 204
}
// OFFLINE LICENSE
export async function activateOfflineLicense(ctx: UserCtx<ActivateOfflineLicenseRequest>) {
const { offlineLicense } = ctx.request.body
await licensing.activateOfflineLicense(offlineLicense)
ctx.status = 200
}
export async function getOfflineLicense(ctx: UserCtx<void, GetOfflineLicenseResponse>) {
const offlineLicense = await licensing.getOfflineLicense()
if (offlineLicense) {
ctx.body = { offlineLicense: "*" }
ctx.status = 200
} else {
ctx.status = 404
}
}
export async function deleteOfflineLicense(ctx: UserCtx<void, void>) {
await licensing.deleteOfflineLicense()
ctx.status = 204
}
// LICENSES
export const refresh = async (ctx: any) => {
await licensing.cache.refresh()
ctx.status = 200
}
export const getInfo = async (ctx: any) => {
const licenseInfo = await licensing.getLicenseInfo()
if (licenseInfo) {
licenseInfo.licenseKey = "*"
ctx.body = licenseInfo
}
ctx.status = 200
}
export const deleteInfo = async (ctx: any) => {
await licensing.deleteLicenseInfo()
ctx.status = 200
}
// USAGE
export const getQuotaUsage = async (ctx: any) => {
ctx.body = await quotas.getQuotaUsage()

View File

@ -1,13 +1,28 @@
import Router from "@koa/router"
import * as controller from "../../controllers/global/license"
import { middleware } from "@budibase/backend-core"
import Joi from "joi"
const activateLicenseKeyValidator = middleware.joiValidator.body(Joi.object({
licenseKey: Joi.string().required(),
}).required())
const activateOfflineLicenseValidator = middleware.joiValidator.body(Joi.object({
offlineLicense: Joi.string().required(),
}).required())
const router: Router = new Router()
router
.post("/api/global/license/activate", controller.activate)
.post("/api/global/license/refresh", controller.refresh)
.get("/api/global/license/info", controller.getInfo)
.delete("/api/global/license/info", controller.deleteInfo)
.get("/api/global/license/usage", controller.getQuotaUsage)
// LICENSE KEY
.post("/api/global/license/key", activateLicenseKeyValidator, controller.activateLicenseKey)
.get("/api/global/license/key", controller.getLicenseKey)
.delete("/api/global/license/key", controller.deleteLicenseKey)
// OFFLINE LICENSE
.post("/api/global/license/offline", activateOfflineLicenseValidator, controller.activateOfflineLicense)
.get("/api/global/license/offline", controller.getOfflineLicense)
.delete("/api/global/license/offline", controller.deleteOfflineLicense)
export default router