forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add content management mSearch to viz, lens, and event annotation gro…
…up (elastic#162450) ## Summary Adds content management api mSearch functionality to `lens`, `visualization`, and `event_annotation_group` types via abstracted function and types. Part of elastic#161545 --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
- Loading branch information
1 parent
7428838
commit b271eac
Showing
8 changed files
with
147 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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 Boom from '@hapi/boom'; | ||
import { pick } from 'lodash'; | ||
|
||
import type { StorageContext } from '@kbn/content-management-plugin/server'; | ||
|
||
import type { | ||
SavedObjectsFindResult, | ||
SavedObject, | ||
SavedObjectReference, | ||
} from '@kbn/core-saved-objects-api-server'; | ||
|
||
import type { ServicesDefinitionSet, SOWithMetadata, SOWithMetadataPartial } from './types'; | ||
|
||
type PartialSavedObject<T> = Omit<SavedObject<Partial<T>>, 'references'> & { | ||
references: SavedObjectReference[] | undefined; | ||
}; | ||
|
||
interface GetMSearchParams { | ||
savedObjectType: string; | ||
cmServicesDefinition: ServicesDefinitionSet; | ||
allowedSavedObjectAttributes: string[]; | ||
} | ||
|
||
function savedObjectToItem<Attributes extends object>( | ||
savedObject: SavedObject<Attributes> | PartialSavedObject<Attributes>, | ||
allowedSavedObjectAttributes: string[] | ||
): SOWithMetadata | SOWithMetadataPartial { | ||
const { | ||
id, | ||
type, | ||
updated_at: updatedAt, | ||
created_at: createdAt, | ||
attributes, | ||
references, | ||
error, | ||
namespaces, | ||
version, | ||
} = savedObject; | ||
|
||
return { | ||
id, | ||
type, | ||
updatedAt, | ||
createdAt, | ||
attributes: pick(attributes, allowedSavedObjectAttributes), | ||
references, | ||
error, | ||
namespaces, | ||
version, | ||
}; | ||
} | ||
|
||
export interface GetMSearchType<ReturnItem> { | ||
savedObjectType: string; | ||
toItemResult: (ctx: StorageContext, savedObject: SavedObjectsFindResult) => ReturnItem; | ||
} | ||
|
||
export const getMSearch = <ReturnItem, SOAttributes extends object>({ | ||
savedObjectType, | ||
cmServicesDefinition, | ||
allowedSavedObjectAttributes, | ||
}: GetMSearchParams) => { | ||
return { | ||
savedObjectType, | ||
toItemResult: (ctx: StorageContext, savedObject: SavedObjectsFindResult): ReturnItem => { | ||
const transforms = ctx.utils.getTransforms(cmServicesDefinition); | ||
|
||
// Validate DB response and DOWN transform to the request version | ||
const { value, error: resultError } = transforms.mSearch.out.result.down< | ||
ReturnItem, | ||
ReturnItem | ||
>( | ||
// Ran into a case where a schema was broken by a SO attribute that wasn't part of the definition | ||
// so we specify which attributes are allowed | ||
savedObjectToItem<SOAttributes>( | ||
savedObject as SavedObjectsFindResult<SOAttributes>, | ||
allowedSavedObjectAttributes | ||
) | ||
); | ||
|
||
if (resultError) { | ||
throw Boom.badRequest(`Invalid response. ${resultError.message}`); | ||
} | ||
|
||
return value; | ||
}, | ||
}; | ||
}; |
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