This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Add search folder by UID #22
Open
overdrive3000
wants to merge
10
commits into
nytm:master
Choose a base branch
from
overdrive3000:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+187
−12
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2acd1c1
Add search folder by UID
overdrive3000 7c083a4
Add support to create folder with custom UID
overdrive3000 1281bda
Change DeleteDashboard method to accept uid
overdrive3000 40db85e
Add GetDashboard find a dashboard by UID, valid from Grafana 5+
overdrive3000 2eeb9c8
Update DashboardMeta struct
overdrive3000 c8bf5a3
Add SearchFolder method
overdrive3000 33b0889
Add SearchDashboard method
overdrive3000 42bb644
Allow search folder only by name and/or folder ID
overdrive3000 400fe0d
Add debug when dashboard is returned
overdrive3000 05cae1b
Add struct type for Dashboards returned by search API
overdrive3000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,19 +7,23 @@ import ( | |
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
type DashboardMeta struct { | ||
IsStarred bool `json:"isStarred"` | ||
Slug string `json:"slug"` | ||
Folder int64 `json:"folderId"` | ||
IsStarred bool `json:"isStarred"` | ||
Slug string `json:"slug"` | ||
Folder int64 `json:"folderId"` | ||
FolderTitle string `json:"folderTitle"` | ||
} | ||
|
||
// DashboardSaveResponse grafana response for create dashboard | ||
type DashboardSaveResponse struct { | ||
Slug string `json:"slug"` | ||
Id int64 `json:"id"` | ||
Uid string `json:"uid"` | ||
ID int64 `json:"id"` | ||
UID string `json:"uid"` | ||
URL string `json:"url"` | ||
Status string `json:"status"` | ||
Version int64 `json:"version"` | ||
} | ||
|
@@ -31,6 +35,24 @@ type Dashboard struct { | |
Overwrite bool `json:overwrite` | ||
} | ||
|
||
// Dashboards represent json returned by search API | ||
type Dashboards struct { | ||
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. If this is specific to the search endpoint can we name it something more along the lines of |
||
ID int64 `json:"id"` | ||
UID string `json:"uid"` | ||
Comment on lines
+40
to
+41
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. Same as above |
||
Title string `json:"title"` | ||
URI string `json:"uri"` | ||
URL string `json:"url"` | ||
Starred bool `json:"isStarred"` | ||
FolderID int64 `json:"folderId"` | ||
FolderUID string `json:"folderUid"` | ||
Comment on lines
+46
to
+47
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. Same as above |
||
FolderTitle string `json:"folderTitle"` | ||
} | ||
|
||
// DashboardDeleteResponse grafana response for delete dashboard | ||
type DashboardDeleteResponse struct { | ||
Title string `json:title` | ||
} | ||
|
||
// Deprecated: use NewDashboard instead | ||
func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*DashboardSaveResponse, error) { | ||
wrapper := map[string]interface{}{ | ||
|
@@ -93,6 +115,68 @@ func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, erro | |
return result, err | ||
} | ||
|
||
// SearchDashboard search a dashboard in Grafana | ||
func (c *Client) SearchDashboard(query string, folderID string) ([]Dashboards, error) { | ||
dashboards := make([]Dashboards, 0) | ||
path := "/api/search" | ||
|
||
params := url.Values{} | ||
params.Add("type", "dash-db") | ||
params.Add("query", query) | ||
params.Add("folderIds", folderID) | ||
|
||
req, err := c.newRequest("GET", path, params, nil) | ||
if err != nil { | ||
return dashboards, err | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return dashboards, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return dashboards, errors.New(resp.Status) | ||
} | ||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return dashboards, err | ||
} | ||
|
||
err = json.Unmarshal(data, &dashboards) | ||
|
||
return dashboards, err | ||
} | ||
|
||
// GetDashboard get a dashboard by UID | ||
func (c *Client) GetDashboard(uid string) (*Dashboard, error) { | ||
path := fmt.Sprintf("/api/dashboards/uid/%s", uid) | ||
req, err := c.newRequest("GET", path, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return nil, errors.New(resp.Status) | ||
} | ||
|
||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := &Dashboard{} | ||
err = json.Unmarshal(data, &result) | ||
result.Folder = result.Meta.Folder | ||
if os.Getenv("GF_LOG") != "" { | ||
log.Printf("got back dashboard response %s", data) | ||
} | ||
return result, err | ||
} | ||
|
||
// Deprecated: use GetDashboard instead | ||
func (c *Client) Dashboard(slug string) (*Dashboard, error) { | ||
path := fmt.Sprintf("/api/dashboards/db/%s", slug) | ||
req, err := c.newRequest("GET", path, nil, nil) | ||
|
@@ -122,20 +206,29 @@ func (c *Client) Dashboard(slug string) (*Dashboard, error) { | |
return result, err | ||
} | ||
|
||
func (c *Client) DeleteDashboard(slug string) error { | ||
path := fmt.Sprintf("/api/dashboards/db/%s", slug) | ||
// DeleteDashboard deletes a grafana dashoboard | ||
func (c *Client) DeleteDashboard(uid string) (string, error) { | ||
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. Can we add a |
||
deleted := &DashboardDeleteResponse{} | ||
path := fmt.Sprintf("/api/dashboards/uid/%s", uid) | ||
req, err := c.newRequest("DELETE", path, nil, nil) | ||
if err != nil { | ||
return err | ||
return "", err | ||
} | ||
|
||
resp, err := c.Do(req) | ||
if err != nil { | ||
return err | ||
return "", err | ||
} | ||
if resp.StatusCode != 200 { | ||
return errors.New(resp.Status) | ||
return "", errors.New(resp.Status) | ||
} | ||
|
||
return nil | ||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
err = json.Unmarshal(data, &deleted) | ||
if err != nil { | ||
return "", err | ||
} | ||
return deleted.Title, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think it's a great idea to be renaming these, as we tend to follow the
Id
andUid
convention for these fields. Would you be alright keeping these as-is?