diff --git a/docs/development/core/server/kibana-plugin-server.authtoolkit.md b/docs/development/core/server/kibana-plugin-server.authtoolkit.md index a6a30dae894ad..4e523a7ce3cf5 100644 --- a/docs/development/core/server/kibana-plugin-server.authtoolkit.md +++ b/docs/development/core/server/kibana-plugin-server.authtoolkit.md @@ -18,5 +18,5 @@ export interface AuthToolkit | --- | --- | --- | | [authenticated](./kibana-plugin-server.authtoolkit.authenticated.md) | (data?: AuthResultParams) => AuthResult | Authentication is successful with given credentials, allow request to pass through | | [notHandled](./kibana-plugin-server.authtoolkit.nothandled.md) | () => AuthResult | User has no credentials. Allows user to access a resource when authRequired: 'optional' Rejects a request when authRequired: true | -| [redirected](./kibana-plugin-server.authtoolkit.redirected.md) | (headers: {
location: string;
} & ResponseHeaders) => AuthResult | Redirect user to IdP when authRequired: true Allows user to access a resource without redirection when authRequired: 'optional' | +| [redirected](./kibana-plugin-server.authtoolkit.redirected.md) | (headers: {
location: string;
} & ResponseHeaders) => AuthResult | Redirects user to another location to complete authentication when authRequired: true Allows user to access a resource without redirection when authRequired: 'optional' | diff --git a/docs/development/core/server/kibana-plugin-server.authtoolkit.redirected.md b/docs/development/core/server/kibana-plugin-server.authtoolkit.redirected.md index 64d1d04a4abc0..15d5498d90119 100644 --- a/docs/development/core/server/kibana-plugin-server.authtoolkit.redirected.md +++ b/docs/development/core/server/kibana-plugin-server.authtoolkit.redirected.md @@ -4,7 +4,7 @@ ## AuthToolkit.redirected property -Redirect user to IdP when authRequired: true Allows user to access a resource without redirection when authRequired: 'optional' +Redirects user to another location to complete authentication when authRequired: true Allows user to access a resource without redirection when authRequired: 'optional' Signature: diff --git a/docs/development/core/server/kibana-plugin-server.isavedobjecttyperegistry.md b/docs/development/core/server/kibana-plugin-server.isavedobjecttyperegistry.md index bbcba50c81027..6b0012b4ce46c 100644 --- a/docs/development/core/server/kibana-plugin-server.isavedobjecttyperegistry.md +++ b/docs/development/core/server/kibana-plugin-server.isavedobjecttyperegistry.md @@ -9,5 +9,5 @@ See [SavedObjectTypeRegistry](./kibana-plugin-server.savedobjecttyperegistry.md) Signature: ```typescript -export declare type ISavedObjectTypeRegistry = Pick; +export declare type ISavedObjectTypeRegistry = Pick; ``` diff --git a/docs/development/core/server/kibana-plugin-server.md b/docs/development/core/server/kibana-plugin-server.md index c84585bf6cb65..ff243dbb91a89 100644 --- a/docs/development/core/server/kibana-plugin-server.md +++ b/docs/development/core/server/kibana-plugin-server.md @@ -116,7 +116,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [PluginManifest](./kibana-plugin-server.pluginmanifest.md) | Describes the set of required and optional properties plugin can define in its mandatory JSON manifest file. | | [PluginsServiceSetup](./kibana-plugin-server.pluginsservicesetup.md) | | | [PluginsServiceStart](./kibana-plugin-server.pluginsservicestart.md) | | -| [RequestHandlerContext](./kibana-plugin-server.requesthandlercontext.md) | Plugin specific context passed to a route handler.Provides the following clients: - [rendering](./kibana-plugin-server.iscopedrenderingclient.md) - Rendering client which uses the data of the incoming request - [savedObjects.client](./kibana-plugin-server.savedobjectsclient.md) - Saved Objects client which uses the credentials of the incoming request - [elasticsearch.dataClient](./kibana-plugin-server.scopedclusterclient.md) - Elasticsearch data client which uses the credentials of the incoming request - [elasticsearch.adminClient](./kibana-plugin-server.scopedclusterclient.md) - Elasticsearch admin client which uses the credentials of the incoming request - [uiSettings.client](./kibana-plugin-server.iuisettingsclient.md) - uiSettings client which uses the credentials of the incoming request | +| [RequestHandlerContext](./kibana-plugin-server.requesthandlercontext.md) | Plugin specific context passed to a route handler.Provides the following clients and services: - [rendering](./kibana-plugin-server.iscopedrenderingclient.md) - Rendering client which uses the data of the incoming request - [savedObjects.client](./kibana-plugin-server.savedobjectsclient.md) - Saved Objects client which uses the credentials of the incoming request - [savedObjects.typeRegistry](./kibana-plugin-server.isavedobjecttyperegistry.md) - Type registry containing all the registered types. - [elasticsearch.dataClient](./kibana-plugin-server.scopedclusterclient.md) - Elasticsearch data client which uses the credentials of the incoming request - [elasticsearch.adminClient](./kibana-plugin-server.scopedclusterclient.md) - Elasticsearch admin client which uses the credentials of the incoming request - [uiSettings.client](./kibana-plugin-server.iuisettingsclient.md) - uiSettings client which uses the credentials of the incoming request | | [RouteConfig](./kibana-plugin-server.routeconfig.md) | Route specific configuration. | | [RouteConfigOptions](./kibana-plugin-server.routeconfigoptions.md) | Additional route options. | | [RouteConfigOptionsBody](./kibana-plugin-server.routeconfigoptionsbody.md) | Additional body options for a route | @@ -164,6 +164,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [SavedObjectsServiceSetup](./kibana-plugin-server.savedobjectsservicesetup.md) | 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, creating and registering Saved Object client wrappers and factories. | | [SavedObjectsServiceStart](./kibana-plugin-server.savedobjectsservicestart.md) | Saved Objects is Kibana's data persisentence mechanism allowing plugins to use Elasticsearch for storing and querying state. The SavedObjectsServiceStart API provides a scoped Saved Objects client for interacting with Saved Objects. | | [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) | | +| [SavedObjectsTypeManagementDefinition](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) | Configuration options for the [type](./kibana-plugin-server.savedobjectstype.md)'s management section. | | [SavedObjectsTypeMappingDefinition](./kibana-plugin-server.savedobjectstypemappingdefinition.md) | Describe a saved object type mapping. | | [SavedObjectsUpdateOptions](./kibana-plugin-server.savedobjectsupdateoptions.md) | | | [SavedObjectsUpdateResponse](./kibana-plugin-server.savedobjectsupdateresponse.md) | | diff --git a/docs/development/core/server/kibana-plugin-server.requesthandlercontext.core.md b/docs/development/core/server/kibana-plugin-server.requesthandlercontext.core.md index 77bfd85e6e54b..18787d1c7c9a4 100644 --- a/docs/development/core/server/kibana-plugin-server.requesthandlercontext.core.md +++ b/docs/development/core/server/kibana-plugin-server.requesthandlercontext.core.md @@ -11,6 +11,7 @@ core: { rendering: IScopedRenderingClient; savedObjects: { client: SavedObjectsClientContract; + typeRegistry: ISavedObjectTypeRegistry; }; elasticsearch: { dataClient: IScopedClusterClient; diff --git a/docs/development/core/server/kibana-plugin-server.requesthandlercontext.md b/docs/development/core/server/kibana-plugin-server.requesthandlercontext.md index 4d14d890f51a2..4365da24d1489 100644 --- a/docs/development/core/server/kibana-plugin-server.requesthandlercontext.md +++ b/docs/development/core/server/kibana-plugin-server.requesthandlercontext.md @@ -6,7 +6,7 @@ Plugin specific context passed to a route handler. -Provides the following clients: - [rendering](./kibana-plugin-server.iscopedrenderingclient.md) - Rendering client which uses the data of the incoming request - [savedObjects.client](./kibana-plugin-server.savedobjectsclient.md) - Saved Objects client which uses the credentials of the incoming request - [elasticsearch.dataClient](./kibana-plugin-server.scopedclusterclient.md) - Elasticsearch data client which uses the credentials of the incoming request - [elasticsearch.adminClient](./kibana-plugin-server.scopedclusterclient.md) - Elasticsearch admin client which uses the credentials of the incoming request - [uiSettings.client](./kibana-plugin-server.iuisettingsclient.md) - uiSettings client which uses the credentials of the incoming request +Provides the following clients and services: - [rendering](./kibana-plugin-server.iscopedrenderingclient.md) - Rendering client which uses the data of the incoming request - [savedObjects.client](./kibana-plugin-server.savedobjectsclient.md) - Saved Objects client which uses the credentials of the incoming request - [savedObjects.typeRegistry](./kibana-plugin-server.isavedobjecttyperegistry.md) - Type registry containing all the registered types. - [elasticsearch.dataClient](./kibana-plugin-server.scopedclusterclient.md) - Elasticsearch data client which uses the credentials of the incoming request - [elasticsearch.adminClient](./kibana-plugin-server.scopedclusterclient.md) - Elasticsearch admin client which uses the credentials of the incoming request - [uiSettings.client](./kibana-plugin-server.iuisettingsclient.md) - uiSettings client which uses the credentials of the incoming request Signature: @@ -18,5 +18,5 @@ export interface RequestHandlerContext | Property | Type | Description | | --- | --- | --- | -| [core](./kibana-plugin-server.requesthandlercontext.core.md) | {
rendering: IScopedRenderingClient;
savedObjects: {
client: SavedObjectsClientContract;
};
elasticsearch: {
dataClient: IScopedClusterClient;
adminClient: IScopedClusterClient;
};
uiSettings: {
client: IUiSettingsClient;
};
} | | +| [core](./kibana-plugin-server.requesthandlercontext.core.md) | {
rendering: IScopedRenderingClient;
savedObjects: {
client: SavedObjectsClientContract;
typeRegistry: ISavedObjectTypeRegistry;
};
elasticsearch: {
dataClient: IScopedClusterClient;
adminClient: IScopedClusterClient;
};
uiSettings: {
client: IUiSettingsClient;
};
} | | diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.management.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.management.md new file mode 100644 index 0000000000000..301e80d74ed57 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.management.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) > [management](./kibana-plugin-server.savedobjectstype.management.md) + +## SavedObjectsType.management property + +An optional [saved objects management section](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) definition for the type. + +Signature: + +```typescript +management?: SavedObjectsTypeManagementDefinition; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.md index 1e989652e52bf..546d83ad0d8dc 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectstype.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.md @@ -21,6 +21,7 @@ This is only internal for now, and will only be public when we expose the regist | [convertToAliasScript](./kibana-plugin-server.savedobjectstype.converttoaliasscript.md) | string | If defined, will be used to convert the type to an alias. | | [hidden](./kibana-plugin-server.savedobjectstype.hidden.md) | boolean | Is the type hidden by default. If true, repositories will not have access to this type unless explicitly declared as an extraType when creating the repository.See [createInternalRepository](./kibana-plugin-server.savedobjectsservicestart.createinternalrepository.md). | | [indexPattern](./kibana-plugin-server.savedobjectstype.indexpattern.md) | string | If defined, the type instances will be stored in the given index instead of the default one. | +| [management](./kibana-plugin-server.savedobjectstype.management.md) | SavedObjectsTypeManagementDefinition | An optional [saved objects management section](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) definition for the type. | | [mappings](./kibana-plugin-server.savedobjectstype.mappings.md) | SavedObjectsTypeMappingDefinition | The [mapping definition](./kibana-plugin-server.savedobjectstypemappingdefinition.md) for the type. | | [migrations](./kibana-plugin-server.savedobjectstype.migrations.md) | SavedObjectMigrationMap | An optional map of [migrations](./kibana-plugin-server.savedobjectmigrationfn.md) to be used to migrate the type. | | [name](./kibana-plugin-server.savedobjectstype.name.md) | string | The name of the type, which is also used as the internal id. | diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.defaultsearchfield.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.defaultsearchfield.md new file mode 100644 index 0000000000000..229f0fd567b5d --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.defaultsearchfield.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeManagementDefinition](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) > [defaultSearchField](./kibana-plugin-server.savedobjectstypemanagementdefinition.defaultsearchfield.md) + +## SavedObjectsTypeManagementDefinition.defaultSearchField property + +The default search field to use for this type. Defaults to `id`. + +Signature: + +```typescript +defaultSearchField?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.getediturl.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.getediturl.md new file mode 100644 index 0000000000000..276167560ebbf --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.getediturl.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeManagementDefinition](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) > [getEditUrl](./kibana-plugin-server.savedobjectstypemanagementdefinition.getediturl.md) + +## SavedObjectsTypeManagementDefinition.getEditUrl property + +Function returning the url to use to redirect to the editing page of this object. If not defined, editing will not be allowed. + +Signature: + +```typescript +getEditUrl?: (savedObject: SavedObject) => string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.getinappurl.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.getinappurl.md new file mode 100644 index 0000000000000..82934985f3ad5 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.getinappurl.md @@ -0,0 +1,16 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeManagementDefinition](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) > [getInAppUrl](./kibana-plugin-server.savedobjectstypemanagementdefinition.getinappurl.md) + +## SavedObjectsTypeManagementDefinition.getInAppUrl property + +Function returning the url to use to redirect to this object from the management section. If not defined, redirecting to the object will not be allowed. + +Signature: + +```typescript +getInAppUrl?: (savedObject: SavedObject) => { + path: string; + uiCapabilitiesPath: string; + }; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.gettitle.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.gettitle.md new file mode 100644 index 0000000000000..348d80031a2e1 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.gettitle.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeManagementDefinition](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) > [getTitle](./kibana-plugin-server.savedobjectstypemanagementdefinition.gettitle.md) + +## SavedObjectsTypeManagementDefinition.getTitle property + +Function returning the title to display in the management table. If not defined, will use the object's type and id to generate a label. + +Signature: + +```typescript +getTitle?: (savedObject: SavedObject) => string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.icon.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.icon.md new file mode 100644 index 0000000000000..1126c77106609 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.icon.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeManagementDefinition](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) > [icon](./kibana-plugin-server.savedobjectstypemanagementdefinition.icon.md) + +## SavedObjectsTypeManagementDefinition.icon property + +The eui icon name to display in the management table. If not defined, the default icon will be used. + +Signature: + +```typescript +icon?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.importableandexportable.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.importableandexportable.md new file mode 100644 index 0000000000000..30a20f1a1b03e --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.importableandexportable.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeManagementDefinition](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) > [importableAndExportable](./kibana-plugin-server.savedobjectstypemanagementdefinition.importableandexportable.md) + +## SavedObjectsTypeManagementDefinition.importableAndExportable property + +Is the type importable or exportable. Defaults to `false`. + +Signature: + +```typescript +importableAndExportable?: boolean; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.md new file mode 100644 index 0000000000000..b54944b24035a --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemanagementdefinition.md @@ -0,0 +1,25 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeManagementDefinition](./kibana-plugin-server.savedobjectstypemanagementdefinition.md) + +## SavedObjectsTypeManagementDefinition interface + +Configuration options for the [type](./kibana-plugin-server.savedobjectstype.md)'s management section. + +Signature: + +```typescript +export interface SavedObjectsTypeManagementDefinition +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [defaultSearchField](./kibana-plugin-server.savedobjectstypemanagementdefinition.defaultsearchfield.md) | string | The default search field to use for this type. Defaults to id. | +| [getEditUrl](./kibana-plugin-server.savedobjectstypemanagementdefinition.getediturl.md) | (savedObject: SavedObject<any>) => string | Function returning the url to use to redirect to the editing page of this object. If not defined, editing will not be allowed. | +| [getInAppUrl](./kibana-plugin-server.savedobjectstypemanagementdefinition.getinappurl.md) | (savedObject: SavedObject<any>) => {
path: string;
uiCapabilitiesPath: string;
} | Function returning the url to use to redirect to this object from the management section. If not defined, redirecting to the object will not be allowed. | +| [getTitle](./kibana-plugin-server.savedobjectstypemanagementdefinition.gettitle.md) | (savedObject: SavedObject<any>) => string | Function returning the title to display in the management table. If not defined, will use the object's type and id to generate a label. | +| [icon](./kibana-plugin-server.savedobjectstypemanagementdefinition.icon.md) | string | The eui icon name to display in the management table. If not defined, the default icon will be used. | +| [importableAndExportable](./kibana-plugin-server.savedobjectstypemanagementdefinition.importableandexportable.md) | boolean | Is the type importable or exportable. Defaults to false. | + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.dynamic.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.dynamic.md index 0efab7bebfbe5..b6a3fa7a39811 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.dynamic.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.dynamic.md @@ -4,7 +4,7 @@ ## SavedObjectsTypeMappingDefinition.dynamic property -The dynamic property of the mapping. either `false` or 'strict'. Defaults to strict +The dynamic property of the mapping. either `false` or 'strict'. Defaults to `false` Signature: diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.md index 8c1a279894ffd..2f60c04f5f917 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.md @@ -41,6 +41,6 @@ const typeDefinition: SavedObjectsTypeMappingDefinition = { | Property | Type | Description | | --- | --- | --- | -| [dynamic](./kibana-plugin-server.savedobjectstypemappingdefinition.dynamic.md) | false | 'strict' | The dynamic property of the mapping. either false or 'strict'. Defaults to strict | +| [dynamic](./kibana-plugin-server.savedobjectstypemappingdefinition.dynamic.md) | false | 'strict' | The dynamic property of the mapping. either false or 'strict'. Defaults to false | | [properties](./kibana-plugin-server.savedobjectstypemappingdefinition.properties.md) | SavedObjectsMappingProperties | The underlying properties of the type mapping | diff --git a/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.getimportableandexportabletypes.md b/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.getimportableandexportabletypes.md new file mode 100644 index 0000000000000..c9eb9c9c0c468 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.getimportableandexportabletypes.md @@ -0,0 +1,17 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectTypeRegistry](./kibana-plugin-server.savedobjecttyperegistry.md) > [getImportableAndExportableTypes](./kibana-plugin-server.savedobjecttyperegistry.getimportableandexportabletypes.md) + +## SavedObjectTypeRegistry.getImportableAndExportableTypes() method + +Return all [types](./kibana-plugin-server.savedobjectstype.md) currently registered that are importable/exportable. + +Signature: + +```typescript +getImportableAndExportableTypes(): SavedObjectsType[]; +``` +Returns: + +`SavedObjectsType[]` + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.isimportableandexportable.md b/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.isimportableandexportable.md new file mode 100644 index 0000000000000..4d6e95e100646 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.isimportableandexportable.md @@ -0,0 +1,24 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectTypeRegistry](./kibana-plugin-server.savedobjecttyperegistry.md) > [isImportableAndExportable](./kibana-plugin-server.savedobjecttyperegistry.isimportableandexportable.md) + +## SavedObjectTypeRegistry.isImportableAndExportable() method + +Returns the `management.importableAndExportable` property for given type, or `false` if the type is not registered or does not define a management section. + +Signature: + +```typescript +isImportableAndExportable(type: string): boolean; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| type | string | | + +Returns: + +`boolean` + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.md b/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.md index 3daad35808624..66ca9768b7187 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjecttyperegistry.md @@ -17,9 +17,11 @@ export declare class SavedObjectTypeRegistry | Method | Modifiers | Description | | --- | --- | --- | | [getAllTypes()](./kibana-plugin-server.savedobjecttyperegistry.getalltypes.md) | | Return all [types](./kibana-plugin-server.savedobjectstype.md) currently registered. | +| [getImportableAndExportableTypes()](./kibana-plugin-server.savedobjecttyperegistry.getimportableandexportabletypes.md) | | Return all [types](./kibana-plugin-server.savedobjectstype.md) currently registered that are importable/exportable. | | [getIndex(type)](./kibana-plugin-server.savedobjecttyperegistry.getindex.md) | | Returns the indexPattern property for given type, or undefined if the type is not registered. | | [getType(type)](./kibana-plugin-server.savedobjecttyperegistry.gettype.md) | | Return the [type](./kibana-plugin-server.savedobjectstype.md) definition for given type name. | | [isHidden(type)](./kibana-plugin-server.savedobjecttyperegistry.ishidden.md) | | Returns the hidden property for given type, or false if the type is not registered. | +| [isImportableAndExportable(type)](./kibana-plugin-server.savedobjecttyperegistry.isimportableandexportable.md) | | Returns the management.importableAndExportable property for given type, or false if the type is not registered or does not define a management section. | | [isNamespaceAgnostic(type)](./kibana-plugin-server.savedobjecttyperegistry.isnamespaceagnostic.md) | | Returns the namespaceAgnostic property for given type, or false if the type is not registered. | | [registerType(type)](./kibana-plugin-server.savedobjecttyperegistry.registertype.md) | | Register a [type](./kibana-plugin-server.savedobjectstype.md) inside the registry. A type can only be registered once. subsequent calls with the same type name will throw an error. | diff --git a/src/core/MIGRATION.md b/src/core/MIGRATION.md index c5e649f7d9d5c..e04d45f77db5d 100644 --- a/src/core/MIGRATION.md +++ b/src/core/MIGRATION.md @@ -1210,6 +1210,7 @@ In server code, `core` can be accessed from either `server.newPlatform` or `kbnS | `kibana.Plugin.savedObjectSchemas` | [`core.savedObjects.registerType`](docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.registertype.md) | [Examples](./MIGRATION_EXAMPLES.md#saved-objects-types) | | `kibana.Plugin.mappings` | [`core.savedObjects.registerType`](docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.registertype.md) | [Examples](./MIGRATION_EXAMPLES.md#saved-objects-types) | | `kibana.Plugin.migrations` | [`core.savedObjects.registerType`](docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.registertype.md) | [Examples](./MIGRATION_EXAMPLES.md#saved-objects-types) | +| `kibana.Plugin.savedObjectsManagement` | [`core.savedObjects.registerType`](docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.registertype.md) | [Examples](./MIGRATION_EXAMPLES.md#saved-objects-types) | _See also: [Server's CoreSetup API Docs](/docs/development/core/server/kibana-plugin-server.coresetup.md)_ diff --git a/src/core/MIGRATION_EXAMPLES.md b/src/core/MIGRATION_EXAMPLES.md index 2953edb535f47..29edef476d7c3 100644 --- a/src/core/MIGRATION_EXAMPLES.md +++ b/src/core/MIGRATION_EXAMPLES.md @@ -749,7 +749,7 @@ using the core `savedObjects`'s `registerType` setup API. The most notable difference is that in the new platform, the type registration is performed in a single call to `registerType`, passing a new `SavedObjectsType` structure that is a superset of the legacy `schema`, `migrations` -and `mappings`. +`mappings` and `savedObjectsManagement`. ### Concrete example @@ -775,6 +775,32 @@ new kibana.Plugin({ isHidden: true, }, }, + savedObjectsManagement: { + 'first-type': { + isImportableAndExportable: true, + icon: 'myFirstIcon', + defaultSearchField: 'title', + getTitle(obj) { + return obj.attributes.title; + }, + getEditUrl(obj) { + return `/some-url/${encodeURIComponent(obj.id)}`; + }, + }, + 'second-type': { + isImportableAndExportable: false, + icon: 'mySecondIcon', + getTitle(obj) { + return obj.attributes.myTitleField; + }, + getInAppUrl(obj) { + return { + path: `/some-url/${encodeURIComponent(obj.id)}`, + uiCapabilitiesPath: 'myPlugin.myType.show', + }; + }, + }, + }, }, }) ``` @@ -844,6 +870,17 @@ export const firstType: SavedObjectsType = { '1.0.0': migrateFirstTypeToV1, '2.0.0': migrateFirstTypeToV2, }, + management: { + importableAndExportable: true, + icon: 'myFirstIcon', + defaultSearchField: 'title', + getTitle(obj) { + return obj.attributes.title; + }, + getEditUrl(obj) { + return `/some-url/${encodeURIComponent(obj.id)}`; + }, + }, }; ``` @@ -870,6 +907,19 @@ export const secondType: SavedObjectsType = { migrations: { '1.5.0': migrateSecondTypeToV15, }, + management: { + importableAndExportable: false, + icon: 'mySecondIcon', + getTitle(obj) { + return obj.attributes.myTitleField; + }, + getInAppUrl(obj) { + return { + path: `/some-url/${encodeURIComponent(obj.id)}`, + uiCapabilitiesPath: 'myPlugin.myType.show', + }; + }, + }, }; ``` @@ -895,6 +945,8 @@ The NP `registerType` expected input is very close to the legacy format. However - 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 `savedObjectsManagement.isImportableAndExportable` property has been renamed: `SavedObjectsType.management.importableAndExportable` + - The migration function signature has changed: In legacy, it was `(doc: SavedObjectUnsanitizedDoc, log: SavedObjectsMigrationLogger) => SavedObjectUnsanitizedDoc;` In new platform, it is now `(doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc;` diff --git a/src/core/server/index.ts b/src/core/server/index.ts index 80eabe778ece3..e2faf49ba7a9e 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -51,7 +51,11 @@ import { PluginsServiceSetup, PluginsServiceStart, PluginOpaqueId } from './plug import { ContextSetup } from './context'; import { IUiSettingsClient, UiSettingsServiceSetup, UiSettingsServiceStart } from './ui_settings'; import { SavedObjectsClientContract } from './saved_objects/types'; -import { SavedObjectsServiceSetup, SavedObjectsServiceStart } from './saved_objects'; +import { + ISavedObjectTypeRegistry, + SavedObjectsServiceSetup, + SavedObjectsServiceStart, +} from './saved_objects'; import { CapabilitiesSetup, CapabilitiesStart } from './capabilities'; import { UuidServiceSetup } from './uuid'; import { MetricsServiceSetup } from './metrics'; @@ -233,6 +237,7 @@ export { SavedObjectTypeRegistry, ISavedObjectTypeRegistry, SavedObjectsType, + SavedObjectsTypeManagementDefinition, SavedObjectMigrationMap, SavedObjectMigrationFn, exportSavedObjectsToStream, @@ -289,11 +294,13 @@ export { /** * Plugin specific context passed to a route handler. * - * Provides the following clients: + * Provides the following clients and services: * - {@link IScopedRenderingClient | rendering} - Rendering client * which uses the data of the incoming request * - {@link SavedObjectsClient | savedObjects.client} - Saved Objects client * which uses the credentials of the incoming request + * - {@link ISavedObjectTypeRegistry | savedObjects.typeRegistry} - Type registry containing + * all the registered types. * - {@link ScopedClusterClient | elasticsearch.dataClient} - Elasticsearch * data client which uses the credentials of the incoming request * - {@link ScopedClusterClient | elasticsearch.adminClient} - Elasticsearch @@ -308,6 +315,7 @@ export interface RequestHandlerContext { rendering: IScopedRenderingClient; savedObjects: { client: SavedObjectsClientContract; + typeRegistry: ISavedObjectTypeRegistry; }; elasticsearch: { dataClient: IScopedClusterClient; diff --git a/src/core/server/mocks.ts b/src/core/server/mocks.ts index 93d8e2c632e38..a0bbe623289d8 100644 --- a/src/core/server/mocks.ts +++ b/src/core/server/mocks.ts @@ -26,6 +26,7 @@ import { httpServiceMock } from './http/http_service.mock'; import { contextServiceMock } from './context/context_service.mock'; import { savedObjectsServiceMock } from './saved_objects/saved_objects_service.mock'; import { savedObjectsClientMock } from './saved_objects/service/saved_objects_client.mock'; +import { typeRegistryMock as savedObjectsTypeRegistryMock } from './saved_objects/saved_objects_type_registry.mock'; import { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock'; import { SharedGlobalConfig } from './plugins'; import { InternalCoreSetup, InternalCoreStart } from './internal_types'; @@ -177,6 +178,7 @@ function createCoreRequestHandlerContextMock() { }, savedObjects: { client: savedObjectsClientMock.create(), + typeRegistry: savedObjectsTypeRegistryMock.create(), }, elasticsearch: { adminClient: elasticsearchServiceMock.createScopedClusterClient(), diff --git a/src/core/server/saved_objects/__snapshots__/utils.test.ts.snap b/src/core/server/saved_objects/__snapshots__/utils.test.ts.snap index 89ff2b542c60f..5431d2ca47892 100644 --- a/src/core/server/saved_objects/__snapshots__/utils.test.ts.snap +++ b/src/core/server/saved_objects/__snapshots__/utils.test.ts.snap @@ -6,6 +6,7 @@ Array [ "convertToAliasScript": undefined, "hidden": false, "indexPattern": undefined, + "management": undefined, "mappings": Object { "properties": Object { "fieldA": Object { @@ -21,6 +22,7 @@ Array [ "convertToAliasScript": undefined, "hidden": false, "indexPattern": undefined, + "management": undefined, "mappings": Object { "properties": Object { "fieldB": Object { @@ -36,6 +38,7 @@ Array [ "convertToAliasScript": undefined, "hidden": false, "indexPattern": undefined, + "management": undefined, "mappings": Object { "properties": Object { "fieldC": Object { @@ -56,6 +59,7 @@ Array [ "convertToAliasScript": undefined, "hidden": true, "indexPattern": "myIndex", + "management": undefined, "mappings": Object { "properties": Object { "fieldA": Object { @@ -74,6 +78,7 @@ Array [ "convertToAliasScript": "some alias script", "hidden": false, "indexPattern": undefined, + "management": undefined, "mappings": Object { "properties": Object { "anotherFieldB": Object { @@ -92,6 +97,7 @@ Array [ "convertToAliasScript": undefined, "hidden": false, "indexPattern": undefined, + "management": undefined, "mappings": Object { "properties": Object { "fieldC": Object { @@ -114,6 +120,7 @@ Array [ "convertToAliasScript": undefined, "hidden": true, "indexPattern": "fooBar", + "management": undefined, "mappings": Object { "properties": Object { "fieldA": Object { @@ -129,6 +136,7 @@ Array [ "convertToAliasScript": undefined, "hidden": false, "indexPattern": undefined, + "management": undefined, "mappings": Object { "properties": Object { "fieldC": Object { diff --git a/src/core/server/saved_objects/index.ts b/src/core/server/saved_objects/index.ts index 661c6cbb79e58..0af8ea7d0e830 100644 --- a/src/core/server/saved_objects/index.ts +++ b/src/core/server/saved_objects/index.ts @@ -70,7 +70,7 @@ export { SavedObjectMigrationContext, } from './migrations'; -export { SavedObjectsType } from './types'; +export { SavedObjectsType, SavedObjectsTypeManagementDefinition } from './types'; export { savedObjectsConfig, savedObjectsMigrationConfig } from './saved_objects_config'; export { SavedObjectTypeRegistry, ISavedObjectTypeRegistry } from './saved_objects_type_registry'; diff --git a/src/core/server/saved_objects/management/index.ts b/src/core/server/saved_objects/management/index.ts index c32639e74d079..a256a1333c5cc 100644 --- a/src/core/server/saved_objects/management/index.ts +++ b/src/core/server/saved_objects/management/index.ts @@ -17,4 +17,4 @@ * under the License. */ -export { SavedObjectsManagement, SavedObjectsManagementDefinition } from './management'; +export { SavedObjectsManagement } from './management'; diff --git a/src/core/server/saved_objects/management/management.mock.ts b/src/core/server/saved_objects/management/management.mock.ts index 2099cc0f77bcc..e7242c30d3961 100644 --- a/src/core/server/saved_objects/management/management.mock.ts +++ b/src/core/server/saved_objects/management/management.mock.ts @@ -24,6 +24,7 @@ const createManagementMock = () => { const mocked: jest.Mocked = { isImportAndExportable: jest.fn().mockReturnValue(true), getDefaultSearchField: jest.fn(), + getImportableAndExportableTypes: jest.fn(), getIcon: jest.fn(), getTitle: jest.fn(), getEditUrl: jest.fn(), diff --git a/src/core/server/saved_objects/management/management.test.ts b/src/core/server/saved_objects/management/management.test.ts index e936326d957f9..dc110dec020f0 100644 --- a/src/core/server/saved_objects/management/management.test.ts +++ b/src/core/server/saved_objects/management/management.test.ts @@ -18,157 +18,185 @@ */ import { SavedObjectsManagement } from './management'; +import { SavedObjectsType } from '../types'; +import { SavedObjectTypeRegistry } from '../saved_objects_type_registry'; -describe('isImportAndExportable()', () => { - it('returns false for unknown types', () => { - const management = new SavedObjectsManagement(); - const result = management.isImportAndExportable('bar'); - expect(result).toBe(false); - }); +describe('SavedObjectsManagement', () => { + let registry: SavedObjectTypeRegistry; + let management: SavedObjectsManagement; - it('returns true for explicitly importable and exportable type', () => { - const management = new SavedObjectsManagement({ - foo: { - isImportableAndExportable: true, - }, + const registerType = (type: Partial) => + registry.registerType({ + name: 'unknown', + hidden: false, + namespaceAgnostic: false, + mappings: { properties: {} }, + migrations: {}, + ...type, }); - const result = management.isImportAndExportable('foo'); - expect(result).toBe(true); + + beforeEach(() => { + registry = new SavedObjectTypeRegistry(); + management = new SavedObjectsManagement(registry); }); - it('returns false for explicitly importable and exportable type', () => { - const management = new SavedObjectsManagement({ - foo: { - isImportableAndExportable: false, - }, + describe('isImportAndExportable()', () => { + it('returns false for unknown types', () => { + const result = management.isImportAndExportable('bar'); + expect(result).toBe(false); }); - const result = management.isImportAndExportable('foo'); - expect(result).toBe(false); - }); -}); -describe('getDefaultSearchField()', () => { - it('returns empty for unknown types', () => { - const management = new SavedObjectsManagement(); - const result = management.getDefaultSearchField('bar'); - expect(result).toEqual(undefined); - }); + it('returns true for explicitly importable and exportable type', () => { + registerType({ + name: 'foo', + management: { + importableAndExportable: true, + }, + }); - it('returns explicit value', () => { - const management = new SavedObjectsManagement({ - foo: { - defaultSearchField: 'value', - }, + const result = management.isImportAndExportable('foo'); + expect(result).toBe(true); }); - const result = management.getDefaultSearchField('foo'); - expect(result).toEqual('value'); - }); -}); -describe('getIcon', () => { - it('returns empty for unknown types', () => { - const management = new SavedObjectsManagement(); - const result = management.getIcon('bar'); - expect(result).toEqual(undefined); - }); + it('returns false for explicitly importable and exportable type', () => { + registerType({ + name: 'foo', + management: { + importableAndExportable: false, + }, + }); - it('returns explicit value', () => { - const management = new SavedObjectsManagement({ - foo: { - icon: 'value', - }, + const result = management.isImportAndExportable('foo'); + expect(result).toBe(false); }); - const result = management.getIcon('foo'); - expect(result).toEqual('value'); }); -}); -describe('getTitle', () => { - it('returns empty for unknown type', () => { - const management = new SavedObjectsManagement(); - const result = management.getTitle({ - id: '1', - type: 'foo', - attributes: {}, - references: [], + describe('getDefaultSearchField()', () => { + it('returns empty for unknown types', () => { + const result = management.getDefaultSearchField('bar'); + expect(result).toEqual(undefined); }); - expect(result).toEqual(undefined); - }); - it('returns explicit value', () => { - const management = new SavedObjectsManagement({ - foo: { - getTitle() { - return 'called'; + it('returns explicit value', () => { + registerType({ + name: 'foo', + management: { + defaultSearchField: 'value', }, - }, - }); - const result = management.getTitle({ - id: '1', - type: 'foo', - attributes: {}, - references: [], + }); + + const result = management.getDefaultSearchField('foo'); + expect(result).toEqual('value'); }); - expect(result).toEqual('called'); }); -}); -describe('getEditUrl()', () => { - it('returns empty for unknown type', () => { - const management = new SavedObjectsManagement(); - const result = management.getEditUrl({ - id: '1', - type: 'foo', - attributes: {}, - references: [], + describe('getIcon()', () => { + it('returns empty for unknown types', () => { + const result = management.getIcon('bar'); + expect(result).toEqual(undefined); }); - expect(result).toEqual(undefined); - }); - it('returns explicit value', () => { - const management = new SavedObjectsManagement({ - foo: { - getEditUrl() { - return 'called'; + it('returns explicit value', () => { + registerType({ + name: 'foo', + management: { + icon: 'value', }, - }, - }); - const result = management.getEditUrl({ - id: '1', - type: 'foo', - attributes: {}, - references: [], + }); + const result = management.getIcon('foo'); + expect(result).toEqual('value'); }); - expect(result).toEqual('called'); }); -}); -describe('getInAppUrl()', () => { - it('returns empty array for unknown type', () => { - const management = new SavedObjectsManagement(); - const result = management.getInAppUrl({ - id: '1', - type: 'foo', - attributes: {}, - references: [], + describe('getTitle()', () => { + it('returns empty for unknown type', () => { + const result = management.getTitle({ + id: '1', + type: 'foo', + attributes: {}, + references: [], + }); + expect(result).toEqual(undefined); + }); + + it('returns explicit value', () => { + registerType({ + name: 'foo', + management: { + getTitle() { + return 'called'; + }, + }, + }); + const result = management.getTitle({ + id: '1', + type: 'foo', + attributes: {}, + references: [], + }); + expect(result).toEqual('called'); }); - expect(result).toEqual(undefined); }); - it('returns explicit value', () => { - const management = new SavedObjectsManagement({ - foo: { - getInAppUrl() { - return { path: 'called', uiCapabilitiesPath: 'my.path' }; + describe('getEditUrl()', () => { + it('returns empty for unknown type', () => { + const result = management.getEditUrl({ + id: '1', + type: 'foo', + attributes: {}, + references: [], + }); + expect(result).toEqual(undefined); + }); + + it('returns explicit value', () => { + registerType({ + name: 'foo', + management: { + getEditUrl() { + return 'called'; + }, }, - }, + }); + + const result = management.getEditUrl({ + id: '1', + type: 'foo', + attributes: {}, + references: [], + }); + expect(result).toEqual('called'); + }); + }); + + describe('getInAppUrl()', () => { + it('returns empty array for unknown type', () => { + const result = management.getInAppUrl({ + id: '1', + type: 'foo', + attributes: {}, + references: [], + }); + expect(result).toEqual(undefined); }); - const result = management.getInAppUrl({ - id: '1', - type: 'foo', - attributes: {}, - references: [], + + it('returns explicit value', () => { + registerType({ + name: 'foo', + management: { + getInAppUrl() { + return { path: 'called', uiCapabilitiesPath: 'my.path' }; + }, + }, + }); + + const result = management.getInAppUrl({ + id: '1', + type: 'foo', + attributes: {}, + references: [], + }); + expect(result).toEqual({ path: 'called', uiCapabilitiesPath: 'my.path' }); }); - expect(result).toEqual({ path: 'called', uiCapabilitiesPath: 'my.path' }); }); }); diff --git a/src/core/server/saved_objects/management/management.ts b/src/core/server/saved_objects/management/management.ts index b7dce2c087c5f..db759c4aec752 100644 --- a/src/core/server/saved_objects/management/management.ts +++ b/src/core/server/saved_objects/management/management.ts @@ -18,74 +18,42 @@ */ import { SavedObject } from '../types'; - -interface SavedObjectsManagementTypeDefinition { - isImportableAndExportable?: boolean; - defaultSearchField?: string; - icon?: string; - getTitle?: (savedObject: SavedObject) => string; - getEditUrl?: (savedObject: SavedObject) => string; - getInAppUrl?: (savedObject: SavedObject) => { path: string; uiCapabilitiesPath: string }; -} - -export interface SavedObjectsManagementDefinition { - [key: string]: SavedObjectsManagementTypeDefinition; -} +import { ISavedObjectTypeRegistry } from '../saved_objects_type_registry'; export class SavedObjectsManagement { - private readonly definition?: SavedObjectsManagementDefinition; + constructor(private readonly registry: ISavedObjectTypeRegistry) {} - constructor(managementDefinition?: SavedObjectsManagementDefinition) { - this.definition = managementDefinition; + public getImportableAndExportableTypes() { + return this.registry + .getAllTypes() + .map(type => type.name) + .filter(type => this.isImportAndExportable(type)); } public isImportAndExportable(type: string) { - if (this.definition && this.definition.hasOwnProperty(type)) { - return this.definition[type].isImportableAndExportable === true; - } - - return false; + return this.registry.isImportableAndExportable(type); } public getDefaultSearchField(type: string) { - if (this.definition && this.definition.hasOwnProperty(type)) { - return this.definition[type].defaultSearchField; - } + return this.registry.getType(type)?.management?.defaultSearchField; } public getIcon(type: string) { - if (this.definition && this.definition.hasOwnProperty(type)) { - return this.definition[type].icon; - } + return this.registry.getType(type)?.management?.icon; } public getTitle(savedObject: SavedObject) { - const { type } = savedObject; - if (this.definition && this.definition.hasOwnProperty(type) && this.definition[type].getTitle) { - const { getTitle } = this.definition[type]; - if (getTitle) { - return getTitle(savedObject); - } - } + const getTitle = this.registry.getType(savedObject.type)?.management?.getTitle; + return getTitle ? getTitle(savedObject) : undefined; } public getEditUrl(savedObject: SavedObject) { - const { type } = savedObject; - if (this.definition && this.definition.hasOwnProperty(type)) { - const { getEditUrl } = this.definition[type]; - if (getEditUrl) { - return getEditUrl(savedObject); - } - } + const getEditUrl = this.registry.getType(savedObject.type)?.management?.getEditUrl; + return getEditUrl ? getEditUrl(savedObject) : undefined; } public getInAppUrl(savedObject: SavedObject) { - const { type } = savedObject; - if (this.definition && this.definition.hasOwnProperty(type)) { - const { getInAppUrl } = this.definition[type]; - if (getInAppUrl) { - return getInAppUrl(savedObject); - } - } + const getInAppUrl = this.registry.getType(savedObject.type)?.management?.getInAppUrl; + return getInAppUrl ? getInAppUrl(savedObject) : undefined; } } diff --git a/src/core/server/saved_objects/mappings/types.ts b/src/core/server/saved_objects/mappings/types.ts index bc556c0429981..47fc29f8cf7d2 100644 --- a/src/core/server/saved_objects/mappings/types.ts +++ b/src/core/server/saved_objects/mappings/types.ts @@ -45,7 +45,7 @@ * @public */ export interface SavedObjectsTypeMappingDefinition { - /** The dynamic property of the mapping. either `false` or 'strict'. Defaults to strict */ + /** The dynamic property of the mapping. either `false` or 'strict'. Defaults to `false` */ dynamic?: false | 'strict'; /** The underlying properties of the type mapping */ properties: SavedObjectsMappingProperties; diff --git a/src/core/server/saved_objects/migrations/core/__snapshots__/build_active_mappings.test.ts.snap b/src/core/server/saved_objects/migrations/core/__snapshots__/build_active_mappings.test.ts.snap index 68f90ea70a0c6..fc26d7e9cf6e9 100644 --- a/src/core/server/saved_objects/migrations/core/__snapshots__/build_active_mappings.test.ts.snap +++ b/src/core/server/saved_objects/migrations/core/__snapshots__/build_active_mappings.test.ts.snap @@ -6,7 +6,6 @@ Object { "migrationMappingPropertyHashes": Object { "aaa": "625b32086eb1d1203564cf85062dd22e", "bbb": "18c78c995965207ed3f6e7fc5c6e55fe", - "config": "87aca8fdb053154f11383fce3dbf3edf", "migrationVersion": "4a1746014a75ade3a714e1db5763276f", "namespace": "2f4316de49999235636386fe51dc06c1", "references": "7997cf5a56cc02bdc9c93361bde732b0", @@ -22,14 +21,6 @@ Object { "bbb": Object { "type": "long", }, - "config": Object { - "dynamic": "true", - "properties": Object { - "buildNum": Object { - "type": "keyword", - }, - }, - }, "migrationVersion": Object { "dynamic": "true", "type": "object", @@ -65,7 +56,6 @@ exports[`buildActiveMappings handles the \`dynamic\` property of types 1`] = ` Object { "_meta": Object { "migrationMappingPropertyHashes": Object { - "config": "87aca8fdb053154f11383fce3dbf3edf", "firstType": "635418ab953d81d93f1190b70a8d3f57", "migrationVersion": "4a1746014a75ade3a714e1db5763276f", "namespace": "2f4316de49999235636386fe51dc06c1", @@ -78,14 +68,6 @@ Object { }, "dynamic": "strict", "properties": Object { - "config": Object { - "dynamic": "true", - "properties": Object { - "buildNum": Object { - "type": "keyword", - }, - }, - }, "firstType": Object { "dynamic": "strict", "properties": Object { diff --git a/src/core/server/saved_objects/migrations/core/build_active_mappings.ts b/src/core/server/saved_objects/migrations/core/build_active_mappings.ts index 3afe8aae119d9..4d1a607414ca6 100644 --- a/src/core/server/saved_objects/migrations/core/build_active_mappings.ts +++ b/src/core/server/saved_objects/migrations/core/build_active_mappings.ts @@ -132,14 +132,6 @@ function defaultMapping(): IndexMapping { return { dynamic: 'strict', properties: { - config: { - dynamic: 'true', - properties: { - buildNum: { - type: 'keyword', - }, - }, - }, migrationVersion: { dynamic: 'true', type: 'object', diff --git a/src/core/server/saved_objects/migrations/core/index_migrator.test.ts b/src/core/server/saved_objects/migrations/core/index_migrator.test.ts index a9d0a339c229f..1c2d3f501ff80 100644 --- a/src/core/server/saved_objects/migrations/core/index_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/core/index_migrator.test.ts @@ -58,7 +58,6 @@ describe('IndexMigrator', () => { dynamic: 'strict', _meta: { migrationMappingPropertyHashes: { - config: '87aca8fdb053154f11383fce3dbf3edf', foo: '18c78c995965207ed3f6e7fc5c6e55fe', migrationVersion: '4a1746014a75ade3a714e1db5763276f', namespace: '2f4316de49999235636386fe51dc06c1', @@ -68,10 +67,6 @@ describe('IndexMigrator', () => { }, }, properties: { - config: { - dynamic: 'true', - properties: { buildNum: { type: 'keyword' } }, - }, foo: { type: 'long' }, migrationVersion: { dynamic: 'true', type: 'object' }, namespace: { type: 'keyword' }, @@ -180,7 +175,6 @@ describe('IndexMigrator', () => { dynamic: 'strict', _meta: { migrationMappingPropertyHashes: { - config: '87aca8fdb053154f11383fce3dbf3edf', foo: '625b32086eb1d1203564cf85062dd22e', migrationVersion: '4a1746014a75ade3a714e1db5763276f', namespace: '2f4316de49999235636386fe51dc06c1', @@ -191,10 +185,6 @@ describe('IndexMigrator', () => { }, properties: { author: { type: 'text' }, - config: { - dynamic: 'true', - properties: { buildNum: { type: 'keyword' } }, - }, foo: { type: 'text' }, migrationVersion: { dynamic: 'true', type: 'object' }, namespace: { type: 'keyword' }, diff --git a/src/core/server/saved_objects/migrations/kibana/__snapshots__/kibana_migrator.test.ts.snap b/src/core/server/saved_objects/migrations/kibana/__snapshots__/kibana_migrator.test.ts.snap index 37a73b11bbc48..507c0b0d9339f 100644 --- a/src/core/server/saved_objects/migrations/kibana/__snapshots__/kibana_migrator.test.ts.snap +++ b/src/core/server/saved_objects/migrations/kibana/__snapshots__/kibana_migrator.test.ts.snap @@ -6,7 +6,6 @@ Object { "migrationMappingPropertyHashes": Object { "amap": "510f1f0adb69830cf8a1c5ce2923ed82", "bmap": "510f1f0adb69830cf8a1c5ce2923ed82", - "config": "87aca8fdb053154f11383fce3dbf3edf", "migrationVersion": "4a1746014a75ade3a714e1db5763276f", "namespace": "2f4316de49999235636386fe51dc06c1", "references": "7997cf5a56cc02bdc9c93361bde732b0", @@ -30,14 +29,6 @@ Object { }, }, }, - "config": Object { - "dynamic": "true", - "properties": Object { - "buildNum": Object { - "type": "keyword", - }, - }, - }, "migrationVersion": Object { "dynamic": "true", "type": "object", diff --git a/src/core/server/saved_objects/routes/export.ts b/src/core/server/saved_objects/routes/export.ts index 04d310681aec5..7205699ddc702 100644 --- a/src/core/server/saved_objects/routes/export.ts +++ b/src/core/server/saved_objects/routes/export.ts @@ -27,32 +27,21 @@ import { import { IRouter } from '../../http'; import { SavedObjectConfig } from '../saved_objects_config'; import { exportSavedObjectsToStream } from '../export'; +import { validateTypes, validateObjects } from './utils'; -export const registerExportRoute = ( - router: IRouter, - config: SavedObjectConfig, - supportedTypes: string[] -) => { +export const registerExportRoute = (router: IRouter, config: SavedObjectConfig) => { const { maxImportExportSize } = config; - const typeSchema = schema.string({ - validate: (type: string) => { - if (!supportedTypes.includes(type)) { - return `${type} is not exportable`; - } - }, - }); - router.post( { path: '/_export', validate: { body: schema.object({ - type: schema.maybe(schema.oneOf([typeSchema, schema.arrayOf(typeSchema)])), + type: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), objects: schema.maybe( schema.arrayOf( schema.object({ - type: typeSchema, + type: schema.string(), id: schema.string(), }), { maxSize: maxImportExportSize } @@ -67,9 +56,36 @@ export const registerExportRoute = ( router.handleLegacyErrors(async (context, req, res) => { const savedObjectsClient = context.core.savedObjects.client; const { type, objects, search, excludeExportDetails, includeReferencesDeep } = req.body; + const types = typeof type === 'string' ? [type] : type; + + // need to access the registry for type validation, can't use the schema for this + const supportedTypes = context.core.savedObjects.typeRegistry + .getImportableAndExportableTypes() + .map(t => t.name); + if (types) { + const validationError = validateTypes(types, supportedTypes); + if (validationError) { + return res.badRequest({ + body: { + message: validationError, + }, + }); + } + } + if (objects) { + const validationError = validateObjects(objects, supportedTypes); + if (validationError) { + return res.badRequest({ + body: { + message: validationError, + }, + }); + } + } + const exportStream = await exportSavedObjectsToStream({ savedObjectsClient, - types: typeof type === 'string' ? [type] : type, + types, search, objects, exportSizeLimit: maxImportExportSize, diff --git a/src/core/server/saved_objects/routes/import.ts b/src/core/server/saved_objects/routes/import.ts index 313e84c0b301d..0731d4159356d 100644 --- a/src/core/server/saved_objects/routes/import.ts +++ b/src/core/server/saved_objects/routes/import.ts @@ -31,11 +31,7 @@ interface FileStream extends Readable { }; } -export const registerImportRoute = ( - router: IRouter, - config: SavedObjectConfig, - supportedTypes: string[] -) => { +export const registerImportRoute = (router: IRouter, config: SavedObjectConfig) => { const { maxImportExportSize, maxImportPayloadBytes } = config; router.post( @@ -65,6 +61,10 @@ export const registerImportRoute = ( return res.badRequest({ body: `Invalid file extension ${fileExtension}` }); } + const supportedTypes = context.core.savedObjects.typeRegistry + .getImportableAndExportableTypes() + .map(type => type.name); + const result = await importSavedObjectsFromStream({ supportedTypes, savedObjectsClient: context.core.savedObjects.client, diff --git a/src/core/server/saved_objects/routes/index.ts b/src/core/server/saved_objects/routes/index.ts index 0afa24b18760b..fd57a9f3059e3 100644 --- a/src/core/server/saved_objects/routes/index.ts +++ b/src/core/server/saved_objects/routes/index.ts @@ -39,13 +39,11 @@ export function registerRoutes({ http, logger, config, - importableExportableTypes, migratorPromise, }: { http: InternalHttpServiceSetup; logger: Logger; config: SavedObjectConfig; - importableExportableTypes: string[]; migratorPromise: Promise; }) { const router = http.createRouter('/api/saved_objects/'); @@ -59,9 +57,9 @@ export function registerRoutes({ registerBulkCreateRoute(router); registerBulkUpdateRoute(router); registerLogLegacyImportRoute(router, logger); - registerExportRoute(router, config, importableExportableTypes); - registerImportRoute(router, config, importableExportableTypes); - registerResolveImportErrorsRoute(router, config, importableExportableTypes); + registerExportRoute(router, config); + registerImportRoute(router, config); + registerResolveImportErrorsRoute(router, config); const internalRouter = http.createRouter('/internal/saved_objects/'); diff --git a/src/core/server/saved_objects/routes/integration_tests/export.test.ts b/src/core/server/saved_objects/routes/integration_tests/export.test.ts index a81079b6825d6..858d34d5a93bf 100644 --- a/src/core/server/saved_objects/routes/integration_tests/export.test.ts +++ b/src/core/server/saved_objects/routes/integration_tests/export.test.ts @@ -27,7 +27,7 @@ import supertest from 'supertest'; import { UnwrapPromise } from '@kbn/utility-types'; import { SavedObjectConfig } from '../../saved_objects_config'; import { registerExportRoute } from '../export'; -import { setupServer } from './test_utils'; +import { setupServer, createExportableType } from './test_utils'; type setupServerReturn = UnwrapPromise>; const exportSavedObjectsToStream = exportMock.exportSavedObjectsToStream as jest.Mock; @@ -40,12 +40,16 @@ const config = { describe('POST /api/saved_objects/_export', () => { let server: setupServerReturn['server']; let httpSetup: setupServerReturn['httpSetup']; + let handlerContext: setupServerReturn['handlerContext']; beforeEach(async () => { - ({ server, httpSetup } = await setupServer()); + ({ server, httpSetup, handlerContext } = await setupServer()); + handlerContext.savedObjects.typeRegistry.getImportableAndExportableTypes.mockReturnValue( + allowedTypes.map(createExportableType) + ); const router = httpSetup.createRouter('/api/saved_objects/'); - registerExportRoute(router, config, allowedTypes); + registerExportRoute(router, config); await server.start(); }); diff --git a/src/core/server/saved_objects/routes/integration_tests/import.test.ts b/src/core/server/saved_objects/routes/integration_tests/import.test.ts index 954e6d9e4831a..c72d3e241b882 100644 --- a/src/core/server/saved_objects/routes/integration_tests/import.test.ts +++ b/src/core/server/saved_objects/routes/integration_tests/import.test.ts @@ -22,7 +22,7 @@ import { UnwrapPromise } from '@kbn/utility-types'; import { registerImportRoute } from '../import'; import { savedObjectsClientMock } from '../../../../../core/server/mocks'; import { SavedObjectConfig } from '../../saved_objects_config'; -import { setupServer } from './test_utils'; +import { setupServer, createExportableType } from './test_utils'; type setupServerReturn = UnwrapPromise>; @@ -47,12 +47,15 @@ describe('POST /internal/saved_objects/_import', () => { beforeEach(async () => { ({ server, httpSetup, handlerContext } = await setupServer()); - savedObjectsClient = handlerContext.savedObjects.client; + handlerContext.savedObjects.typeRegistry.getImportableAndExportableTypes.mockReturnValue( + allowedTypes.map(createExportableType) + ); + savedObjectsClient = handlerContext.savedObjects.client; savedObjectsClient.find.mockResolvedValue(emptyResponse); const router = httpSetup.createRouter('/internal/saved_objects/'); - registerImportRoute(router, config, allowedTypes); + registerImportRoute(router, config); await server.start(); }); diff --git a/src/core/server/saved_objects/routes/integration_tests/resolve_import_errors.test.ts b/src/core/server/saved_objects/routes/integration_tests/resolve_import_errors.test.ts index c2974395217f8..a36f246f9dbc5 100644 --- a/src/core/server/saved_objects/routes/integration_tests/resolve_import_errors.test.ts +++ b/src/core/server/saved_objects/routes/integration_tests/resolve_import_errors.test.ts @@ -21,7 +21,7 @@ import supertest from 'supertest'; import { UnwrapPromise } from '@kbn/utility-types'; import { registerResolveImportErrorsRoute } from '../resolve_import_errors'; import { savedObjectsClientMock } from '../../../../../core/server/mocks'; -import { setupServer } from './test_utils'; +import { setupServer, createExportableType } from './test_utils'; import { SavedObjectConfig } from '../../saved_objects_config'; type setupServerReturn = UnwrapPromise>; @@ -40,10 +40,14 @@ describe('POST /api/saved_objects/_resolve_import_errors', () => { beforeEach(async () => { ({ server, httpSetup, handlerContext } = await setupServer()); + handlerContext.savedObjects.typeRegistry.getImportableAndExportableTypes.mockReturnValue( + allowedTypes.map(createExportableType) + ); + savedObjectsClient = handlerContext.savedObjects.client; const router = httpSetup.createRouter('/api/saved_objects/'); - registerResolveImportErrorsRoute(router, config, allowedTypes); + registerResolveImportErrorsRoute(router, config); await server.start(); }); diff --git a/src/core/server/saved_objects/routes/integration_tests/test_utils.ts b/src/core/server/saved_objects/routes/integration_tests/test_utils.ts index 093b36a413214..82a889f75d3c1 100644 --- a/src/core/server/saved_objects/routes/integration_tests/test_utils.ts +++ b/src/core/server/saved_objects/routes/integration_tests/test_utils.ts @@ -20,6 +20,7 @@ import { ContextService } from '../../../context'; import { createHttpServer, createCoreContext } from '../../../http/test_utils'; import { coreMock } from '../../../mocks'; +import { SavedObjectsType } from '../../types'; const coreId = Symbol('core'); @@ -43,3 +44,17 @@ export const setupServer = async () => { handlerContext, }; }; + +export const createExportableType = (name: string): SavedObjectsType => { + return { + name, + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: {}, + }, + management: { + importableAndExportable: true, + }, + }; +}; diff --git a/src/core/server/saved_objects/routes/resolve_import_errors.ts b/src/core/server/saved_objects/routes/resolve_import_errors.ts index a10a19ba1d8ff..05bff871b3520 100644 --- a/src/core/server/saved_objects/routes/resolve_import_errors.ts +++ b/src/core/server/saved_objects/routes/resolve_import_errors.ts @@ -31,11 +31,7 @@ interface FileStream extends Readable { }; } -export const registerResolveImportErrorsRoute = ( - router: IRouter, - config: SavedObjectConfig, - supportedTypes: string[] -) => { +export const registerResolveImportErrorsRoute = (router: IRouter, config: SavedObjectConfig) => { const { maxImportExportSize, maxImportPayloadBytes } = config; router.post( @@ -75,6 +71,11 @@ export const registerResolveImportErrorsRoute = ( if (fileExtension !== '.ndjson') { return res.badRequest({ body: `Invalid file extension ${fileExtension}` }); } + + const supportedTypes = context.core.savedObjects.typeRegistry + .getImportableAndExportableTypes() + .map(type => type.name); + const result = await resolveSavedObjectsImportErrors({ supportedTypes, savedObjectsClient: context.core.savedObjects.client, diff --git a/src/core/server/saved_objects/routes/utils.test.ts b/src/core/server/saved_objects/routes/utils.test.ts index 83dceda2e1398..24719724785af 100644 --- a/src/core/server/saved_objects/routes/utils.test.ts +++ b/src/core/server/saved_objects/routes/utils.test.ts @@ -17,7 +17,7 @@ * under the License. */ -import { createSavedObjectsStreamFromNdJson } from './utils'; +import { createSavedObjectsStreamFromNdJson, validateTypes, validateObjects } from './utils'; import { Readable } from 'stream'; import { createPromiseFromStreams, createConcatStream } from '../../../../legacy/utils/streams'; @@ -104,3 +104,53 @@ describe('createSavedObjectsStreamFromNdJson', () => { ]); }); }); + +describe('validateTypes', () => { + const allowedTypes = ['config', 'index-pattern', 'dashboard']; + + it('returns an error message if some types are not allowed', () => { + expect(validateTypes(['config', 'not-allowed-type'], allowedTypes)).toMatchInlineSnapshot( + `"Trying to export non-exportable type(s): not-allowed-type"` + ); + expect( + validateTypes(['index-pattern', 'not-allowed-type', 'not-allowed-type-2'], allowedTypes) + ).toMatchInlineSnapshot( + `"Trying to export non-exportable type(s): not-allowed-type, not-allowed-type-2"` + ); + }); + it('returns undefined if all types are allowed', () => { + expect(validateTypes(allowedTypes, allowedTypes)).toBeUndefined(); + expect(validateTypes(['config'], allowedTypes)).toBeUndefined(); + }); +}); + +describe('validateObjects', () => { + const allowedTypes = ['config', 'index-pattern', 'dashboard']; + + it('returns an error message if some objects have types that are not allowed', () => { + expect( + validateObjects( + [ + { id: '1', type: 'config' }, + { id: '1', type: 'not-allowed' }, + { id: '42', type: 'not-allowed-either' }, + ], + allowedTypes + ) + ).toMatchInlineSnapshot( + `"Trying to export object(s) with non-exportable types: not-allowed:1, not-allowed-either:42"` + ); + }); + it('returns undefined if all objects have allowed types', () => { + expect( + validateObjects( + [ + { id: '1', type: 'config' }, + { id: '2', type: 'config' }, + { id: '1', type: 'index-pattern' }, + ], + allowedTypes + ) + ).toBeUndefined(); + }); +}); diff --git a/src/core/server/saved_objects/routes/utils.ts b/src/core/server/saved_objects/routes/utils.ts index 5536391341da3..5f0db3c4d548c 100644 --- a/src/core/server/saved_objects/routes/utils.ts +++ b/src/core/server/saved_objects/routes/utils.ts @@ -41,3 +41,22 @@ export function createSavedObjectsStreamFromNdJson(ndJsonStream: Readable) { ) ); } + +export function validateTypes(types: string[], supportedTypes: string[]): string | undefined { + const invalidTypes = types.filter(t => !supportedTypes.includes(t)); + if (invalidTypes.length) { + return `Trying to export non-exportable type(s): ${invalidTypes.join(', ')}`; + } +} + +export function validateObjects( + objects: Array<{ id: string; type: string }>, + supportedTypes: string[] +): string | undefined { + const invalidObjects = objects.filter(obj => !supportedTypes.includes(obj.type)); + if (invalidObjects.length) { + return `Trying to export object(s) with non-exportable types: ${invalidObjects + .map(obj => `${obj.type}:${obj.id}`) + .join(', ')}`; + } +} diff --git a/src/core/server/saved_objects/saved_objects_service.test.ts b/src/core/server/saved_objects/saved_objects_service.test.ts index 554acf8d43dcb..58b9abfbcdb3a 100644 --- a/src/core/server/saved_objects/saved_objects_service.test.ts +++ b/src/core/server/saved_objects/saved_objects_service.test.ts @@ -23,7 +23,7 @@ import { clientProviderInstanceMock, typeRegistryInstanceMock, } from './saved_objects_service.test.mocks'; - +import { BehaviorSubject } from 'rxjs'; import { ByteSizeValue } from '@kbn/config-schema'; import { SavedObjectsService } from './saved_objects_service'; import { mockCoreContext } from '../core_context.mock'; @@ -34,7 +34,6 @@ import { elasticsearchServiceMock } from '../elasticsearch/elasticsearch_service import { legacyServiceMock } from '../legacy/legacy_service.mock'; import { httpServiceMock } from '../http/http_service.mock'; import { SavedObjectsClientFactoryProvider } from './service/lib'; -import { BehaviorSubject } from 'rxjs'; import { NodesVersionCompatibility } from '../elasticsearch/version_check/ensure_es_version'; describe('SavedObjectsService', () => { diff --git a/src/core/server/saved_objects/saved_objects_service.ts b/src/core/server/saved_objects/saved_objects_service.ts index 89f7990c771c8..175eac3c1bd95 100644 --- a/src/core/server/saved_objects/saved_objects_service.ts +++ b/src/core/server/saved_objects/saved_objects_service.ts @@ -38,7 +38,7 @@ import { SavedObjectConfig, } from './saved_objects_config'; import { KibanaRequest, InternalHttpServiceSetup } from '../http'; -import { SavedObjectsClientContract, SavedObjectsType, SavedObjectsLegacyUiExports } from './types'; +import { SavedObjectsClientContract, SavedObjectsType } from './types'; import { ISavedObjectsRepository, SavedObjectsRepository } from './service/lib/repository'; import { SavedObjectsClientFactoryProvider, @@ -301,10 +301,6 @@ export class SavedObjectsService legacyTypes.forEach(type => this.typeRegistry.registerType(type)); this.validations = setupDeps.legacyPlugins.uiExports.savedObjectValidations || {}; - const importableExportableTypes = getImportableAndExportableTypes( - setupDeps.legacyPlugins.uiExports - ); - const savedObjectsConfig = await this.coreContext.configService .atPath('savedObjects') .pipe(first()) @@ -320,7 +316,6 @@ export class SavedObjectsService logger: this.logger, config: this.config, migratorPromise: this.migrator$.pipe(first()).toPromise(), - importableExportableTypes, }); return { @@ -479,16 +474,3 @@ export class SavedObjectsService }); } } - -function getImportableAndExportableTypes({ - savedObjectMappings = [], - savedObjectsManagement = {}, -}: SavedObjectsLegacyUiExports) { - const visibleTypes = savedObjectMappings.reduce( - (types, mapping) => [...types, ...Object.keys(mapping.properties)], - [] as string[] - ); - return visibleTypes.filter( - type => savedObjectsManagement[type]?.isImportableAndExportable === true ?? false - ); -} diff --git a/src/core/server/saved_objects/saved_objects_type_registry.mock.ts b/src/core/server/saved_objects/saved_objects_type_registry.mock.ts index 435e352335ecf..8c8458d7a5ce4 100644 --- a/src/core/server/saved_objects/saved_objects_type_registry.mock.ts +++ b/src/core/server/saved_objects/saved_objects_type_registry.mock.ts @@ -25,14 +25,20 @@ const createRegistryMock = (): jest.Mocked type === 'global'); + mock.isImportableAndExportable.mockReturnValue(true); return mock; }; diff --git a/src/core/server/saved_objects/saved_objects_type_registry.test.ts b/src/core/server/saved_objects/saved_objects_type_registry.test.ts index 4268ab7718f8d..4d1d5c1eacc25 100644 --- a/src/core/server/saved_objects/saved_objects_type_registry.test.ts +++ b/src/core/server/saved_objects/saved_objects_type_registry.test.ts @@ -212,4 +212,45 @@ describe('SavedObjectTypeRegistry', () => { expect(registry.getIndex('unknownType')).toBeUndefined(); }); }); + + describe('#isImportableAndExportable', () => { + it('returns correct value for the type', () => { + registry.registerType( + createType({ name: 'typeA', management: { importableAndExportable: true } }) + ); + registry.registerType( + createType({ name: 'typeB', management: { importableAndExportable: false } }) + ); + + expect(registry.isImportableAndExportable('typeA')).toBe(true); + expect(registry.isImportableAndExportable('typeB')).toBe(false); + }); + it('returns false when the type is not registered', () => { + registry.registerType(createType({ name: 'typeA', management: {} })); + registry.registerType(createType({ name: 'typeB', management: {} })); + + expect(registry.isImportableAndExportable('typeA')).toBe(false); + }); + it('returns false when management is not defined for the type', () => { + registry.registerType(createType({ name: 'typeA' })); + expect(registry.isImportableAndExportable('unknownType')).toBe(false); + }); + }); + + describe('#getImportableAndExportableTypes', () => { + it('returns all registered types that are importable/exportable', () => { + const typeA = createType({ name: 'typeA', management: { importableAndExportable: true } }); + const typeB = createType({ name: 'typeB' }); + const typeC = createType({ name: 'typeC', management: { importableAndExportable: false } }); + const typeD = createType({ name: 'typeD', management: { importableAndExportable: true } }); + registry.registerType(typeA); + registry.registerType(typeB); + registry.registerType(typeC); + registry.registerType(typeD); + + const types = registry.getImportableAndExportableTypes(); + expect(types.length).toEqual(2); + expect(types.map(t => t.name)).toEqual(['typeA', 'typeD']); + }); + }); }); diff --git a/src/core/server/saved_objects/saved_objects_type_registry.ts b/src/core/server/saved_objects/saved_objects_type_registry.ts index b73c80ad9dff7..5580ce3815d0d 100644 --- a/src/core/server/saved_objects/saved_objects_type_registry.ts +++ b/src/core/server/saved_objects/saved_objects_type_registry.ts @@ -27,7 +27,13 @@ import { SavedObjectsType } from './types'; */ export type ISavedObjectTypeRegistry = Pick< SavedObjectTypeRegistry, - 'getType' | 'getAllTypes' | 'getIndex' | 'isNamespaceAgnostic' | 'isHidden' + | 'getType' + | 'getAllTypes' + | 'getIndex' + | 'isNamespaceAgnostic' + | 'isHidden' + | 'getImportableAndExportableTypes' + | 'isImportableAndExportable' >; /** @@ -63,6 +69,13 @@ export class SavedObjectTypeRegistry { return [...this.types.values()]; } + /** + * Return all {@link SavedObjectsType | types} currently registered that are importable/exportable. + */ + public getImportableAndExportableTypes() { + return this.getAllTypes().filter(type => this.isImportableAndExportable(type.name)); + } + /** * Returns the `namespaceAgnostic` property for given type, or `false` if * the type is not registered. @@ -86,4 +99,12 @@ export class SavedObjectTypeRegistry { public getIndex(type: string) { return this.types.get(type)?.indexPattern; } + + /** + * Returns the `management.importableAndExportable` property for given type, or + * `false` if the type is not registered or does not define a management section. + */ + public isImportableAndExportable(type: string) { + return this.types.get(type)?.management?.importableAndExportable ?? false; + } } diff --git a/src/core/server/saved_objects/service/lib/repository_create_repository.test.ts b/src/core/server/saved_objects/service/lib/repository_create_repository.test.ts index 4a87bb1043ca2..a6b580e9b3461 100644 --- a/src/core/server/saved_objects/service/lib/repository_create_repository.test.ts +++ b/src/core/server/saved_objects/service/lib/repository_create_repository.test.ts @@ -102,7 +102,6 @@ describe('SavedObjectsRepository#createRepository', () => { expect(repository).toBeDefined(); expect(RepositoryConstructor.mock.calls[0][0].allowedTypes).toMatchInlineSnapshot(` Array [ - "config", "nsAgnosticType", "nsType", ] @@ -121,7 +120,6 @@ describe('SavedObjectsRepository#createRepository', () => { expect(repository).toBeDefined(); expect(RepositoryConstructor.mock.calls[0][0].allowedTypes).toMatchInlineSnapshot(` Array [ - "config", "nsAgnosticType", "nsType", "hiddenType", diff --git a/src/core/server/saved_objects/types.ts b/src/core/server/saved_objects/types.ts index c9c672d0f8b1c..1d927211b43e5 100644 --- a/src/core/server/saved_objects/types.ts +++ b/src/core/server/saved_objects/types.ts @@ -21,7 +21,6 @@ import { SavedObjectsClient } from './service/saved_objects_client'; import { SavedObjectsTypeMappingDefinition, SavedObjectsTypeMappingDefinitions } from './mappings'; import { SavedObjectMigrationMap } from './migrations'; import { PropertyValidators } from './validation'; -import { SavedObjectsManagementDefinition } from './management'; export { SavedObjectsImportResponse, @@ -246,6 +245,50 @@ export interface SavedObjectsType { * An optional map of {@link SavedObjectMigrationFn | migrations} to be used to migrate the type. */ migrations?: SavedObjectMigrationMap; + /** + * An optional {@link SavedObjectsTypeManagementDefinition | saved objects management section} definition for the type. + */ + management?: SavedObjectsTypeManagementDefinition; +} + +/** + * Configuration options for the {@link SavedObjectsType | type}'s management section. + * + * @public + */ +export interface SavedObjectsTypeManagementDefinition { + /** + * Is the type importable or exportable. Defaults to `false`. + */ + importableAndExportable?: boolean; + /** + * The default search field to use for this type. Defaults to `id`. + */ + defaultSearchField?: string; + /** + * The eui icon name to display in the management table. + * If not defined, the default icon will be used. + */ + icon?: string; + /** + * Function returning the title to display in the management table. + * If not defined, will use the object's type and id to generate a label. + */ + getTitle?: (savedObject: SavedObject) => string; + /** + * Function returning the url to use to redirect to the editing page of this object. + * If not defined, editing will not be allowed. + */ + getEditUrl?: (savedObject: SavedObject) => string; + /** + * Function returning the url to use to redirect to this object from the management section. + * If not defined, redirecting to the object will not be allowed. + * + * @returns an object containing a `path` and `uiCapabilitiesPath` properties. the `path` is the path to + * the object page, relative to the base path. `uiCapabilitiesPath` is the path to check in the + * {@link Capabilities | uiCapabilities} to check if the user has permission to access the object. + */ + getInAppUrl?: (savedObject: SavedObject) => { path: string; uiCapabilitiesPath: string }; } /** @@ -257,7 +300,7 @@ export interface SavedObjectsLegacyUiExports { savedObjectMigrations: SavedObjectsLegacyMigrationDefinitions; savedObjectSchemas: SavedObjectsLegacySchemaDefinitions; savedObjectValidations: PropertyValidators; - savedObjectsManagement: SavedObjectsManagementDefinition; + savedObjectsManagement: SavedObjectsLegacyManagementDefinition; } /** @@ -269,6 +312,28 @@ export interface SavedObjectsLegacyMapping { properties: SavedObjectsTypeMappingDefinitions; } +/** + * @internal + * @deprecated Use {@link SavedObjectsTypeManagementDefinition | management definition} when registering + * from new platform plugins + */ +export interface SavedObjectsLegacyManagementDefinition { + [key: string]: SavedObjectsLegacyManagementTypeDefinition; +} + +/** + * @internal + * @deprecated + */ +export interface SavedObjectsLegacyManagementTypeDefinition { + isImportableAndExportable?: boolean; + defaultSearchField?: string; + icon?: string; + getTitle?: (savedObject: SavedObject) => string; + getEditUrl?: (savedObject: SavedObject) => string; + getInAppUrl?: (savedObject: SavedObject) => { path: string; uiCapabilitiesPath: string }; +} + /** * @internal * @deprecated diff --git a/src/core/server/saved_objects/utils.test.ts b/src/core/server/saved_objects/utils.test.ts index 0a56535ac8509..0719fe7138e8a 100644 --- a/src/core/server/saved_objects/utils.test.ts +++ b/src/core/server/saved_objects/utils.test.ts @@ -235,6 +235,75 @@ describe('convertLegacyTypes', () => { expect(legacyMigration).toHaveBeenCalledWith(doc, context.log); }); + it('imports type management information', () => { + const uiExports: SavedObjectsLegacyUiExports = { + savedObjectMappings: [ + { + pluginId: 'pluginA', + properties: { + typeA: { + properties: { + fieldA: { type: 'text' }, + }, + }, + }, + }, + { + pluginId: 'pluginB', + properties: { + typeB: { + properties: { + fieldB: { type: 'text' }, + }, + }, + typeC: { + properties: { + fieldC: { type: 'text' }, + }, + }, + }, + }, + ], + savedObjectsManagement: { + typeA: { + isImportableAndExportable: true, + icon: 'iconA', + defaultSearchField: 'searchFieldA', + getTitle: savedObject => savedObject.id, + }, + typeB: { + isImportableAndExportable: false, + icon: 'iconB', + getEditUrl: savedObject => `/some-url/${savedObject.id}`, + getInAppUrl: savedObject => ({ path: 'path', uiCapabilitiesPath: 'ui-path' }), + }, + }, + savedObjectMigrations: {}, + savedObjectSchemas: {}, + savedObjectValidations: {}, + }; + + const converted = convertLegacyTypes(uiExports, legacyConfig); + expect(converted.length).toEqual(3); + const [typeA, typeB, typeC] = converted; + + expect(typeA.management).toEqual({ + importableAndExportable: true, + icon: 'iconA', + defaultSearchField: 'searchFieldA', + getTitle: uiExports.savedObjectsManagement.typeA.getTitle, + }); + + expect(typeB.management).toEqual({ + importableAndExportable: false, + icon: 'iconB', + getEditUrl: uiExports.savedObjectsManagement.typeB.getEditUrl, + getInAppUrl: uiExports.savedObjectsManagement.typeB.getInAppUrl, + }); + + expect(typeC.management).toBeUndefined(); + }); + it('merges everything when all are present', () => { const uiExports: SavedObjectsLegacyUiExports = { savedObjectMappings: [ diff --git a/src/core/server/saved_objects/utils.ts b/src/core/server/saved_objects/utils.ts index bb2c42c6a362c..ea90efd8b9fbd 100644 --- a/src/core/server/saved_objects/utils.ts +++ b/src/core/server/saved_objects/utils.ts @@ -23,6 +23,8 @@ import { SavedObjectsType, SavedObjectsLegacyUiExports, SavedObjectLegacyMigrationMap, + SavedObjectsLegacyManagementTypeDefinition, + SavedObjectsTypeManagementDefinition, } from './types'; import { SavedObjectsSchemaDefinition } from './schema'; @@ -35,15 +37,17 @@ export const convertLegacyTypes = ( savedObjectMappings = [], savedObjectMigrations = {}, savedObjectSchemas = {}, + savedObjectsManagement = {}, }: SavedObjectsLegacyUiExports, legacyConfig: LegacyConfig ): SavedObjectsType[] => { - return savedObjectMappings.reduce((types, { pluginId, properties }) => { + return savedObjectMappings.reduce((types, { properties }) => { return [ ...types, ...Object.entries(properties).map(([type, mappings]) => { const schema = savedObjectSchemas[type]; const migrations = savedObjectMigrations[type]; + const management = savedObjectsManagement[type]; return { name: type, hidden: schema?.hidden ?? false, @@ -55,6 +59,7 @@ export const convertLegacyTypes = ( : schema?.indexPattern, convertToAliasScript: schema?.convertToAliasScript, migrations: convertLegacyMigrations(migrations ?? {}), + management: management ? convertLegacyTypeManagement(management) : undefined, }; }), ]; @@ -90,3 +95,16 @@ const convertLegacyMigrations = ( }; }, {} as SavedObjectMigrationMap); }; + +const convertLegacyTypeManagement = ( + legacyTypeManagement: SavedObjectsLegacyManagementTypeDefinition +): SavedObjectsTypeManagementDefinition => { + return { + importableAndExportable: legacyTypeManagement.isImportableAndExportable, + defaultSearchField: legacyTypeManagement.defaultSearchField, + icon: legacyTypeManagement.icon, + getTitle: legacyTypeManagement.getTitle, + getEditUrl: legacyTypeManagement.getEditUrl, + getInAppUrl: legacyTypeManagement.getInAppUrl, + }; +}; diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md index f7afe7a6a290a..5ede98a1e6e6d 100644 --- a/src/core/server/server.api.md +++ b/src/core/server/server.api.md @@ -968,7 +968,7 @@ export type IsAuthenticated = (request: KibanaRequest | LegacyRequest) => boolea export type ISavedObjectsRepository = Pick; // @public -export type ISavedObjectTypeRegistry = Pick; +export type ISavedObjectTypeRegistry = Pick; // @public export type IScopedClusterClient = Pick; @@ -1456,6 +1456,7 @@ export interface RequestHandlerContext { rendering: IScopedRenderingClient; savedObjects: { client: SavedObjectsClientContract; + typeRegistry: ISavedObjectTypeRegistry; }; elasticsearch: { dataClient: IScopedClusterClient; @@ -2150,12 +2151,26 @@ export interface SavedObjectsType { convertToAliasScript?: string; hidden: boolean; indexPattern?: string; + management?: SavedObjectsTypeManagementDefinition; mappings: SavedObjectsTypeMappingDefinition; migrations?: SavedObjectMigrationMap; name: string; namespaceAgnostic: boolean; } +// @public +export interface SavedObjectsTypeManagementDefinition { + defaultSearchField?: string; + getEditUrl?: (savedObject: SavedObject) => string; + getInAppUrl?: (savedObject: SavedObject) => { + path: string; + uiCapabilitiesPath: string; + }; + getTitle?: (savedObject: SavedObject) => string; + icon?: string; + importableAndExportable?: boolean; +} + // @public export interface SavedObjectsTypeMappingDefinition { dynamic?: false | 'strict'; @@ -2180,9 +2195,11 @@ export interface SavedObjectsUpdateResponse extends Omit { + let service: UiSettingsService; + let setupDeps: SetupDeps; + let savedObjectsClient: ReturnType; + + beforeEach(() => { + const coreContext = mockCoreContext.create(); + coreContext.configService.atPath.mockReturnValue(new BehaviorSubject({ overrides })); + const httpSetup = httpServiceMock.createSetupContract(); + const savedObjectsSetup = savedObjectsServiceMock.createInternalSetupContract(); + setupDeps = { http: httpSetup, savedObjects: savedObjectsSetup }; + savedObjectsClient = savedObjectsClientMock.create(); + service = new UiSettingsService(coreContext); + }); -afterEach(() => { - MockUiSettingsClientConstructor.mockClear(); -}); + afterEach(() => { + MockUiSettingsClientConstructor.mockClear(); + }); -describe('uiSettings', () => { describe('#setup', () => { + it('registers the uiSettings type to the savedObjects registry', async () => { + await service.setup(setupDeps); + expect(setupDeps.savedObjects.registerType).toHaveBeenCalledTimes(1); + expect(setupDeps.savedObjects.registerType).toHaveBeenCalledWith(uiSettingsType); + }); + describe('#asScopedToClient', () => { it('passes saved object type "config" to UiSettingsClient', async () => { - const service = new UiSettingsService(coreContext); const setup = await service.setup(setupDeps); setup.asScopedToClient(savedObjectsClient); expect(MockUiSettingsClientConstructor).toBeCalledTimes(1); @@ -60,7 +73,6 @@ describe('uiSettings', () => { }); it('passes overrides to UiSettingsClient', async () => { - const service = new UiSettingsService(coreContext); const setup = await service.setup(setupDeps); setup.asScopedToClient(savedObjectsClient); expect(MockUiSettingsClientConstructor).toBeCalledTimes(1); @@ -69,7 +81,6 @@ describe('uiSettings', () => { }); it('passes a copy of set defaults to UiSettingsClient', async () => { - const service = new UiSettingsService(coreContext); const setup = await service.setup(setupDeps); setup.register(defaults); @@ -83,7 +94,6 @@ describe('uiSettings', () => { describe('#register', () => { it('throws if registers the same key twice', async () => { - const service = new UiSettingsService(coreContext); const setup = await service.setup(setupDeps); setup.register(defaults); expect(() => setup.register(defaults)).toThrowErrorMatchingInlineSnapshot( @@ -96,7 +106,6 @@ describe('uiSettings', () => { describe('#start', () => { describe('#asScopedToClient', () => { it('passes saved object type "config" to UiSettingsClient', async () => { - const service = new UiSettingsService(coreContext); await service.setup(setupDeps); const start = await service.start(); start.asScopedToClient(savedObjectsClient); @@ -106,7 +115,6 @@ describe('uiSettings', () => { }); it('passes overrides to UiSettingsClient', async () => { - const service = new UiSettingsService(coreContext); await service.setup(setupDeps); const start = await service.start(); start.asScopedToClient(savedObjectsClient); @@ -116,7 +124,6 @@ describe('uiSettings', () => { }); it('passes a copy of set defaults to UiSettingsClient', async () => { - const service = new UiSettingsService(coreContext); const setup = await service.setup(setupDeps); setup.register(defaults); const start = await service.start(); diff --git a/src/core/server/ui_settings/ui_settings_service.ts b/src/core/server/ui_settings/ui_settings_service.ts index 942c2625ac8e7..de2cc9d510e0c 100644 --- a/src/core/server/ui_settings/ui_settings_service.ts +++ b/src/core/server/ui_settings/ui_settings_service.ts @@ -24,6 +24,7 @@ import { CoreContext } from '../core_context'; import { Logger } from '../logging'; import { SavedObjectsClientContract } from '../saved_objects/types'; +import { InternalSavedObjectsServiceSetup } from '../saved_objects'; import { InternalHttpServiceSetup } from '../http'; import { UiSettingsConfigType, config as uiConfigDefinition } from './ui_settings_config'; import { UiSettingsClient } from './ui_settings_client'; @@ -33,11 +34,12 @@ import { UiSettingsParams, } from './types'; import { mapToObject } from '../../utils/'; - +import { uiSettingsType } from './saved_objects'; import { registerRoutes } from './routes'; -interface SetupDeps { +export interface SetupDeps { http: InternalHttpServiceSetup; + savedObjects: InternalSavedObjectsServiceSetup; } /** @internal */ @@ -53,9 +55,11 @@ export class UiSettingsService this.config$ = coreContext.configService.atPath(uiConfigDefinition.path); } - public async setup(deps: SetupDeps): Promise { - registerRoutes(deps.http.createRouter('')); + public async setup({ http, savedObjects }: SetupDeps): Promise { this.log.debug('Setting up ui settings service'); + + savedObjects.registerType(uiSettingsType); + registerRoutes(http.createRouter('')); const config = await this.config$.pipe(first()).toPromise(); this.overrides = config.overrides; diff --git a/src/legacy/core_plugins/kibana/index.js b/src/legacy/core_plugins/kibana/index.js index 221133a17d59a..092eed924f330 100644 --- a/src/legacy/core_plugins/kibana/index.js +++ b/src/legacy/core_plugins/kibana/index.js @@ -201,18 +201,6 @@ export default function(kibana) { return `/goto/${encodeURIComponent(obj.id)}`; }, }, - config: { - isImportableAndExportable: true, - getInAppUrl() { - return { - path: `/app/kibana#/management/kibana/settings`, - uiCapabilitiesPath: 'advancedSettings.show', - }; - }, - getTitle(obj) { - return `Advanced Settings [${obj.id}]`; - }, - }, }, savedObjectSchemas: { diff --git a/src/legacy/plugin_discovery/plugin_spec/plugin_spec_options.d.ts b/src/legacy/plugin_discovery/plugin_spec/plugin_spec_options.d.ts index 228ef96f8c9f3..d668739436726 100644 --- a/src/legacy/plugin_discovery/plugin_spec/plugin_spec_options.d.ts +++ b/src/legacy/plugin_discovery/plugin_spec/plugin_spec_options.d.ts @@ -19,13 +19,13 @@ import { Server } from '../../server/kbn_server'; import { Capabilities } from '../../../core/server'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { SavedObjectsManagementDefinition } from '../../../core/server/saved_objects/management'; +import { SavedObjectsLegacyManagementDefinition } from '../../../core/server/saved_objects/types'; export type InitPluginFunction = (server: Server) => void; export interface UiExports { injectDefaultVars?: (server: Server) => { [key: string]: any }; styleSheetPaths?: string; - savedObjectsManagement?: SavedObjectsManagementDefinition; + savedObjectsManagement?: SavedObjectsLegacyManagementDefinition; mappings?: unknown; visTypes?: string[]; interpreter?: string[]; diff --git a/src/legacy/plugin_discovery/types.ts b/src/legacy/plugin_discovery/types.ts index 9425003eae874..4d8090a138ffb 100644 --- a/src/legacy/plugin_discovery/types.ts +++ b/src/legacy/plugin_discovery/types.ts @@ -23,7 +23,7 @@ import { Capabilities } from '../../core/server'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { SavedObjectsSchemaDefinition } from '../../core/server/saved_objects/schema'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { SavedObjectsManagementDefinition } from '../../core/server/saved_objects/management'; +import { SavedObjectsLegacyManagementDefinition } from '../../core/server/saved_objects/types'; import { AppCategory } from '../../core/types'; /** @@ -73,7 +73,7 @@ export interface LegacyPluginOptions { mappings: any; migrations: any; savedObjectSchemas: SavedObjectsSchemaDefinition; - savedObjectsManagement: SavedObjectsManagementDefinition; + savedObjectsManagement: SavedObjectsLegacyManagementDefinition; visTypes: string[]; embeddableActions?: string[]; embeddableFactories?: string[]; diff --git a/src/legacy/server/kbn_server.d.ts b/src/legacy/server/kbn_server.d.ts index 8da1b3b05fa76..68b5a63871372 100644 --- a/src/legacy/server/kbn_server.d.ts +++ b/src/legacy/server/kbn_server.d.ts @@ -77,7 +77,7 @@ declare module 'hapi' { addScopedTutorialContextFactory: ( scopedTutorialContextFactory: (...args: any[]) => any ) => void; - savedObjectsManagement(): SavedObjectsManagement; + getSavedObjectsManagement(): SavedObjectsManagement; getInjectedUiAppVars: (pluginName: string) => { [key: string]: any }; getUiNavLinks(): Array<{ _id: string }>; addMemoizedFactoryToRequest: ( diff --git a/src/legacy/server/saved_objects/saved_objects_mixin.js b/src/legacy/server/saved_objects/saved_objects_mixin.js index 0039fb19bb086..cc63099c8a211 100644 --- a/src/legacy/server/saved_objects/saved_objects_mixin.js +++ b/src/legacy/server/saved_objects/saved_objects_mixin.js @@ -43,7 +43,7 @@ export function savedObjectsMixin(kbnServer, server) { server.decorate( 'server', 'getSavedObjectsManagement', - () => new SavedObjectsManagement(kbnServer.uiExports.savedObjectsManagement) + () => new SavedObjectsManagement(typeRegistry) ); const warn = message => server.log(['warning', 'saved-objects'], message); diff --git a/src/legacy/server/saved_objects/saved_objects_mixin.test.js b/src/legacy/server/saved_objects/saved_objects_mixin.test.js index b8636d510b979..3745f0b92123c 100644 --- a/src/legacy/server/saved_objects/saved_objects_mixin.test.js +++ b/src/legacy/server/saved_objects/saved_objects_mixin.test.js @@ -201,7 +201,7 @@ describe('Saved Objects Mixin', () => { it('should return all but hidden types', async () => { expect(service).toBeDefined(); - expect(service.types).toEqual(['config', 'testtype', 'doc1', 'doc2']); + expect(service.types).toEqual(['testtype', 'doc1', 'doc2']); }); const mockCallEs = jest.fn(); @@ -215,16 +215,12 @@ describe('Saved Objects Mixin', () => { it('should create a repository without hidden types', () => { const repository = service.getSavedObjectsRepository(mockCallEs); expect(repository).toBeDefined(); - expect(repository._allowedTypes).toEqual(['config', 'testtype', 'doc1', 'doc2']); + expect(repository._allowedTypes).toEqual(['testtype', 'doc1', 'doc2']); }); it('should create a repository with a unique list of allowed types', () => { - const repository = service.getSavedObjectsRepository(mockCallEs, [ - 'config', - 'config', - 'config', - ]); - expect(repository._allowedTypes).toEqual(['config', 'testtype', 'doc1', 'doc2']); + const repository = service.getSavedObjectsRepository(mockCallEs, ['doc1', 'doc1', 'doc1']); + expect(repository._allowedTypes).toEqual(['testtype', 'doc1', 'doc2']); }); it('should create a repository with extraTypes minus duplicate', () => { @@ -232,13 +228,7 @@ describe('Saved Objects Mixin', () => { 'hiddentype', 'hiddentype', ]); - expect(repository._allowedTypes).toEqual([ - 'config', - 'testtype', - 'doc1', - 'doc2', - 'hiddentype', - ]); + expect(repository._allowedTypes).toEqual(['testtype', 'doc1', 'doc2', 'hiddentype']); }); it('should not allow a repository without a callCluster function', () => { diff --git a/test/api_integration/apis/saved_objects/export.js b/test/api_integration/apis/saved_objects/export.js index ace65f190dec2..fc9ab8140869c 100644 --- a/test/api_integration/apis/saved_objects/export.js +++ b/test/api_integration/apis/saved_objects/export.js @@ -191,10 +191,28 @@ export default function({ getService }) { expect(resp.body).to.eql({ statusCode: 400, error: 'Bad Request', - message: - '[request body.type]: types that failed validation:\n' + - '- [request body.type.0]: expected value of type [string] but got [Array]\n' + - '- [request body.type.1.0]: wigwags is not exportable', + message: 'Trying to export non-exportable type(s): wigwags', + }); + }); + }); + + it(`should return 400 when exporting objects with unsupported type`, async () => { + await supertest + .post('/api/saved_objects/_export') + .send({ + objects: [ + { + type: 'wigwags', + id: '1', + }, + ], + }) + .expect(400) + .then(resp => { + expect(resp.body).to.eql({ + statusCode: 400, + error: 'Bad Request', + message: 'Trying to export object(s) with non-exportable types: wigwags:1', }); }); }); diff --git a/x-pack/plugins/infra/public/components/logging/log_analysis_results/analyze_in_ml_button.tsx b/x-pack/plugins/infra/public/components/logging/log_analysis_results/analyze_in_ml_button.tsx index 6b946898f6330..7d523faafdb3c 100644 --- a/x-pack/plugins/infra/public/components/logging/log_analysis_results/analyze_in_ml_button.tsx +++ b/x-pack/plugins/infra/public/components/logging/log_analysis_results/analyze_in_ml_button.tsx @@ -8,22 +8,19 @@ import { EuiButton } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import React from 'react'; import { encode } from 'rison-node'; -import url from 'url'; -import { stringify } from 'query-string'; -import { useKibana } from '../../../../../../../src/plugins/kibana_react/public'; import { TimeRange } from '../../../../common/http_api/shared/time_range'; -import { url as urlUtils } from '../../../../../../../src/plugins/kibana_utils/public'; +import { useLinkProps, LinkDescriptor } from '../../../hooks/use_link_props'; export const AnalyzeInMlButton: React.FunctionComponent<{ jobId: string; partition?: string; timeRange: TimeRange; }> = ({ jobId, partition, timeRange }) => { - const prependBasePath = useKibana().services.http?.basePath?.prepend; - if (!prependBasePath) { - return null; - } - const pathname = prependBasePath('/app/ml'); + const linkProps = useLinkProps( + typeof partition === 'string' + ? getPartitionSpecificSingleMetricViewerLinkDescriptor(jobId, partition, timeRange) + : getOverallAnomalyExplorerLinkDescriptor(jobId, timeRange) + ); const buttonLabel = ( ); return typeof partition === 'string' ? ( - + {buttonLabel} ) : ( - + {buttonLabel} ); }; -const getOverallAnomalyExplorerLink = (pathname: string, jobId: string, timeRange: TimeRange) => { +const getOverallAnomalyExplorerLinkDescriptor = ( + jobId: string, + timeRange: TimeRange +): LinkDescriptor => { const { from, to } = convertTimeRangeToParams(timeRange); const _g = encode({ @@ -62,20 +54,18 @@ const getOverallAnomalyExplorerLink = (pathname: string, jobId: string, timeRang }, }); - const hash = `/explorer?${stringify(urlUtils.encodeQuery({ _g }), { encode: false })}`; - - return url.format({ - pathname, - hash, - }); + return { + app: 'ml', + hash: '/explorer', + search: { _g }, + }; }; -const getPartitionSpecificSingleMetricViewerLink = ( - pathname: string, +const getPartitionSpecificSingleMetricViewerLinkDescriptor = ( jobId: string, partition: string, timeRange: TimeRange -) => { +): LinkDescriptor => { const { from, to } = convertTimeRangeToParams(timeRange); const _g = encode({ @@ -95,15 +85,11 @@ const getPartitionSpecificSingleMetricViewerLink = ( }, }); - const hash = `/timeseriesexplorer?${stringify(urlUtils.encodeQuery({ _g, _a }), { - sort: false, - encode: false, - })}`; - - return url.format({ - pathname, - hash, - }); + return { + app: 'ml', + hash: '/timeseriesexplorer', + search: { _g, _a }, + }; }; const convertTimeRangeToParams = (timeRange: TimeRange): { from: string; to: string } => { diff --git a/x-pack/plugins/infra/public/components/logging/log_analysis_setup/user_management_link.tsx b/x-pack/plugins/infra/public/components/logging/log_analysis_setup/user_management_link.tsx index 9a2bbd3dabffc..e045e78471513 100644 --- a/x-pack/plugins/infra/public/components/logging/log_analysis_setup/user_management_link.tsx +++ b/x-pack/plugins/infra/public/components/logging/log_analysis_setup/user_management_link.tsx @@ -7,12 +7,19 @@ import { EuiButton, EuiButtonProps } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import React from 'react'; +import { useLinkProps } from '../../../hooks/use_link_props'; -export const UserManagementLink: React.FunctionComponent = props => ( - - - -); +export const UserManagementLink: React.FunctionComponent = props => { + const linkProps = useLinkProps({ + app: 'kibana', + hash: '/management/security/users', + }); + return ( + + + + ); +}; diff --git a/x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_actions_menu.test.tsx b/x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_actions_menu.test.tsx index 16a91e3727c98..b4fa6b8800fba 100644 --- a/x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_actions_menu.test.tsx +++ b/x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_actions_menu.test.tsx @@ -10,22 +10,35 @@ import { act } from 'react-dom/test-utils'; import { mount } from 'enzyme'; import { LogEntryActionsMenu } from './log_entry_actions_menu'; +import { KibanaContextProvider } from '../../../../../../../src/plugins/kibana_react/public'; +import { coreMock } from 'src/core/public/mocks'; + +const coreStartMock = coreMock.createStart(); +coreStartMock.application.getUrlForApp.mockImplementation((app, options) => { + return `/test-basepath/s/test-space/app/${app}${options?.path}`; +}); + +const ProviderWrapper: React.FC = ({ children }) => { + return {children}; +}; describe('LogEntryActionsMenu component', () => { describe('uptime link', () => { it('renders with a host ip filter when present in log entry', () => { const elementWrapper = mount( - + + + ); act(() => { @@ -38,22 +51,24 @@ describe('LogEntryActionsMenu component', () => { expect( elementWrapper.find(`a${testSubject('~uptimeLogEntryActionsMenuItem')}`).prop('href') - ).toMatchInlineSnapshot(`"/app/uptime#/?search=(host.ip:HOST_IP)"`); + ).toBe('/test-basepath/s/test-space/app/uptime#/?search=host.ip:HOST_IP'); }); it('renders with a container id filter when present in log entry', () => { const elementWrapper = mount( - + + + ); act(() => { @@ -66,22 +81,24 @@ describe('LogEntryActionsMenu component', () => { expect( elementWrapper.find(`a${testSubject('~uptimeLogEntryActionsMenuItem')}`).prop('href') - ).toMatchInlineSnapshot(`"/app/uptime#/?search=(container.id:CONTAINER_ID)"`); + ).toBe('/test-basepath/s/test-space/app/uptime#/?search=container.id:CONTAINER_ID'); }); it('renders with a pod uid filter when present in log entry', () => { const elementWrapper = mount( - + + + ); act(() => { @@ -94,26 +111,28 @@ describe('LogEntryActionsMenu component', () => { expect( elementWrapper.find(`a${testSubject('~uptimeLogEntryActionsMenuItem')}`).prop('href') - ).toMatchInlineSnapshot(`"/app/uptime#/?search=(kubernetes.pod.uid:POD_UID)"`); + ).toBe('/test-basepath/s/test-space/app/uptime#/?search=kubernetes.pod.uid:POD_UID'); }); it('renders with a disjunction of filters when multiple present in log entry', () => { const elementWrapper = mount( - + + + ); act(() => { @@ -126,24 +145,26 @@ describe('LogEntryActionsMenu component', () => { expect( elementWrapper.find(`a${testSubject('~uptimeLogEntryActionsMenuItem')}`).prop('href') - ).toMatchInlineSnapshot( - `"/app/uptime#/?search=(container.id:CONTAINER_ID OR host.ip:HOST_IP OR kubernetes.pod.uid:POD_UID)"` + ).toBe( + '/test-basepath/s/test-space/app/uptime#/?search=container.id:CONTAINER_ID%20or%20host.ip:HOST_IP%20or%20kubernetes.pod.uid:POD_UID' ); }); it('renders as disabled when no supported field is present in log entry', () => { const elementWrapper = mount( - + + + ); act(() => { @@ -165,17 +186,19 @@ describe('LogEntryActionsMenu component', () => { describe('apm link', () => { it('renders with a trace id filter when present in log entry', () => { const elementWrapper = mount( - + + + ); act(() => { @@ -194,20 +217,22 @@ describe('LogEntryActionsMenu component', () => { it('renders with a trace id filter and timestamp when present in log entry', () => { const timestamp = '2019-06-27T17:44:08.693Z'; const elementWrapper = mount( - + + + ); act(() => { @@ -225,17 +250,19 @@ describe('LogEntryActionsMenu component', () => { it('renders as disabled when no supported field is present in log entry', () => { const elementWrapper = mount( - + + + ); act(() => { diff --git a/x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_actions_menu.tsx b/x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_actions_menu.tsx index 60e50f486d22a..206e9821190fb 100644 --- a/x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_actions_menu.tsx +++ b/x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_actions_menu.tsx @@ -4,41 +4,44 @@ * you may not use this file except in compliance with the Elastic License. */ +import * as rt from 'io-ts'; import { EuiButtonEmpty, EuiContextMenuItem, EuiContextMenuPanel, EuiPopover } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import React, { useMemo } from 'react'; -import url from 'url'; -import { useKibana } from '../../../../../../../src/plugins/kibana_react/public'; import { useVisibilityState } from '../../../utils/use_visibility_state'; import { getTraceUrl } from '../../../../../../legacy/plugins/apm/public/components/shared/Links/apm/ExternalLinks'; import { LogEntriesItem } from '../../../../common/http_api'; +import { useLinkProps, LinkDescriptor } from '../../../hooks/use_link_props'; +import { decodeOrThrow } from '../../../../common/runtime_types'; const UPTIME_FIELDS = ['container.id', 'host.ip', 'kubernetes.pod.uid']; export const LogEntryActionsMenu: React.FunctionComponent<{ logItem: LogEntriesItem; }> = ({ logItem }) => { - const prependBasePath = useKibana().services.http?.basePath?.prepend; const { hide, isVisible, show } = useVisibilityState(false); - const uptimeLink = useMemo(() => { - const link = getUptimeLink(logItem); - return prependBasePath && link ? prependBasePath(link) : link; - }, [logItem, prependBasePath]); + const apmLinkDescriptor = useMemo(() => getAPMLink(logItem), [logItem]); + const uptimeLinkDescriptor = useMemo(() => getUptimeLink(logItem), [logItem]); - const apmLink = useMemo(() => { - const link = getAPMLink(logItem); - return prependBasePath && link ? prependBasePath(link) : link; - }, [logItem, prependBasePath]); + const uptimeLinkProps = useLinkProps({ + app: 'uptime', + ...(uptimeLinkDescriptor ? uptimeLinkDescriptor : {}), + }); + + const apmLinkProps = useLinkProps({ + app: 'apm', + ...(apmLinkDescriptor ? apmLinkDescriptor : {}), + }); const menuItems = useMemo( () => [ , , ], - [apmLink, uptimeLink] + [uptimeLinkDescriptor, apmLinkDescriptor, apmLinkProps, uptimeLinkProps] ); const hasMenuItems = useMemo(() => menuItems.length > 0, [menuItems]); @@ -89,22 +92,32 @@ export const LogEntryActionsMenu: React.FunctionComponent<{ ); }; -const getUptimeLink = (logItem: LogEntriesItem) => { +const getUptimeLink = (logItem: LogEntriesItem): LinkDescriptor | undefined => { const searchExpressions = logItem.fields .filter(({ field, value }) => value != null && UPTIME_FIELDS.includes(field)) - .map(({ field, value }) => `${field}:${value}`); + .reduce((acc, fieldItem) => { + const { field, value } = fieldItem; + try { + const parsedValue = decodeOrThrow(rt.array(rt.string))(JSON.parse(value)); + return acc.concat(parsedValue.map(val => `${field}:${val}`)); + } catch (e) { + return acc.concat([`${field}:${value}`]); + } + }, []); if (searchExpressions.length === 0) { return undefined; } - - return url.format({ - pathname: '/app/uptime', - hash: `/?search=(${searchExpressions.join(' OR ')})`, - }); + return { + app: 'uptime', + hash: '/', + search: { + search: `${searchExpressions.join(' or ')}`, + }, + }; }; -const getAPMLink = (logItem: LogEntriesItem) => { +const getAPMLink = (logItem: LogEntriesItem): LinkDescriptor | undefined => { const traceIdEntry = logItem.fields.find( ({ field, value }) => value != null && field === 'trace.id' ); @@ -127,8 +140,8 @@ const getAPMLink = (logItem: LogEntriesItem) => { })() : { rangeFrom: 'now-1y', rangeTo: 'now' }; - return url.format({ - pathname: '/app/apm', + return { + app: 'apm', hash: getTraceUrl({ traceId: traceIdEntry.value, rangeFrom, rangeTo }), - }); + }; }; diff --git a/x-pack/plugins/infra/public/components/metrics_explorer/chart_context_menu.test.tsx b/x-pack/plugins/infra/public/components/metrics_explorer/chart_context_menu.test.tsx index 9c3319d467ae2..a23a2739a8e23 100644 --- a/x-pack/plugins/infra/public/components/metrics_explorer/chart_context_menu.test.tsx +++ b/x-pack/plugins/infra/public/components/metrics_explorer/chart_context_menu.test.tsx @@ -8,10 +8,11 @@ import React from 'react'; import { MetricsExplorerChartContextMenu, createNodeDetailLink, Props } from './chart_context_menu'; import { ReactWrapper, mount } from 'enzyme'; import { options, source, timeRange, chartOptions } from '../../utils/fixtures/metrics_explorer'; -import DateMath from '@elastic/datemath'; import { Capabilities } from 'src/core/public'; import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public'; +import { coreMock } from 'src/core/public/mocks'; +const coreStartMock = coreMock.createStart(); const series = { id: 'exmaple-01', rows: [], columns: [] }; const uiCapabilities: Capabilities = { navLinks: { show: false }, @@ -25,17 +26,8 @@ const getTestSubject = (component: ReactWrapper, name: string) => { }; const mountComponentWithProviders = (props: Props): ReactWrapper => { - const services = { - http: { - fetch: jest.fn(), - }, - application: { - getUrlForApp: jest.fn(), - }, - }; - return mount( - + ); @@ -159,10 +151,12 @@ describe('MetricsExplorerChartContextMenu', () => { test('createNodeDetailLink()', () => { const fromDateStrig = '2019-01-01T11:00:00Z'; const toDateStrig = '2019-01-01T12:00:00Z'; - const to = DateMath.parse(toDateStrig, { roundUp: true })!; - const from = DateMath.parse(fromDateStrig)!; const link = createNodeDetailLink('host', 'example-01', fromDateStrig, toDateStrig); - expect(link).toBe(`link-to/host-detail/example-01?to=${to.valueOf()}&from=${from.valueOf()}`); + expect(link).toStrictEqual({ + app: 'metrics', + pathname: 'link-to/host-detail/example-01', + search: { from: '1546340400000', to: '1546344000000' }, + }); }); }); }); diff --git a/x-pack/plugins/infra/public/components/metrics_explorer/chart_context_menu.tsx b/x-pack/plugins/infra/public/components/metrics_explorer/chart_context_menu.tsx index f7c97033f8d50..c50550f1de56f 100644 --- a/x-pack/plugins/infra/public/components/metrics_explorer/chart_context_menu.tsx +++ b/x-pack/plugins/infra/public/components/metrics_explorer/chart_context_menu.tsx @@ -24,7 +24,7 @@ import { createTSVBLink } from './helpers/create_tsvb_link'; import { getNodeDetailUrl } from '../../pages/link_to/redirect_to_node_detail'; import { SourceConfiguration } from '../../utils/source_configuration'; import { InventoryItemType } from '../../../common/inventory_models/types'; -import { usePrefixPathWithBasepath } from '../../hooks/use_prefix_path_with_basepath'; +import { useLinkProps } from '../../hooks/use_link_props'; export interface Props { options: MetricsExplorerOptions; @@ -80,7 +80,6 @@ export const MetricsExplorerChartContextMenu: React.FC = ({ uiCapabilities, chartOptions, }: Props) => { - const urlPrefixer = usePrefixPathWithBasepath(); const [isPopoverOpen, setPopoverState] = useState(false); const supportFiltering = options.groupBy != null && onFilter != null; const handleFilter = useCallback(() => { @@ -92,8 +91,6 @@ export const MetricsExplorerChartContextMenu: React.FC = ({ setPopoverState(false); }, [supportFiltering, options.groupBy, series.id, onFilter]); - const tsvbUrl = createTSVBLink(source, options, series, timeRange, chartOptions); - // Only display the "Add Filter" option if it's supported const filterByItem = supportFiltering ? [ @@ -109,6 +106,13 @@ export const MetricsExplorerChartContextMenu: React.FC = ({ : []; const nodeType = source && options.groupBy && fieldToNodeType(source, options.groupBy); + const nodeDetailLinkProps = useLinkProps({ + app: 'metrics', + ...(nodeType ? createNodeDetailLink(nodeType, series.id, timeRange.from, timeRange.to) : {}), + }); + const tsvbLinkProps = useLinkProps({ + ...createTSVBLink(source, options, series, timeRange, chartOptions), + }); const viewNodeDetail = nodeType ? [ { @@ -117,10 +121,7 @@ export const MetricsExplorerChartContextMenu: React.FC = ({ values: { name: nodeType }, }), icon: 'metricsApp', - href: urlPrefixer( - 'metrics', - createNodeDetailLink(nodeType, series.id, timeRange.from, timeRange.to) - ), + ...(nodeType ? nodeDetailLinkProps : {}), 'data-test-subj': 'metricsExplorerAction-ViewNodeMetrics', }, ] @@ -132,7 +133,7 @@ export const MetricsExplorerChartContextMenu: React.FC = ({ name: i18n.translate('xpack.infra.metricsExplorer.openInTSVB', { defaultMessage: 'Open in Visualize', }), - href: tsvbUrl, + ...tsvbLinkProps, icon: 'visualizeApp', disabled: options.metrics.length === 0, 'data-test-subj': 'metricsExplorerAction-OpenInTSVB', diff --git a/x-pack/plugins/infra/public/components/metrics_explorer/helpers/create_tsvb_link.test.ts b/x-pack/plugins/infra/public/components/metrics_explorer/helpers/create_tsvb_link.test.ts index 111f6678081f7..05637642b8dd9 100644 --- a/x-pack/plugins/infra/public/components/metrics_explorer/helpers/create_tsvb_link.test.ts +++ b/x-pack/plugins/infra/public/components/metrics_explorer/helpers/create_tsvb_link.test.ts @@ -22,9 +22,16 @@ const series = { id: 'example-01', rows: [], columns: [] }; describe('createTSVBLink()', () => { it('should just work', () => { const link = createTSVBLink(source, options, series, timeRange, chartOptions); - expect(link).toBe( - "../app/kibana#/visualize/create?type=metrics&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:%236092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))" - ); + expect(link).toStrictEqual({ + app: 'kibana', + hash: '/visualize/create', + search: { + _a: + "(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:#6092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))", + _g: '(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))', + type: 'metrics', + }, + }); }); it('should work with rates', () => { @@ -33,16 +40,30 @@ describe('createTSVBLink()', () => { metrics: [{ aggregation: 'rate', field: 'system.network.out.bytes' }], }; const link = createTSVBLink(source, customOptions, series, timeRange, chartOptions); - expect(link).toBe( - "../app/kibana#/visualize/create?type=metrics&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:%236092C0,fill:0,formatter:bytes,id:test-id,label:'rate(system.network.out.bytes)',line_width:2,metrics:!((field:system.network.out.bytes,id:test-id,type:max),(field:test-id,id:test-id,type:derivative,unit:'1s'),(field:test-id,id:test-id,type:positive_only)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}}/s)),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))" - ); + expect(link).toStrictEqual({ + app: 'kibana', + hash: '/visualize/create', + search: { + _a: + "(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:#6092C0,fill:0,formatter:bytes,id:test-id,label:'rate(system.network.out.bytes)',line_width:2,metrics:!((field:system.network.out.bytes,id:test-id,type:max),(field:test-id,id:test-id,type:derivative,unit:'1s'),(field:test-id,id:test-id,type:positive_only)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}}/s)),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))", + _g: '(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))', + type: 'metrics', + }, + }); }); it('should work with time range', () => { const customTimeRange = { ...timeRange, from: 'now-10m', to: 'now' }; const link = createTSVBLink(source, options, series, customTimeRange, chartOptions); - expect(link).toBe( - "../app/kibana#/visualize/create?type=metrics&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-10m,to:now))&_a=(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:%236092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))" - ); + expect(link).toStrictEqual({ + app: 'kibana', + hash: '/visualize/create', + search: { + _a: + "(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:#6092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))", + _g: '(refreshInterval:(pause:!t,value:0),time:(from:now-10m,to:now))', + type: 'metrics', + }, + }); }); it('should work with source', () => { const customSource = { @@ -51,9 +72,16 @@ describe('createTSVBLink()', () => { fields: { ...source.fields, timestamp: 'time' }, }; const link = createTSVBLink(customSource, options, series, timeRange, chartOptions); - expect(link).toBe( - "../app/kibana#/visualize/create?type=metrics&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'my-beats-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'my-beats-*',interval:auto,series:!((axis_position:right,chart_type:line,color:%236092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:time,type:timeseries),title:example-01,type:metrics))" - ); + expect(link).toStrictEqual({ + app: 'kibana', + hash: '/visualize/create', + search: { + _a: + "(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'my-beats-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'my-beats-*',interval:auto,series:!((axis_position:right,chart_type:line,color:#6092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:time,type:timeseries),title:example-01,type:metrics))", + _g: '(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))', + type: 'metrics', + }, + }); }); it('should work with filterQuery', () => { const customSource = { @@ -63,25 +91,46 @@ describe('createTSVBLink()', () => { }; const customOptions = { ...options, filterQuery: 'system.network.name:lo*' }; const link = createTSVBLink(customSource, customOptions, series, timeRange, chartOptions); - expect(link).toBe( - "../app/kibana#/visualize/create?type=metrics&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'my-beats-*',filter:(language:kuery,query:'system.network.name:lo* and host.name : \"example-01\"'),id:test-id,index_pattern:'my-beats-*',interval:auto,series:!((axis_position:right,chart_type:line,color:%236092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:time,type:timeseries),title:example-01,type:metrics))" - ); + expect(link).toStrictEqual({ + app: 'kibana', + hash: '/visualize/create', + search: { + _a: + "(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'my-beats-*',filter:(language:kuery,query:'system.network.name:lo* and host.name : \"example-01\"'),id:test-id,index_pattern:'my-beats-*',interval:auto,series:!((axis_position:right,chart_type:line,color:#6092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:time,type:timeseries),title:example-01,type:metrics))", + _g: '(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))', + type: 'metrics', + }, + }); }); it('should remove axis_min from link', () => { const customChartOptions = { ...chartOptions, yAxisMode: MetricsExplorerYAxisMode.auto }; const link = createTSVBLink(source, options, series, timeRange, customChartOptions); - expect(link).toBe( - "../app/kibana#/visualize/create?type=metrics&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:%236092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))" - ); + expect(link).toStrictEqual({ + app: 'kibana', + hash: '/visualize/create', + search: { + _a: + "(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:#6092C0,fill:0,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))", + _g: '(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))', + type: 'metrics', + }, + }); }); it('should change series to area', () => { const customChartOptions = { ...chartOptions, type: MetricsExplorerChartType.area }; const link = createTSVBLink(source, options, series, timeRange, customChartOptions); - expect(link).toBe( - "../app/kibana#/visualize/create?type=metrics&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:%236092C0,fill:0.5,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))" - ); + expect(link).toStrictEqual({ + app: 'kibana', + hash: '/visualize/create', + search: { + _a: + "(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:#6092C0,fill:0.5,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:none,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))", + _g: '(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))', + type: 'metrics', + }, + }); }); it('should change series to area and stacked', () => { @@ -91,9 +140,16 @@ describe('createTSVBLink()', () => { stack: true, }; const link = createTSVBLink(source, options, series, timeRange, customChartOptions); - expect(link).toBe( - "../app/kibana#/visualize/create?type=metrics&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:%236092C0,fill:0.5,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:stacked,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))" - ); + expect(link).toStrictEqual({ + app: 'kibana', + hash: '/visualize/create', + search: { + _a: + "(filters:!(),linked:!f,query:(language:kuery,query:''),uiState:(),vis:(aggs:!(),params:(axis_formatter:number,axis_min:0,axis_position:left,axis_scale:normal,default_index_pattern:'metricbeat-*',filter:(language:kuery,query:'host.name : \"example-01\"'),id:test-id,index_pattern:'metricbeat-*',interval:auto,series:!((axis_position:right,chart_type:line,color:#6092C0,fill:0.5,formatter:percent,id:test-id,label:'avg(system.cpu.user.pct)',line_width:2,metrics:!((field:system.cpu.user.pct,id:test-id,type:avg)),point_size:0,separate_axis:0,split_mode:everything,stacked:stacked,value_template:{{value}})),show_grid:1,show_legend:1,time_field:'@timestamp',type:timeseries),title:example-01,type:metrics))", + _g: '(refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))', + type: 'metrics', + }, + }); }); test('createFilterFromOptions()', () => { diff --git a/x-pack/plugins/infra/public/components/metrics_explorer/helpers/create_tsvb_link.ts b/x-pack/plugins/infra/public/components/metrics_explorer/helpers/create_tsvb_link.ts index cb23a96b9c163..20706f563ec63 100644 --- a/x-pack/plugins/infra/public/components/metrics_explorer/helpers/create_tsvb_link.ts +++ b/x-pack/plugins/infra/public/components/metrics_explorer/helpers/create_tsvb_link.ts @@ -21,6 +21,7 @@ import { metricToFormat } from './metric_to_format'; import { InfraFormatterType } from '../../../lib/lib'; import { SourceQuery } from '../../../graphql/types'; import { createMetricLabel } from './create_metric_label'; +import { LinkDescriptor } from '../../../hooks/use_link_props'; export const metricsExplorerMetricToTSVBMetric = (metric: MetricsExplorerOptionsMetric) => { if (metric.aggregation === 'rate') { @@ -64,10 +65,9 @@ const mapMetricToSeries = (chartOptions: MetricsExplorerChartOptions) => ( label: createMetricLabel(metric), axis_position: 'right', chart_type: 'line', - color: encodeURIComponent( + color: (metric.color && colorTransformer(metric.color)) || - colorTransformer(MetricsExplorerColor.color0) - ), + colorTransformer(MetricsExplorerColor.color0), fill: chartOptions.type === MetricsExplorerChartType.area ? 0.5 : 0, formatter: format === InfraFormatterType.bits ? InfraFormatterType.bytes : format, value_template: 'rate' === metric.aggregation ? '{{value}}/s' : '{{value}}', @@ -102,7 +102,7 @@ export const createTSVBLink = ( series: MetricsExplorerSeries, timeRange: MetricsExplorerTimeOptions, chartOptions: MetricsExplorerChartOptions -) => { +): LinkDescriptor => { const appState = { filters: [], linked: false, @@ -139,7 +139,13 @@ export const createTSVBLink = ( time: { from: timeRange.from, to: timeRange.to }, }; - return `../app/kibana#/visualize/create?type=metrics&_g=${encode(globalState)}&_a=${encode( - appState as any - )}`; + return { + app: 'kibana', + hash: '/visualize/create', + search: { + type: 'metrics', + _g: encode(globalState), + _a: encode(appState as any), + }, + }; }; diff --git a/x-pack/plugins/infra/public/components/navigation/routed_tabs.tsx b/x-pack/plugins/infra/public/components/navigation/routed_tabs.tsx index 2838ac6cda6dd..d9ea44e2f1f6a 100644 --- a/x-pack/plugins/infra/public/components/navigation/routed_tabs.tsx +++ b/x-pack/plugins/infra/public/components/navigation/routed_tabs.tsx @@ -9,55 +9,51 @@ import React from 'react'; import { Route } from 'react-router-dom'; import { euiStyled } from '../../../../observability/public'; +import { useLinkProps } from '../../hooks/use_link_props'; +import { LinkDescriptor } from '../../hooks/use_link_props'; -interface TabConfiguration { +interface TabConfig { title: string | React.ReactNode; - path: string; } +type TabConfiguration = TabConfig & LinkDescriptor; + interface RoutedTabsProps { tabs: TabConfiguration[]; } const noop = () => {}; -export class RoutedTabs extends React.Component { - public render() { - return {this.renderTabs()}; - } - - private renderTabs() { - return this.props.tabs.map(tab => { - return ( - { - return ( - - {/* eslint-disable-next-line @elastic/eui/href-or-on-click */} - ) => { - e.preventDefault(); - history.push(tab.path); - }} - > - - {tab.title} - - - - ); - }} - /> - ); - }); - } -} +export const RoutedTabs = ({ tabs }: RoutedTabsProps) => { + return ( + + {tabs.map(tab => { + return ; + })} + + ); +}; + +const Tab = ({ title, pathname, app }: TabConfiguration) => { + const linkProps = useLinkProps({ app, pathname }); + return ( + { + return ( + + {/* eslint-disable-next-line @elastic/eui/href-or-on-click */} + + + {title} + + + + ); + }} + /> + ); +}; const TabContainer = euiStyled.div` .euiLink { diff --git a/x-pack/plugins/infra/public/components/source_configuration/view_source_configuration_button.tsx b/x-pack/plugins/infra/public/components/source_configuration/view_source_configuration_button.tsx index 9c3a40fb7ecf0..93cec8a1c7242 100644 --- a/x-pack/plugins/infra/public/components/source_configuration/view_source_configuration_button.tsx +++ b/x-pack/plugins/infra/public/components/source_configuration/view_source_configuration_button.tsx @@ -6,28 +6,23 @@ import { EuiButton } from '@elastic/eui'; import React from 'react'; -import { Route } from 'react-router-dom'; +import { useLinkProps } from '../../hooks/use_link_props'; interface ViewSourceConfigurationButtonProps { 'data-test-subj'?: string; children: React.ReactNode; + app: 'logs' | 'metrics'; } export const ViewSourceConfigurationButton = ({ 'data-test-subj': dataTestSubj, + app, children, }: ViewSourceConfigurationButtonProps) => { - const href = '/settings'; - + const linkProps = useLinkProps({ app, pathname: '/settings' }); return ( - ( - history.push(href)}> - {children} - - )} - /> + + {children} + ); }; diff --git a/x-pack/plugins/infra/public/components/waffle/lib/create_uptime_link.test.ts b/x-pack/plugins/infra/public/components/waffle/lib/create_uptime_link.test.ts index fb9791fae9b5e..18e5838a15b56 100644 --- a/x-pack/plugins/infra/public/components/waffle/lib/create_uptime_link.test.ts +++ b/x-pack/plugins/infra/public/components/waffle/lib/create_uptime_link.test.ts @@ -46,7 +46,11 @@ describe('createUptimeLink()', () => { avg: 0.6, }, }; - expect(createUptimeLink(options, 'host', node)).toBe('#/?search=host.ip:"10.0.1.2"'); + expect(createUptimeLink(options, 'host', node)).toStrictEqual({ + app: 'uptime', + hash: '/', + search: { search: 'host.ip:"10.0.1.2"' }, + }); }); it('should work for hosts without ip', () => { @@ -62,7 +66,11 @@ describe('createUptimeLink()', () => { avg: 0.6, }, }; - expect(createUptimeLink(options, 'host', node)).toBe('#/?search=host.name:"host-01"'); + expect(createUptimeLink(options, 'host', node)).toStrictEqual({ + app: 'uptime', + hash: '/', + search: { search: 'host.name:"host-01"' }, + }); }); it('should work for pods', () => { @@ -78,9 +86,11 @@ describe('createUptimeLink()', () => { avg: 0.6, }, }; - expect(createUptimeLink(options, 'pod', node)).toBe( - '#/?search=kubernetes.pod.uid:"29193-pod-02939"' - ); + expect(createUptimeLink(options, 'pod', node)).toStrictEqual({ + app: 'uptime', + hash: '/', + search: { search: 'kubernetes.pod.uid:"29193-pod-02939"' }, + }); }); it('should work for container', () => { @@ -96,8 +106,10 @@ describe('createUptimeLink()', () => { avg: 0.6, }, }; - expect(createUptimeLink(options, 'container', node)).toBe( - '#/?search=container.id:"docker-1234"' - ); + expect(createUptimeLink(options, 'container', node)).toStrictEqual({ + app: 'uptime', + hash: '/', + search: { search: 'container.id:"docker-1234"' }, + }); }); }); diff --git a/x-pack/plugins/infra/public/components/waffle/lib/create_uptime_link.ts b/x-pack/plugins/infra/public/components/waffle/lib/create_uptime_link.ts index 023a1a4b6ecef..72b46f4fb5c7b 100644 --- a/x-pack/plugins/infra/public/components/waffle/lib/create_uptime_link.ts +++ b/x-pack/plugins/infra/public/components/waffle/lib/create_uptime_link.ts @@ -7,15 +7,28 @@ import { get } from 'lodash'; import { InfraWaffleMapNode, InfraWaffleMapOptions } from '../../../lib/lib'; import { InventoryItemType } from '../../../../common/inventory_models/types'; +import { LinkDescriptor } from '../../../hooks/use_link_props'; export const createUptimeLink = ( options: InfraWaffleMapOptions, nodeType: InventoryItemType, node: InfraWaffleMapNode -) => { +): LinkDescriptor => { if (nodeType === 'host' && node.ip) { - return `#/?search=host.ip:"${node.ip}"`; + return { + app: 'uptime', + hash: '/', + search: { + search: `host.ip:"${node.ip}"`, + }, + }; } const field = get(options, ['fields', nodeType], ''); - return `#/?search=${field ? field + ':' : ''}"${node.id}"`; + return { + app: 'uptime', + hash: '/', + search: { + search: `${field ? field + ':' : ''}"${node.id}"`, + }, + }; }; diff --git a/x-pack/plugins/infra/public/components/waffle/node_context_menu.tsx b/x-pack/plugins/infra/public/components/waffle/node_context_menu.tsx index 43d27bb8259b3..cc6a94c8a41a2 100644 --- a/x-pack/plugins/infra/public/components/waffle/node_context_menu.tsx +++ b/x-pack/plugins/infra/public/components/waffle/node_context_menu.tsx @@ -24,7 +24,7 @@ import { SectionLinks, SectionLink, } from '../../../../observability/public'; -import { usePrefixPathWithBasepath } from '../../hooks/use_prefix_path_with_basepath'; +import { useLinkProps } from '../../hooks/use_link_props'; interface Props { options: InfraWaffleMapOptions; @@ -46,10 +46,9 @@ export const NodeContextMenu: React.FC = ({ nodeType, popoverPosition, }) => { - const urlPrefixer = usePrefixPathWithBasepath(); - const uiCapabilities = useKibana().services.application?.capabilities; const inventoryModel = findInventoryModel(nodeType); const nodeDetailFrom = currentTime - inventoryModel.metrics.defaultTimeRangeInSeconds * 1000; + const uiCapabilities = useKibana().services.application?.capabilities; // Due to the changing nature of the fields between APM and this UI, // We need to have some exceptions until 7.0 & ECS is finalized. Reference // #26620 for the details for these fields. @@ -81,19 +80,37 @@ export const NodeContextMenu: React.FC = ({ return { label: '', value: '' }; }, [nodeType, node.ip, node.id, options.fields]); + const nodeLogsMenuItemLinkProps = useLinkProps({ + app: 'logs', + ...getNodeLogsUrl({ + nodeType, + nodeId: node.id, + time: currentTime, + }), + }); + const nodeDetailMenuItemLinkProps = useLinkProps({ + ...getNodeDetailUrl({ + nodeType, + nodeId: node.id, + from: nodeDetailFrom, + to: currentTime, + }), + }); + const apmTracesMenuItemLinkProps = useLinkProps({ + app: 'apm', + hash: 'traces', + search: { + kuery: `${apmField}:"${node.id}"`, + }, + }); + const uptimeMenuItemLinkProps = useLinkProps(createUptimeLink(options, nodeType, node)); + const nodeLogsMenuItem: SectionLinkProps = { label: i18n.translate('xpack.infra.nodeContextMenu.viewLogsName', { defaultMessage: '{inventoryName} logs', values: { inventoryName: inventoryModel.singularDisplayName }, }), - href: urlPrefixer( - 'logs', - getNodeLogsUrl({ - nodeType, - nodeId: node.id, - time: currentTime, - }) - ), + ...nodeLogsMenuItemLinkProps, 'data-test-subj': 'viewLogsContextMenuItem', isDisabled: !showLogsLink, }; @@ -103,15 +120,7 @@ export const NodeContextMenu: React.FC = ({ defaultMessage: '{inventoryName} metrics', values: { inventoryName: inventoryModel.singularDisplayName }, }), - href: urlPrefixer( - 'metrics', - getNodeDetailUrl({ - nodeType, - nodeId: node.id, - from: nodeDetailFrom, - to: currentTime, - }) - ), + ...nodeDetailMenuItemLinkProps, isDisabled: !showDetail, }; @@ -120,7 +129,7 @@ export const NodeContextMenu: React.FC = ({ defaultMessage: '{inventoryName} APM traces', values: { inventoryName: inventoryModel.singularDisplayName }, }), - href: urlPrefixer('apm', `#traces?_g=()&kuery=${apmField}:"${node.id}"`), + ...apmTracesMenuItemLinkProps, 'data-test-subj': 'viewApmTracesContextMenuItem', isDisabled: !showAPMTraceLink, }; @@ -130,7 +139,7 @@ export const NodeContextMenu: React.FC = ({ defaultMessage: '{inventoryName} in Uptime', values: { inventoryName: inventoryModel.singularDisplayName }, }), - href: urlPrefixer('uptime', createUptimeLink(options, nodeType, node)), + ...uptimeMenuItemLinkProps, isDisabled: !showUptimeLink, }; @@ -163,28 +172,10 @@ export const NodeContextMenu: React.FC = ({ )} - - - - + + + + diff --git a/x-pack/plugins/infra/public/hooks/use_link_props.test.tsx b/x-pack/plugins/infra/public/hooks/use_link_props.test.tsx new file mode 100644 index 0000000000000..13e054de2dcf7 --- /dev/null +++ b/x-pack/plugins/infra/public/hooks/use_link_props.test.tsx @@ -0,0 +1,186 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { encode } from 'rison-node'; +import { createMemoryHistory, LocationDescriptorObject } from 'history'; +import { renderHook } from '@testing-library/react-hooks'; +import React from 'react'; +import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public'; +import { HistoryContext } from '../utils/history_context'; +import { coreMock } from 'src/core/public/mocks'; +import { useLinkProps, LinkDescriptor } from './use_link_props'; + +const PREFIX = '/test-basepath/s/test-space/app/'; + +const coreStartMock = coreMock.createStart(); + +coreStartMock.application.getUrlForApp.mockImplementation((app, options) => { + return `${PREFIX}${app}${options?.path}`; +}); + +const INTERNAL_APP = 'metrics'; + +// Note: Memory history doesn't support basename, +// we'll work around this by re-assigning 'createHref' so that +// it includes a basename, this then acts as our browserHistory instance would. +const history = createMemoryHistory(); +const originalCreateHref = history.createHref; +history.createHref = (location: LocationDescriptorObject): string => { + return `${PREFIX}${INTERNAL_APP}${originalCreateHref.call(history, location)}`; +}; + +const ProviderWrapper: React.FC = ({ children }) => { + return ( + + {children}; + + ); +}; + +const renderUseLinkPropsHook = (props?: Partial) => { + return renderHook(() => useLinkProps({ app: INTERNAL_APP, ...props }), { + wrapper: ProviderWrapper, + }); +}; +describe('useLinkProps hook', () => { + describe('Handles internal linking', () => { + it('Provides the correct baseline props', () => { + const { result } = renderUseLinkPropsHook({ pathname: '/' }); + expect(result.current.href).toBe('/test-basepath/s/test-space/app/metrics/'); + expect(result.current.onClick).toBeDefined(); + }); + + it('Provides the correct props with options', () => { + const { result } = renderUseLinkPropsHook({ + pathname: '/inventory', + search: { + type: 'host', + id: 'some-id', + count: '12345', + }, + }); + expect(result.current.href).toBe( + '/test-basepath/s/test-space/app/metrics/inventory?type=host&id=some-id&count=12345' + ); + expect(result.current.onClick).toBeDefined(); + }); + + it('Provides the correct props with more complex encoding', () => { + const { result } = renderUseLinkPropsHook({ + pathname: '/inventory', + search: { + type: 'host + host', + name: 'this name has spaces and ** and %', + id: 'some-id', + count: '12345', + animals: ['dog', 'cat', 'bear'], + }, + }); + expect(result.current.href).toBe( + '/test-basepath/s/test-space/app/metrics/inventory?type=host%20%2B%20host&name=this%20name%20has%20spaces%20and%20**%20and%20%25&id=some-id&count=12345&animals=dog,cat,bear' + ); + expect(result.current.onClick).toBeDefined(); + }); + + it('Provides the correct props with a consumer using Rison encoding for search', () => { + const state = { + refreshInterval: { pause: true, value: 0 }, + time: { from: 12345, to: 54321 }, + }; + const { result } = renderUseLinkPropsHook({ + pathname: '/inventory', + search: { + type: 'host + host', + state: encode(state), + }, + }); + expect(result.current.href).toBe( + '/test-basepath/s/test-space/app/metrics/inventory?type=host%20%2B%20host&state=(refreshInterval:(pause:!t,value:0),time:(from:12345,to:54321))' + ); + expect(result.current.onClick).toBeDefined(); + }); + }); + + describe('Handles external linking', () => { + it('Provides the correct baseline props', () => { + const { result } = renderUseLinkPropsHook({ + app: 'ml', + pathname: '/', + }); + expect(result.current.href).toBe('/test-basepath/s/test-space/app/ml/'); + expect(result.current.onClick).not.toBeDefined(); + }); + + it('Provides the correct props with pathname options', () => { + const { result } = renderUseLinkPropsHook({ + app: 'ml', + pathname: '/explorer', + search: { + type: 'host', + id: 'some-id', + count: '12345', + }, + }); + expect(result.current.href).toBe( + '/test-basepath/s/test-space/app/ml/explorer?type=host&id=some-id&count=12345' + ); + expect(result.current.onClick).not.toBeDefined(); + }); + + it('Provides the correct props with hash options', () => { + const { result } = renderUseLinkPropsHook({ + app: 'ml', + hash: '/explorer', + search: { + type: 'host', + id: 'some-id', + count: '12345', + }, + }); + expect(result.current.href).toBe( + '/test-basepath/s/test-space/app/ml#/explorer?type=host&id=some-id&count=12345' + ); + expect(result.current.onClick).not.toBeDefined(); + }); + + it('Provides the correct props with more complex encoding', () => { + const { result } = renderUseLinkPropsHook({ + app: 'ml', + hash: '/explorer', + search: { + type: 'host + host', + name: 'this name has spaces and ** and %', + id: 'some-id', + count: '12345', + animals: ['dog', 'cat', 'bear'], + }, + }); + expect(result.current.href).toBe( + '/test-basepath/s/test-space/app/ml#/explorer?type=host%20%2B%20host&name=this%20name%20has%20spaces%20and%20**%20and%20%25&id=some-id&count=12345&animals=dog,cat,bear' + ); + expect(result.current.onClick).not.toBeDefined(); + }); + + it('Provides the correct props with a consumer using Rison encoding for search', () => { + const state = { + refreshInterval: { pause: true, value: 0 }, + time: { from: 12345, to: 54321 }, + }; + const { result } = renderUseLinkPropsHook({ + app: 'rison-app', + hash: 'rison-route', + search: { + type: 'host + host', + state: encode(state), + }, + }); + expect(result.current.href).toBe( + '/test-basepath/s/test-space/app/rison-app#rison-route?type=host%20%2B%20host&state=(refreshInterval:(pause:!t,value:0),time:(from:12345,to:54321))' + ); + expect(result.current.onClick).not.toBeDefined(); + }); + }); +}); diff --git a/x-pack/plugins/infra/public/hooks/use_link_props.tsx b/x-pack/plugins/infra/public/hooks/use_link_props.tsx new file mode 100644 index 0000000000000..e60ab32046832 --- /dev/null +++ b/x-pack/plugins/infra/public/hooks/use_link_props.tsx @@ -0,0 +1,109 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { useMemo } from 'react'; +import { stringify } from 'query-string'; +import url from 'url'; +import { url as urlUtils } from '../../../../../src/plugins/kibana_utils/public'; +import { usePrefixPathWithBasepath } from './use_prefix_path_with_basepath'; +import { useHistory } from '../utils/history_context'; + +type Search = Record; + +export interface LinkDescriptor { + app: string; + pathname?: string; + hash?: string; + search?: Search; +} + +interface LinkProps { + href?: string; + onClick?: (e: React.MouseEvent | React.MouseEvent) => void; +} + +export const useLinkProps = ({ app, pathname, hash, search }: LinkDescriptor): LinkProps => { + validateParams({ app, pathname, hash, search }); + + const history = useHistory(); + const prefixer = usePrefixPathWithBasepath(); + + const encodedSearch = useMemo(() => { + return search ? encodeSearch(search) : undefined; + }, [search]); + + const internalLinkResult = useMemo(() => { + // When the logs / metrics apps are first mounted a history instance is setup with a 'basename' equal to the + // 'appBasePath' received from Core's 'AppMountParams', e.g. /BASE_PATH/s/SPACE_ID/app/APP_ID. With internal + // linking we are using 'createHref' and 'push' on top of this history instance. So a pathname of /inventory used within + // the metrics app will ultimatey end up as /BASE_PATH/s/SPACE_ID/app/metrics/inventory. React-router responds to this + // as it is instantiated with the same history instance. + return history?.createHref({ + pathname: pathname ? formatPathname(pathname) : undefined, + search: encodedSearch, + }); + }, [history, pathname, encodedSearch]); + + const externalLinkResult = useMemo(() => { + // The URI spec defines that the query should appear before the fragment + // https://tools.ietf.org/html/rfc3986#section-3 (e.g. url.format()). However, in Kibana, apps that use + // hash based routing expect the query to be part of the hash. This will handle that. + const mergedHash = hash && encodedSearch ? `${hash}?${encodedSearch}` : hash; + + const link = url.format({ + pathname, + hash: mergedHash, + search: !hash ? encodedSearch : undefined, + }); + + return prefixer(app, link); + }, [hash, encodedSearch, pathname, prefixer, app]); + + const onClick = useMemo(() => { + // If these results are equal we know we're trying to navigate within the same application + // that the current history instance is representing + if (internalLinkResult && linksAreEquivalent(externalLinkResult, internalLinkResult)) { + return (e: React.MouseEvent | React.MouseEvent) => { + e.preventDefault(); + if (history) { + history.push({ + pathname: pathname ? formatPathname(pathname) : undefined, + search: encodedSearch, + }); + } + }; + } else { + return undefined; + } + }, [internalLinkResult, externalLinkResult, history, pathname, encodedSearch]); + + return { + href: externalLinkResult, + onClick, + }; +}; + +const encodeSearch = (search: Search) => { + return stringify(urlUtils.encodeQuery(search), { sort: false, encode: false }); +}; + +const formatPathname = (pathname: string) => { + return pathname[0] === '/' ? pathname : `/${pathname}`; +}; + +const validateParams = ({ app, pathname, hash, search }: LinkDescriptor) => { + if (!app && hash) { + throw new Error( + 'The metrics and logs apps use browserHistory. Please provide a pathname rather than a hash.' + ); + } +}; + +const linksAreEquivalent = (externalLink: string, internalLink: string): boolean => { + // Compares with trailing slashes removed. This handles the case where the pathname is '/' + // and 'createHref' will include the '/' but Kibana's 'getUrlForApp' will remove it. + return externalLink.replace(/\/$/, '') === internalLink.replace(/\/$/, ''); +}; diff --git a/x-pack/plugins/infra/public/pages/infrastructure/index.tsx b/x-pack/plugins/infra/public/pages/infrastructure/index.tsx index 2271147c9d088..b4ff7aeff696c 100644 --- a/x-pack/plugins/infra/public/pages/infrastructure/index.tsx +++ b/x-pack/plugins/infra/public/pages/infrastructure/index.tsx @@ -62,22 +62,25 @@ export const InfrastructurePage = ({ match }: RouteComponentProps) => { diff --git a/x-pack/plugins/infra/public/pages/infrastructure/snapshot/index.tsx b/x-pack/plugins/infra/public/pages/infrastructure/snapshot/index.tsx index ba0e9b436e4e7..dbb8b2d8e2952 100644 --- a/x-pack/plugins/infra/public/pages/infrastructure/snapshot/index.tsx +++ b/x-pack/plugins/infra/public/pages/infrastructure/snapshot/index.tsx @@ -6,7 +6,6 @@ import { EuiButton, EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import { identity } from 'fp-ts/lib/function'; import React, { useContext } from 'react'; import { SnapshotPageContent } from './page_content'; @@ -25,6 +24,7 @@ import { WithWaffleOptionsUrlState } from '../../../containers/waffle/with_waffl import { WithWaffleTimeUrlState } from '../../../containers/waffle/with_waffle_time'; import { useTrackPageview } from '../../../../../observability/public'; import { useKibana } from '../../../../../../../src/plugins/kibana_react/public'; +import { useLinkProps } from '../../../hooks/use_link_props'; export const SnapshotPage = () => { const uiCapabilities = useKibana().services.application?.capabilities; @@ -39,7 +39,10 @@ export const SnapshotPage = () => { useTrackPageview({ app: 'infra_metrics', path: 'inventory' }); useTrackPageview({ app: 'infra_metrics', path: 'inventory', delay: 15000 }); - const prependBasePath = useKibana().services.http?.basePath.prepend ?? identity; + const tutorialLinkProps = useLinkProps({ + app: 'kibana', + hash: '/home/tutorial_directory/metrics', + }); return ( @@ -77,7 +80,7 @@ export const SnapshotPage = () => { { {uiCapabilities?.infrastructure?.configureSource ? ( - + {i18n.translate('xpack.infra.configureSourceActionLabel', { defaultMessage: 'Change source configuration', })} diff --git a/x-pack/plugins/infra/public/pages/link_to/redirect_to_node_detail.tsx b/x-pack/plugins/infra/public/pages/link_to/redirect_to_node_detail.tsx index 55dd15158b96f..9eae632756a3f 100644 --- a/x-pack/plugins/infra/public/pages/link_to/redirect_to_node_detail.tsx +++ b/x-pack/plugins/infra/public/pages/link_to/redirect_to_node_detail.tsx @@ -10,6 +10,7 @@ import { Redirect, RouteComponentProps } from 'react-router-dom'; import { replaceMetricTimeInQueryString } from '../metrics/containers/with_metrics_time'; import { getFromFromLocation, getToFromLocation } from './query_params'; import { InventoryItemType } from '../../../common/inventory_models/types'; +import { LinkDescriptor } from '../../hooks/use_link_props'; type RedirectToNodeDetailProps = RouteComponentProps<{ nodeId: string; @@ -40,7 +41,16 @@ export const getNodeDetailUrl = ({ nodeId: string; to?: number; from?: number; -}) => { - const args = to && from ? `?to=${to}&from=${from}` : ''; - return `link-to/${nodeType}-detail/${nodeId}${args}`; +}): LinkDescriptor => { + return { + app: 'metrics', + pathname: `link-to/${nodeType}-detail/${nodeId}`, + search: + to && from + ? { + to: `${to}`, + from: `${from}`, + } + : undefined, + }; }; diff --git a/x-pack/plugins/infra/public/pages/link_to/redirect_to_node_logs.tsx b/x-pack/plugins/infra/public/pages/link_to/redirect_to_node_logs.tsx index 9c998085400ca..d9aaa2da7bbc8 100644 --- a/x-pack/plugins/infra/public/pages/link_to/redirect_to_node_logs.tsx +++ b/x-pack/plugins/infra/public/pages/link_to/redirect_to_node_logs.tsx @@ -19,6 +19,7 @@ import { getFilterFromLocation, getTimeFromLocation } from './query_params'; import { useSource } from '../../containers/source/source'; import { findInventoryFields } from '../../../common/inventory_models'; import { InventoryItemType } from '../../../common/inventory_models/types'; +import { LinkDescriptor } from '../../hooks/use_link_props'; type RedirectToNodeLogsType = RouteComponentProps<{ nodeId: string; @@ -81,6 +82,14 @@ export const getNodeLogsUrl = ({ nodeId: string; nodeType: InventoryItemType; time?: number; -}) => { - return [`link-to/${nodeType}-logs/`, nodeId, ...(time ? [`?time=${time}`] : [])].join(''); +}): LinkDescriptor => { + return { + app: 'logs', + pathname: `link-to/${nodeType}-logs/${nodeId}`, + search: time + ? { + time: `${time}`, + } + : undefined, + }; }; diff --git a/x-pack/plugins/infra/public/pages/logs/page_content.tsx b/x-pack/plugins/infra/public/pages/logs/page_content.tsx index 48ead15b2a232..3ead026b10065 100644 --- a/x-pack/plugins/infra/public/pages/logs/page_content.tsx +++ b/x-pack/plugins/infra/public/pages/logs/page_content.tsx @@ -31,23 +31,27 @@ export const LogsPageContent: React.FunctionComponent = () => { const logAnalysisCapabilities = useLogAnalysisCapabilitiesContext(); const streamTab = { + app: 'logs', title: streamTabTitle, - path: '/stream', + pathname: '/stream', }; const logRateTab = { + app: 'logs', title: logRateTabTitle, - path: '/log-rate', + pathname: '/log-rate', }; const logCategoriesTab = { + app: 'logs', title: logCategoriesTabTitle, - path: '/log-categories', + pathname: '/log-categories', }; const settingsTab = { + app: 'logs', title: settingsTabTitle, - path: '/settings', + pathname: '/settings', }; return ( @@ -85,11 +89,11 @@ export const LogsPageContent: React.FunctionComponent = () => { - - - - - + + + + + )} diff --git a/x-pack/plugins/infra/public/pages/logs/stream/page_no_indices_content.tsx b/x-pack/plugins/infra/public/pages/logs/stream/page_no_indices_content.tsx index 739bad5689a96..7a84e67e8fc3d 100644 --- a/x-pack/plugins/infra/public/pages/logs/stream/page_no_indices_content.tsx +++ b/x-pack/plugins/infra/public/pages/logs/stream/page_no_indices_content.tsx @@ -6,20 +6,24 @@ import { EuiButton, EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import { identity } from 'fp-ts/lib/function'; import React from 'react'; import { NoIndices } from '../../../components/empty_states/no_indices'; import { ViewSourceConfigurationButton } from '../../../components/source_configuration'; import { useKibana } from '../../../../../../../src/plugins/kibana_react/public'; +import { useLinkProps } from '../../../hooks/use_link_props'; export const LogsPageNoIndicesContent = () => { const { - services: { application, http }, + services: { application }, } = useKibana<{}>(); const canConfigureSource = application?.capabilities?.logs?.configureSource ? true : false; - const prependBasePath = http?.basePath.prepend ?? identity; + + const tutorialLinkProps = useLinkProps({ + app: 'kibana', + hash: '/home/tutorial_directory/logging', + }); return ( { { {canConfigureSource ? ( - + {i18n.translate('xpack.infra.configureSourceActionLabel', { defaultMessage: 'Change source configuration', })} diff --git a/x-pack/plugins/infra/public/pages/metrics/components/invalid_node.tsx b/x-pack/plugins/infra/public/pages/metrics/components/invalid_node.tsx index 43f684cd5a585..b089e2237c2e5 100644 --- a/x-pack/plugins/infra/public/pages/metrics/components/invalid_node.tsx +++ b/x-pack/plugins/infra/public/pages/metrics/components/invalid_node.tsx @@ -6,19 +6,20 @@ import { EuiButton, EuiEmptyPrompt, EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; -import { identity } from 'fp-ts/lib/function'; import React from 'react'; - import { euiStyled } from '../../../../../observability/public'; import { ViewSourceConfigurationButton } from '../../../components/source_configuration'; -import { useKibana } from '../../../../../../../src/plugins/kibana_react/public'; +import { useLinkProps } from '../../../hooks/use_link_props'; interface InvalidNodeErrorProps { nodeName: string; } export const InvalidNodeError: React.FunctionComponent = ({ nodeName }) => { - const prependBasePath = useKibana().services.http?.basePath.prepend ?? identity; + const tutorialLinkProps = useLinkProps({ + app: 'kibana', + hash: '/home/tutorial_directory/metrics', + }); return ( = actions={ - + = - +