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

Support exporting dashboards using the saved objects API #1357

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
75 changes: 74 additions & 1 deletion internal/kibana/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
package kibana

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"

"github.com/Masterminds/semver/v3"

"github.com/elastic/elastic-package/internal/common"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/multierror"
Expand All @@ -19,8 +23,77 @@ type exportedType struct {
Objects []common.MapStr `json:"objects"`
}

// Export method exports selected dashboards using the Kibana Export API.
// Export method exports selected dashboards using the Kibana APIs.
func (c *Client) Export(dashboardIDs []string) ([]common.MapStr, error) {
versionInfo, err := c.Version()
if err != nil {
return nil, fmt.Errorf("could not get Kibana version info: %w", err)
}

sv, err := semver.NewVersion(versionInfo.Number)
if err != nil {
return nil, fmt.Errorf("failed to parse Kibana version: %w", err)
}

if sv.LessThan(semver.MustParse("8.8.0")) {
return c.exportWithDashboardsAPI(dashboardIDs)
}

return c.exportWithSavedObjectsAPI(dashboardIDs)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}

type exportSavedObjectsRequest struct {
ExcludeExportDetails bool `json:"excludeExportDetails"`
IncludeReferencesDeep bool `json:"includeReferencesDeep"`
Objects []exportSavedObjectsRequestObject `json:"objects"`
}

type exportSavedObjectsRequestObject struct {
ID string `json:"id"`
Type string `json:"type"`
}

func (c *Client) exportWithSavedObjectsAPI(dashboardIDs []string) ([]common.MapStr, error) {
logger.Debug("Export dashboards using the Kibana Saved Objects Export API")

exportRequest := exportSavedObjectsRequest{
ExcludeExportDetails: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be needed to set IncludeReferencesDeep to true, to get other references used in the dashboards like tags, searches, maps... ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure, trying to add it.

}
for _, dashboardID := range dashboardIDs {
exportRequest.Objects = append(exportRequest.Objects, exportSavedObjectsRequestObject{
ID: dashboardID,
Type: "dashboard",
})
}

body, err := json.Marshal(exportRequest)
if err != nil {
return nil, err
}

path := SavedObjectsAPI + "/_export"
statusCode, respBody, err := c.sendRequest(http.MethodPost, path, body)
if err != nil {
return nil, fmt.Errorf("could not export dashboards; API status code = %d; response body = %s: %w", statusCode, respBody, err)
}

var dashboards []common.MapStr
decoder := json.NewDecoder(bytes.NewReader(respBody))

for decoder.More() {
var dashboard common.MapStr
err := decoder.Decode(&dashboard)
if err != nil {
return nil, fmt.Errorf("unmarshalling response failed (body: \n%s): %w", respBody, err)
}

dashboards = append(dashboards, dashboard)
}

return dashboards, nil
Copy link
Member Author

Choose a reason for hiding this comment

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

To reuse the code introduced in #1565 if finally merged.

}

func (c *Client) exportWithDashboardsAPI(dashboardIDs []string) ([]common.MapStr, error) {
logger.Debug("Export dashboards using the Kibana Export API")

var query strings.Builder
Expand Down