Skip to content

chore: remove UpsertCustomRole in favor of Insert + Update #14217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup codersdk
  • Loading branch information
Emyrk committed Aug 13, 2024
commit e6282d0ea135de5d53510dc6eb2023b4c1a4903e
7 changes: 0 additions & 7 deletions coderd/database/dbmetrics/dbmetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion coderd/database/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func TestReadCustomRoles(t *testing.T) {
orgID = uuid.NullUUID{}
}

role, err := db.UpsertCustomRole(ctx, database.UpsertCustomRoleParams{
role, err := db.InsertCustomRole(ctx, database.InsertCustomRoleParams{
Name: fmt.Sprintf("role-%d", i),
OrganizationID: orgID,
})
Expand Down
35 changes: 29 additions & 6 deletions codersdk/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type Role struct {
UserPermissions []Permission `json:"user_permissions" table:"user_permissions"`
}

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

// PatchOrganizationRole will upsert a custom organization role
func (c *Client) PatchOrganizationRole(ctx context.Context, role Role) (Role, error) {
req := PatchRoleRequest{
// CreateOrganizationRole will create a custom organization role
func (c *Client) CreateOrganizationRole(ctx context.Context, role Role) (Role, error) {
req := CustomRoleRequest{
Name: role.Name,
DisplayName: role.DisplayName,
SitePermissions: role.SitePermissions,
OrganizationPermissions: role.OrganizationPermissions,
UserPermissions: role.UserPermissions,
}

res, err := c.Request(ctx, http.MethodPatch,
res, err := c.Request(ctx, http.MethodPost,
fmt.Sprintf("/api/v2/organizations/%s/members/roles", role.OrganizationID), req)
if err != nil {
return Role{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return Role{}, ReadBodyAsError(res)
}
var r Role
return r, json.NewDecoder(res.Body).Decode(&r)
}

// UpdateOrganizationRole will update an existing custom organization role
func (c *Client) UpdateOrganizationRole(ctx context.Context, role Role) (Role, error) {
req := CustomRoleRequest{
Name: role.Name,
DisplayName: role.DisplayName,
SitePermissions: role.SitePermissions,
OrganizationPermissions: role.OrganizationPermissions,
UserPermissions: role.UserPermissions,
}

res, err := c.Request(ctx, http.MethodPut,
fmt.Sprintf("/api/v2/organizations/%s/members/roles", role.OrganizationID), req)
if err != nil {
return Role{}, err
Expand Down
10 changes: 5 additions & 5 deletions enterprise/coderd/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// @Accept json
// @Produce json
// @Param organization path string true "Organization ID" format(uuid)
// @Param request body codersdk.PatchRoleRequest true "Insert role request"
// @Param request body codersdk.CustomRoleRequest true "Insert role request"
// @Tags Members
// @Success 200 {array} codersdk.Role
// @Router /organizations/{organization}/members/roles [post]
Expand All @@ -46,7 +46,7 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) {
)
defer commitAudit()

var req codersdk.PatchRoleRequest
var req codersdk.CustomRoleRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) {
// @Accept json
// @Produce json
// @Param organization path string true "Organization ID" format(uuid)
// @Param request body codersdk.PatchRoleRequest true "Upsert role request"
// @Param request body codersdk.CustomRoleRequest true "Upsert role request"
// @Tags Members
// @Success 200 {array} codersdk.Role
// @Router /organizations/{organization}/members/roles [patch]
Expand All @@ -110,7 +110,7 @@ func (api *API) putOrgRoles(rw http.ResponseWriter, r *http.Request) {
)
defer commitAudit()

var req codersdk.PatchRoleRequest
var req codersdk.CustomRoleRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func sdkPermissionToDB(p codersdk.Permission) database.CustomRolePermission {
}
}

func validOrganizationRoleRequest(ctx context.Context, req codersdk.PatchRoleRequest, rw http.ResponseWriter) bool {
func validOrganizationRoleRequest(ctx context.Context, req codersdk.CustomRoleRequest, rw http.ResponseWriter) bool {
// This check is not ideal, but we cannot enforce a unique role name in the db against
// the built-in role names.
if rbac.ReservedRoleName(req.Name) {
Expand Down
24 changes: 12 additions & 12 deletions enterprise/coderd/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestCustomOrganizationRole(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

customRoleIdentifier := rbac.RoleIdentifier{
Expand Down