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
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
roleKey | string | Yes | The key of the role to update. The role ID also works. |
roleUpdate | models.RoleUpdate | Yes | The 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.
| Field | Type | Required | Description |
|---|---|---|---|
Name | *string | No | The name of the role. Set it with SetName(). |
Description | *string | No | What the role represents, or which permissions it grants. Set it with SetDescription(). |
Permissions | []string | No | The permissions the role grants, each in the format <resource-key>:<action-key>, such as document:read. Set them with SetPermissions(). |
Extends | []string | No | The keys of roles that this role extends. The role inherits all the permissions of those roles. Set them with SetExtends(). |
Attributes | map[string]string | No | Key-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:
ErrorCode | Cause |
|---|---|
NotFound | HTTP 404: no role with that key or ID exists in the environment. |
UnprocessableEntityError | HTTP 422: a field failed validation. |
Unauthorized, ForbiddenAccess | HTTP 401 or 403: the API key is invalid or has no access to the environment. |
UnexpectedError | A server error (HTTP 5xx) or a network error. |