How to get/use indexPatternService in server side api in kibana plugin

I want to use indexPatternService on server api side of plugin, e.g. to get fields for a pattern(/api/index_patterns/_fields_for_wildcard).

I know in server there is indexPatternsServiceFactory, but I don't know how to get service from it or how to use it.
I was able to get savedObjectClient with below code(found it from kibana's source code), I know something similar is needed for indexPattern. Any example is very much appreciated.

init(server, options) {
  server.route({
    path: '/api/manage/something',
    method: 'GET',
    async handler(req, reply) {
      const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects;
      const { callWithInternalUser } = server.plugins.elasticsearch.getCluster(
        'admin'
      );
      const internalRepository = getSavedObjectsRepository(callWithInternalUser);
      const savedObjectClint = new SavedObjectsClient(internalRepository);

      reply("Hello World");
    }
  });
}

kibana version 6.4.2

Thanks!

Not sure if it was available in 6.4.2, but have you tried accessing it from the req object?

const indexPatterns = req.getIndexPatternsService();
1 Like

@lukas, req.getIndexPatternsService() works! don't know why I didn't print the req object! Thanks a lot. Also it has getSavedObjectsClient() as well.
Although I've got the service I'm getting 401 authentication error if I try to do:

await req.getIndexPatternsService().getFieldsForWildcard('test')
or
await req.getSavedObjectsClient().get('index-pattern', 'abcd-1234-abcd')

How can we specify to use admin cluster, so it will use auth from kibana config to call es?

Thanks!

Hmm, in that case you'll probably have to use the factory. Maybe this will work:

const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin');
const indexPatterns = server.indexPatternsServiceFactory({ callCluster: callWithInternalUser });

@lukas, yes, this works! Thank you very much.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.