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

Add edit dashboards command #1573

Merged
merged 10 commits into from
Dec 1, 2023

Conversation

jillguyonnet
Copy link
Contributor

@jillguyonnet jillguyonnet commented Nov 29, 2023

Summary

This PR adds the elastic-package edit dashboards <dashboardIds> command using the method implemented in #1565.

Relates to elastic/kibana#170517

Example usage

Using the interactive dashboard selection prompt

elastic-package edit dashboards

Passing a comma-separated list of dashboard ids (success)

elastic-package edit dashboards -d elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824,elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

Output:

Make Kibana dashboards editable

The following dashboards are now editable in Kibana:
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

Remember to export modified dashboards with elastic-package export dashboards

Done

Passing a comma-separated list of dashboard ids (partial failure)

In this example the first two ids are valid and the third one is not):

elastic-package edit dashboards -d elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824,elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395,12345

Output:

The following dashboards are now editable in Kibana:
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

Remember to export modified dashboards with elastic-package export dashboards

Error: failed to make one or more dashboards editable: failed to export dashboard 123456: could not export saved objects; API status code = 400; response body = {"statusCode":400,"error":"Bad Request","message":"Error fetching objects to export","attributes":{"objects":[{"id":"123456","type":"dashboard","error":{"statusCode":404,"error":"Not Found","message":"Saved object [dashboard/123456] not found"}}]}}

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

Manually tested and it works great, I think this will make a good workflow. It would be great to add a howto in the docs directory with the workflows to create and modify dashboards.

cmd/edit.go Outdated Show resolved Hide resolved
cmd/edit.go Outdated Show resolved Hide resolved
cmd/edit.go Show resolved Hide resolved
@jillguyonnet jillguyonnet self-assigned this Nov 29, 2023
@jillguyonnet jillguyonnet marked this pull request as ready for review November 29, 2023 14:51
@jillguyonnet jillguyonnet added the Team:Fleet Label for the Fleet team label Nov 29, 2023
@elasticmachine
Copy link
Collaborator

Pinging @elastic/fleet (Team:Fleet)

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

Looks good, added only some nitpicking mostly related to Go style.

Would you like to add the documentation here or in a future PR?

internal/kibana/client.go Outdated Show resolved Hide resolved
cmd/edit.go Outdated Show resolved Hide resolved
cmd/edit.go Outdated Show resolved Hide resolved
cmd/edit.go Outdated Show resolved Hide resolved
@jillguyonnet
Copy link
Contributor Author

Thanks @jsoriano for the feedback! I can certainly add documentation in this PR.

@jillguyonnet
Copy link
Contributor Author

jillguyonnet commented Nov 30, 2023

Hey @jsoriano 👋

I was thinking about the output of this command and realised it wouldn't be a good user experience if the user tried to make multiple dashboards editable and one of them failed: the command would stop on the first failure and fail to report any success or other potential failures. For example, if dashboard ids 123,456,789 were passed and the queries worked for 123 but not for 456 and 789, then the user would only see an error message for 456, despite 123 being successfully updated, and they wouldn't know that 789 would fail too.

In this spirit, I've made some changes while incorporating your suggestions. The output currently looks like so:

With 2 valid ids and 1 invalid id:

./elastic-package edit dashboards -d elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824,elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395,12345
Make Kibana dashboards editable

Failed to make the following dashboards editable in Kibana:
failed to export dashboard 12345: could not export saved objects; API status code = 400; response body = {"statusCode":400,"error":"Bad Request","message":"Error fetching objects to export","attributes":{"objects":[{"id":"12345","type":"dashboard","error":{"statusCode":404,"error":"Not Found","message":"Saved object [dashboard/12345] not found"}}]}}

The following dashboards are now editable in Kibana:
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

And if there was an issue with building the dashboard URLs, I think the output should still be successful:

./elastic-package edit dashboards -d elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824,elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395,12345
Make Kibana dashboards editable

Failed to make the following dashboards editable in Kibana:
failed to export dashboard 12345: could not export saved objects; API status code = 400; response body = {"statusCode":400,"error":"Bad Request","message":"Error fetching objects to export","attributes":{"objects":[{"id":"12345","type":"dashboard","error":{"statusCode":404,"error":"Not Found","message":"Saved object [dashboard/12345] not found"}}]}}

Failed to retrieve dashboard URLS: failed to retrieve Kibana URL: parse "https://127.0.0.1:5601foo": invalid port ":5601foo" after host
The following dashboards are now editable in Kibana:
elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

What do you think?

@jsoriano
Copy link
Member

/test

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

What do you think?

Sounds great, I like the new output, including the successful parts when something went wrong.

