Skip to content

Commit

Permalink
[synthetic monitoring] replace some api call (grafana#132)
Browse files Browse the repository at this point in the history
* replace http call by synthetic monitoring go client

* fix apply + comments

* refactory the validate of handlers

* Fix error

* Small fixes in synthetic-monitoring.go

Co-authored-by: dsotirakis <[email protected]>
  • Loading branch information
ying-jeanne and dsotirakis authored Jul 16, 2021
1 parent 2a5acfd commit 2673a84
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 632 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ require (
github.com/gobwas/glob v0.2.3
github.com/google/go-jsonnet v0.17.0
github.com/grafana/cortex-tools v0.10.2
github.com/grafana/synthetic-monitoring-agent v0.0.23
github.com/grafana/synthetic-monitoring-api-go-client v0.0.2
github.com/grafana/tanka v0.14.0
github.com/pmezard/go-difflib v1.0.0
github.com/rivo/tview v0.0.0-20200818120338-53d50e499bf9
Expand Down
413 changes: 32 additions & 381 deletions go.sum

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions pkg/grafana/dashboard-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ func NewDashboardHandler(provider Provider) *DashboardHandler {
}
}

// Validate returns the uid of resource
func (h *DashboardHandler) Validate(resource grizzly.Resource) error {
uid, exist := resource.GetSpecString("uid")
if exist {
if uid != resource.Name() {
return fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
}
return nil
}

// Kind returns the name for this handler
func (h *DashboardHandler) Kind() string {
return "Dashboard"
Expand Down Expand Up @@ -83,9 +94,9 @@ func (h *DashboardHandler) GetByUID(UID string) (*grizzly.Resource, error) {

// GetRemote retrieves a dashboard as a resource
func (h *DashboardHandler) GetRemote(resource grizzly.Resource) (*grizzly.Resource, error) {
uid := resource.GetSpecString("uid")
uid, _ := resource.GetSpecString("uid")
if uid != resource.Name() {
return nil, fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.GetSpecString("uid"))
return nil, fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
return getRemoteDashboard(resource.Name())
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/grafana/datasource-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ func (h *DatasourceHandler) Kind() string {
return "Datasource"
}

// Validate returns the uid of resource
func (h *DatasourceHandler) Validate(resource grizzly.Resource) error {
uid, exist := resource.GetSpecString("uid")
if exist {
if uid != resource.Name() {
return fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
}
return nil
}

// APIVersion returns group and version of the provider of this resource
func (h *DatasourceHandler) APIVersion() string {
return h.Provider.APIVersion()
Expand Down
12 changes: 12 additions & 0 deletions pkg/grafana/folder-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func (h *FolderHandler) Kind() string {
return "DashboardFolder"
}

// Validate returns the uid of resource
func (h *FolderHandler) Validate(resource grizzly.Resource) error {
uid, exist := resource.GetSpecString("uid")
if exist {
if uid != resource.Name() {
return fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
}

return nil
}

// APIVersion returns the group and version for the provider of which this handler is a part
func (h *FolderHandler) APIVersion() string {
return h.Provider.APIVersion()
Expand Down
11 changes: 11 additions & 0 deletions pkg/grafana/rules-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ func (h *RuleHandler) Kind() string {
return "PrometheusRuleGroup"
}

// Validate returns the uid of resource
func (h *RuleHandler) Validate(resource grizzly.Resource) error {
uid, exist := resource.GetSpecString("uid")
if exist {
if uid != resource.Name() {
return fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
}
return nil
}

// APIVersion returns the group and version for the provider of which this handler is a part
func (h *RuleHandler) APIVersion() string {
return h.Provider.APIVersion()
Expand Down
21 changes: 17 additions & 4 deletions pkg/grafana/synthetic-monitoring-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ func (h *SyntheticMonitoringHandler) FindResourceFiles(dir string) ([]string, er
return filepath.Glob(path)
}

// Validate returns the uid of resource
func (h *SyntheticMonitoringHandler) Validate(resource grizzly.Resource) error {
job, exist := resource.GetSpecString("job")
if exist {
if job != resource.Name() {
return fmt.Errorf("job '%s' and name '%s', don't match", job, resource.Name())
}
}
settings := resource.GetSpecValue("settings").(map[string]interface{})
if _, ok := settings[resource.GetMetadata("type")]; !ok {
return fmt.Errorf("type '%s' is incorrect", resource.GetMetadata("type"))
}
return nil
}

// ResourceFilePath returns the location on disk where a resource should be updated
func (h *SyntheticMonitoringHandler) ResourceFilePath(resource grizzly.Resource, filetype string) string {
return fmt.Sprintf(syntheticMonitoringPattern, resource.Name(), filetype)
Expand Down Expand Up @@ -104,12 +119,10 @@ func (h *SyntheticMonitoringHandler) ListRemote() ([]string, error) {

// Add adds a new check to the SyntheticMonitoring endpoint
func (h *SyntheticMonitoringHandler) Add(resource grizzly.Resource) error {
url := getSyntheticMonitoringURL("api/v1/check/add")
return postCheck(url, resource)
return addCheck(resource)
}

// Update pushes an updated check to the SyntheticMonitoring endpoing
func (h *SyntheticMonitoringHandler) Update(existing, resource grizzly.Resource) error {
url := getSyntheticMonitoringURL("api/v1/check/update")
return postCheck(url, resource)
return updateCheck(resource)
}
Loading

0 comments on commit 2673a84

Please sign in to comment.