Skip to content

Commit

Permalink
Expose Elasticsearch from start and deprecate from setup
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolf committed Mar 11, 2020
1 parent babf81b commit d3426d6
Show file tree
Hide file tree
Showing 20 changed files with 212 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [CoreStart](./kibana-plugin-core-server.corestart.md) &gt; [elasticsearch](./kibana-plugin-core-server.corestart.elasticsearch.md)

## CoreStart.elasticsearch property

[ElasticsearchServiceStart](./kibana-plugin-core-server.elasticsearchservicestart.md)

<b>Signature:</b>

```typescript
elasticsearch: ElasticsearchServiceStart;
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CoreStart
| Property | Type | Description |
| --- | --- | --- |
| [capabilities](./kibana-plugin-core-server.corestart.capabilities.md) | <code>CapabilitiesStart</code> | [CapabilitiesStart](./kibana-plugin-core-server.capabilitiesstart.md) |
| [elasticsearch](./kibana-plugin-core-server.corestart.elasticsearch.md) | <code>ElasticsearchServiceStart</code> | [ElasticsearchServiceStart](./kibana-plugin-core-server.elasticsearchservicestart.md) |
| [savedObjects](./kibana-plugin-core-server.corestart.savedobjects.md) | <code>SavedObjectsServiceStart</code> | [SavedObjectsServiceStart](./kibana-plugin-core-server.savedobjectsservicestart.md) |
| [uiSettings](./kibana-plugin-core-server.corestart.uisettings.md) | <code>UiSettingsServiceStart</code> | [UiSettingsServiceStart](./kibana-plugin-core-server.uisettingsservicestart.md) |

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## ElasticsearchServiceSetup.adminClient property

> Warning: This API is now obsolete.
>
>
A client for the `admin` cluster. All Elasticsearch config value changes are processed under the hood. See [IClusterClient](./kibana-plugin-core-server.iclusterclient.md)<!-- -->.

<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## ElasticsearchServiceSetup.createClient property

> Warning: This API is now obsolete.
>
>
Create application specific Elasticsearch cluster API client with customized config. See [IClusterClient](./kibana-plugin-core-server.iclusterclient.md)<!-- -->.

<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## ElasticsearchServiceSetup.dataClient property

> Warning: This API is now obsolete.
>
>
A client for the `data` cluster. All Elasticsearch config value changes are processed under the hood. See [IClusterClient](./kibana-plugin-core-server.iclusterclient.md)<!-- -->.

<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [ElasticsearchServiceStart](./kibana-plugin-core-server.elasticsearchservicestart.md) &gt; [client](./kibana-plugin-core-server.elasticsearchservicestart.client.md)

## ElasticsearchServiceStart.client property

A pre-configured Elasticsearch client. All Elasticsearch config value changes are processed under the hood. See [IClusterClient](./kibana-plugin-core-server.iclusterclient.md)<!-- -->.

<b>Signature:</b>

```typescript
readonly client: IClusterClient;
```

## Example


```js
const client = core.elasticsearch.client;

```

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [ElasticsearchServiceStart](./kibana-plugin-core-server.elasticsearchservicestart.md) &gt; [createClient](./kibana-plugin-core-server.elasticsearchservicestart.createclient.md)

## ElasticsearchServiceStart.createClient property

Create application specific Elasticsearch cluster API client with customized config. See [IClusterClient](./kibana-plugin-core-server.iclusterclient.md)<!-- -->.

<b>Signature:</b>

```typescript
readonly createClient: (type: string, clientConfig?: Partial<ElasticsearchClientConfig>) => ICustomClusterClient;
```

## Example


```js
const client = elasticsearch.createCluster('my-app-name', config);
const data = await client.callAsInternalUser();

