How to use Kibana API in plugin development

Hi there,

we're trying to develop a custom plugin and we're stuck when it comes to call a Kibana APIs.

We can import the RequestHandlerContext interface to provide a context we can use to call Elasticsearch APIs, but we didn't find anything useful inside context.core to make the code equivalent of the following call

curl -XGET "localhost:5601/api/index_patterns/index_pattern/<index_pattern_id>

Our final goal would be to retrieve all the fields from each index pattern, which means retrieve all index patterns (we can use the context.core.savedObjects.client.find({ type: 'index-pattern' }) call to achieve that) and for each one take its id and make the above call, extracting its fields.

Is it possible the function we're looking for is the getFieldsForIndexPattern defined in the src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts file? If so, how can I import it in my file to properly use it? It requires a param indexPattern of type IndexPattern | IndexPatternSpec, which leads me to "how do I instantiate a index pattern object? I may hopefully retrieve it from a getIndexPattern() method, passing the right index_pattern_id string, but all the functions with such a name/behavior I found are private and only usable inside the src/plugins subfolder.

Do I have to initialize my plugins taking into account the src/plugins/data/... plugins in order to use such calls?
Thanks!

EDIT: I forgot to mention I'd like to call those APIs from the server side.

Hi @Fabio-sama, inside Kibana plugin you shouldn't use REST API for this, but should use other plugins instead.

In this case you need to depend on data plugin which provides services for working with index patterns (they are called data views now in main branch).

Not sure if you've seen, but we have a bunch of examples plugins. We don't have many with server-side code though, but this one might be useful:

To run kibana with these plugins start development with: yarn start --run-examples and you will have Developer Examples app in a side menu

Similar how that examples plugins uses data.search server side, you'll need to use data.indexPatterns

This is how index pattern rest API is implemented. You can use it as an example on how to use index pattern service server-side:

Hi there,

first of all thank you for your reply.

Yes I didn't mean to directly use the REST API. What I meant was to find the right plugin (object, interface, whatever) which could return me what the API call returns (to be more specific, the index pattern fields).

I think I might have sorted it out. I was looking in the wrong path since I was looking in the src/plugins/data/public for something I need to use in server side.

What I need was in src/plugins/data/server/ and was the IndexPatternFetcher class, which takes the Elasticsearch client in its constructor (which I have in server side from context.core.elasticsearch.client.asCurrentUser) and has the getFieldsForWildcard function which does exactly what I need (it takes the index_pattern.attributes.title and not the index_pattern.attributes.id but it's ok).

So thank you so much for you interest.
I'll test my solution a little bit more before mark it as Solution.

1 Like

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