Skip to content

Commit 2ff95a7

Browse files
Added folder permission REST support (#198)
* Added folder permission REST support * Removed unneeded UpdateFolderPermissionRequest * Handle return value of defer to fix linting * Removed UpdateFolderPermissionRequest
1 parent 62ad1b3 commit 2ff95a7

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

folder-permissions.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package sdk
2+
3+
/*
4+
Copyright 2016 Alexander I.Grafov <[email protected]>
5+
Copyright 2016-2022 The Grafana SDK authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
ॐ तारे तुत्तारे तुरे स्व
20+
*/
21+
22+
type PermissionType uint
23+
24+
const PermissionView = PermissionType(1)
25+
const PermissionEdit = PermissionType(2)
26+
const PermissionAdmin = PermissionType(4)
27+
28+
type FolderPermission struct {
29+
Id uint `json:"id"`
30+
FolderId uint `json:"folderId"`
31+
Created string `json:"created"`
32+
Updated string `json:"updated"`
33+
UserId uint `json:"userId,omitempty"`
34+
UserLogin string `json:"userLogin,omitempty"`
35+
UserEmail string `json:"userEmail,omitempty"`
36+
TeamId uint `json:"teamId,omitempty"`
37+
Team string `json:"team,omitempty"`
38+
Role string `json:"role,omitempty"`
39+
Permission PermissionType `json:"permission"`
40+
PermissionName string `json:"permissionName"`
41+
Uid string `json:"uid,omitempty"`
42+
Title string `json:"title,omitempty"`
43+
Slug string `json:"slug,omitempty"`
44+
IsFolder bool `json:"isFolder"`
45+
Url string `json:"url,omitempty"`
46+
}

rest-folder-permissions.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package sdk
2+
3+
/*
4+
Copyright 2016 Alexander I.Grafov <[email protected]>
5+
Copyright 2016-2022 The Grafana SDK authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
ॐ तारे तुत्तारे तुरे स्व
20+
*/
21+
22+
import (
23+
"context"
24+
"encoding/json"
25+
"fmt"
26+
)
27+
28+
// https://grafana.com/docs/grafana/latest/http_api/folder_permissions/
29+
30+
// GetFolderPermissions gets permissions for a folder.
31+
// Reflects GET /api/folders/:uid/permissions API call.
32+
func (r *Client) GetFolderPermissions(ctx context.Context, folderUID string) ([]FolderPermission, error) {
33+
var (
34+
raw []byte
35+
fs []FolderPermission
36+
code int
37+
err error
38+
)
39+
if raw, code, err = r.get(ctx, fmt.Sprintf("api/folders/%s/permissions", folderUID), nil); err != nil {
40+
return nil, err
41+
}
42+
if code != 200 {
43+
return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw)
44+
}
45+
err = json.Unmarshal(raw, &fs)
46+
return fs, err
47+
}
48+
49+
// UpdateFolderPermissions update folders permission
50+
// Reflects PUT /api/folders/:uid/permissions API call.
51+
func (r *Client) UpdateFolderPermissions(ctx context.Context, folderUID string, up ...FolderPermission) (StatusMessage, error) {
52+
var (
53+
raw []byte
54+
rf StatusMessage
55+
code int
56+
err error
57+
)
58+
request := struct {
59+
Items []FolderPermission `json:"items"`
60+
}{
61+
Items: up,
62+
}
63+
if raw, err = json.Marshal(request); err != nil {
64+
return rf, err
65+
}
66+
if raw, code, err = r.post(ctx, fmt.Sprintf("api/folders/%s/permissions", folderUID), nil, raw); err != nil {
67+
return rf, err
68+
}
69+
if code != 200 {
70+
return rf, fmt.Errorf("HTTP error %d: returns %s", code, raw)
71+
}
72+
err = json.Unmarshal(raw, &rf)
73+
return rf, err
74+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package sdk_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/grafana-tools/sdk"
8+
)
9+
10+
func Test_FolderPermissions(t *testing.T) {
11+
shouldSkip(t)
12+
13+
client := getClient(t)
14+
ctx := context.Background()
15+
16+
var f = sdk.Folder{
17+
Title: "test-permissions-folder",
18+
}
19+
var err error
20+
21+
folder, err := client.CreateFolder(ctx, f)
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
defer func(client *sdk.Client, ctx context.Context, UID string) {
26+
_, _ = client.DeleteFolderByUID(ctx, UID)
27+
}(client, ctx, folder.UID)
28+
29+
permissions, err := client.GetFolderPermissions(ctx, folder.UID)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
defaultPermissionCount := len(permissions)
34+
if defaultPermissionCount == 0 {
35+
t.Fatalf("Expected default permission count to be zero but was %d", defaultPermissionCount)
36+
}
37+
38+
actualUser, err := client.GetActualUser(ctx)
39+
if err != nil {
40+
t.Fatalf("failed to get actual user: %s", err.Error())
41+
}
42+
43+
var updatePermissions []sdk.FolderPermission
44+
updatePermissions = append(updatePermissions, permissions...)
45+
updatePermissions = append(updatePermissions, sdk.FolderPermission{
46+
Permission: sdk.PermissionAdmin,
47+
UserId: actualUser.ID,
48+
})
49+
50+
_, err = client.UpdateFolderPermissions(ctx, folder.UID,
51+
updatePermissions...,
52+
)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
57+
updatedPermissions, err := client.GetFolderPermissions(ctx, folder.UID)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
updatedPermissionCount := len(updatedPermissions)
62+
if (defaultPermissionCount + 1) != updatedPermissionCount {
63+
t.Fatal("Expected permissions count to increase by 1")
64+
}
65+
}

0 commit comments

Comments
 (0)