Trouble with adding and removing indexes using core Kibana Api

We are really facing problems in implementing elasticsearch requests from Kibana Plugin using the core kibana API for elasticsearch? Also referred migration examples and the legacy to new platform migration document. Could you please provide sample query to implement adding indexes, removing indexes/index pattern, updating indexes, adding alias name, etc for third party kibana plugin?

Hello @kibanaPluginDev ,

In the latest version of Kibana to work with Index Patterns you need to get hold of the Index Pattern client from the data plugin through the plugin contract. However, an extra complication is that it must happen within a route handler.

I could look something like this:

class MyPlugin {
  setup(setupCore) {
    const router = setupCore.http.createRouter();
    router.post({
      path: '/api/my-plugin/do-something',
      validate: {
        body: schema.object({
          // ...
        }),
      },
    async (ctx, req, res) => {
      const [startCore, startPlugins] = await setupCore.getStartServices();
      const savedObjectsClient = startCore.savedObjects.client;
      const elasticsearchClient = startCore.elasticsearch.client.asCurrentUser;
      const indexPatternsService = await startPlugins.data.indexPatterns.indexPatternsServiceFactory(
        savedObjectsClient,
        elasticsearchClient
      );

      // For example, create new index pattern:
      const indexPattern = await indexPatternsService.createAndSave(
        // ..
      );

      return res.ok({})
  }
}

Hello @Vadims_Daleckis ,
I tried things as you suggested . Still getting bad request any clue?

import { IRouter, CoreSetup } from '../../../../src/core/server';

export function defineRoutes(router: IRouter, core: CoreSetup) {
router.post(
{
path: '/api/sip/addTribeIndexPattern2',
validate: false,
},
async (context, request, response) => {
const [startCore, startPlugins] = await core.getStartServices();
const savedObjectsClient = startCore.savedObjects.client;
const elasticsearchClient = startCore.elasticsearch.client.asCurrentUser;
const indexPatternsService = await startPlugins.data.indexPatterns.indexPatternsServiceFactory(
savedObjectsClient,
elasticsearchClient
);
const pattern = 'pattern1';
const indexPattern = await indexPatternsService.createAndSave(
{
title: pattern,
},
true
);

  return response.ok({
    body: { indexPattern },
  });
}

);
}

What error are you seeing?

It says Property 'client' does not exist on type 'SavedObjectsServiceStart'. Similarly for asCurrentUser, and data . Getting a bad request in Postman.

In Postman, are you providing all the necessary authentication headers? Maybe worth executing the request from Kibana browser-side, to make sure all authentication information is set properly.

In a browser-side plugin, you could do:

class MyPlugin {
  start(core) {
    core.http.post(/* ... */)
      .then(console.log)
      .catch(console.error);
  }
}

Hello @Vadims_Daleckis ,
I'm still getting error in the postman while trying to hit that post request .

{
"statusCode": 400,
"error": "Bad Request",
"message": "Request must contain a kbn-xsrf header."
}

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

You are not able to execute your HTTP request from Postman because your request is missing the XSRF header, see this thread on a similar question about the XSRF header: Where can I get the correct kbn-xsrf value for my plugin HTTP requests?