-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dashboard navigation] Inject/extract references (#164330)
Fixes #154363 ## Summary Extracts and injects references to Dashboards in Link panels to allow sharing / importing to other Spaces. To test this: 1. Create an empty dashboard. 1. Create a Links panel on the dashboard and save to library. 1. Save the dashboard as one or more new dashboards. 1. Update the Links panel with links to the new dashboards. 1. Create two new Spaces, "space1" and "space2" with the default settings. 1. In Saved Objects, click the Actions button for your Links and choose "Copy to Spaces". 1. Copy the Links to "space1". 1. Switch to the "space1" space and verify all the dashboards using that Links panel were included. 1. In Saved Objects, select the Links saved object and export it including all related objects 1. Switch to "space2" and import the Links saved object from the file downloaded in the previous step. 1. Verify all the dashboards were also imported. 1. Inspect the saved object for the Links. The `references` array should contain objects for each dashboard. 1. The `attributes.links[].destination` property should be a string in the format of `links_<someuuid>_dashboard`. This string should match one of the `references[].id`. --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
d51bcd8
commit 409a8f9
Showing
20 changed files
with
560 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
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
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
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
64 changes: 64 additions & 0 deletions
64
src/plugins/navigation_embeddable/common/embeddable/extract.test.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,64 @@ | ||
/* | ||
* 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 { extract } from './extract'; | ||
|
||
test('Should return original state and empty references with by-reference embeddable state', () => { | ||
const navigationEmbeddableByReferenceInput = { | ||
id: '2192e502-0ec7-4316-82fb-c9bbf78525c4', | ||
type: 'navigation_embeddable', | ||
}; | ||
|
||
expect(extract!(navigationEmbeddableByReferenceInput)).toEqual({ | ||
state: navigationEmbeddableByReferenceInput, | ||
references: [], | ||
}); | ||
}); | ||
|
||
test('Should update state with refNames with by-value embeddable state', () => { | ||
const navigationEmbeddableByValueInput = { | ||
id: '8d62c3f0-c61f-4c09-ac24-9b8ee4320e20', | ||
attributes: { | ||
links: [ | ||
{ | ||
type: 'dashboardLink', | ||
id: 'fc7b8c70-2eb9-40b2-936d-457d1721a438', | ||
destination: 'elastic_agent-1a4e7280-6b5e-11ed-98de-67bdecd21824', | ||
order: 0, | ||
}, | ||
], | ||
layout: 'horizontal', | ||
}, | ||
type: 'navigation_embeddable', | ||
}; | ||
|
||
expect(extract!(navigationEmbeddableByValueInput)).toEqual({ | ||
references: [ | ||
{ | ||
name: 'link_fc7b8c70-2eb9-40b2-936d-457d1721a438_dashboard', | ||
type: 'dashboard', | ||
id: 'elastic_agent-1a4e7280-6b5e-11ed-98de-67bdecd21824', | ||
}, | ||
], | ||
state: { | ||
id: '8d62c3f0-c61f-4c09-ac24-9b8ee4320e20', | ||
attributes: { | ||
links: [ | ||
{ | ||
type: 'dashboardLink', | ||
id: 'fc7b8c70-2eb9-40b2-936d-457d1721a438', | ||
destinationRefName: 'link_fc7b8c70-2eb9-40b2-936d-457d1721a438_dashboard', | ||
order: 0, | ||
}, | ||
], | ||
layout: 'horizontal', | ||
}, | ||
type: 'navigation_embeddable', | ||
}, | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
src/plugins/navigation_embeddable/common/embeddable/extract.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,35 @@ | ||
/* | ||
* 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 { EmbeddableRegistryDefinition } from '@kbn/embeddable-plugin/common'; | ||
import type { NavigationEmbeddableAttributes } from '../content_management'; | ||
import { extractReferences } from '../persistable_state'; | ||
import { NavigationEmbeddablePersistableState } from './types'; | ||
|
||
export const extract: EmbeddableRegistryDefinition['extract'] = (state) => { | ||
const typedState = state as NavigationEmbeddablePersistableState; | ||
|
||
// by-reference embeddable | ||
if (!('attributes' in typedState) || typedState.attributes === undefined) { | ||
// No references to extract for by-reference embeddable since all references are stored with by-reference saved object | ||
return { state, references: [] }; | ||
} | ||
|
||
// by-value embeddable | ||
const { attributes, references } = extractReferences({ | ||
attributes: typedState.attributes as unknown as NavigationEmbeddableAttributes, | ||
}); | ||
|
||
return { | ||
state: { | ||
...state, | ||
attributes, | ||
}, | ||
references, | ||
}; | ||
}; |
10 changes: 10 additions & 0 deletions
10
src/plugins/navigation_embeddable/common/embeddable/index.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,10 @@ | ||
/* | ||
* 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 { inject } from './inject'; | ||
export { extract } from './extract'; |
69 changes: 69 additions & 0 deletions
69
src/plugins/navigation_embeddable/common/embeddable/inject.test.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,69 @@ | ||
/* | ||
* 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 { inject } from './inject'; | ||
|
||
test('Should return original state with by-reference embeddable state', () => { | ||
const navigationEmbeddableByReferenceInput = { | ||
id: 'ea40fd4e-216c-49a7-917f-f733c8a2c817', | ||
type: 'navigation_embeddable', | ||
}; | ||
|
||
const references = [ | ||
{ | ||
name: 'panel_ea40fd4e-216c-49a7-917f-f733c8a2c817', | ||
type: 'navigation_embeddable', | ||
id: '7f92d7d0-8e5f-11ec-9477-312c8a6de896', | ||
}, | ||
]; | ||
|
||
expect(inject!(navigationEmbeddableByReferenceInput, references)).toEqual( | ||
navigationEmbeddableByReferenceInput | ||
); | ||
}); | ||
|
||
test('Should inject refNames with by-value embeddable state', () => { | ||
const navigationEmbeddableByValueInput = { | ||
id: 'c3937cf9-29be-43df-a4af-a4df742d7d35', | ||
attributes: { | ||
links: [ | ||
{ | ||
type: 'dashboardLink', | ||
id: 'fc7b8c70-2eb9-40b2-936d-457d1721a438', | ||
destinationRefName: 'link_fc7b8c70-2eb9-40b2-936d-457d1721a438_dashboard', | ||
order: 0, | ||
}, | ||
], | ||
layout: 'horizontal', | ||
}, | ||
type: 'navigation_embeddable', | ||
}; | ||
const references = [ | ||
{ | ||
name: 'link_fc7b8c70-2eb9-40b2-936d-457d1721a438_dashboard', | ||
type: 'dashboard', | ||
id: 'elastic_agent-1a4e7280-6b5e-11ed-98de-67bdecd21824', | ||
}, | ||
]; | ||
|
||
expect(inject!(navigationEmbeddableByValueInput, references)).toEqual({ | ||
id: 'c3937cf9-29be-43df-a4af-a4df742d7d35', | ||
attributes: { | ||
links: [ | ||
{ | ||
type: 'dashboardLink', | ||
id: 'fc7b8c70-2eb9-40b2-936d-457d1721a438', | ||
destination: 'elastic_agent-1a4e7280-6b5e-11ed-98de-67bdecd21824', | ||
order: 0, | ||
}, | ||
], | ||
layout: 'horizontal', | ||
}, | ||
type: 'navigation_embeddable', | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
src/plugins/navigation_embeddable/common/embeddable/inject.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,40 @@ | ||
/* | ||
* 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 { EmbeddableRegistryDefinition } from '@kbn/embeddable-plugin/common'; | ||
import { NavigationEmbeddableAttributes } from '../content_management'; | ||
import { injectReferences } from '../persistable_state'; | ||
import { NavigationEmbeddablePersistableState } from './types'; | ||
|
||
export const inject: EmbeddableRegistryDefinition['inject'] = (state, references) => { | ||
const typedState = state as NavigationEmbeddablePersistableState; | ||
|
||
// by-reference embeddable | ||
if (!('attributes' in typedState) || typedState.attributes === undefined) { | ||
return typedState; | ||
} | ||
|
||
// by-value embeddable | ||
try { | ||
const { attributes: attributesWithInjectedIds } = injectReferences({ | ||
attributes: typedState.attributes as unknown as NavigationEmbeddableAttributes, | ||
references, | ||
}); | ||
|
||
return { | ||
...typedState, | ||
attributes: attributesWithInjectedIds, | ||
}; | ||
} catch (error) { | ||
// inject exception prevents entire dashboard from display | ||
// Instead of throwing, swallow error and let dashboard display | ||
// Errors will surface in navigation embeddable panel. | ||
// Users can then manually edit links to resolve any problems. | ||
return typedState; | ||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
src/plugins/navigation_embeddable/common/embeddable/types.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,14 @@ | ||
/* | ||
* 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 { EmbeddableStateWithType } from '@kbn/embeddable-plugin/common'; | ||
import { SerializableRecord } from '@kbn/utility-types'; | ||
|
||
export type NavigationEmbeddablePersistableState = EmbeddableStateWithType & { | ||
attributes: SerializableRecord; | ||
}; |
9 changes: 9 additions & 0 deletions
9
src/plugins/navigation_embeddable/common/persistable_state/index.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,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 { extractReferences, injectReferences } from './references'; |
Oops, something went wrong.