Skip to content

Commit 031da75

Browse files
committed
fixup codersdk
1 parent 0315226 commit 031da75

File tree

5 files changed

+47
-31
lines changed

5 files changed

+47
-31
lines changed

coderd/database/dbmetrics/dbmetrics.go

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ func TestReadCustomRoles(t *testing.T) {
579579
orgID = uuid.NullUUID{}
580580
}
581581

582-
role, err := db.UpsertCustomRole(ctx, database.UpsertCustomRoleParams{
582+
role, err := db.InsertCustomRole(ctx, database.InsertCustomRoleParams{
583583
Name: fmt.Sprintf("role-%d", i),
584584
OrganizationID: orgID,
585585
})

codersdk/roles.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ type Role struct {
6161
UserPermissions []Permission `json:"user_permissions" table:"user_permissions"`
6262
}
6363

64-
// PatchRoleRequest is used to edit custom roles.
65-
type PatchRoleRequest struct {
64+
// CustomRoleRequest is used to edit custom roles.
65+
type CustomRoleRequest struct {
6666
Name string `json:"name" table:"name,default_sort" validate:"username"`
6767
DisplayName string `json:"display_name" table:"display_name"`
6868
SitePermissions []Permission `json:"site_permissions" table:"site_permissions"`
@@ -82,17 +82,40 @@ func (r Role) FullName() string {
8282
return r.Name + ":" + r.OrganizationID
8383
}
8484

85-
// PatchOrganizationRole will upsert a custom organization role
86-
func (c *Client) PatchOrganizationRole(ctx context.Context, role Role) (Role, error) {
87-
req := PatchRoleRequest{
85+
// CreateOrganizationRole will create a custom organization role
86+
func (c *Client) CreateOrganizationRole(ctx context.Context, role Role) (Role, error) {
87+
req := CustomRoleRequest{
8888
Name: role.Name,
8989
DisplayName: role.DisplayName,
9090
SitePermissions: role.SitePermissions,
9191
OrganizationPermissions: role.OrganizationPermissions,
9292
UserPermissions: role.UserPermissions,
9393
}
9494

95-
res, err := c.Request(ctx, http.MethodPatch,
95+
res, err := c.Request(ctx, http.MethodPost,
96+
fmt.Sprintf("/api/v2/organizations/%s/members/roles", role.OrganizationID), req)
97+
if err != nil {
98+
return Role{}, err
99+
}
100+
defer res.Body.Close()
101+
if res.StatusCode != http.StatusOK {
102+
return Role{}, ReadBodyAsError(res)
103+
}
104+
var r Role
105+
return r, json.NewDecoder(res.Body).Decode(&r)
106+
}
107+
108+
// UpdateOrganizationRole will update an existing custom organization role
109+
func (c *Client) UpdateOrganizationRole(ctx context.Context, role Role) (Role, error) {
110+
req := CustomRoleRequest{
111+
Name: role.Name,
112+
DisplayName: role.DisplayName,
113+
SitePermissions: role.SitePermissions,
114+
OrganizationPermissions: role.OrganizationPermissions,
115+
UserPermissions: role.UserPermissions,
116+
}
117+
118+
res, err := c.Request(ctx, http.MethodPut,
96119
fmt.Sprintf("/api/v2/organizations/%s/members/roles", role.OrganizationID), req)
97120
if err != nil {
98121
return Role{}, err

enterprise/coderd/roles.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// @Accept json
2727
// @Produce json
2828
// @Param organization path string true "Organization ID" format(uuid)
29-
// @Param request body codersdk.PatchRoleRequest true "Insert role request"
29+
// @Param request body codersdk.CustomRoleRequest true "Insert role request"
3030
// @Tags Members
3131
// @Success 200 {array} codersdk.Role
3232
// @Router /organizations/{organization}/members/roles [post]
@@ -46,7 +46,7 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) {
4646
)
4747
defer commitAudit()
4848

49-
var req codersdk.PatchRoleRequest
49+
var req codersdk.CustomRoleRequest
5050
if !httpapi.Read(ctx, rw, r, &req) {
5151
return
5252
}
@@ -90,7 +90,7 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) {
9090
// @Accept json
9191
// @Produce json
9292
// @Param organization path string true "Organization ID" format(uuid)
93-
// @Param request body codersdk.PatchRoleRequest true "Upsert role request"
93+
// @Param request body codersdk.CustomRoleRequest true "Upsert role request"
9494
// @Tags Members
9595
// @Success 200 {array} codersdk.Role
9696
// @Router /organizations/{organization}/members/roles [patch]
@@ -110,7 +110,7 @@ func (api *API) putOrgRoles(rw http.ResponseWriter, r *http.Request) {
110110
)
111111
defer commitAudit()
112112

113-
var req codersdk.PatchRoleRequest
113+
var req codersdk.CustomRoleRequest
114114
if !httpapi.Read(ctx, rw, r, &req) {
115115
return
116116
}
@@ -255,7 +255,7 @@ func sdkPermissionToDB(p codersdk.Permission) database.CustomRolePermission {
255255
}
256256
}
257257

258-
func validOrganizationRoleRequest(ctx context.Context, req codersdk.PatchRoleRequest, rw http.ResponseWriter) bool {
258+
func validOrganizationRoleRequest(ctx context.Context, req codersdk.CustomRoleRequest, rw http.ResponseWriter) bool {
259259
// This check is not ideal, but we cannot enforce a unique role name in the db against
260260
// the built-in role names.
261261
if rbac.ReservedRoleName(req.Name) {

enterprise/coderd/roles_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestCustomOrganizationRole(t *testing.T) {
5757
ctx := testutil.Context(t, testutil.WaitMedium)
5858

5959
//nolint:gocritic // owner is required for this
60-
role, err := owner.PatchOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
60+
role, err := owner.CreateOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
6161
require.NoError(t, err, "upsert role")
6262

6363
// Assign the custom template admin role
@@ -111,7 +111,7 @@ func TestCustomOrganizationRole(t *testing.T) {
111111
ctx := testutil.Context(t, testutil.WaitMedium)
112112

113113
//nolint:gocritic // owner is required for this
114-
role, err := owner.PatchOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
114+
role, err := owner.CreateOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
115115
require.NoError(t, err, "upsert role")
116116

117117
// Remove the license to block enterprise functionality
@@ -124,7 +124,7 @@ func TestCustomOrganizationRole(t *testing.T) {
124124
}
125125

126126
// Verify functionality is lost
127-
_, err = owner.PatchOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
127+
_, err = owner.UpdateOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
128128
require.ErrorContains(t, err, "Custom Roles is an Enterprise feature")
129129

130130
// Assign the custom template admin role
@@ -152,7 +152,7 @@ func TestCustomOrganizationRole(t *testing.T) {
152152

153153
ctx := testutil.Context(t, testutil.WaitMedium)
154154
//nolint:gocritic // owner is required for this
155-
role, err := owner.PatchOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
155+
role, err := owner.CreateOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
156156
require.NoError(t, err, "upsert role")
157157

158158
// Assign the custom template admin role
@@ -169,7 +169,7 @@ func TestCustomOrganizationRole(t *testing.T) {
169169
newRole.SitePermissions = nil
170170
newRole.OrganizationPermissions = nil
171171
newRole.UserPermissions = nil
172-
_, err = owner.PatchOrganizationRole(ctx, newRole)
172+
_, err = owner.UpdateOrganizationRole(ctx, newRole)
173173
require.NoError(t, err, "upsert role with override")
174174

175175
// The role should no longer have template perms
@@ -203,7 +203,7 @@ func TestCustomOrganizationRole(t *testing.T) {
203203
ctx := testutil.Context(t, testutil.WaitMedium)
204204

205205
//nolint:gocritic // owner is required for this
206-
_, err := owner.PatchOrganizationRole(ctx, codersdk.Role{
206+
_, err := owner.CreateOrganizationRole(ctx, codersdk.Role{
207207
Name: "Bad_Name", // No underscores allowed
208208
DisplayName: "Testing Purposes",
209209
OrganizationID: first.OrganizationID.String(),
@@ -232,7 +232,7 @@ func TestCustomOrganizationRole(t *testing.T) {
232232
ctx := testutil.Context(t, testutil.WaitMedium)
233233

234234
//nolint:gocritic // owner is required for this
235-
_, err := owner.PatchOrganizationRole(ctx, codersdk.Role{
235+
_, err := owner.CreateOrganizationRole(ctx, codersdk.Role{
236236
Name: "owner", // Reserved
237237
DisplayName: "Testing Purposes",
238238
OrganizationID: first.OrganizationID.String(),
@@ -270,7 +270,7 @@ func TestCustomOrganizationRole(t *testing.T) {
270270
}
271271

272272
//nolint:gocritic // owner is required for this
273-
_, err := owner.PatchOrganizationRole(ctx, siteRole)
273+
_, err := owner.CreateOrganizationRole(ctx, siteRole)
274274
require.ErrorContains(t, err, "site wide permissions")
275275

276276
userRole := templateAdminCustom(first.OrganizationID)
@@ -282,7 +282,7 @@ func TestCustomOrganizationRole(t *testing.T) {
282282
}
283283

284284
//nolint:gocritic // owner is required for this
285-
_, err = owner.PatchOrganizationRole(ctx, userRole)
285+
_, err = owner.UpdateOrganizationRole(ctx, userRole)
286286
require.ErrorContains(t, err, "not allowed to assign user permissions")
287287
})
288288

@@ -307,7 +307,7 @@ func TestCustomOrganizationRole(t *testing.T) {
307307
newRole.OrganizationID = "0000" // This is not a valid uuid
308308

309309
//nolint:gocritic // owner is required for this
310-
_, err := owner.PatchOrganizationRole(ctx, newRole)
310+
_, err := owner.CreateOrganizationRole(ctx, newRole)
311311
require.ErrorContains(t, err, "Resource not found")
312312
})
313313

@@ -329,7 +329,7 @@ func TestCustomOrganizationRole(t *testing.T) {
329329
orgAdmin, orgAdminUser := coderdtest.CreateAnotherUser(t, owner, first.OrganizationID, rbac.ScopedRoleOrgAdmin(first.OrganizationID))
330330
ctx := testutil.Context(t, testutil.WaitMedium)
331331

332-
createdRole, err := orgAdmin.PatchOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
332+
createdRole, err := orgAdmin.CreateOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
333333
require.NoError(t, err, "upsert role")
334334

335335
//nolint:gocritic // org_admin cannot assign to themselves
@@ -389,7 +389,7 @@ func TestCustomOrganizationRole(t *testing.T) {
389389
orgAdmin, orgAdminUser := coderdtest.CreateAnotherUser(t, owner, first.OrganizationID, rbac.ScopedRoleOrgAdmin(first.OrganizationID))
390390
ctx := testutil.Context(t, testutil.WaitMedium)
391391

392-
createdRole, err := orgAdmin.PatchOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
392+
createdRole, err := orgAdmin.CreateOrganizationRole(ctx, templateAdminCustom(first.OrganizationID))
393393
require.NoError(t, err, "upsert role")
394394

395395
customRoleIdentifier := rbac.RoleIdentifier{

0 commit comments

Comments
 (0)