Skip to main content

Update a role with the Go SDK

Permit.Api.Roles.Update() changes the name, description, permissions, or extended roles of an existing role in the environment that your API key belongs to. This reference is for Go developers who manage role-based access control (RBAC) roles from backend code.

Roles.Update signature

func (r *Roles) Update(ctx context.Context, roleKey string, roleUpdate models.RoleUpdate) (*models.RoleRead, error)

Roles.Update parameters

ParameterTypeRequiredDescription
ctxcontext.ContextYesThe context of the request.
roleKeystringYesThe key of the role to update. The role ID also works.
roleUpdatemodels.RoleUpdateYesThe fields to change. Build it with models.NewRoleUpdate().

RoleUpdate fields

Roles.Update is a partial update. The Permit API changes only the fields you set, and each field you set replaces the stored value completely. For example, setting Permissions replaces the role's whole permission list, so include every permission the role keeps.

FieldTypeRequiredDescription
Name*stringNoThe name of the role. Set it with SetName().
Description*stringNoWhat the role represents, or which permissions it grants. Set it with SetDescription().
Permissions[]stringNoThe permissions the role grants, each in the format <resource-key>:<action-key>, such as document:read. Set them with SetPermissions().
Extends[]stringNoThe keys of roles that this role extends. The role inherits all the permissions of those roles. Set them with SetExtends().
Attributesmap[string]stringNoKey-value metadata about the role. Set it with SetAttributes().

Example: change a role's permissions with Roles.Update

The example uses a client named Permit, created with permit.NewPermit() as shown in Check permissions with the Go SDK, and a ctx of type context.Context.

Build the RoleUpdate struct with the full list of permissions the role grants after the update:

roleUpdate := models.NewRoleUpdate()
roleUpdate.SetPermissions([]string{"resource-key:read", "resource-key:write"})

Pass the key of the role and the struct to Roles.Update:

role, err := Permit.Api.Roles.Update(ctx, "role-key", *roleUpdate)

Roles.Update return value and errors

On success, Roles.Update returns a *models.RoleRead with the updated role.

If the call fails, err holds an errors.PermitError from the github.com/permitio/permit-golang/pkg/errors package. Its StatusCode field has the HTTP status, and its ErrorCode field has one of these codes:

ErrorCodeCause
NotFoundHTTP 404: no role with that key or ID exists in the environment.
UnprocessableEntityErrorHTTP 422: a field failed validation.
Unauthorized, ForbiddenAccessHTTP 401 or 403: the API key is invalid or has no access to the environment.
UnexpectedErrorA server error (HTTP 5xx) or a network error.