Skip to content

Commit

Permalink
Switch to grafana-openapi-client-go (grafana#262)
Browse files Browse the repository at this point in the history
* Add grafana-openapi-client-go, add to provider

* Update unit test for rules and workflow

* folders use Grafana golang api

* datasources use Grafana golang api

* dashboards use Grafana golang api

* Remove code that is dead due to previous changes

* Unit test fixes
  • Loading branch information
mbarrien authored Nov 13, 2023
1 parent a4962f6 commit 8b3ce5e
Show file tree
Hide file tree
Showing 22 changed files with 489 additions and 960 deletions.
7 changes: 6 additions & 1 deletion cmd/grr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ func main() {
Version: Version,
}

gclient, err := grafana.GetClient()
if err != nil {
log.Fatalln(err)
}

grizzly.ConfigureProviderRegistry(
[]grizzly.Provider{
&grafana.Provider{},
grafana.NewProvider(gclient),
})

// workflow commands
Expand Down
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ require (
github.com/fatih/color v1.15.0
github.com/gdamore/tcell v1.4.0
github.com/go-clix/cli v0.2.0
github.com/go-openapi/errors v0.20.4
github.com/gobwas/glob v0.2.3
github.com/google/go-jsonnet v0.20.0
github.com/grafana/grafana-openapi-client-go v0.0.0-20231016093917-88fc2f84f257
github.com/grafana/synthetic-monitoring-agent v0.16.5
github.com/grafana/synthetic-monitoring-api-go-client v0.7.0
github.com/grafana/tanka v0.25.0
Expand All @@ -25,22 +27,40 @@ require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/runtime v0.26.0 // indirect
github.com/go-openapi/spec v0.20.9 // indirect
github.com/go-openapi/strfmt v0.21.7 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-openapi/validate v0.22.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/rivo/uniseg v0.1.0 // indirect
Expand All @@ -49,6 +69,10 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.mongodb.org/mongo-driver v1.12.1 // indirect
go.opentelemetry.io/otel v1.17.0 // indirect
go.opentelemetry.io/otel/metric v1.17.0 // indirect
go.opentelemetry.io/otel/trace v1.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
Expand Down
128 changes: 47 additions & 81 deletions go.sum

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions pkg/grafana/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package grafana

import (
"fmt"
"net/url"
"os"

gclient "github.com/grafana/grafana-openapi-client-go/client"
)

func GetClient() (*gclient.GrafanaHTTPAPI, error) {
grafanaURL, exists := os.LookupEnv("GRAFANA_URL")
if !exists {
return nil, fmt.Errorf("require GRAFANA_URL (optionally GRAFANA_TOKEN & GRAFANA_USER)")
}
parsedUrl, err := url.Parse(grafanaURL)
if err != nil {
return nil, fmt.Errorf("invalid Grafana URL")
}

transportConfig := gclient.DefaultTransportConfig().WithHost(parsedUrl.Host).WithSchemes([]string{parsedUrl.Scheme})
if token, exists := os.LookupEnv("GRAFANA_TOKEN"); exists {
if user, exists := os.LookupEnv("GRAFANA_USER"); exists {
transportConfig.BasicAuth = url.UserPassword(user, token)
} else {
transportConfig.APIKey = token
}
}
grafanaClient := gclient.NewHTTPClientWithConfig(nil, transportConfig)
return grafanaClient, nil
}
68 changes: 0 additions & 68 deletions pkg/grafana/config.go

This file was deleted.

75 changes: 0 additions & 75 deletions pkg/grafana/config_test.go

This file was deleted.

22 changes: 9 additions & 13 deletions pkg/grafana/dashboard-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ func (h *DashboardHandler) APIVersion() string {
return h.Provider.APIVersion()
}

const (
dashboardFolderDefault = "General"
)

// GetExtension returns the file name extension for a dashboard
func (h *DashboardHandler) GetExtension() string {
return "json"
Expand All @@ -70,9 +66,9 @@ func (h *DashboardHandler) ResourceFilePath(resource grizzly.Resource, filetype
// Parse parses a manifest object into a struct for this resource type
func (h *DashboardHandler) Parse(m manifest.Manifest) (grizzly.Resources, error) {
resource := grizzly.Resource(m)
resource.SetSpecString("uid", resource.GetMetadata("name"))
resource.SetSpecString("uid", resource.Name())
if !resource.HasMetadata("folder") {
resource.SetMetadata("folder", dashboardFolderDefault)
resource.SetMetadata("folder", generalFolderUID)
}
return grizzly.Resources{resource}, nil
}
Expand All @@ -94,9 +90,9 @@ func (h *DashboardHandler) GetUID(resource grizzly.Resource) (string, error) {

// GetByUID retrieves JSON for a resource from an endpoint, by UID
func (h *DashboardHandler) GetByUID(UID string) (*grizzly.Resource, error) {
resource, err := getRemoteDashboard(UID)
resource, err := getRemoteDashboard(h.Provider.client, UID)
if err != nil {
return nil, fmt.Errorf("Error retrieving dashboard %s: %v", UID, err)
return nil, fmt.Errorf("Error retrieving dashboard %s: %w", UID, err)
}
return resource, nil
}
Expand All @@ -107,27 +103,27 @@ func (h *DashboardHandler) GetRemote(resource grizzly.Resource) (*grizzly.Resour
if uid != resource.Name() {
return nil, fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
return getRemoteDashboard(resource.Name())
return getRemoteDashboard(h.Provider.client, resource.Name())
}

// ListRemote retrieves as list of UIDs of all remote resources
func (h *DashboardHandler) ListRemote() ([]string, error) {
return getRemoteDashboardList()
return getRemoteDashboardList(h.Provider.client)
}

// Add pushes a new dashboard to Grafana via the API
func (h *DashboardHandler) Add(resource grizzly.Resource) error {
return postDashboard(resource)
return postDashboard(h.Provider.client, resource)
}

// Update pushes a dashboard to Grafana via the API
func (h *DashboardHandler) Update(existing, resource grizzly.Resource) error {
return postDashboard(resource)
return postDashboard(h.Provider.client, resource)
}

// Preview renders Jsonnet then pushes them to the endpoint if previews are possible
func (h *DashboardHandler) Preview(resource grizzly.Resource, opts *grizzly.PreviewOpts) error {
s, err := postSnapshot(resource, opts)
s, err := postSnapshot(h.Provider.client, resource, opts)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 8b3ce5e

Please sign in to comment.