Skip to content

Commit

Permalink
Remove id attribute from canvas attributes (#30736) (#31138)
Browse files Browse the repository at this point in the history
* Remove id attribute from canvas attributes

* Fix create and update API

* Add migration tests

* Use constants

* Add API tests

* Cleanup tests

* Apply PR feedback
  • Loading branch information
mikecote committed Feb 14, 2019
1 parent 718aef3 commit 5b40f3a
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 13 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { resolve } from 'path';
import init from './init';
import { mappings } from './server/mappings';
import { CANVAS_APP } from './common/lib';
import { migrations } from './migrations';

export function canvas(kibana) {
return new kibana.Plugin({
Expand All @@ -30,6 +31,7 @@ export function canvas(kibana) {
],
home: ['plugins/canvas/register_feature'],
mappings,
migrations,
},

config: Joi => {
Expand Down
18 changes: 18 additions & 0 deletions x-pack/plugins/canvas/migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 { CANVAS_TYPE } from './common/lib';

export const migrations = {
[CANVAS_TYPE]: {
'7.0.0': doc => {
if (doc.attributes) {
delete doc.attributes.id;
}
return doc;
},
},
};
37 changes: 37 additions & 0 deletions x-pack/plugins/canvas/migrations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 { migrations } from './migrations';
import { CANVAS_TYPE } from './common/lib';

describe(CANVAS_TYPE, () => {
describe('7.0.0', () => {
const migrate = doc => migrations[CANVAS_TYPE]['7.0.0'](doc);

it('does not throw error on empty object', () => {
const migratedDoc = migrate({});
expect(migratedDoc).toMatchInlineSnapshot(`Object {}`);
});

it('removes id from "attributes"', () => {
const migratedDoc = migrate({
foo: true,
attributes: {
id: '123',
bar: true,
},
});
expect(migratedDoc).toMatchInlineSnapshot(`
Object {
"attributes": Object {
"bar": true,
},
"foo": true,
}
`);
});
});
});
1 change: 0 additions & 1 deletion x-pack/plugins/canvas/server/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const mappings = {
},
},
},
id: { type: 'text', index: false },
'@timestamp': { type: 'date' },
'@created': { type: 'date' },
},
Expand Down
14 changes: 8 additions & 6 deletions x-pack/plugins/canvas/server/routes/workpad.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import boom from 'boom';
import { omit } from 'lodash';
import {
CANVAS_TYPE,
API_ROUTE_WORKPAD,
Expand Down Expand Up @@ -44,22 +45,23 @@ export function workpad(server) {
return resp;
}

function createWorkpad(req, id) {
function createWorkpad(req) {
const savedObjectsClient = req.getSavedObjectsClient();

if (!req.payload) {
return Promise.reject(boom.badRequest('A workpad payload is required'));
}

const now = new Date().toISOString();
const { id, ...payload } = req.payload;
return savedObjectsClient.create(
CANVAS_TYPE,
{
...req.payload,
...payload,
'@timestamp': now,
'@created': now,
},
{ id: id || req.payload.id || getId('workpad') }
{ id: id || getId('workpad') }
);
}

Expand All @@ -74,7 +76,7 @@ export function workpad(server) {
return savedObjectsClient.create(
CANVAS_TYPE,
{
...req.payload,
...omit(req.payload, 'id'),
'@timestamp': now,
'@created': workpad.attributes['@created'],
},
Expand Down Expand Up @@ -158,7 +160,7 @@ export function workpad(server) {

return savedObjectsClient
.get(CANVAS_TYPE, id)
.then(obj => obj.attributes)
.then(obj => ({ id: obj.id, ...obj.attributes }))
.then(formatResponse)
.catch(formatResponse);
},
Expand Down Expand Up @@ -233,7 +235,7 @@ export function workpad(server) {
.then(resp => {
return {
total: resp.total,
workpads: resp.saved_objects.map(hit => hit.attributes),
workpads: resp.saved_objects.map(hit => ({ id: hit.id, ...hit.attributes })),
};
})
.catch(() => {
Expand Down
Loading

0 comments on commit 5b40f3a

Please sign in to comment.