Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"

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

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

fmt.Printf("* Testing RBAC API client function CreateRole...\n\n")

var createdRoleIDString string

// Try creating the same role (same display name) multiple times,
// the second attempt should return a HTTP 409 status.
roleDisplayName := fmt.Sprintf("Testing %d", time.Now().UnixNano())
Expand All @@ -152,7 +159,7 @@ Get the RBAC token: go run cmd/test/main.go <pe-server> <login> <password> e.g.
}, token)
if err != nil {
if apiErr, ok := err.(*rbac.APIError); ok {
if apiErr.GetStatusCode() == 409 {
if apiErr.GetStatusCode() == http.StatusConflict {
fmt.Printf("Create role \"%s\" failed as expected because role already exists\n",
roleDisplayName)
} else {
Expand All @@ -165,14 +172,36 @@ Get the RBAC token: go run cmd/test/main.go <pe-server> <login> <password> e.g.
}
panic(err)
}
createdRoleIDString = location[strings.LastIndex(location, "/")+1:]

fmt.Printf("Create role \"%s\" was successful, location: %s\n",
roleDisplayName,
location)
}
fmt.Println()

fmt.Printf("* Testing RBAC API client function GetRole...\n\n")

var role *rbac.Role

createdRoleID, _ := strconv.Atoi(createdRoleIDString)
role, err := rbacClient.GetRole(uint(createdRoleID), token)
if err != nil {
panic(err)
}

if role.DisplayName == roleDisplayName {
fmt.Printf("Get role was successful, as the role \"%s\" was returned in response\n\n",
roleDisplayName)
} else {
panic(fmt.Sprintf("Expected the role \"%s\" to be returned in response from get role, but it was not",
roleDisplayName))
}

fmt.Printf("* Testing RBAC API client function GetRoles...\n\n")

var roles []rbac.Role
roles, err := rbacClient.GetRoles(token)
roles, err = rbacClient.GetRoles(token)
if err != nil {
panic(err)
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/rbac/roles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package rbac

import "strconv"

const (
rolePath = "/rbac-api/v1/roles/{id}"
rolesPath = "/rbac-api/v1/roles"
)

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

// GetRole fetches information about a single role, identified by its ID.
func (c *Client) GetRole(id uint, token string) (*Role, error) {
var role Role

response, err := c.resty.R().
SetHeader("X-Authentication", token).
SetPathParams(map[string]string{"id": strconv.FormatUint(uint64(id), 10)}).
SetResult(&role).
Get(rolePath)
if err != nil {
return nil, FormatError(response, err.Error())
}

return &role, nil
}

// CreateRole creates a role, and attaches to it the specified permissions and
// the specified users and groups. Authentication is required.
//
Expand Down
20 changes: 20 additions & 0 deletions pkg/rbac/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"encoding/json"
"net/http"
"os"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

const (
getRoleResponseFilePath = "testdata/apidocs/GetRole-response.json"
getRolesResponseFilePath = "testdata/apidocs/GetRoles-response.json"
token = "dummy-token"
)
Expand All @@ -30,6 +33,23 @@ func TestGetRoles(t *testing.T) {
require.Equal(t, expectedRoles, actualRoles)
}

func TestGetRole(t *testing.T) {
var expectedRole *Role

expectedRoleJSONFile, err := os.Open(getRoleResponseFilePath)
require.Nil(t, err, "failed to open expected role JSON file")

err = json.NewDecoder(expectedRoleJSONFile).Decode(&expectedRole)
require.Nil(t, err, "error decoding expected role")

rolePathWithID := strings.ReplaceAll(rolePath, "{id}", strconv.Itoa(int(expectedRole.ID)))
setUpOKResponder(t, http.MethodGet, rolePathWithID, getRoleResponseFilePath)

actualRole, err := rbacClient.GetRole(expectedRole.ID, token)
require.Nil(t, err)
require.Equal(t, expectedRole, actualRole)
}

func TestCreateRole(t *testing.T) {
role := &Role{
DisplayName: "Testing",
Expand Down
17 changes: 17 additions & 0 deletions pkg/rbac/testdata/apidocs/GetRole-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"description": "Role used in go-pe-client get role test",
"display_name": "Test Role",
"id": 1,
"group_ids": [],
"user_ids": [
"af94921f-bd76-4b58-b5ce-e17c029a2790",
"42bf351c-f9ec-40af-84ad-e976fec7f4bd"
],
"permissions": [
{
"object_type": "node_groups",
"action": "view",
"instance": "*"
}
]
}