Skip to main content

Update a resource with the Go SDK

Permit.Api.Resources.Update() changes the name, URN, description, actions, or attributes of an existing resource in the environment that your API key belongs to. This reference is for Go developers who maintain their policy schema from backend code.

Resources.Update signature

func (r *Resources) Update(ctx context.Context, resourceKey string, resourceUpdate models.ResourceUpdate) (*models.ResourceRead, error)

Resources.Update parameters

ParameterTypeRequiredDescription
ctxcontext.ContextYesThe context of the request.
resourceKeystringYesThe key of the resource to update. The resource ID also works.
resourceUpdatemodels.ResourceUpdateYesThe fields to change. Build it with models.NewResourceUpdate().

ResourceUpdate fields

Resources.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 Actions replaces the resource's whole action map, so include every action the resource keeps.

FieldTypeRequiredDescription
Name*stringNoThe name of the resource. Set it with SetName().
Urn*stringNoThe Uniform Resource Name (URN) of the resource. Set it with SetUrn().
Description*stringNoA longer description of what the resource represents in your system. Set it with SetDescription().
Actions*map[string]models.ActionBlockEditableNoThe actions users can perform on the resource, keyed by action key. Set them with SetActions().
Attributes*map[string]models.AttributeBlockEditableNoThe attributes that each resource of this type defines, for use in attribute-based access control (ABAC) policies. Set them with SetAttributes().

The Go ResourceUpdate struct has no fields for resource roles or relations.

Example: rename a resource with Resources.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 ResourceUpdate struct with a new name and description:

resourceUpdate := models.NewResourceUpdate()
resourceUpdate.SetName("New Name")
resourceUpdate.SetDescription("New Description")

Pass the key of the resource and the struct to Resources.Update. Replace resourceKey with the key of your resource:

resource, err := Permit.Api.Resources.Update(ctx, "resourceKey", *resourceUpdate)

Resources.Update return value and errors

On success, Resources.Update returns a *models.ResourceRead with the updated resource.

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 resource 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.