```

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [ElasticsearchServiceStart](./kibana-plugin-core-server.elasticsearchservicestart.md)

## ElasticsearchServiceStart interface


<b>Signature:</b>

```typescript
export interface ElasticsearchServiceStart
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [client](./kibana-plugin-core-server.elasticsearchservicestart.client.md) | <code>IClusterClient</code> | A pre-configured Elasticsearch client. All Elasticsearch config value changes are processed under the hood. See [IClusterClient](./kibana-plugin-core-server.iclusterclient.md)<!-- -->. |
| [createClient](./kibana-plugin-core-server.elasticsearchservicestart.createclient.md) | <code>(type: string, clientConfig?: Partial&lt;ElasticsearchClientConfig&gt;) =&gt; ICustomClusterClient</code> | Create application specific Elasticsearch cluster API client with customized config. See [IClusterClient](./kibana-plugin-core-server.iclusterclient.md)<!-- -->. |

1 change: 1 addition & 0 deletions docs/development/core/server/kibana-plugin-core-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [DiscoveredPlugin](./kibana-plugin-core-server.discoveredplugin.md) | Small container object used to expose information about discovered plugins that may or may not have been started. |
| [ElasticsearchError](./kibana-plugin-core-server.elasticsearcherror.md) | |
| [ElasticsearchServiceSetup](./kibana-plugin-core-server.elasticsearchservicesetup.md) | |
| [ElasticsearchServiceStart](./kibana-plugin-core-server.elasticsearchservicestart.md) | |
| [EnvironmentMode](./kibana-plugin-core-server.environmentmode.md) | |
| [ErrorHttpResponseOptions](./kibana-plugin-core-server.errorhttpresponseoptions.md) | HTTP response parameters |
| [FakeRequest](./kibana-plugin-core-server.fakerequest.md) | Fake request object created manually by Kibana plugins. |
Expand Down
24 changes: 23 additions & 1 deletion src/core/server/elasticsearch/elasticsearch_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import { IClusterClient, ICustomClusterClient } from './cluster_client';
import { IScopedClusterClient } from './scoped_cluster_client';
import { ElasticsearchConfig } from './elasticsearch_config';
import { ElasticsearchService } from './elasticsearch_service';
import { InternalElasticsearchServiceSetup, ElasticsearchServiceSetup } from './types';
import {
InternalElasticsearchServiceSetup,
ElasticsearchServiceSetup,
ElasticsearchServiceStart,
} from './types';
import { NodesVersionCompatibility } from './version_check/ensure_es_version';

