2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../../types/option';
|
|
|
|
import { IUnleashServices } from '../../types/services';
|
|
|
|
import StrategyService from '../../services/strategy-service';
|
|
|
|
import { Logger } from '../../logger';
|
2021-09-14 19:58:48 +02:00
|
|
|
import Controller from '../controller';
|
|
|
|
import { extractUsername } from '../../util/extract-user';
|
|
|
|
import {
|
2018-12-19 10:36:56 +01:00
|
|
|
DELETE_STRATEGY,
|
|
|
|
CREATE_STRATEGY,
|
|
|
|
UPDATE_STRATEGY,
|
2022-06-23 08:10:20 +02:00
|
|
|
NONE,
|
2021-09-14 19:58:48 +02:00
|
|
|
} from '../../types/permissions';
|
2022-04-28 12:40:38 +02:00
|
|
|
import { Request, Response } from 'express';
|
|
|
|
import { IAuthRequest } from '../unleash-types';
|
2022-06-23 08:10:20 +02:00
|
|
|
import { OpenApiService } from '../../services/openapi-service';
|
2022-07-01 08:06:33 +02:00
|
|
|
import { emptyResponse } from '../../openapi/util/standard-responses';
|
|
|
|
import { createRequestSchema } from '../../openapi/util/create-request-schema';
|
|
|
|
import { createResponseSchema } from '../../openapi/util/create-response-schema';
|
2022-06-23 08:10:20 +02:00
|
|
|
import {
|
|
|
|
strategySchema,
|
|
|
|
StrategySchema,
|
|
|
|
} from '../../openapi/spec/strategy-schema';
|
|
|
|
import {
|
|
|
|
strategiesSchema,
|
|
|
|
StrategiesSchema,
|
|
|
|
} from '../../openapi/spec/strategies-schema';
|
|
|
|
import { UpsertStrategySchema } from '../../openapi/spec/upsert-strategy-schema';
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2017-06-28 10:20:22 +02:00
|
|
|
const version = 1;
|
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
class StrategyController extends Controller {
|
2021-04-22 10:07:10 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private strategyService: StrategyService;
|
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
2022-06-23 08:10:20 +02:00
|
|
|
{
|
|
|
|
strategyService,
|
|
|
|
openApiService,
|
|
|
|
}: Pick<IUnleashServices, 'strategyService' | 'openApiService'>,
|
2021-04-22 10:07:10 +02:00
|
|
|
) {
|
2018-12-19 14:50:01 +01:00
|
|
|
super(config);
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = config.getLogger('/admin-api/strategy.js');
|
2021-01-18 12:32:19 +01:00
|
|
|
this.strategyService = strategyService;
|
2022-06-23 08:10:20 +02:00
|
|
|
this.openApiService = openApiService;
|
2018-12-05 21:07:45 +01:00
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '',
|
|
|
|
handler: this.getAllStrategies,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Strategies'],
|
2022-06-23 08:10:20 +02:00
|
|
|
operationId: 'getAllStrategies',
|
|
|
|
responses: {
|
|
|
|
200: createResponseSchema('strategiesSchema'),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '/:name',
|
|
|
|
handler: this.getStrategy,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Strategies'],
|
2022-06-23 08:10:20 +02:00
|
|
|
operationId: 'getStrategy',
|
|
|
|
responses: { 200: createResponseSchema('strategySchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'delete',
|
|
|
|
path: '/:name',
|
|
|
|
handler: this.removeStrategy,
|
|
|
|
permission: DELETE_STRATEGY,
|
|
|
|
acceptAnyContentType: true,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Strategies'],
|
2022-06-23 08:10:20 +02:00
|
|
|
operationId: 'removeStrategy',
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '',
|
|
|
|
handler: this.createStrategy,
|
|
|
|
permission: CREATE_STRATEGY,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Strategies'],
|
2022-06-23 08:10:20 +02:00
|
|
|
operationId: 'createStrategy',
|
|
|
|
requestBody: createRequestSchema('upsertStrategySchema'),
|
|
|
|
responses: { 201: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'put',
|
|
|
|
path: '/:strategyName',
|
|
|
|
handler: this.updateStrategy,
|
|
|
|
permission: UPDATE_STRATEGY,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Strategies'],
|
2022-06-23 08:10:20 +02:00
|
|
|
operationId: 'updateStrategy',
|
|
|
|
requestBody: createRequestSchema('upsertStrategySchema'),
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/:strategyName/deprecate',
|
|
|
|
handler: this.deprecateStrategy,
|
|
|
|
permission: UPDATE_STRATEGY,
|
|
|
|
acceptAnyContentType: true,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Strategies'],
|
2022-06-23 08:10:20 +02:00
|
|
|
operationId: 'deprecateStrategy',
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/:strategyName/reactivate',
|
|
|
|
handler: this.reactivateStrategy,
|
|
|
|
permission: UPDATE_STRATEGY,
|
|
|
|
acceptAnyContentType: true,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Strategies'],
|
2022-06-23 08:10:20 +02:00
|
|
|
operationId: 'reactivateStrategy',
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2017-06-28 10:20:22 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
async getAllStrategies(
|
|
|
|
req: Request,
|
|
|
|
res: Response<StrategiesSchema>,
|
|
|
|
): Promise<void> {
|
|
|
|
const strategies = await this.strategyService.getStrategies();
|
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
strategiesSchema.$id,
|
|
|
|
{ version, strategies },
|
|
|
|
);
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
async getStrategy(
|
|
|
|
req: Request,
|
|
|
|
res: Response<StrategySchema>,
|
|
|
|
): Promise<void> {
|
|
|
|
const strategy = await this.strategyService.getStrategy(
|
|
|
|
req.params.name,
|
|
|
|
);
|
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
strategySchema.$id,
|
|
|
|
strategy,
|
|
|
|
);
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
2017-06-28 21:10:43 +02:00
|
|
|
|
2022-04-28 12:40:38 +02:00
|
|
|
async removeStrategy(req: IAuthRequest, res: Response): Promise<void> {
|
2018-12-05 21:07:45 +01:00
|
|
|
const strategyName = req.params.name;
|
2021-09-14 19:58:48 +02:00
|
|
|
const userName = extractUsername(req);
|
2017-06-28 21:10:43 +02:00
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
await this.strategyService.removeStrategy(strategyName, userName);
|
|
|
|
res.status(200).end();
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
async createStrategy(
|
|
|
|
req: IAuthRequest<unknown, UpsertStrategySchema>,
|
|
|
|
res: Response<void>,
|
|
|
|
): Promise<void> {
|
2021-09-14 19:58:48 +02:00
|
|
|
const userName = extractUsername(req);
|
2022-06-23 08:10:20 +02:00
|
|
|
|
|
|
|
await this.strategyService.createStrategy(req.body, userName);
|
|
|
|
res.status(201).end();
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
async updateStrategy(
|
|
|
|
req: IAuthRequest<unknown, UpsertStrategySchema>,
|
|
|
|
res: Response<void>,
|
|
|
|
): Promise<void> {
|
2021-09-14 19:58:48 +02:00
|
|
|
const userName = extractUsername(req);
|
2022-06-23 08:10:20 +02:00
|
|
|
|
|
|
|
await this.strategyService.updateStrategy(req.body, userName);
|
|
|
|
res.status(200).end();
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
2021-01-21 13:47:08 +01:00
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
async deprecateStrategy(
|
|
|
|
req: IAuthRequest,
|
|
|
|
res: Response<void>,
|
|
|
|
): Promise<void> {
|
2021-09-14 19:58:48 +02:00
|
|
|
const userName = extractUsername(req);
|
2021-01-21 13:47:08 +01:00
|
|
|
const { strategyName } = req.params;
|
2022-06-23 08:10:20 +02:00
|
|
|
|
2021-01-22 10:03:01 +01:00
|
|
|
if (strategyName === 'default') {
|
|
|
|
res.status(403).end();
|
2022-06-23 08:10:20 +02:00
|
|
|
return;
|
2021-01-21 13:47:08 +01:00
|
|
|
}
|
2022-06-23 08:10:20 +02:00
|
|
|
|
|
|
|
await this.strategyService.deprecateStrategy(strategyName, userName);
|
|
|
|
res.status(200).end();
|
2021-01-21 13:47:08 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 08:10:20 +02:00
|
|
|
async reactivateStrategy(
|
|
|
|
req: IAuthRequest,
|
|
|
|
res: Response<void>,
|
|
|
|
): Promise<void> {
|
2021-09-14 19:58:48 +02:00
|
|
|
const userName = extractUsername(req);
|
2021-01-21 13:47:08 +01:00
|
|
|
const { strategyName } = req.params;
|
2022-06-23 08:10:20 +02:00
|
|
|
|
|
|
|
await this.strategyService.reactivateStrategy(strategyName, userName);
|
|
|
|
res.status(200).end();
|
2021-01-21 13:47:08 +01:00
|
|
|
}
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
2022-06-23 08:10:20 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export default StrategyController;
|