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
Merged
22 changes: 16 additions & 6 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,17 @@ func editDashboardsCmd(cmd *cobra.Command, args []string) error {
}
}

var updatedDashboardIDs []string
var errMsgs []string
updatedDashboardIDs := make([]string, 0, len(dashboardIDs))
failedDashboardUpdates := make(map[string]string, len(dashboardIDs))
for _, dashboardID := range dashboardIDs {
err = kibanaClient.SetManagedSavedObject("dashboard", dashboardID, false)
if err != nil {
errMsgs = append(errMsgs, err.Error())
failedDashboardUpdates[dashboardID] = err.Error()
} else {
updatedDashboardIDs = append(updatedDashboardIDs, dashboardID)
}
}

if len(errMsgs) > 0 {
cmd.Println(fmt.Sprintf("\nFailed to make the following dashboards editable in Kibana:\n%s", strings.Join(errMsgs, "\n")))
}
if len(updatedDashboardIDs) > 0 {
urls, err := dashboardURLs(*kibanaClient, updatedDashboardIDs)
if err != nil {
Expand All @@ -126,6 +123,18 @@ func editDashboardsCmd(cmd *cobra.Command, args []string) error {
cmd.Println(fmt.Sprintf("\nThe following dashboards are now editable in Kibana:%s", urls))
jillguyonnet marked this conversation as resolved.
Show resolved Hide resolved
}
}

if len(failedDashboardUpdates) > 0 {
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.

formattedErrMsgs := strings.Join(errMsgs, "\n")
fmt.Println("")
return fmt.Errorf("failed to make one or more dashboards editable: %s", formattedErrMsgs)
}

fmt.Println("\nDone")
return nil
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -138,6 +147,7 @@ func dashboardURLs(kibanaClient kibana.Client, dashboardIDs []string) (string, e
var urls strings.Builder
for _, dashboardID := range dashboardIDs {
dashboardURL := *kibanaURL
dashboardURL.Path = "app/dashboards"
dashboardURL.Fragment = "/view/" + dashboardID
fmt.Fprintf(&urls, "\n%s", dashboardURL.String())
}
Expand Down
4 changes: 4 additions & 0 deletions docs/howto/create_new_package.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ _Enter the package directory. For nginx integration, it's: [packages/nginx/](htt
3. Verify the package:
1. Enter the package directory: `cd <new_package>`.
2. Check package correctness: `elastic-package check`.

### Edit package dashboards
jsoriano marked this conversation as resolved.
Show resolved Hide resolved

Once a package has been created, assets may need to be updated, e.g. make use of recently added features. Dashboards can be made editable by using the [`elastic-package edit dashboards` command](https://github.com/elastic/elastic-package/blob/main/docs/howto/make_dashboards_editable.md).
68 changes: 68 additions & 0 deletions docs/howto/make_dashboards_editable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# HOWTO: Make dashboards editable in Kibana

## Introduction

As of 8.11, managed assets, including dashboards, are read-only in Kibana. This change was introduced to prevent users from losing changes on package upgrades. Integrations authors, however, need the ability to edit assets in order to adopt new features.

## Making a dashboard editable

Dashboards can be made editable in Kibana by using the `elastic-package edit dashboards` command. This command can eiter be run interactively, allowing manual selection of dashboards, or be passed a comma-separated list of dashboard ids.
jillguyonnet marked this conversation as resolved.
Show resolved Hide resolved

### Using the interactive dashboard selection prompt

Run the following command:
```
elastic-package edit dashboards
```

Use the interactive dashboard selection prompt to select the dashboard(s) that should be made editable.

### Using a comma-separated list of dashboard ids

Pass the list with the `-d` flag:
```
elastic-package edit dashboards -d 123,456,789
```

Each dashboard id will be processed and the outcome of the updates will be listed in the command's final output.

### Command output

The final output will provide the outcome (success or failure) of the update for each dashboard.

For example, assuming the following command:
```
elastic-package edit dashboards -d 123,456,789
```

#### Success

Assuming '123', '456' and '789' are valid dashboard ids and all three updates succeed, the output will be successful and report the URLs of the updated dashboards:
```
Make Kibana dashboards editable

The following dashboards are now editable in Kibana:
https://<kibanaURL>/app/dashboards#/view/123
https://<kibanaURL>/app/dashboards#/view/456
https://<kibanaURL>/app/dashboards#/view/789

Done
```

#### Partial failure

Assuming that `456` is an invalid dashboard id and that the update is successful for ids `123` and `789`, the output will report the URLs of the updated dashboards as well as an error listing the failures:
```
Make Kibana dashboards editable

The following dashboards are now editable in Kibana:
https://<kibanaURL>/app/dashboards#/view/123
https://<kibanaURL>/app/dashboards#/view/789

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.

### Optional flags

* `allow-snapshot`: to allow exporting dashboards from a Elastic stack SNAPSHOT version
* `tls-skip-verify`: to skip TLS verify