refactor: Return the display_name and name in the roles endpoint (#1328)

This commit is contained in:
Bruno Quaresma
2022-05-06 14:18:00 -05:00
committed by GitHub
parent 97ee5600c7
commit 00806580f5
6 changed files with 71 additions and 35 deletions

View File

@ -6,12 +6,18 @@ import (
"fmt"
"net/http"
"github.com/coder/coder/coderd/rbac"
"github.com/google/uuid"
)
type Role struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
}
// ListSiteRoles lists all available site wide roles.
// This is not user specific.
func (c *Client) ListSiteRoles(ctx context.Context) ([]string, error) {
func (c *Client) ListSiteRoles(ctx context.Context) ([]Role, error) {
res, err := c.request(ctx, http.MethodGet, "/api/v2/users/roles", nil)
if err != nil {
return nil, err
@ -20,13 +26,13 @@ func (c *Client) ListSiteRoles(ctx context.Context) ([]string, error) {
if res.StatusCode != http.StatusOK {
return nil, readBodyAsError(res)
}
var roles []string
var roles []Role
return roles, json.NewDecoder(res.Body).Decode(&roles)
}
// ListOrganizationRoles lists all available roles for a given organization.
// This is not user specific.
func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]string, error) {
func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]Role, error) {
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s/members/roles/", org.String()), nil)
if err != nil {
return nil, err
@ -35,6 +41,17 @@ func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]st
if res.StatusCode != http.StatusOK {
return nil, readBodyAsError(res)
}
var roles []string
var roles []Role
return roles, json.NewDecoder(res.Body).Decode(&roles)
}
func ConvertRoles(roles []rbac.Role) []Role {
converted := make([]Role, 0, len(roles))
for _, role := range roles {
converted = append(converted, Role{
DisplayName: role.DisplayName,
Name: role.Name,
})
}
return converted
}