-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up stale
augment-vis
saved objs (#4059)
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
- Loading branch information
Showing
39 changed files
with
757 additions
and
100 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
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
9 changes: 9 additions & 0 deletions
9
src/plugins/saved_objects_management/public/triggers/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 OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { | ||
SAVED_OBJECT_DELETE_TRIGGER, | ||
savedObjectDeleteTrigger, | ||
} from './saved_object_delete_trigger'; |
18 changes: 18 additions & 0 deletions
18
src/plugins/saved_objects_management/public/triggers/saved_object_delete_trigger.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,18 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { i18n } from '@osd/i18n'; | ||
import { Trigger } from '../../../ui_actions/public'; | ||
|
||
export const SAVED_OBJECT_DELETE_TRIGGER = 'SAVED_OBJECT_DELETE_TRIGGER'; | ||
export const savedObjectDeleteTrigger: Trigger<'SAVED_OBJECT_DELETE_TRIGGER'> = { | ||
id: SAVED_OBJECT_DELETE_TRIGGER, | ||
title: i18n.translate('savedObjectsManagement.triggers.savedObjectDeleteTitle', { | ||
defaultMessage: 'Saved object delete', | ||
}), | ||
description: i18n.translate('savedObjectsManagement.triggers.savedObjectDeleteDescription', { | ||
defaultMessage: 'Perform additional actions after deleting a saved object', | ||
}), | ||
}; |
22 changes: 22 additions & 0 deletions
22
src/plugins/saved_objects_management/public/ui_actions_bootstrap.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,22 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { UiActionsSetup } from '../../ui_actions/public'; | ||
import { SAVED_OBJECT_DELETE_TRIGGER, savedObjectDeleteTrigger } from './triggers'; | ||
|
||
export interface SavedObjectDeleteContext { | ||
type: string; | ||
savedObjectId: string; | ||
} | ||
|
||
declare module '../../ui_actions/public' { | ||
export interface TriggerContextMapping { | ||
[SAVED_OBJECT_DELETE_TRIGGER]: SavedObjectDeleteContext; | ||
} | ||
} | ||
|
||
export const bootstrap = (uiActions: UiActionsSetup) => { | ||
uiActions.registerTrigger(savedObjectDeleteTrigger); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { | ||
PLUGIN_RESOURCE_DELETE_ACTION, | ||
PluginResourceDeleteAction, | ||
} from './plugin_resource_delete_action'; | ||
export { SAVED_OBJECT_DELETE_ACTION, SavedObjectDeleteAction } from './saved_object_delete_action'; |
81 changes: 81 additions & 0 deletions
81
src/plugins/vis_augmenter/public/actions/plugin_resource_delete_action.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,81 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { createPointInTimeEventsVisLayer } from '../mocks'; | ||
import { generateAugmentVisSavedObject } from '../saved_augment_vis'; | ||
import { PluginResourceDeleteAction } from './plugin_resource_delete_action'; | ||
|
||
const sampleSavedObj = generateAugmentVisSavedObject( | ||
'test-id', | ||
{ | ||
type: 'PointInTimeEvents', | ||
name: 'test-fn-name', | ||
args: {}, | ||
}, | ||
'test-vis-id', | ||
'test-origin-plugin', | ||
{ | ||
type: 'test-resource-type', | ||
id: 'test-resource-id', | ||
} | ||
); | ||
|
||
const sampleVisLayer = createPointInTimeEventsVisLayer(); | ||
|
||
describe('SavedObjectDeleteAction', () => { | ||
it('is incompatible with invalid saved obj list', async () => { | ||
const action = new PluginResourceDeleteAction(); | ||
const visLayers = [sampleVisLayer]; | ||
// @ts-ignore | ||
expect(await action.isCompatible({ savedObjs: null, visLayers })).toBe(false); | ||
// @ts-ignore | ||
expect(await action.isCompatible({ savedObjs: undefined, visLayers })).toBe(false); | ||
expect(await action.isCompatible({ savedObjs: [], visLayers })).toBe(false); | ||
}); | ||
|
||
it('is incompatible with invalid vislayer list', async () => { | ||
const action = new PluginResourceDeleteAction(); | ||
const savedObjs = [sampleSavedObj]; | ||
// @ts-ignore | ||
expect(await action.isCompatible({ savedObjs, visLayers: null })).toBe(false); | ||
// @ts-ignore | ||
expect(await action.isCompatible({ savedObjs, visLayers: undefined })).toBe(false); | ||
expect(await action.isCompatible({ savedObjs, visLayers: [] })).toBe(false); | ||
}); | ||
|
||
it('execute throws error if incompatible saved objs list', async () => { | ||
const action = new PluginResourceDeleteAction(); | ||
async function check(savedObjs: any, visLayers: any) { | ||
await action.execute({ savedObjs, visLayers }); | ||
} | ||
await expect(check(null, [sampleVisLayer])).rejects.toThrow(Error); | ||
}); | ||
|
||
it('execute throws error if incompatible vis layer list', async () => { | ||
const action = new PluginResourceDeleteAction(); | ||
async function check(savedObjs: any, visLayers: any) { | ||
await action.execute({ savedObjs, visLayers }); | ||
} | ||
await expect(check([sampleSavedObj], null)).rejects.toThrow(Error); | ||
}); | ||
|
||
it('execute is successful if valid saved obj and vis layer lists', async () => { | ||
const action = new PluginResourceDeleteAction(); | ||
async function check(savedObjs: any, visLayers: any) { | ||
await action.execute({ savedObjs, visLayers }); | ||
} | ||
await expect(check([sampleSavedObj], [sampleVisLayer])).resolves; | ||
}); | ||
|
||
it('Returns display name', async () => { | ||
const action = new PluginResourceDeleteAction(); | ||
expect(action.getDisplayName()).toBeDefined(); | ||
}); | ||
|
||
it('Returns icon type', async () => { | ||
const action = new PluginResourceDeleteAction(); | ||
expect(action.getIconType()).toBeDefined(); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
src/plugins/vis_augmenter/public/actions/plugin_resource_delete_action.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,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { isEmpty } from 'lodash'; | ||
import { i18n } from '@osd/i18n'; | ||
import { EuiIconType } from '@elastic/eui/src/components/icon/icon'; | ||
import { Action, IncompatibleActionError } from '../../../ui_actions/public'; | ||
import { getSavedAugmentVisLoader } from '../services'; | ||
import { PluginResourceDeleteContext } from '../ui_actions_bootstrap'; | ||
import { cleanupStaleObjects } from '../utils'; | ||
|
||
export const PLUGIN_RESOURCE_DELETE_ACTION = 'PLUGIN_RESOURCE_DELETE_ACTION'; | ||
|
||
export class PluginResourceDeleteAction implements Action<PluginResourceDeleteContext> { | ||
public readonly type = PLUGIN_RESOURCE_DELETE_ACTION; | ||
public readonly id = PLUGIN_RESOURCE_DELETE_ACTION; | ||
public order = 1; | ||
|
||
public getIconType(): EuiIconType { | ||
return `trash`; | ||
} | ||
|
||
public getDisplayName() { | ||
return i18n.translate('dashboard.actions.deleteSavedObject.name', { | ||
defaultMessage: | ||
'Clean up all augment-vis saved objects associated to the deleted visualization', | ||
}); | ||
} | ||
|
||
public async isCompatible({ savedObjs, visLayers }: PluginResourceDeleteContext) { | ||
return !isEmpty(savedObjs) && !isEmpty(visLayers); | ||
} | ||
|
||
/** | ||
* If we have just collected all of the saved objects and generated vis layers, | ||
* sweep through them all and if any of the resources are deleted, delete those | ||
* corresponding saved objects | ||
*/ | ||
public async execute({ savedObjs, visLayers }: PluginResourceDeleteContext) { | ||
if (!(await this.isCompatible({ savedObjs, visLayers }))) { | ||
throw new IncompatibleActionError(); | ||
} | ||
cleanupStaleObjects(savedObjs, visLayers, getSavedAugmentVisLoader()); | ||
} | ||
} |
Oops, something went wrong.