kibanaURL, err := url.Parse(kibanaHost)
if err != nil {
return "", fmt.Errorf("failed to retrieve Kibana URL: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

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

I am getting this output:

The following dashboards are now editable in Kibana:
https://127.0.0.1:5601#/view/elastic_agent-a148dc70-6b3c-11ed-98de-67bdecd21824
https://127.0.0.1:5601#/view/elastic_agent-1badd650-d136-11ed-b85f-4be0157fc90c

I think we are missing to add the /app/dashboards part here.

Suggested change
}
}
dashboardsURL, err := kibanaURL.JoinPath("app", "dashboards")
if err != nil {
return "", fmt.Errorf("failed to build base dashboards URL: %w", err)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, sorry, just realised that too, pushed a fix.

urls, err := dashboardURLs(*kibanaClient, updatedDashboardIDs)
if err != nil {
cmd.Println(fmt.Sprintf("\nFailed to retrieve dashboard URLS: %s", err.Error()))
cmd.Println(fmt.Sprintf("The following dashboards are now editable in Kibana:\n%s", strings.Join(updatedDashboardIDs, "\n")))
Copy link
Member

Choose a reason for hiding this comment

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

This will probably never happen, because we were using the URL before. But good to handle the error just in case 👍

cmd/edit.go Show resolved Hide resolved
@jillguyonnet
Copy link
Contributor Author

@jsoriano I iterated based on your feedback. With the command returning an error when at least one dashboard could not be updated, I thought printing the failures was redundant with the final command output. This is what it looks like in the current implementation:

Success:

./elastic-package edit dashboards -d elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824,elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395
Make Kibana dashboards editable

The following dashboards are now editable in Kibana:
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

Done

Failure:

./elastic-package edit dashboards -d elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824,elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395,12345
Make Kibana dashboards editable

The following dashboards are now editable in Kibana:
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

Error: failed to make one or more dashboards editable: failed to export dashboard 12345: could not export saved objects; API status code = 400; response body = {"statusCode":400,"error":"Bad Request","message":"Error fetching objects to export","attributes":{"objects":[{"id":"12345","type":"dashboard","error":{"statusCode":404,"error":"Not Found","message":"Saved object [dashboard/12345] not found"}}]}}

WDYT?

Note that the case where the dashboard URLs doesn't return a failure if all dashboards were successfully edited, e.g.:

./elastic-package edit dashboards -d elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824,elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395
Make Kibana dashboards editable

Failed to retrieve dashboard URLS: failed to retrieve Kibana URL: parse "https://127.0.0.1:5601foo": invalid port ":5601foo" after host
The following dashboards are now editable in Kibana:
elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

Done

But, as you pointed out, this should in theory never happen 🙂

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

Looks great, thanks for adding the docs too!

errMsgs := make([]string, 0, len(failedDashboardUpdates))
for _, value := range failedDashboardUpdates {
errMsgs = append(errMsgs, value)
}
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if errors.Join() could help here https://pkg.go.dev/errors#Join. I haven't used it a lot, it was recently added to the standard library.


Error: failed to make one or more dashboards editable: failed to export dashboard 456: could not export saved objects; API status code = 400; response body = {"statusCode":400,"error":"Bad Request","message":"Error fetching objects to export","attributes":{"objects":[{"id":"456","type":"dashboard","error":{"statusCode":404,"error":"Not Found","message":"Saved object [dashboard/456] not found"}}]}}
```

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we have to remember that after modifying the dashboard, it should be exported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean that this error message could be confusing to the user?

Copy link
Member

Choose a reason for hiding this comment

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

Ah no, the error is fine, probably I put my comment in the wrong place.
I mean that after following this howto the user is going to be able to edit a dashboard. Maybe we have to mention in some place that after editing the dashboard, the developer should remember to export it with elastic-package export.

docs/howto/create_new_package.md Show resolved Hide resolved
@jillguyonnet
Copy link
Contributor Author

@jsoriano I pushed a change making use of errors.Join() (thanks!) and adding a small paragraph about dashboard exports to the howto.

Was there any action you were thinking of regarding this comment?

Maybe we have to remember that after modifying the dashboard, it should be exported.

@jsoriano
Copy link
Member

Was there any action you were thinking of regarding this comment?

Maybe we have to remember that after modifying the dashboard, it should be exported.

It would be only to mention that after modifying the dashboard, the developer needs to remember to export it, but it is fine as it is now too.

@jillguyonnet
Copy link
Contributor Author

It would be only to mention that after modifying the dashboard, the developer needs to remember to export it, but it is fine as it is now too.

Ah, I see! OK, I can add e.g.

Done. Remember to export your modified dashboards.

to the output and the doc for extra clarity.

Copy link
Contributor

@mrodm mrodm left a comment

Choose a reason for hiding this comment

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

LGTM
Just added minor suggestions

docs/howto/make_dashboards_editable.md Outdated Show resolved Hide resolved
cmd/edit.go Outdated Show resolved Hide resolved
@jillguyonnet
Copy link
Contributor Author

Latest version 🙂

./elastic-package edit dashboards -d elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824,elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395
Make Kibana dashboards editable

The following dashboards are now editable in Kibana:
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

Remember to export modified dashboards with elastic-package export dashboards

Done
Make Kibana dashboards editable

The following dashboards are now editable in Kibana:
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-0600ffa0-6b5e-11ed-98de-67bdecd21824
https://127.0.0.1:5601/app/dashboards#/view/elastic_agent-f47f18cc-9c7d-4278-b2ea-a6dee816d395

Remember to export modified dashboards with elastic-package export dashboards

Error: failed to make one or more dashboards editable: failed to export dashboard 123456: could not export saved objects; API status code = 400; response body = {"statusCode":400,"error":"Bad Request","message":"Error fetching objects to export","attributes":{"objects":[{"id":"123456","type":"dashboard","error":{"statusCode":404,"error":"Not Found","message":"Saved object [dashboard/123456] not found"}}]}}

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

cc @jillguyonnet

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

Perfect 👍

@jillguyonnet jillguyonnet merged commit 5eb9eab into elastic:main Dec 1, 2023
4 checks passed
@jillguyonnet jillguyonnet deleted the add-edit-dashboards-command branch December 1, 2023 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Fleet Label for the Fleet team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants