Skip to content

Commit

Permalink
Fixing up Connection Permissions to upgrade grafana API version
Browse files Browse the repository at this point in the history
ChangeLog:
  - Enable Enterprise Grafana integration tests.
  - Removed enterprise config flag, in favor of API check
  - Update Connection Permission to use new API endpoints introduced with library updates
  - Change testing to be testcontainer driven rather than github actions driven. (ie. go test -v ./... is all you need)
  - Adding both an API and general debug flag
  - Removing the enterprise configuration flag, instead programmatically determining state
  - Migrated away from folder ID to folderUID for dashboard CRUD
  • Loading branch information
safaci2000 committed Sep 3, 2024
1 parent 92f9eb2 commit e2a20c5
Show file tree
Hide file tree
Showing 61 changed files with 1,803 additions and 965 deletions.
37 changes: 6 additions & 31 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,18 @@ on:

jobs:
test:
strategy:
matrix:
go: [ {version: 1.23.0, token: 1}, {version: 1.23.0, token: 0}]
grafana: [ 10.1.4 ]

env:
GRAFANA_INTEGRATION: 1
TEST_TOKEN_CONFIG: "${{ matrix.go.token }}"

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go.version }}
go-version: "1.23.0"

- name: Verify go version
run: go version
- uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Validate ENV Value
shell: bash
run: |
echo "token IS $TEST_TOKEN_CONFIG"
- name: Calc coverage
if: "${{ matrix.go.version == '1.23.0' && matrix.grafana == '10.1.4' && matrix.go.token == '0' }}"
run: |
go test -v -covermode=atomic -coverprofile=coverage.out ./...
- name: Convert coverage.out to coverage.lcov
if: "${{ matrix.go.version == '1.23.0' && matrix.grafana == '10.1.4' && matrix.go.token == '0' }}"
uses: jandelgado/[email protected]
- name: Test
if: "${{ matrix.go.token == '1' }}"
- name: Test
env:
ENTERPRISE_LICENSE: ${{ secrets.ENTERPRISE_LICENSE }}
run: go test -v ./...

2 changes: 0 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,13 @@ tasks:
- go test -v -coverpkg=./... -covermode=atomic -coverprofile=coverage.out ./...
- go tool cover -html=coverage.out
env:
GRAFANA_INTEGRATION: "1"
TEST_TOKEN_CONFIG: "0"
test_tokens:
desc: "test Token Based Only"
cmds:
- go test -v -coverpkg=./... -covermode=atomic -coverprofile=coverage.out ./...
- go tool cover -html=coverage.out
env:
GRAFANA_INTEGRATION: "1"
TEST_TOKEN_CONFIG: "1"

vuln_check:
Expand Down
27 changes: 19 additions & 8 deletions cli/backup/connection_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,28 @@ func newConnectionsPermissionListCmd() simplecobra.Commander {
slog.Info("Listing Connection Permissions for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"id", "uid", "name", "slug", "type", "default", "url"})
connections := rootCmd.GrafanaSvc().ListConnectionPermissions(filters)
_ = connections

if len(connections) == 0 {
slog.Info("No connections found")
} else {
for link, perms := range connections {
url := fmt.Sprintf("%s/datasource/edit/%d", config.Config().GetDefaultGrafanaConfig().URL, link.ID)
rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, link.Name, service.GetSlug(link.Name), link.Type, link.IsDefault, url})
if perms != nil && perms.Enabled {
for _, perm := range perms.Permissions {
rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, " PERMISSION-->", perm.PermissionName, perm.Team, perm.UserEmail})
for _, item := range connections {
rootCmd.TableObj.AppendRow(table.Row{item.Connection.ID, item.Connection.UID, item.Connection.Name, service.GetSlug(item.Connection.Name), item.Connection.Type, item.Connection.IsDefault, getConnectionURL(item.Connection.UID)})
if item.Permissions != nil {
for _, perm := range item.Permissions {
permissionType := "BuiltinRole"
value := ""
if perm.BuiltInRole != "" {
value = "builtInRole:" + perm.BuiltInRole
} else if perm.UserLogin != "" {
permissionType = "User"
value = "user:" + perm.UserLogin
} else if perm.Team != "" {
permissionType = "Team"
value = "team:" + perm.Team
} else {
permissionType = "unsupported"
}
rootCmd.TableObj.AppendRow(table.Row{item.Connection.ID, item.Connection.UID, " PERMISSION-->", perm.Permission, permissionType, value})
}
}
}
Expand Down Expand Up @@ -144,7 +155,7 @@ func newConnectionsPermissionUploadCmd() simplecobra.Commander {
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Uploading connections permissions")
rootCmd.TableObj.AppendHeader(table.Row{"connection permission"})
rootCmd.TableObj.AppendHeader(table.Row{"connection permission applied"})
connectionFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(connectionFilter)
connections := rootCmd.GrafanaSvc().UploadConnectionPermissions(filters)
Expand Down
8 changes: 6 additions & 2 deletions cli/backup/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,16 @@ func newListConnectionsCmd() simplecobra.Commander {
slog.Info("No connections found")
} else {
for _, link := range dsListing {
url := fmt.Sprintf("%s/datasource/edit/%d", config.Config().GetDefaultGrafanaConfig().URL, link.ID)
rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, link.Name, service.GetSlug(link.Name), link.Type, link.IsDefault, url})
rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, link.Name, service.GetSlug(link.Name), link.Type, link.IsDefault, getConnectionURL(link.UID)})
}
rootCmd.Render(cd.CobraCommand, dsListing)
}
return nil
},
}
}

