Skip to content

Commit

Permalink
Allow offline providers (#436)
Browse files Browse the repository at this point in the history
* Allow offline providers

* Fix tests

* Add an integration test

* simplify (as per PR suggestion)

* Lint
  • Loading branch information
malcolmholmes authored May 9, 2024
1 parent 4dc308b commit 241cea4
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 52 deletions.
18 changes: 8 additions & 10 deletions cmd/grr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,20 @@ func main() {
}

func createRegistry(context *config.Context) grizzly.Registry {
providerInitFuncs := []func() (grizzly.Provider, error){
func() (grizzly.Provider, error) { return grafana.NewProvider(&context.Grafana) },
func() (grizzly.Provider, error) { return mimir.NewProvider(&context.Mimir) },
func() (grizzly.Provider, error) { return syntheticmonitoring.NewProvider(&context.SyntheticMonitoring) },
providers := []grizzly.Provider{
grafana.NewProvider(&context.Grafana),
mimir.NewProvider(&context.Mimir),
syntheticmonitoring.NewProvider(&context.SyntheticMonitoring),
}

var providers []grizzly.Provider

var providerList []string
for _, initFunc := range providerInitFuncs {
provider, err := initFunc()
for _, provider := range providers {
err := provider.Validate()
if err != nil {
providerList = append(providerList, fmt.Sprintf("%s - inactive (%s)", provider.Name(), err.Error()))
continue
} else {
providerList = append(providerList, provider.Name()+" - active")
}
providerList = append(providerList, provider.Name()+" - active")
providers = append(providers, provider)
}
notifier.InfoStderr(nil, "Providers: "+strings.Join(providerList, ", "))
Expand Down
3 changes: 1 addition & 2 deletions integration/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ func TestDashboard(t *testing.T) {
}

func TestDashboardHandler(t *testing.T) {
provider, err := grafana.NewProvider(&testutil.TestContext().Grafana)
require.NoError(t, err)
provider := grafana.NewProvider(&testutil.TestContext().Grafana)
handler := grafana.NewDashboardHandler(provider)

t.Run("get remote dashboard - success", func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions integration/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

func TestDatasources(t *testing.T) {
provider, err := grafana.NewProvider(&testutil.TestContext().Grafana)
require.NoError(t, err)
provider := grafana.NewProvider(&testutil.TestContext().Grafana)
handler := grafana.NewDatasourceHandler(provider)

t.Run("get remote datasource - success", func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions integration/folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

func TestFolders(t *testing.T) {
provider, err := grafana.NewProvider(&testutil.TestContext().Grafana)
require.NoError(t, err)
provider := grafana.NewProvider(&testutil.TestContext().Grafana)
handler := grafana.NewFolderHandler(provider)

dir := "testdata/folders"
Expand Down
3 changes: 1 addition & 2 deletions integration/library-element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

func TestLibraryElements(t *testing.T) {
provider, err := grafana.NewProvider(&testutil.TestContext().Grafana)
require.NoError(t, err)
provider := grafana.NewProvider(&testutil.TestContext().Grafana)
handler := grafana.NewLibraryElementHandler(provider)

t.Run("create libraryElement - success", func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions integration/mimir/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
)

func TestRules(t *testing.T) {
provider, err := mimir.NewProvider(&testutil.TestContext().Mimir)
require.NoError(t, err)
provider := mimir.NewProvider(&testutil.TestContext().Mimir)
handler := provider.GetHandlers()[0]

t.Run("create rule group", func(t *testing.T) {
Expand Down
42 changes: 42 additions & 0 deletions integration/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package integration_test

import (
"testing"
)

func TestEmptyProviders(t *testing.T) {
dir := "testdata/providers"
setupContexts(t, dir)

t.Run("Providers work for offline commands", func(t *testing.T) {
runTest(t, GrizzlyTest{
TestDir: dir,
Commands: []Command{
{
Command: "config use-context empty",
ExpectedCode: 0,
},
{
Command: "show folder.yaml",
ExpectedCode: 0,
ExpectedOutputFile: "folder-output.txt",
},
},
})
})

t.Run("Providers fail online commands without configs", func(t *testing.T) {
runTest(t, GrizzlyTest{
TestDir: dir,
Commands: []Command{
{
Command: "config use-context empty",
},
{
Command: "apply folder.yaml",
ExpectedCode: 1,
},
},
})
})
}
3 changes: 2 additions & 1 deletion integration/pull_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration_test

import (
"fmt"
"path/filepath"
"testing"

Expand All @@ -18,7 +19,7 @@ func TestPull(t *testing.T) {
RunOnContexts: allContexts,
Commands: []Command{
{
Command: "pull " + pullDir,
Command: fmt.Sprintf("pull %s -t Dashboard -t Dashboardfolder -t Datasource", pullDir),
ExpectedCode: 0,
ExpectedOutputContains: `Dashboard.ReciqtgGk pulled`,
},
Expand Down
1 change: 1 addition & 0 deletions integration/testdata/contexts/get-contexts.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
basic_auth
* default
empty
invalid_auth
subpath
7 changes: 7 additions & 0 deletions integration/testdata/providers/folder-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DashboardFolder.sample:
apiVersion: grizzly.grafana.com/v1alpha1
kind: DashboardFolder
metadata:
name: sample
spec:
title: Sample Folder
6 changes: 6 additions & 0 deletions integration/testdata/providers/folder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: grizzly.grafana.com/v1alpha1
kind: DashboardFolder
metadata:
name: sample
spec:
title: Sample Folder
2 changes: 2 additions & 0 deletions integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func setupContexts(t *testing.T, dir string) {
"config set grafana.url http://localhost:3004",
"config set grafana.user admin",
"config set grafana.token invalid",

"config create-context empty",
} {
_, _, err = runLocalGrizzly(t, dir, command)
require.NoError(t, err)
Expand Down
14 changes: 9 additions & 5 deletions pkg/grafana/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ type ClientProvider interface {
}

// NewProvider instantiates a new Provider.
func NewProvider(config *config.GrafanaConfig) (*Provider, error) {
if config.URL == "" {
return nil, fmt.Errorf("grafana URL is not set")
}
func NewProvider(config *config.GrafanaConfig) *Provider {
return &Provider{
config: config,
}, nil
}
}

func (p *Provider) Validate() error {
if p.config.URL == "" {
return fmt.Errorf("grafana URL is not set")
}
return nil
}

func (p *Provider) Name() string {
Expand Down
3 changes: 1 addition & 2 deletions pkg/grafana/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

func TestExtractFolderUID(t *testing.T) {
provider, err := NewProvider(&config.GrafanaConfig{URL: "http://localhost:3000"})
require.NoError(t, err)
provider := NewProvider(&config.GrafanaConfig{URL: "http://localhost:3000"})

client, err := provider.Client()
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/grizzly/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Provider interface {
Version() string
APIVersion() string
GetHandlers() []Handler
Validate() error
}

type ProxyProvider interface {
Expand Down
21 changes: 12 additions & 9 deletions pkg/mimir/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ type Provider struct {
}

// NewProvider instantiates a new Provider.
func NewProvider(config *config.MimirConfig) (*Provider, error) {
func NewProvider(config *config.MimirConfig) *Provider {
clientTool := client.NewHTTPClient(config)
if config.Address == "" {
return nil, fmt.Errorf("mimir address is not set")
}
if config.TenantID == "" {
return nil, fmt.Errorf("mimir tenant id is not set")
}

return &Provider{
config: config,
clientTool: clientTool,
}, nil
}
}

func (p *Provider) Validate() error {
if p.config.Address == "" {
return fmt.Errorf("mimir address is not set")
}
if p.config.TenantID == "" {
return fmt.Errorf("mimir tenant id is not set")
}
return nil
}

func (p *Provider) Name() string {
Expand Down
33 changes: 18 additions & 15 deletions pkg/syntheticmonitoring/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,29 @@ type ClientProvider interface {
}

// NewProvider instantiates a new Provider.
func NewProvider(config *config.SyntheticMonitoringConfig) (*Provider, error) {
if config.URL == "" {
config.URL = "https://synthetic-monitoring-api.grafana.net"
func NewProvider(config *config.SyntheticMonitoringConfig) *Provider {
return &Provider{
config: config,
}
}

func (p *Provider) Validate() error {
if p.config.URL == "" {
p.config.URL = "https://synthetic-monitoring-api.grafana.net"
}
if config.StackID == 0 {
return nil, fmt.Errorf("stack id is not set")
if p.config.StackID == 0 {
return fmt.Errorf("stack id is not set")
}
if config.MetricsID == 0 {
return nil, fmt.Errorf("metrics id is not set")
if p.config.MetricsID == 0 {
return fmt.Errorf("metrics id is not set")
}
if config.LogsID == 0 {
return nil, fmt.Errorf("logs id is not set")
if p.config.LogsID == 0 {
return fmt.Errorf("logs id is not set")
}
if config.Token == "" {
return nil, fmt.Errorf("token is not set")
if p.config.Token == "" {
return fmt.Errorf("token is not set")
}

return &Provider{
config: config,
}, nil
return nil
}

func (p *Provider) Name() string {
Expand Down

0 comments on commit 241cea4

Please sign in to comment.