Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Demo][Dashboard] Public create API #181950

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
119 changes: 119 additions & 0 deletions src/plugins/dashboard/common/api/2023_10_31/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema, type TypeOf } from '@kbn/config-schema';

const controlGroupInputSchema = schema
.object({
panelsJSON: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })),
controlStyle: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })),
chainingSystem: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })),
ignoreParentSettingsJSON: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })),
})
.extends({}, { unknowns: 'ignore' });

const baseDashboard = schema.object({
// General
title: schema.string({ meta: { description: 'A human-readable title for the dashboard' } }),
description: schema.string({ defaultValue: '', meta: { description: 'A short description.' } }),

// Search
kibanaSavedObjectMeta: schema.object(
{
searchSourceJSON: schema.maybe(
schema.string({
meta: {
description: 'A string containing search source',
},
})
),
},
{
meta: {
description: 'A container for various metadata',
},
}
),

// Time
timeRestore: schema.maybe(
schema.boolean({ meta: { description: 'Whether to restore time upon viewing this dashboard' } })
),
timeFrom: schema.maybe(
schema.string({ meta: { description: 'An ISO string indicating when to restore time from' } })
),
timeTo: schema.maybe(
schema.string({ meta: { description: 'An ISO string indicating when to restore time from' } })
),
refreshInterval: schema.maybe(
schema.object(
{
pause: schema.boolean({
meta: {
description:
'Whether the refresh interval is set to be paused while viewing the dashboard.',
},
}),
value: schema.number({
meta: {
description:
'A numeric value indicating refresh frequency expressed as, for example, 30s.',
},
}),
display: schema.maybe(
schema.string({
meta: {
description: 'Lorem ipsum',
},
})
),
section: schema.maybe(
schema.number({
meta: {
description: 'Lorem ipsum',
},
})
),
},
{
meta: {
description: 'A container for various refresh interval settings',
},
}
)
),

// Dashboard Content
controlGroupInput: schema.maybe(controlGroupInputSchema),
panelsJSON: schema.string({ defaultValue: '[]', meta: { description: 'Lorem ipsum' } }),
optionsJSON: schema.string({ defaultValue: '{}', meta: { description: 'Lorem ipsum' } }),

// Legacy
// hits: schema.maybe(schema.number()),
// version: schema.maybe(schema.number()),
});

export const dashboardCreate = baseDashboard.extends(
{
id: schema.maybe(schema.string({ meta: { description: 'The ID of the dashboard' } })),
},
{
meta: { id: 'dashboard.create' },
}
);

export const dashboard = baseDashboard.extends(
{
id: schema.string({ meta: { description: 'The ID of the dashboard' } }),
},
{
meta: { id: 'dashboard' },
}
);

export type Dashboard = TypeOf<typeof dashboard>;
9 changes: 9 additions & 0 deletions src/plugins/dashboard/common/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * as v2023_10_31 from './2023_10_31';
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const dashboardAttributesSchema = schema.object(
hits: schema.maybe(schema.number()),
version: schema.maybe(schema.number()),
},
{ unknowns: 'forbid' }
{ unknowns: 'forbid', meta: { id: 'dashboard.publicSchema' } }
);

export const dashboardSavedObjectSchema = savedObjectSchema(dashboardAttributesSchema);
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/dashboard/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { createDashboardSavedObjectType } from './dashboard_saved_object';
import { CONTENT_ID, LATEST_VERSION } from '../common/content_management';
import { registerDashboardUsageCollector } from './usage/register_collector';
import { dashboardPersistableStateServiceFactory } from './dashboard_container/dashboard_container_embeddable_factory';
import { registerCreate } from './routes/create';

interface SetupDeps {
embeddable: EmbeddableSetup;
Expand Down Expand Up @@ -60,12 +61,15 @@ export class DashboardPlugin
})
);

const dashboardCMStorage = new DashboardStorage({
throwOnResultValidationError: this.initializerContext.env.mode.dev,
logger: this.logger.get('storage'),
});
registerCreate(core.http.createRouter().versioned, dashboardCMStorage);

plugins.contentManagement.register({
id: CONTENT_ID,
storage: new DashboardStorage({
throwOnResultValidationError: this.initializerContext.env.mode.dev,
logger: this.logger.get('storage'),
}),
storage: dashboardCMStorage,
version: {
latest: LATEST_VERSION,
},
Expand Down
59 changes: 59 additions & 0 deletions src/plugins/dashboard/server/routes/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { VersionedRouter } from '@kbn/core-http-server';
import { LATEST_VERSION } from '@kbn/data-views-plugin/common';
import { v2023_10_31 } from '../../common/api';
import { DashboardStorage } from '../content_management';

export function registerCreate(router: VersionedRouter, cmStorage: DashboardStorage) {
router
.post({
path: '/api/dashboard',
access: 'public',
description: 'Create a dashboard',
})
.addVersion(
{
version: '2023-10-31',
validate: {
request: {
body: v2023_10_31.dashboardCreate,
},
response: {
200: { body: v2023_10_31.dashboard },
},
},
},
async (ctx, req, res) => {
const { id: reqId, ...attrs } = req.body;
let id = reqId;
try {
({
item: { id },
} = await cmStorage.create(
{} as any,
jloleysens marked this conversation as resolved.
Show resolved Hide resolved
{
...attrs,
timeRestore: attrs.timeRestore ?? false,
kibanaSavedObjectMeta: {
searchSourceJSON: attrs.kibanaSavedObjectMeta.searchSourceJSON ?? '',
},
panelsJSON: attrs.panelsJSON ?? '[]',
version: LATEST_VERSION,
},
{ id: req.body.id }
));
} catch (e) {
// do some handling;
throw e;
}
return res.ok({ body: { id, ...req.body } });
}
);
}