Skip to content

Commit

Permalink
add route to fetch single component template
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed May 15, 2020
1 parent 35e9cfe commit 93be994
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const registerCreateRoute = ({

const { component_templates: componentTemplates } = componentTemplateResponse;

if (componentTemplates.length && componentTemplates[0].name) {
if (componentTemplates.length) {
return res.conflict({
body: new Error(
i18n.translate('xpack.idxMgmt.componentTemplates.createRoute.duplicateErrorMessage', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
* 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';

export function registerGetAllRoute({ router, license, lib }: RouteDependencies) {
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) => {
Expand All @@ -17,7 +24,46 @@ export function registerGetAllRoute({ router, license, lib }: RouteDependencies)

return res.ok({ body: response.component_templates });
} catch (error) {
if (lib.isEsError(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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ export default function({ getService }: FtrProviderContext) {
});
});
});

describe('one component template', () => {
it('should return a single component template', async () => {
const uri = `${API_BASE_PATH}/component_templates/${COMPONENT_NAME}`;

const { body } = await supertest
.get(uri)
.set('kbn-xsrf', 'xxx')
.expect(200);

expect(body).to.eql({
name: COMPONENT_NAME,
component_template: {
...COMPONENT,
},
});
});
});
});

describe('Create', () => {
Expand Down

0 comments on commit 93be994

Please sign in to comment.