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 datasources list method #26
Open
danielllek
wants to merge
4
commits into
nytm:master
Choose a base branch
from
danielllek: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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -31,19 +31,10 @@ type DataSource struct { | |
} | ||
|
||
// JSONData is a representation of the datasource `jsonData` property | ||
type JSONData struct { | ||
AssumeRoleArn string `json:"assumeRoleArn,omitempty"` | ||
AuthType string `json:"authType,omitempty"` | ||
CustomMetricsNamespaces string `json:"customMetricsNamespaces,omitempty"` | ||
DefaultRegion string `json:"defaultRegion,omitempty"` | ||
TlsSkipVerify bool `json:"tlsSkipVerify,omitempty"` | ||
} | ||
type JSONData map[string]interface{} | ||
|
||
// SecureJSONData is a representation of the datasource `secureJsonData` property | ||
type SecureJSONData struct { | ||
AccessKey string `json:"accessKey,omitempty"` | ||
SecretKey string `json:"secretKey,omitempty"` | ||
} | ||
type SecureJSONData map[string]interface{} | ||
|
||
func (c *Client) NewDataSource(s *DataSource) (int64, error) { | ||
data, err := json.Marshal(s) | ||
|
@@ -97,6 +88,33 @@ func (c *Client) UpdateDataSource(s *DataSource) error { | |
return nil | ||
} | ||
|
||
func (c *Client) DataSources() ([]DataSource, 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. It would be nice to have a test for this method, and even nicer that it'd have an example of the payload the api returns. 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. We will prepare some additional tests. Regarding payload, for example we use Azure Monitor and Zabbix, both having different jsonData and secureJsopData Azure Monitor:
Zabbix:
|
||
datasources := make([]DataSource, 0) | ||
|
||
path := fmt.Sprintf("/api/datasources") | ||
req, err := c.newRequest("GET", path, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.Do(req) | ||
if err != nil { | ||
return datasources, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return datasources, errors.New(resp.Status) | ||
} | ||
|
||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return datasources, err | ||
} | ||
|
||
err = json.Unmarshal(data, &datasources) | ||
|
||
return datasources, err | ||
} | ||
|
||
func (c *Client) DataSource(id int64) (*DataSource, error) { | ||
path := fmt.Sprintf("/api/datasources/%d", id) | ||
req, err := c.newRequest("GET", path, nil, 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'm curious to hear your reasoning for changing the types here. Do we have to worry about any potential consumers of Datasource method since they'll have to change how they access the JSONData and SecureJSONData properties?
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.
There are multiple DataSource types available for Grafana and struct here allows only to use go-grafana-api only with one DataSource (I assume it was something with AWS).
If we loosen typing here to
map[string]interface{}
we are able to pass different jsonData and secureJsonData needed for other datasource types.We know breaking changes are not welcomed into the interfaces and we are more than happy to make this possible without breaking changes but I don't see a way how to achieve that. Maybe with reflect or changing type to some Inteface but I am not a fan of either.
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.
Breaking changes aside, I'm not sure it's necessary to remove the schema from
jsonData
andsecureJsonData
entirely to support additional data source types. There tends to be some overlap in values between the different data sources; how we've handled different schemas for similar objects is to merge the maps of all the possible keys of the different data source types and tack on theomitempty
tag to most of them so that they won't be sent unless explicitly used. Does that sound like an alright approach to you?