Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #151 from skatsaounis/add-get-roles
Browse files Browse the repository at this point in the history
Add GetRoles call to fetch all Grafana roles
  • Loading branch information
inkel authored May 31, 2023
2 parents e5fb6a3 + 2081248 commit 655166f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 7 deletions.
27 changes: 27 additions & 0 deletions role.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gapi
import (
"encoding/json"
"fmt"
"net/url"
)

type Role struct {
Expand All @@ -22,6 +23,32 @@ type Permission struct {
Scope string `json:"scope"`
}

// GetRole fetches and returns Grafana roles. Available only in Grafana Enterprise 8.+.
func (c *Client) GetRoles() ([]Role, error) {
const limit = 1000
var (
page = 0
newRoles []Role
roles []Role
query = make(url.Values)
)
query.Set("limit", fmt.Sprint(limit))
for {
page++
query.Set("page", fmt.Sprint(page))

if err := c.request("GET", "/api/access-control/roles", query, nil, &newRoles); err != nil {
return nil, err
}

roles = append(roles, newRoles...)

if len(newRoles) < limit {
return roles, nil
}
}
}

// GetRole gets a role with permissions for the given UID. Available only in Grafana Enterprise 8.+.
func (c *Client) GetRole(uid string) (*Role, error) {
r := &Role{}
Expand Down
90 changes: 83 additions & 7 deletions role_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gapi

import (
"strings"
"testing"

"github.com/gobs/pretty"
Expand Down Expand Up @@ -55,8 +56,86 @@ const (

updatedRoleResponse = `{"message":"Role updated"}`
deleteRoleResponse = `{"message":"Role deleted"}`

roleUID = "vc3SCSsGz"
)

func TestRoles(t *testing.T) {
mockData := strings.Repeat(getRoleResponse+",", 1000) // make 1000 roles.
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.

// This creates 1000 + 1000 + 1 (2001, 3 calls) worth of roles.
client := gapiTestToolsFromCalls(t, []mockServerCall{
{200, mockData},
{200, mockData},
{200, "[" + getRoleResponse + "]"},
})

const dashCount = 2001

roles, err := client.GetRoles()
if err != nil {
t.Fatal(err)
}

t.Log(pretty.PrettyFormat(roles))

if len(roles) != dashCount {
t.Fatalf("Length of returned roles should be %d", dashCount)
}
if roles[0].UID != roleUID || roles[0].Name != "test:policy" {
t.Error("Not correctly parsing returned roles.")
}
if roles[dashCount-1].UID != roleUID || roles[dashCount-1].Name != "test:policy" {
t.Error("Not correctly parsing returned roles.")
}
}

func TestRolesZeroResults(t *testing.T) {
// This return zero roles.
client := gapiTestToolsFromCalls(t, []mockServerCall{
{200, "[]"},
})

roles, err := client.GetRoles()
if err != nil {
t.Fatal(err)
}

if len(roles) != 0 {
t.Errorf("Length of returned roles should be zero")
}
}

func TestRolesSinglePage(t *testing.T) {
mockData := strings.Repeat(getRoleResponse+",", 999) // make 999 roles.
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.

// This creates 999 worth of roles.
client := gapiTestToolsFromCalls(t, []mockServerCall{
{200, mockData},
})

const dashCount = 999

roles, err := client.GetRoles()
if err != nil {
t.Fatal(err)
}

t.Log(pretty.PrettyFormat(roles))

if len(roles) != dashCount {
t.Fatalf("Length of returned roles should be %d", dashCount)
}
if roles[0].UID != roleUID || roles[0].Name != "test:policy" {
t.Error("Not correctly parsing returned roles.")
}
if roles[dashCount-1].UID != roleUID || roles[dashCount-1].Name != "test:policy" {
t.Error("Not correctly parsing returned roles.")
}
}

func TestNewRole(t *testing.T) {
client := gapiTestTools(t, 201, newRoleResponse)

Expand All @@ -79,26 +158,23 @@ func TestNewRole(t *testing.T) {

t.Log(pretty.PrettyFormat(resp))

if resp.UID != "vc3SCSsGz" {
if resp.UID != roleUID {
t.Error("Not correctly parsing returned role uid.")
}
}

func TestGetRole(t *testing.T) {
client := gapiTestTools(t, 200, getRoleResponse)

uid := "vc3SCSsGz"

resp, err := client.GetRole(uid)

resp, err := client.GetRole(roleUID)
if err != nil {
t.Error(err)
}

expected := Role{
Global: false,
Version: 1,
UID: "vc3SCSsGz",
UID: roleUID,
Name: "test:policy",
Description: "Test policy description",
Group: "test group",
Expand Down Expand Up @@ -143,7 +219,7 @@ func TestUpdateRole(t *testing.T) {
func TestDeleteRole(t *testing.T) {
client := gapiTestTools(t, 200, deleteRoleResponse)

err := client.DeleteRole("vc3SCSsGz", false)
err := client.DeleteRole(roleUID, false)
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 655166f

Please sign in to comment.