2022-06-10 10:04:56 +02:00
import { Request , Response } from 'express' ;
import Controller from '../controller' ;
import { IUnleashServices } from '../../types/services' ;
import { IUnleashConfig } from '../../types/option' ;
import EnvironmentService from '../../services/environment-service' ;
import { Logger } from '../../logger' ;
import { ADMIN , NONE } from '../../types/permissions' ;
import { OpenApiService } from '../../services/openapi-service' ;
2022-07-01 08:06:33 +02:00
import { createRequestSchema } from '../../openapi/util/create-request-schema' ;
import { createResponseSchema } from '../../openapi/util/create-response-schema' ;
2022-06-10 10:04:56 +02:00
import {
environmentsSchema ,
EnvironmentsSchema ,
} from '../../openapi/spec/environments-schema' ;
import {
environmentSchema ,
EnvironmentSchema ,
} from '../../openapi/spec/environment-schema' ;
import { SortOrderSchema } from '../../openapi/spec/sort-order-schema' ;
2023-04-04 15:45:34 +02:00
import {
emptyResponse ,
getStandardResponses ,
} from '../../openapi/util/standard-responses' ;
2022-11-11 11:24:56 +01:00
import {
environmentsProjectSchema ,
EnvironmentsProjectSchema ,
} from '../../openapi/spec/environments-project-schema' ;
2022-06-10 10:04:56 +02:00
interface EnvironmentParam {
name : string ;
}
2022-11-11 11:24:56 +01:00
interface ProjectParam {
projectId : string ;
}
2022-06-10 10:04:56 +02:00
export class EnvironmentsController extends Controller {
private logger : Logger ;
private openApiService : OpenApiService ;
private service : EnvironmentService ;
constructor (
config : IUnleashConfig ,
{
environmentService ,
openApiService ,
} : Pick < IUnleashServices , ' environmentService ' | ' openApiService ' > ,
) {
super ( config ) ;
this . logger = config . getLogger ( 'admin-api/environments-controller.ts' ) ;
this . openApiService = openApiService ;
this . service = environmentService ;
this . route ( {
method : 'get' ,
path : '' ,
handler : this.getAllEnvironments ,
permission : NONE ,
middleware : [
openApiService . validPath ( {
2022-08-12 11:37:57 +02:00
tags : [ 'Environments' ] ,
2023-04-04 15:45:34 +02:00
summary : 'Get all environments' ,
description :
'Retrieves all environments that exist in this Unleash instance.' ,
2022-06-10 10:04:56 +02:00
operationId : 'getAllEnvironments' ,
2023-04-04 15:45:34 +02:00
responses : {
200 : createResponseSchema ( 'environmentsSchema' ) ,
. . . getStandardResponses ( 401 , 403 ) ,
} ,
2022-06-10 10:04:56 +02:00
} ) ,
] ,
} ) ;
this . route ( {
method : 'get' ,
path : '/:name' ,
handler : this.getEnvironment ,
permission : NONE ,
middleware : [
openApiService . validPath ( {
2022-08-12 11:37:57 +02:00
tags : [ 'Environments' ] ,
2022-06-10 10:04:56 +02:00
operationId : 'getEnvironment' ,
2023-04-04 15:45:34 +02:00
summary : 'Get the environment with `name`' ,
description :
'Retrieves the environment with `name` if it exists in this Unleash instance' ,
2022-06-10 10:04:56 +02:00
responses : {
200 : createResponseSchema ( 'environmentSchema' ) ,
2023-04-04 15:45:34 +02:00
. . . getStandardResponses ( 401 , 403 , 404 ) ,
2022-06-10 10:04:56 +02:00
} ,
} ) ,
] ,
} ) ;
2022-11-11 11:24:56 +01:00
this . route ( {
method : 'get' ,
path : '/project/:projectId' ,
handler : this.getProjectEnvironments ,
permission : NONE ,
middleware : [
openApiService . validPath ( {
tags : [ 'Environments' ] ,
operationId : 'getProjectEnvironments' ,
2023-04-04 15:45:34 +02:00
summary : 'Get the environments available to a project' ,
description :
'Gets the environments that are available for this project. An environment is available for a project if enabled in the [project configuration](https://docs.getunleash.io/reference/environments#step-1-enable-new-environments-for-your-project)' ,
2022-11-11 11:24:56 +01:00
responses : {
200 : createResponseSchema ( 'environmentsProjectSchema' ) ,
2023-04-04 15:45:34 +02:00
. . . getStandardResponses ( 401 , 403 , 404 ) ,
2022-11-11 11:24:56 +01:00
} ,
} ) ,
] ,
} ) ;
2022-06-10 10:04:56 +02:00
this . route ( {
method : 'put' ,
path : '/sort-order' ,
handler : this.updateSortOrder ,
permission : ADMIN ,
middleware : [
openApiService . validPath ( {
2022-08-12 11:37:57 +02:00
tags : [ 'Environments' ] ,
2023-04-04 15:45:34 +02:00
summary : 'Update environment sort orders' ,
description :
'Updates sort orders for the named environments. Environments not specified are unaffected.' ,
2022-06-10 10:04:56 +02:00
operationId : 'updateSortOrder' ,
requestBody : createRequestSchema ( 'sortOrderSchema' ) ,
2023-04-04 15:45:34 +02:00
responses : {
200 : emptyResponse ,
. . . getStandardResponses ( 401 , 403 , 404 ) ,
} ,
2022-06-10 10:04:56 +02:00
} ) ,
] ,
} ) ;
this . route ( {
method : 'post' ,
path : '/:name/on' ,
acceptAnyContentType : true ,
handler : this.toggleEnvironmentOn ,
permission : ADMIN ,
middleware : [
openApiService . validPath ( {
2022-08-12 11:37:57 +02:00
tags : [ 'Environments' ] ,
2023-04-04 15:45:34 +02:00
summary : 'Toggle the environment with `name` on' ,
description :
'Makes it possible to enable this environment for a project. An environment must first be globally enabled using this endpoint before it can be enabled for a project' ,
2022-06-10 10:04:56 +02:00
operationId : 'toggleEnvironmentOn' ,
2023-04-04 15:45:34 +02:00
responses : {
204 : emptyResponse ,
. . . getStandardResponses ( 401 , 403 , 404 ) ,
} ,
2022-06-10 10:04:56 +02:00
} ) ,
] ,
} ) ;
this . route ( {
method : 'post' ,
path : '/:name/off' ,
acceptAnyContentType : true ,
handler : this.toggleEnvironmentOff ,
permission : ADMIN ,
middleware : [
openApiService . validPath ( {
2022-08-12 11:37:57 +02:00
tags : [ 'Environments' ] ,
2023-04-04 15:45:34 +02:00
summary : 'Toggle the environment with `name` off' ,
description :
'Removes this environment from the list of available environments for projects to use' ,
2022-06-10 10:04:56 +02:00
operationId : 'toggleEnvironmentOff' ,
2023-04-04 15:45:34 +02:00
responses : {
204 : emptyResponse ,
. . . getStandardResponses ( 401 , 403 , 404 ) ,
} ,
2022-06-10 10:04:56 +02:00
} ) ,
] ,
} ) ;
}
async getAllEnvironments (
req : Request ,
res : Response < EnvironmentsSchema > ,
) : Promise < void > {
this . openApiService . respondWithValidation (
200 ,
res ,
environmentsSchema . $id ,
{ version : 1 , environments : await this . service . getAll ( ) } ,
) ;
}
async updateSortOrder (
req : Request < unknown , unknown , SortOrderSchema > ,
res : Response ,
) : Promise < void > {
await this . service . updateSortOrder ( req . body ) ;
res . status ( 200 ) . end ( ) ;
}
async toggleEnvironmentOn (
req : Request < EnvironmentParam > ,
res : Response ,
) : Promise < void > {
const { name } = req . params ;
await this . service . toggleEnvironment ( name , true ) ;
res . status ( 204 ) . end ( ) ;
}
async toggleEnvironmentOff (
req : Request < EnvironmentParam > ,
res : Response ,
) : Promise < void > {
const { name } = req . params ;
await this . service . toggleEnvironment ( name , false ) ;
res . status ( 204 ) . end ( ) ;
}
async getEnvironment (
req : Request < EnvironmentParam > ,
res : Response < EnvironmentSchema > ,
) : Promise < void > {
this . openApiService . respondWithValidation (
200 ,
res ,
environmentSchema . $id ,
await this . service . get ( req . params . name ) ,
) ;
}
2022-11-11 11:24:56 +01:00
async getProjectEnvironments (
req : Request < ProjectParam > ,
res : Response < EnvironmentsProjectSchema > ,
) : Promise < void > {
this . openApiService . respondWithValidation (
200 ,
res ,
environmentsProjectSchema . $id ,
{
version : 1 ,
2023-05-05 13:32:44 +02:00
environments : ( await this . service . getProjectEnvironments (
2022-11-11 11:24:56 +01:00
req . params . projectId ,
2023-05-05 13:32:44 +02:00
) ) as any ,
2022-11-11 11:24:56 +01:00
} ,
) ;
}
2022-06-10 10:04:56 +02:00
}