-
Notifications
You must be signed in to change notification settings - Fork 116
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
ebd3f56
3f9d2c2
ef8dbed
4eeb03f
7dfa630
1632a3d
af71012
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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) | ||
} | ||
|
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be needed to set There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