-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Component templates] Add APIs (#66684)
- Loading branch information
1 parent
4b4f04b
commit 5baef1d
Showing
15 changed files
with
729 additions
and
7 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
x-pack/plugins/index_management/server/client/elasticsearch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export const elasticsearchJsPlugin = (Client: any, config: any, components: any) => { | ||
const ca = components.clientAction.factory; | ||
|
||
Client.prototype.dataManagement = components.clientAction.namespaceFactory(); | ||
const dataManagement = Client.prototype.dataManagement.prototype; | ||
|
||
dataManagement.getComponentTemplates = ca({ | ||
urls: [ | ||
{ | ||
fmt: '/_component_template', | ||
}, | ||
], | ||
method: 'GET', | ||
}); | ||
|
||
dataManagement.getComponentTemplate = ca({ | ||
urls: [ | ||
{ | ||
fmt: '/_component_template/<%=name%>', | ||
req: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
], | ||
method: 'GET', | ||
}); | ||
|
||
dataManagement.saveComponentTemplate = ca({ | ||
urls: [ | ||
{ | ||
fmt: '/_component_template/<%=name%>', | ||
req: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
], | ||
method: 'PUT', | ||
}); | ||
|
||
dataManagement.deleteComponentTemplate = ca({ | ||
urls: [ | ||
{ | ||
fmt: '/_component_template/<%=name%>', | ||
req: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
], | ||
method: 'DELETE', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
x-pack/plugins/index_management/server/routes/api/component_templates/create.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { i18n } from '@kbn/i18n'; | ||
import { schema } from '@kbn/config-schema'; | ||
|
||
import { RouteDependencies } from '../../../types'; | ||
import { addBasePath } from '../index'; | ||
import { componentTemplateSchema } from './schema_validation'; | ||
|
||
const bodySchema = schema.object({ | ||
name: schema.string(), | ||
...componentTemplateSchema, | ||
}); | ||
|
||
export const registerCreateRoute = ({ | ||
router, | ||
license, | ||
lib: { isEsError }, | ||
}: RouteDependencies): void => { | ||
router.post( | ||
{ | ||
path: addBasePath('/component_templates'), | ||
validate: { | ||
body: bodySchema, | ||
}, | ||
}, | ||
license.guardApiRoute(async (ctx, req, res) => { | ||
const { callAsCurrentUser } = ctx.dataManagement!.client; | ||
|
||
const { name, ...componentTemplateDefinition } = req.body; | ||
|
||
try { | ||
// Check that a component template with the same name doesn't already exist | ||
const componentTemplateResponse = await callAsCurrentUser( | ||
'dataManagement.getComponentTemplate', | ||
{ name } | ||
); | ||
|
||
const { component_templates: componentTemplates } = componentTemplateResponse; | ||
|
||
if (componentTemplates.length) { | ||
return res.conflict({ | ||
body: new Error( | ||
i18n.translate('xpack.idxMgmt.componentTemplates.createRoute.duplicateErrorMessage', { | ||
defaultMessage: "There is already a component template with name '{name}'.", | ||
values: { | ||
name, | ||
}, | ||
}) | ||
), | ||
}); | ||
} | ||
} catch (e) { | ||
// Silently swallow error | ||
} | ||
|
||
try { | ||
const response = await callAsCurrentUser('dataManagement.saveComponentTemplate', { | ||
name, | ||
body: componentTemplateDefinition, | ||
}); | ||
|
||
return res.ok({ body: response }); | ||
} catch (error) { | ||
if (isEsError(error)) { | ||
return res.customError({ | ||
statusCode: error.statusCode, | ||
body: error, | ||
}); | ||
} | ||
|
||
return res.internalError({ body: error }); | ||
} | ||
}) | ||
); | ||
}; |
51 changes: 51 additions & 0 deletions
51
x-pack/plugins/index_management/server/routes/api/component_templates/delete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { schema } from '@kbn/config-schema'; | ||
|
||
import { RouteDependencies } from '../../../types'; | ||
import { addBasePath } from '../index'; | ||
|
||
const paramsSchema = schema.object({ | ||
names: schema.string(), | ||
}); | ||
|
||
export const registerDeleteRoute = ({ router, license }: RouteDependencies): void => { | ||
router.delete( | ||
{ | ||
path: addBasePath('/component_templates/{names}'), | ||
validate: { | ||
params: paramsSchema, | ||
}, | ||
}, | ||
license.guardApiRoute(async (ctx, req, res) => { | ||
const { callAsCurrentUser } = ctx.dataManagement!.client; | ||
const { names } = req.params; | ||
const componentNames = names.split(','); | ||
|
||
const response: { itemsDeleted: string[]; errors: any[] } = { | ||
itemsDeleted: [], | ||
errors: [], | ||
}; | ||
|
||
await Promise.all( | ||
componentNames.map((componentName) => { | ||
return callAsCurrentUser('dataManagement.deleteComponentTemplate', { | ||
name: componentName, | ||
}) | ||
.then(() => response.itemsDeleted.push(componentName)) | ||
.catch((e) => | ||
response.errors.push({ | ||
name: componentName, | ||
error: e, | ||
}) | ||
); | ||
}) | ||
); | ||
|
||
return res.ok({ body: response }); | ||
}) | ||
); | ||
}; |
77 changes: 77 additions & 0 deletions
77
x-pack/plugins/index_management/server/routes/api/component_templates/get.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { schema } from '@kbn/config-schema'; | ||
|
||
import { RouteDependencies } from '../../../types'; | ||
import { addBasePath } from '../index'; | ||
|
||
const paramsSchema = schema.object({ | ||
name: schema.string(), | ||
}); | ||
|
||
export function registerGetAllRoute({ router, license, lib: { isEsError } }: RouteDependencies) { | ||
// Get all component templates | ||
router.get( | ||
{ path: addBasePath('/component_templates'), validate: false }, | ||
license.guardApiRoute(async (ctx, req, res) => { | ||
const { callAsCurrentUser } = ctx.dataManagement!.client; | ||
|
||
try { | ||
const response = await callAsCurrentUser('dataManagement.getComponentTemplates'); | ||
|
||
return res.ok({ body: response.component_templates }); | ||
} catch (error) { | ||
if (isEsError(error)) { | ||
return res.customError({ | ||
statusCode: error.statusCode, | ||
body: error, | ||
}); | ||
} | ||
|
||
return res.internalError({ body: error }); | ||
} | ||
}) | ||
); | ||
|
||
// Get single component template | ||
router.get( | ||
{ | ||
path: addBasePath('/component_templates/{name}'), | ||
validate: { | ||
params: paramsSchema, | ||
}, | ||
}, | ||
license.guardApiRoute(async (ctx, req, res) => { | ||
const { callAsCurrentUser } = ctx.dataManagement!.client; | ||
const { name } = req.params; | ||
|
||
try { | ||
const { component_templates: componentTemplates } = await callAsCurrentUser( | ||
'dataManagement.getComponentTemplates', | ||
{ | ||
name, | ||
} | ||
); | ||
|
||
return res.ok({ | ||
body: { | ||
...componentTemplates[0], | ||
name, | ||
}, | ||
}); | ||
} catch (error) { | ||
if (isEsError(error)) { | ||
return res.customError({ | ||
statusCode: error.statusCode, | ||
body: error, | ||
}); | ||
} | ||
|
||
return res.internalError({ body: error }); | ||
} | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...ex_management/server/routes/api/component_templates/register_component_template_routes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { RouteDependencies } from '../../../types'; | ||
|
||
import { registerGetAllRoute } from './get'; | ||
import { registerCreateRoute } from './create'; | ||
import { registerUpdateRoute } from './update'; | ||
import { registerDeleteRoute } from './delete'; | ||
|
||
export function registerComponentTemplateRoutes(dependencies: RouteDependencies) { | ||
registerGetAllRoute(dependencies); | ||
registerCreateRoute(dependencies); | ||
registerUpdateRoute(dependencies); | ||
registerDeleteRoute(dependencies); | ||
} |
Oops, something went wrong.