Skip to content

Commit 87c474b

Browse files
committed
Move convertRole(s) to coderd
1 parent b1bb55f commit 87c474b

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

coderd/roles.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (*api) assignableSiteRoles(rw http.ResponseWriter, _ *http.Request) {
1515
// TODO: @emyrk in the future, allow granular subsets of roles to be returned based on the
1616
// role of the user.
1717
roles := rbac.SiteRoles()
18-
httpapi.Write(rw, http.StatusOK, codersdk.ConvertRoles(roles))
18+
httpapi.Write(rw, http.StatusOK, ConvertRoles(roles))
1919
}
2020

2121
// assignableSiteRoles returns all site wide roles that can be assigned.
@@ -24,5 +24,20 @@ func (*api) assignableOrgRoles(rw http.ResponseWriter, r *http.Request) {
2424
// role of the user.
2525
organization := httpmw.OrganizationParam(r)
2626
roles := rbac.OrganizationRoles(organization.ID)
27-
httpapi.Write(rw, http.StatusOK, codersdk.ConvertRoles(roles))
27+
httpapi.Write(rw, http.StatusOK, ConvertRoles(roles))
28+
}
29+
30+
func ConvertRole(role rbac.Role) codersdk.Role {
31+
return codersdk.Role{
32+
DisplayName: role.DisplayName,
33+
Name: role.Name,
34+
}
35+
}
36+
37+
func ConvertRoles(roles []rbac.Role) []codersdk.Role {
38+
converted := make([]codersdk.Role, 0, len(roles))
39+
for _, role := range roles {
40+
converted = append(converted, ConvertRole(role))
41+
}
42+
return converted
2843
}

coderd/roles_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/stretchr/testify/require"
99

10+
"github.com/coder/coder/coderd"
1011
"github.com/coder/coder/coderd/coderdtest"
1112
"github.com/coder/coder/coderd/rbac"
1213
"github.com/coder/coder/codersdk"
@@ -84,7 +85,7 @@ func TestListRoles(t *testing.T) {
8485
APICall: func() ([]codersdk.Role, error) {
8586
return orgAdmin.ListOrganizationRoles(ctx, admin.OrganizationID)
8687
},
87-
ExpectedRoles: codersdk.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
88+
ExpectedRoles: coderd.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
8889
},
8990
{
9091
Name: "OrgAdminListOtherOrg",
@@ -99,14 +100,14 @@ func TestListRoles(t *testing.T) {
99100
APICall: func() ([]codersdk.Role, error) {
100101
return client.ListSiteRoles(ctx)
101102
},
102-
ExpectedRoles: codersdk.ConvertRoles(rbac.SiteRoles()),
103+
ExpectedRoles: coderd.ConvertRoles(rbac.SiteRoles()),
103104
},
104105
{
105106
Name: "AdminListOrg",
106107
APICall: func() ([]codersdk.Role, error) {
107108
return client.ListOrganizationRoles(ctx, admin.OrganizationID)
108109
},
109-
ExpectedRoles: codersdk.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
110+
ExpectedRoles: coderd.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
110111
},
111112
}
112113

codersdk/roles.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net/http"
88

9-
"github.com/coder/coder/coderd/rbac"
109
"github.com/google/uuid"
1110
)
1211

@@ -44,18 +43,3 @@ func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]Ro
4443
var roles []Role
4544
return roles, json.NewDecoder(res.Body).Decode(&roles)
4645
}
47-
48-
func ConvertRole(role rbac.Role) Role {
49-
return Role{
50-
DisplayName: role.DisplayName,
51-
Name: role.Name,
52-
}
53-
}
54-
55-
func ConvertRoles(roles []rbac.Role) []Role {
56-
converted := make([]Role, 0, len(roles))
57-
for _, role := range roles {
58-
converted = append(converted, ConvertRole(role))
59-
}
60-
return converted
61-
}

0 commit comments

Comments
 (0)