-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Allow savedObjects types registration from NP #57430
Allow savedObjects types registration from NP #57430
Conversation
…register-type-so-api
Pinging @elastic/kibana-platform (Team:Platform) |
* Note: The Saved Object setup API's should only be used for creating and | ||
* registering client wrappers. Constructing a Saved Objects client or | ||
* repository for use within your own plugin won't have any of the registered | ||
* wrappers applied and is considered an anti-pattern. Use the Saved Objects | ||
* client from the | ||
* {@link SavedObjectsServiceStart | SavedObjectsServiceStart#getScopedClient } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed outdated comment, as it's no longer possible to create clients or repository from the setup contract
/** | ||
* Registry holding information about all the registered {@link SavedObjectsType | savedObject types}. | ||
* | ||
* @internal | ||
* @public | ||
*/ | ||
export class SavedObjectTypeRegistry { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was 'forced' to expose this class as public to get proper doc generation for the associated ISavedObjectTypeRegistry
interface. I'm starting to wonder if we really should use Pick
-based interface when exposing public interfaces from internal implementations...
…register-type-so-api
…register-type-so-api
@@ -4,7 +4,7 @@ | |||
|
|||
## SavedObjectsServiceSetup interface | |||
|
|||
Saved Objects is Kibana's data persistence mechanism allowing plugins to use Elasticsearch for storing and querying state. The SavedObjectsServiceSetup API exposes methods for creating and registering Saved Object client wrappers. | |||
Saved Objects is Kibana's data persistence mechanism allowing plugins to use Elasticsearch for storing and querying state. The SavedObjectsServiceSetup API exposes methods for registering Saved Object types and creating and registering Saved Object client wrappers and factories |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SavedObjectsServiceSetup API exposes methods for registering Saved Object types, creating and registering Saved Object client wrappers and factories.
🤔 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be indeed better 😄
src/core/MIGRATION_EXAMPLES.md
Outdated
|
||
### Changes in structure compared to legacy | ||
|
||
The NP `registerType` expected input is very close to the legacy format, However there are some minor changes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The NP `registerType` expected input is very close to the legacy format, However there are some minor changes: | |
The NP `registerType` expected input is very close to the legacy format. However, there are some minor changes: |
* | ||
* @example | ||
* ```ts | ||
* // src/plugins/my_plugin/server/saved_objects/types.ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a convention to place SO definition under the saved_objects
folder? I can't see it in https://github.com/elastic/kibana/blob/ec31611911c8d7630d61361f6aa22d57f86a964f/src/core/CONVENTIONS.md Probably we should add one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should. Added a section and updated the example folder structure
expect(Object.keys(converted[1]!.migrations!)).toEqual(Object.keys(migrationsB)); | ||
}); | ||
|
||
it('migrates the migration to the new format', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('migrates the migration to the new format', () => { | |
it('converts the migration to the new format', () => { |
retest |
From an offline discussion: It would be great if we can expose the type registry (or just the getters) in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some documentation nits, but otherwise 👍
@@ -4,10 +4,26 @@ | |||
|
|||
## SavedObjectMigrationFn type | |||
|
|||
A migration function defined for a [saved objects type](./kibana-plugin-server.savedobjectstype.md) used to migrate it's | |||
A migration function defined for a [saved objects type](./kibana-plugin-server.savedobjectstype.md) used to migrate it's to a given version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
A migration function defined for a [saved objects type](./kibana-plugin-server.savedobjectstype.md) used to migrate it's to a given version | |
A migration function for a [saved object type](./kibana-plugin-server.savedobjectstype.md) used to migrate it to a given version |
src/core/MIGRATION_EXAMPLES.md
Outdated
|
||
## Saved Objects types | ||
|
||
In the legacy platform, saved object types were registered using static definitions in `uiExports` part |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the legacy platform, saved object types were registered using static definitions in `uiExports` part | |
In the legacy platform, saved object types were registered using static definitions in the `uiExports` part of |
src/core/MIGRATION_EXAMPLES.md
Outdated
- The `schema.indexPattern` was accepting either a `string` or a `(config: LegacyConfig) => string`. `SavedObjectsType.indexPattern` only accepts a string, as you can access the configuration during your plugin's setup phase. | ||
|
||
- The migration function signature has changed: | ||
In legacy, is was `(doc: SavedObjectUnsanitizedDoc, log: SavedObjectsMigrationLogger) => SavedObjectUnsanitizedDoc;` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In legacy, is was `(doc: SavedObjectUnsanitizedDoc, log: SavedObjectsMigrationLogger) => SavedObjectUnsanitizedDoc;` | |
In legacy, it was `(doc: SavedObjectUnsanitizedDoc, log: SavedObjectsMigrationLogger) => SavedObjectUnsanitizedDoc;` |
* try { | ||
* doc.attributes.someProp = migrateProperty(doc.attributes.someProp); | ||
* } catch(e) { | ||
* log.warn('Error migrating `someProp`'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching migration exceptions is a common pattern in existing migrations but I think it's something we should discourage. It effectively causes a bug in some unknown place higher up in the stack when this object is eventually used. Also when we introduce "invalid" saved objects we want the migration system to catch and handle these migration exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was mostly to demonstrate a use of the log
context property. I will remove the try/catch and add another dummy check instead
* | ||
* @example | ||
* ```ts | ||
* // src/plugins/my_plugin/server/saved_objects/types.ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* // src/plugins/my_plugin/server/saved_objects/types.ts | |
* // src/plugins/my_plugin/server/saved_objects/my_type.ts |
src/core/MIGRATION_EXAMPLES.md
Outdated
First type: | ||
|
||
```typescript | ||
// src/plugins/my-plugin/server/saved_objects/first_type.ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// src/plugins/my-plugin/server/saved_objects/first_type.ts | |
// src/plugins/my_plugin/server/saved_objects/first_type.ts |
src/core/MIGRATION_EXAMPLES.md
Outdated
``` | ||
|
||
```js | ||
// src/legacy/core_plugins/my-plugin/migrations.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// src/legacy/core_plugins/my-plugin/migrations.js | |
// src/legacy/core_plugins/my_plugin/migrations.js |
src/core/MIGRATION_EXAMPLES.md
Outdated
``` | ||
|
||
```json | ||
// src/legacy/core_plugins/my-plugin/mappings.json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// src/legacy/core_plugins/my-plugin/mappings.json | |
// src/legacy/core_plugins/my_plugin/mappings.json |
src/core/MIGRATION_EXAMPLES.md
Outdated
Let say we have the following in a legacy plugin: | ||
|
||
```js | ||
// src/legacy/core_plugins/my-plugin/index.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// src/legacy/core_plugins/my-plugin/index.js | |
// src/legacy/core_plugins/my_plugin/index.js |
src/core/MIGRATION_EXAMPLES.md
Outdated
Second type: | ||
|
||
```typescript | ||
// src/plugins/my-plugin/server/saved_objects/second_type.ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// src/plugins/my-plugin/server/saved_objects/second_type.ts | |
// src/plugins/my_plugin/server/saved_objects/second_type.ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more nits (unfortunately I couldn't always make suggestions on the source code)
|
||
## SavedObjectTypeRegistry class | ||
|
||
Registry holding information about all the registered [savedObject types](./kibana-plugin-server.savedobjectstype.md)<!-- -->. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registry holding information about all the registered [savedObject types](./kibana-plugin-server.savedobjectstype.md)<!-- -->. | |
Registry holding information about all the registered [saved object types](./kibana-plugin-server.savedobjectstype.md)<!-- -->. |
@@ -158,6 +203,11 @@ export interface SavedObjectsServiceStart { | |||
* Creates a {@link SavedObjectsSerializer | serializer} that is aware of all registered types. | |||
*/ | |||
createSerializer: () => SavedObjectsSerializer; | |||
/** | |||
* Returns the {@link ISavedObjectTypeRegistry | registry} containing all registered | |||
* {@link SavedObjectsType | savedObject types} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* {@link SavedObjectsType | savedObject types} | |
* {@link SavedObjectsType | saved object types} |
@@ -27,6 +27,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- --> | |||
| [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) | | | |||
| [SavedObjectsRepository](./kibana-plugin-server.savedobjectsrepository.md) | | | |||
| [SavedObjectsSerializer](./kibana-plugin-server.savedobjectsserializer.md) | A serializer that can be used to manually convert [raw](./kibana-plugin-server.savedobjectsrawdoc.md) or [sanitized](./kibana-plugin-server.savedobjectsanitizeddoc.md) documents to the other kind. | | |||
| [SavedObjectTypeRegistry](./kibana-plugin-server.savedobjecttyperegistry.md) | Registry holding information about all the registered [savedObject types](./kibana-plugin-server.savedobjectstype.md)<!-- -->. | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| [SavedObjectTypeRegistry](./kibana-plugin-server.savedobjecttyperegistry.md) | Registry holding information about all the registered [savedObject types](./kibana-plugin-server.savedobjectstype.md)<!-- -->. | | |
| [SavedObjectTypeRegistry](./kibana-plugin-server.savedobjecttyperegistry.md) | Registry holding information about all the registered [saved object types](./kibana-plugin-server.savedobjectstype.md)<!-- -->. | |
…register-type-so-api
💚 Build SucceededHistory
To update your PR or re-run it, just comment with: |
* expose `registerType` API * expose `getTypeRegistry` API * change SavedObjectMigrationFn signature to add context * fix exported types * update generated doc * update migration documentation * fix legacy service test * fix typings * update service setup description * add saved_objects server folder convention * fix unit test * documentation NITs * add typeRegistry to SavedObjectClientWrapperOptions
* expose `registerType` API * expose `getTypeRegistry` API * change SavedObjectMigrationFn signature to add context * fix exported types * update generated doc * update migration documentation * fix legacy service test * fix typings * update service setup description * add saved_objects server folder convention * fix unit test * documentation NITs * add typeRegistry to SavedObjectClientWrapperOptions
Summary
This PR officially makes the
registerType
API introduced in #55857 a public one, and allow plugins to register savedObjects types from the new platform.PR does the following:
registerType
SO setup API from internal to publicgetTypeRegistry
API to the SO start contract to allow plugin to get informations on the registered types (this was a need for a least security)SavedObjectMigrationFn
signature to have a context object as second parameter instead of only a logger. This context only exposes the logger atm, but makes any futur addition now way easier (thanks to @restrry for the suggestion). Legacy migration methods are wrapped to continue passing the logger as the second parameter to ensure BWC.Requires #56971 to be merged.
Fix #50313, #50311, #50309
Checklist
For maintainers
Dev Docs
A new
registerType
API has been added to the core savedObjectssetup
API, allowing to register savedObject types from new platform pluginsPlease check the migration guide for more complete examples and migration procedure.