const createScopedClusterClientMock = (): jest.Mocked<IScopedClusterClient> => ({
Expand Down Expand Up @@ -63,6 +67,22 @@ const createSetupContractMock = () => {
return setupContract;
};

type MockedElasticSearchServiceStart = jest.Mocked<
ElasticsearchServiceStart & {
client: jest.Mocked<IClusterClient>;
}
>;

const createStartContractMock = () => {
const startContract: MockedElasticSearchServiceStart = {
createClient: jest.fn(),
client: createClusterClientMock(),
};
startContract.createClient.mockReturnValue(createCustomClusterClientMock());
startContract.client.asScoped.mockReturnValue(createScopedClusterClientMock());
return startContract;
};

type MockedInternalElasticSearchServiceSetup = jest.Mocked<
InternalElasticsearchServiceSetup & {
adminClient: jest.Mocked<IClusterClient>;
Expand Down Expand Up @@ -95,6 +115,7 @@ const createMock = () => {
stop: jest.fn(),
};
mocked.setup.mockResolvedValue(createInternalSetupContractMock());
mocked.start.mockResolvedValueOnce(createStartContractMock());
mocked.stop.mockResolvedValue();
return mocked;
};
Expand All @@ -103,6 +124,7 @@ export const elasticsearchServiceMock = {
create: createMock,
createInternalSetup: createInternalSetupContractMock,
createSetup: createSetupContractMock,
createStart: createStartContractMock,
createClusterClient: createClusterClientMock,
createCustomClusterClient: createCustomClusterClientMock,
createScopedClusterClient: createScopedClusterClientMock,
Expand Down
44 changes: 29 additions & 15 deletions src/core/server/elasticsearch/elasticsearch_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { ClusterClient, ScopeableRequest } from './cluster_client';
import { ElasticsearchClientConfig } from './elasticsearch_client_config';
import { ElasticsearchConfig, ElasticsearchConfigType } from './elasticsearch_config';
import { InternalHttpServiceSetup, GetAuthHeaders } from '../http/';
import { InternalElasticsearchServiceSetup } from './types';
import { InternalElasticsearchServiceSetup, ElasticsearchServiceStart } from './types';
import { CallAPIOptions } from './api_types';
import { pollEsNodesVersion } from './version_check/ensure_es_version';

Expand All @@ -53,12 +53,16 @@ interface SetupDeps {
}

/** @internal */
export class ElasticsearchService implements CoreService<InternalElasticsearchServiceSetup> {
export class ElasticsearchService
implements CoreService<InternalElasticsearchServiceSetup, ElasticsearchServiceStart> {
private readonly log: Logger;
private readonly config$: Observable<ElasticsearchConfig>;
private subscription: Subscription | undefined;
private stop$ = new Subject();
private kibanaVersion: string;
createClient: InternalElasticsearchServiceSetup['createClient'] | undefined;
dataClient: InternalElasticsearchServiceSetup['dataClient'] | undefined;
adminClient: InternalElasticsearchServiceSetup['adminClient'] | undefined;

constructor(private readonly coreContext: CoreContext) {
this.kibanaVersion = coreContext.env.packageInfo.version;
Expand Down Expand Up @@ -111,7 +115,7 @@ export class ElasticsearchService implements CoreService<InternalElasticsearchSe
const adminClient$ = clients$.pipe(map(clients => clients.adminClient));
const dataClient$ = clients$.pipe(map(clients => clients.dataClient));

const adminClient = {
this.adminClient = {
async callAsInternalUser(
endpoint: string,
clientParams: Record<string, any> = {},
Expand All @@ -120,9 +124,9 @@ export class ElasticsearchService implements CoreService<InternalElasticsearchSe
const client = await adminClient$.pipe(take(1)).toPromise();
return await client.callAsInternalUser(endpoint, clientParams, options);
},
asScoped(request: ScopeableRequest) {
asScoped: (request: ScopeableRequest) => {
return {
callAsInternalUser: adminClient.callAsInternalUser,
callAsInternalUser: this.adminClient!.callAsInternalUser,
async callAsCurrentUser(
endpoint: string,
clientParams: Record<string, any> = {},
Expand All @@ -136,6 +140,7 @@ export class ElasticsearchService implements CoreService<InternalElasticsearchSe
};
},
};

const dataClient = {
async callAsInternalUser(
endpoint: string,
Expand Down Expand Up @@ -163,28 +168,37 @@ export class ElasticsearchService implements CoreService<InternalElasticsearchSe
};

const esNodesCompatibility$ = pollEsNodesVersion({
callWithInternalUser: adminClient.callAsInternalUser,
callWithInternalUser: this.adminClient.callAsInternalUser,
log: this.log,
ignoreVersionMismatch: config.ignoreVersionMismatch,
esVersionCheckInterval: config.healthCheckDelay.asMilliseconds(),
kibanaVersion: this.kibanaVersion,
}).pipe(takeUntil(this.stop$), shareReplay({ refCount: true, bufferSize: 1 }));

this.createClient = (type: string, clientConfig: Partial<ElasticsearchClientConfig> = {}) => {
const finalConfig = merge({}, config, clientConfig);
return this.createClusterClient(type, finalConfig, deps.http.getAuthHeaders);
};

return {
legacy: { config$: clients$.pipe(map(clients => clients.config)) },

adminClient,
dataClient,
esNodesCompatibility$,

createClient: (type: string, clientConfig: Partial<ElasticsearchClientConfig> = {}) => {
const finalConfig = merge({}, config, clientConfig);
return this.createClusterClient(type, finalConfig, deps.http.getAuthHeaders);
},
adminClient: this.adminClient,
dataClient,
createClient: this.createClient,
};
}

public async start() {}
public async start() {
if (typeof this.adminClient === 'undefined' || typeof this.createClient === 'undefined') {
throw new Error('ElasticsearchService needs to be setup before calling start');
} else {
return {
client: this.adminClient,
createClient: this.createClient,
};
}
}

public async stop() {
this.log.debug('Stopping elasticsearch service');
Expand Down
41 changes: 41 additions & 0 deletions src/core/server/elasticsearch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface ElasticsearchServiceSetup {
* const client = elasticsearch.createCluster('my-app-name', config);
* const data = await client.callAsInternalUser();
* ```
* @deprecated
*/
readonly createClient: (
type: string,
Expand All @@ -57,6 +58,7 @@ export interface ElasticsearchServiceSetup {
* ```js
* const client = core.elasticsearch.adminClient;
* ```
* @deprecated
*/
readonly adminClient: IClusterClient;

Expand All @@ -68,10 +70,49 @@ export interface ElasticsearchServiceSetup {
* ```js
* const client = core.elasticsearch.dataClient;
* ```
* @deprecated
*/
readonly dataClient: IClusterClient;
}

/**
* @public
*/
export interface ElasticsearchServiceStart {
/**
* Create application specific Elasticsearch cluster API client with customized config. See {@link IClusterClient}.
*
* @param type Unique identifier of the client
* @param clientConfig A config consists of Elasticsearch JS client options and
* valid sub-set of Elasticsearch service config.
* We fill all the missing properties in the `clientConfig` using the default
* Elasticsearch config so that we don't depend on default values set and
* controlled by underlying Elasticsearch JS client.
* We don't run validation against the passed config and expect it to be valid.
*
* @example
* ```js
* const client = elasticsearch.createCluster('my-app-name', config);
* const data = await client.callAsInternalUser();
* ```
*/
readonly createClient: (
type: string,
clientConfig?: Partial<ElasticsearchClientConfig>
) => ICustomClusterClient;

/**
* A pre-configured Elasticsearch client. All Elasticsearch config value changes are processed under the hood.
* See {@link IClusterClient}.
*
* @example
* ```js
* const client = core.elasticsearch.client;
* ```
*/
readonly client: IClusterClient;
}

/** @internal */
export interface InternalElasticsearchServiceSetup extends ElasticsearchServiceSetup {
// Required for the BWC with the legacy Kibana only.
Expand Down
4 changes: 4 additions & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
ElasticsearchServiceSetup,
IScopedClusterClient,
configSchema as elasticsearchConfigSchema,
ElasticsearchServiceStart,
} from './elasticsearch';

import { HttpServiceSetup } from './http';
Expand Down Expand Up @@ -93,6 +94,7 @@ export {
ElasticsearchError,
ElasticsearchErrorHelpers,
ElasticsearchServiceSetup,
ElasticsearchServiceStart,
APICaller,
FakeRequest,
ScopeableRequest,
Expand Down Expand Up @@ -366,6 +368,8 @@ export interface CoreSetup<TPluginsStart extends object = object> {
export interface CoreStart {
/** {@link CapabilitiesStart} */
capabilities: CapabilitiesStart;
/** {@link ElasticsearchServiceStart} */
elasticsearch: ElasticsearchServiceStart;
/** {@link SavedObjectsServiceStart} */
savedObjects: SavedObjectsServiceStart;
/** {@link UiSettingsServiceStart} */
Expand Down
3 changes: 2 additions & 1 deletion src/core/server/internal_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Type } from '@kbn/config-schema';
import { CapabilitiesSetup, CapabilitiesStart } from './capabilities';
import { ConfigDeprecationProvider } from './config';
import { ContextSetup } from './context';
import { InternalElasticsearchServiceSetup } from './elasticsearch';
import { InternalElasticsearchServiceSetup, ElasticsearchServiceStart } from './elasticsearch';
import { InternalHttpServiceSetup } from './http';
import {
InternalSavedObjectsServiceSetup,
Expand All @@ -49,6 +49,7 @@ export interface InternalCoreSetup {
*/
export interface InternalCoreStart {
capabilities: CapabilitiesStart;
elasticsearch: ElasticsearchServiceStart;
savedObjects: InternalSavedObjectsServiceStart;
uiSettings: InternalUiSettingsServiceStart;
}
Expand Down
Loading

0 comments on commit d3426d6

Please sign in to comment.