Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] Add endpoint to server for creating empty index & index pattern #94028

Merged
merged 24 commits into from
Mar 18, 2021

Conversation

kindsun
Copy link
Contributor

@kindsun kindsun commented Mar 9, 2021

This PR adds a simple endpoint in the Maps app for creating an index and index pattern:

image

This functionality exists behind a feature flag. To enable, add the following to your kibana.yml:
xpack.maps.enableDrawingFeature: true

Testing is doable if you're comfortable with using cUrl commands or using a client such as Insomnia. Running the PR locally, the following test cUrl command can be used after editing the --url arg and the --header 'Cookie: arg.

curl --request POST \
  --url 'http://localhost:5601/<REPLACE WITH DEV ENV PREFIX>/api/maps/docSource' \
  --header 'Accept: */*' \
  --header 'Accept-Language: en-US,en;q=0.9' \
  --header 'Connection: keep-alive' \
  --header 'Content-Type: application/json' \
  --header 'Cookie: <REPLACE WITH COOKIE>' \
  --header 'DNT: 1' \
  --header 'Origin: http://localhost:5601' \
  --header 'Referer: http://localhost:5601/hwc/app/maps/map' \
  --header 'Sec-Fetch-Dest: empty' \
  --header 'Sec-Fetch-Mode: cors' \
  --header 'Sec-Fetch-Site: same-origin' \
  --header 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36' \
  --header 'kbn-version: 8.0.0' \
  --header 'sec-ch-ua: "Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"' \
  --header 'sec-ch-ua-mobile: ?0' \
  --data '{"index":"testing1234","mappings":{"properties":{"coordinates":{"type":"geo_point"}}}}'

You will need to use your own cookie for this to work. To obtain one:

  1. Run this PR locally and navigate to the maps app
  2. Upload a GeoJSON file via the GeoJSON Uploader
  3. Inspect the network request for either of the 2 import requests used to upload the file
  4. Copy as cUrl the network request and pull out the cookie portion for use in the above cUrl command

Notes:

  • Jest tests don't really offer us a lot here since the bulk of the functions are simple and ultimately just rely on the ES API to do the heavy lifting
  • Functional tests will be really beneficial but will have to wait until we have a corresponding UI portion

@kindsun kindsun added [Deprecated-Use Team:Presentation]Team:Geo Former Team Label for Geo Team. Now use Team:Presentation enhancement New value added to drive a business result v8.0.0 v7.13.0 labels Mar 9, 2021
@kindsun kindsun added the release_note:feature Makes this part of the condensed release notes label Mar 9, 2021
@kindsun kindsun marked this pull request as ready for review March 9, 2021 16:02
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-gis (Team:Geo)

Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the exposed endpoint is to low level and would be better organized around the use case. For example, instead of having the endpoint be CREATE_INDEX_API_PATH and return ImportResponse, how about having a CREATE_DRAWING_LAYER and return success or failure boolean.

@kindsun
Copy link
Contributor Author

kindsun commented Mar 9, 2021

I feel like the exposed endpoint is to low level and would be better organized around the use case. For example, instead of having the endpoint be CREATE_INDEX_API_PATH and return ImportResponse, how about having a CREATE_DRAWING_LAYER and return success or failure boolean.

There are still some details to be decided around the flow of the front end and how the back end will ultimately be used. At this point, this endpoint is built to be a placeholder with no unnecessary handling such as cluster settings or import pipeline details. We can confidently say we will need an index name and we will need mappings so these have been included. We'll plan to iterate on the API as we get further into it, test our use cases and get a better idea for what we need. This was similar to alerting where we built a best effort back end, followed by the front end which informed further changes to the back end. Consider this a very early phase in a slightly bigger effort.

@kindsun kindsun marked this pull request as draft March 11, 2021 00:24
@kindsun kindsun changed the title [Maps] Add points/shapes indexing endpoint to server [Maps] Add endpoint for creating empty index & index pattern to server Mar 11, 2021
@kindsun
Copy link
Contributor Author

