-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
create.ts
93 lines (89 loc) · 2.78 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { schema } from '@kbn/config-schema';
import { IRouter } from '@kbn/core/server';
import { ActionResult, ActionsRequestHandlerContext } from '../types';
import { ILicenseState, validateEmptyStrings } from '../lib';
import { BASE_ACTION_API_PATH, RewriteRequestCase, RewriteResponseCase } from '../../common';
import { verifyAccessAndContext } from './verify_access_and_context';
import { CreateOptions } from '../actions_client';
export const bodySchema = schema.object({
name: schema.string({
validate: validateEmptyStrings,
meta: { description: 'The display name for the connector.' },
}),
connector_type_id: schema.string({
validate: validateEmptyStrings,
meta: { description: 'The type of connector.' },
}),
config: schema.recordOf(schema.string(), schema.any({ validate: validateEmptyStrings }), {
defaultValue: {},
}),
secrets: schema.recordOf(schema.string(), schema.any({ validate: validateEmptyStrings }), {
defaultValue: {},
}),
});
const rewriteBodyReq: RewriteRequestCase<CreateOptions['action']> = ({
connector_type_id: actionTypeId,
name,
config,
secrets,
}) => ({ actionTypeId, name, config, secrets });
const rewriteBodyRes: RewriteResponseCase<ActionResult> = ({
actionTypeId,
isPreconfigured,
isDeprecated,
isMissingSecrets,
isSystemAction,
...res
}) => ({
...res,
connector_type_id: actionTypeId,
is_preconfigured: isPreconfigured,
is_deprecated: isDeprecated,
is_missing_secrets: isMissingSecrets,
is_system_action: isSystemAction,
});
export const createActionRoute = (
router: IRouter<ActionsRequestHandlerContext>,
licenseState: ILicenseState
) => {
router.post(
{
path: `${BASE_ACTION_API_PATH}/connector/{id?}`,
options: {
access: 'public',
summary: 'Create a connector',
tags: ['oas-tag:connectors'],
},
validate: {
request: {
params: schema.maybe(
schema.object({
id: schema.maybe(schema.string()),
})
),
body: bodySchema,
},
response: {
200: {
description: 'Indicates a successful call.',
},
},
},
},
router.handleLegacyErrors(
verifyAccessAndContext(licenseState, async function (context, req, res) {
const actionsClient = (await context.actions).getActionsClient();
const action = rewriteBodyReq(req.body);
return res.ok({
body: rewriteBodyRes(await actionsClient.create({ action, options: req.params })),
});
})
)
);
};