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

Commit b041ab2

Browse files
author
Aaron Godin
authored
Merge pull request #167 from grafana/feat/resource-permissions-methods
RBAC assignment methods
2 parents 6d42666 + fdea1ff commit b041ab2

22 files changed

+448
-66
lines changed

.drone/drone.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local image = 'grafana/build-container:1.2.27';
1+
local image = 'grafana/build-container:1.7.7';
22

33
local pipeline(name, trigger) = {
44
kind: 'pipeline',

.drone/drone.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ platform:
99

1010
steps:
1111
- name: test
12-
image: grafana/build-container:1.2.27
12+
image: grafana/build-container:1.7.7
1313
commands:
1414
- make test
1515

@@ -28,7 +28,7 @@ platform:
2828

2929
steps:
3030
- name: test
31-
image: grafana/build-container:1.2.27
31+
image: grafana/build-container:1.7.7
3232
commands:
3333
- make test
3434

.golangci.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
[run]
22
timeout = "10m"
33

4-
[linters-settings.golint]
5-
min-confidence = 3
6-
74
[linters-settings.goconst]
85
min-len = 5
96
min-occurrences = 5
@@ -13,14 +10,13 @@ disable-all = true
1310
enable = [
1411
"bodyclose",
1512
"deadcode",
16-
"depguard",
1713
"dogsled",
1814
"errcheck",
1915
"gochecknoinits",
2016
"goconst",
2117
"gocritic",
2218
"goimports",
23-
"golint",
19+
"revive",
2420
"goprintffuncname",
2521
"gosec",
2622
"gosimple",
@@ -81,3 +77,4 @@ text = "404"
8177
[[issues.exclude-rules]]
8278
linters = ["misspell"]
8379
text = "Unknwon` is a misspelling of `Unknown"
80+

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ drone:
77

88
test:
99
go version
10-
golangci-lint --version
11-
golangci-lint run ./...
10+
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.1 run ./...
1211
go test -cover -race -vet all -mod readonly ./...
1312

api_key_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
"role": "Admin",
2323
"expiration": "2021-10-30T10:52:03+03:00"
2424
}
25-
]` //#nosec
25+
]` //#nosec
2626
)
2727

2828
func TestCreateAPIKey(t *testing.T) {

client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"log"
109
"net/http"
1110
"net/url"
@@ -112,7 +111,7 @@ func (c *Client) request(method, requestPath string, query url.Values, body []by
112111
}
113112

114113
// read the body (even on non-successful HTTP status codes), as that's what the unit tests expect
115-
bodyContents, err = ioutil.ReadAll(resp.Body)
114+
bodyContents, err = io.ReadAll(resp.Body)
116115
resp.Body.Close() //nolint:errcheck
117116

118117
// if there was an error reading the body, try again

client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"net/http/httptest"
1010
"net/url"
@@ -195,7 +195,7 @@ func TestClient_requestWithRetries(t *testing.T) {
195195

196196
try++
197197

198-
got, err := ioutil.ReadAll(r.Body)
198+
got, err := io.ReadAll(r.Body)
199199
if err != nil {
200200
t.Errorf("retry %d: unexpected error reading body: %v", try, err)
201201
}

cloud_plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package gapi
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
)
99

@@ -74,7 +74,7 @@ func (c *Client) IsCloudPluginInstalled(stackSlug string, pluginSlug string) (bo
7474
if resp.StatusCode == http.StatusNotFound {
7575
return false, nil
7676
}
77-
bodyContents, err := ioutil.ReadAll(resp.Body)
77+
bodyContents, err := io.ReadAll(resp.Body)
7878
if err != nil {
7979
return false, err
8080
}

dashboard_permissions.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,56 @@ func (c *Client) UpdateDashboardPermissionsByUID(uid string, items *PermissionIt
5858

5959
return c.request("POST", path, nil, data, nil)
6060
}
61+
62+
func (c *Client) ListDashboardResourcePermissions(uid string) ([]*ResourcePermission, error) {
63+
return c.listResourcePermissions(DashboardsResource, ResourceUID(uid))
64+
}
65+
66+
func (c *Client) SetDashboardResourcePermissions(uid string, body SetResourcePermissionsBody) (*SetResourcePermissionsResponse, error) {
67+
return c.setResourcePermissions(DashboardsResource, ResourceUID(uid), body)
68+
}
69+
70+
func (c *Client) SetUserDashboardResourcePermissions(dashboardUID string, userID int64, permission string) (*SetResourcePermissionsResponse, error) {
71+
return c.setResourcePermissionByAssignment(
72+
DashboardsResource,
73+
ResourceUID(dashboardUID),
74+
UsersResource,
75+
ResourceID(userID),
76+
SetResourcePermissionBody{
77+
Permission: SetResourcePermissionItem{
78+
UserID: userID,
79+
Permission: permission,
80+
},
81+
},
82+
)
83+
}
84+
85+
func (c *Client) SetTeamDashboardResourcePermissions(dashboardUID string, teamID int64, permission string) (*SetResourcePermissionsResponse, error) {
86+
return c.setResourcePermissionByAssignment(
87+
DashboardsResource,
88+
ResourceUID(dashboardUID),
89+
TeamsResource,
90+
ResourceID(teamID),
91+
SetResourcePermissionBody{
92+
Permission: SetResourcePermissionItem{
93+
TeamID: teamID,
94+
Permission: permission,
95+
},
96+
},
97+
)
98+
}
99+
100+
func (c *Client) SetBuiltInRoleDashboardResourcePermissions(dashboardUID string, builtInRole string, permission string) (*SetResourcePermissionsResponse, error) {
101+
return c.setResourcePermissionByAssignment(
102+
DashboardsResource,
103+
ResourceUID(dashboardUID),
104+
BuiltInRolesResource,
105+
ResourceUID(builtInRole),
106+
SetResourcePermissionBody{
107+
Permission: SetResourcePermissionItem{
108+
BuiltinRole: builtInRole,
109+
Permission: permission,
110+
},
111+
},
112+
)
113+
}

datasource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func JSONDataWithHeaders(jsonData, secureJSONData map[string]interface{}, header
173173
for name, value := range headers {
174174
jsonData[fmt.Sprintf("httpHeaderName%d", idx)] = name
175175
secureJSONData[fmt.Sprintf("httpHeaderValue%d", idx)] = value
176-
idx += 1
176+
idx++
177177
}
178178

179179
return jsonData, secureJSONData

0 commit comments

Comments
 (0)