kindsun commented Mar 11, 2021

Following some offline discussion, this initial PR has been simplified to an endpoint with the following qualities:

  • Creates an empty index & index pattern
  • Accepts a JSON body describing the index name and mappings
  • Returns a simplified success: boolean and errors if success: false

The portion focused on adding data will be a separate PR following this one.

The new endpoint introduced in this PR accepts POSTs at http://<kibana host>/api/maps/indexSource. The next PR to follow this will focus on adding data, likely POSTing to an endpoint like http://<kibana host>/api/maps/feature. If there are any thoughts on a better pattern, this would be a good PR for discussing them!

@kindsun kindsun marked this pull request as ready for review March 11, 2021 17:44
@kindsun kindsun requested a review from nreese March 11, 2021 17:44
@kindsun kindsun changed the title [Maps] Add endpoint for creating empty index & index pattern to server [Maps] Add endpoint to server for creating empty index & index pattern Mar 11, 2021
Copy link
Contributor

@thomasneirynck thomasneirynck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx tested. works!

For testing, Kibana has a dedicated test-suite api_integration, specifically for this kind of middle-ware:

https://github.com/elastic/kibana/tree/4584a8b570402aa07832cf3e5b520e5d2cfa7166/x-pack/test/api_integration/apis/maps

I'd add a file create_index and some tests there.

},
};

export async function createIndexSource(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: createIndexAndIndexPattern ?

Copy link
Contributor Author

@kindsun kindsun Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with this name, just seems long. Would you also be okay with posting to the endpoint api/maps/indexAndIndexPattern? Trying to keep them consistent. I went with createIndexSource and api/maps/indexSource for brevity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some offline conversation, changed refs to doc source

const indexPatternsService = await dataPlugin.indexPatterns.indexPatternsServiceFactory(
context.core.savedObjects.client,
context.core.elasticsearch.client.asCurrentUser
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should do a privilige-check here to see if current-user can create index and index-pattern. If not, we can shortcut and return meaningful human-readable error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that we need to verify user permissions. I previously chatted with @nreese about this and we agreed we should probably leverage the same solution that file upload ends up using to resolve #41928. This might be in the form of a dedicated endpoint to check permissions, not sure. Regardless I think we probably want to handle it separately from this PR since it's not straightforward yet how best to do this.

@kindsun kindsun requested a review from a team as a code owner March 16, 2021 16:20
Copy link
Contributor

@thomasneirynck thomasneirynck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one comment wrt tests, otherwise lgtm thx!

expect(resp.body.success).to.be(true);
});

it('should fail to create index and pattern with invalid index', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two test-stubs are dependent so I would not isolate them. (ie. disabling the first, will cause the other to succeed). I would move them together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chatted about this offline. I've added a test case confirming requests fail if a user attempts to clobber an existing index. We'll likely catch this ahead of time on the UI side as well similar to how we do with GeoJSON Upload

@kindsun
Copy link
Contributor Author

kindsun commented Mar 17, 2021

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
maps 143.3KB 143.7KB +346.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@kindsun kindsun merged commit ad18739 into elastic:master Mar 18, 2021
@kindsun kindsun deleted the add-custom-shapes-and-points-back-end branch March 18, 2021 01:40
kindsun pushed a commit to kindsun/kibana that referenced this pull request Mar 18, 2021
elastic#94028)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
kindsun pushed a commit that referenced this pull request Mar 18, 2021
#94028) (#94880)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
@rashmivkulkarni rashmivkulkarni added release_note:skip Skip the PR/issue when compiling release notes and removed release_note:feature Makes this part of the condensed release notes labels Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Deprecated-Use Team:Presentation]Team:Geo Former Team Label for Geo Team. Now use Team:Presentation enhancement New value added to drive a business result release_note:skip Skip the PR/issue when compiling release notes v7.13.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants