Skip to content

Commit 07d5308

Browse files
authored
Add support for RBAC API get role endpoint (#81)
1 parent 8769a76 commit 07d5308

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

cmd/test/main.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package main
33
import (
44
"crypto/tls"
55
"fmt"
6+
"net/http"
67
"os"
8+
"strconv"
9+
"strings"
710
"time"
811

912
"github.com/davecgh/go-spew/spew"
@@ -135,6 +138,10 @@ Get the RBAC token: go run cmd/test/main.go <pe-server> <login> <password> e.g.
135138

136139
fmt.Printf("Connecting to: %s\n\n", peServer)
137140

141+
fmt.Printf("* Testing RBAC API client function CreateRole...\n\n")
142+
143+
var createdRoleIDString string
144+
138145
// Try creating the same role (same display name) multiple times,
139146
// the second attempt should return a HTTP 409 status.
140147
roleDisplayName := fmt.Sprintf("Testing %d", time.Now().UnixNano())
@@ -152,7 +159,7 @@ Get the RBAC token: go run cmd/test/main.go <pe-server> <login> <password> e.g.
152159
}, token)
153160
if err != nil {
154161
if apiErr, ok := err.(*rbac.APIError); ok {
155-
if apiErr.GetStatusCode() == 409 {
162+
if apiErr.GetStatusCode() == http.StatusConflict {
156163
fmt.Printf("Create role \"%s\" failed as expected because role already exists\n",
157164
roleDisplayName)
158165
} else {
@@ -165,14 +172,36 @@ Get the RBAC token: go run cmd/test/main.go <pe-server> <login> <password> e.g.
165172
}
166173
panic(err)
167174
}
175+
createdRoleIDString = location[strings.LastIndex(location, "/")+1:]
176+
168177
fmt.Printf("Create role \"%s\" was successful, location: %s\n",
169178
roleDisplayName,
170179
location)
171180
}
172181
fmt.Println()
173182

183+
fmt.Printf("* Testing RBAC API client function GetRole...\n\n")
184+
185+
var role *rbac.Role
186+
187+
createdRoleID, _ := strconv.Atoi(createdRoleIDString)
188+
role, err := rbacClient.GetRole(uint(createdRoleID), token)
189+
if err != nil {
190+
panic(err)
191+
}
192+
193+
if role.DisplayName == roleDisplayName {
194+
fmt.Printf("Get role was successful, as the role \"%s\" was returned in response\n\n",
195+
roleDisplayName)
196+
} else {
197+
panic(fmt.Sprintf("Expected the role \"%s\" to be returned in response from get role, but it was not",
198+
roleDisplayName))
199+
}
200+
201+
fmt.Printf("* Testing RBAC API client function GetRoles...\n\n")
202+
174203
var roles []rbac.Role
175-
roles, err := rbacClient.GetRoles(token)
204+
roles, err = rbacClient.GetRoles(token)
176205
if err != nil {
177206
panic(err)
178207
}

pkg/rbac/roles.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package rbac
22

3+
import "strconv"
4+
35
const (
6+
rolePath = "/rbac-api/v1/roles/{id}"
47
rolesPath = "/rbac-api/v1/roles"
58
)
69

@@ -19,6 +22,22 @@ func (c *Client) GetRoles(token string) ([]Role, error) {
1922
return roles, nil
2023
}
2124

25+
// GetRole fetches information about a single role, identified by its ID.
26+
func (c *Client) GetRole(id uint, token string) (*Role, error) {
27+
var role Role
28+
29+
response, err := c.resty.R().
30+
SetHeader("X-Authentication", token).
31+
SetPathParams(map[string]string{"id": strconv.FormatUint(uint64(id), 10)}).
32+
SetResult(&role).
33+
Get(rolePath)
34+
if err != nil {
35+
return nil, FormatError(response, err.Error())
36+
}
37+
38+
return &role, nil
39+
}
40+
2241
// CreateRole creates a role, and attaches to it the specified permissions and
2342
// the specified users and groups. Authentication is required.
2443
//

pkg/rbac/roles_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import (
44
"encoding/json"
55
"net/http"
66
"os"
7+
"strconv"
8+
"strings"
79
"testing"
810

911
"github.com/stretchr/testify/require"
1012
)
1113

1214
const (
15+
getRoleResponseFilePath = "testdata/apidocs/GetRole-response.json"
1316
getRolesResponseFilePath = "testdata/apidocs/GetRoles-response.json"
1417
token = "dummy-token"
1518
)
@@ -30,6 +33,23 @@ func TestGetRoles(t *testing.T) {
3033
require.Equal(t, expectedRoles, actualRoles)
3134
}
3235

36+
func TestGetRole(t *testing.T) {
37+
var expectedRole *Role
38+
39+
expectedRoleJSONFile, err := os.Open(getRoleResponseFilePath)
40+
require.Nil(t, err, "failed to open expected role JSON file")
41+
42+
err = json.NewDecoder(expectedRoleJSONFile).Decode(&expectedRole)
43+
require.Nil(t, err, "error decoding expected role")
44+
45+
rolePathWithID := strings.ReplaceAll(rolePath, "{id}", strconv.Itoa(int(expectedRole.ID)))
46+
setUpOKResponder(t, http.MethodGet, rolePathWithID, getRoleResponseFilePath)
47+
48+
actualRole, err := rbacClient.GetRole(expectedRole.ID, token)
49+
require.Nil(t, err)
50+
require.Equal(t, expectedRole, actualRole)
51+
}
52+
3353
func TestCreateRole(t *testing.T) {
3454
role := &Role{
3555
DisplayName: "Testing",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"description": "Role used in go-pe-client get role test",
3+
"display_name": "Test Role",
4+
"id": 1,
5+
"group_ids": [],
6+
"user_ids": [
7+
"af94921f-bd76-4b58-b5ce-e17c029a2790",
8+
"42bf351c-f9ec-40af-84ad-e976fec7f4bd"
9+
],
10+
"permissions": [
11+
{
12+
"object_type": "node_groups",
13+
"action": "view",
14+
"instance": "*"
15+
}
16+
]
17+
}

0 commit comments

Comments
 (0)