func getConnectionURL(uid string) string {
url := config.Config().GetDefaultGrafanaConfig().URL
return fmt.Sprintf("%s/connections/datasources/edit/%s", url, uid)
}
3 changes: 2 additions & 1 deletion cli/test/conections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/mocks"
"github.com/esnet/gdg/pkg/test_tooling/common"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestConnectionCommand(t *testing.T) {
}
r, w, cleanup := InterceptStdout()

err := cli.Execute("testing.yml", []string{"backup", "connections", "list"}, optionMockSvc())
err := cli.Execute(common.DefaultTestConfig, []string{"backup", "connections", "list"}, optionMockSvc())
assert.Nil(t, err)
defer cleanup()
w.Close()
Expand Down
3 changes: 2 additions & 1 deletion cli/test/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
applog "github.com/esnet/gdg/internal/log"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/mocks"
"github.com/esnet/gdg/pkg/test_tooling/common"
"log/slog"

"github.com/stretchr/testify/assert"
Expand All @@ -29,7 +30,7 @@ func setupAndExecuteMockingServices(t *testing.T, process func(mock *mocks.Grafa
}

r, w, cleanup := InterceptStdout()
data, err := os.ReadFile("../../config/testing.yml")
data, err := os.ReadFile("../../config/" + common.DefaultTestConfig)
assert.Nil(t, err)

err = process(testSvc, data, optionMockSvc)
Expand Down
3 changes: 2 additions & 1 deletion cli/test/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/mocks"
"github.com/esnet/gdg/internal/version"
"github.com/esnet/gdg/pkg/test_tooling/common"
"github.com/stretchr/testify/assert"
"os"
"strings"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestVersionErrCommand(t *testing.T) {
}
path, _ := os.Getwd()
fmt.Println(path)
data, err := os.ReadFile("../../config/testing.yml")
data, err := os.ReadFile("../../config/" + common.DefaultTestConfig)
assert.Nil(t, err)

err = cli.Execute(string(data), []string{"dumb", "dumb"}, optionMockSvc())
Expand Down
1 change: 1 addition & 0 deletions config/importer-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ contexts:

global:
debug: true
api_debug: false
ignore_ssl_errors: false ##when set to true will ignore invalid SSL errors
retry_count: 3 ## Will retry any failed API request up to 3 times.
retry_delay: 5s ## will wait for specified duration before trying again.
Expand Down
6 changes: 5 additions & 1 deletion config/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ context_name: testing
contexts:
testing:
output_path: test/data
enterprise_support: true
connections:
credential_rules:
- rules:
Expand All @@ -18,6 +19,7 @@ contexts:
secure_data: "default.json"
url: https://localhost:3000
user_name: admin
organization_name: Main Org.
password: admin
ignore_filters: False # When set to true all Watched filtered folders will be ignored and ALL folders will be acted on
watched:
Expand Down Expand Up @@ -72,8 +74,10 @@ contexts:

global:
debug: true
api_debug: false
ignore_ssl_errors: false ##when set to true will ignore invalid SSL errors

retry_count: 1 ## Will retry any failed API request up to 3 times.
retry_delay: 1s ## will wait for specified duration before trying again.

storage_engine:
test:
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ services:
- MINIO_ROOT_USER=test
- MINIO_ROOT_PASSWORD=secretsss
grafana:
image: grafana/grafana:10.0.0-ubuntu
image: grafana/grafana-enterprise:11.1.4-ubuntu
ports:
- 3000:3000
environment:
- GF_INSTALL_PLUGINS=grafana-googlesheets-datasource
- GF_ENTERPRISE_LICENSE_TEXT=${ENTERPRISE_LICENSE}

influx:
image: influxdb:latest
Expand Down
Loading

0 comments on commit e2a20c5

Please sign in to comment.