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
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
resourceKey | string | Yes | The key of the resource to update. The resource ID also works. |
resourceUpdate | models.ResourceUpdate | Yes | The 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.
| Field | Type | Required | Description |
|---|---|---|---|
Name | *string | No | The name of the resource. Set it with SetName(). |
Urn | *string | No | The Uniform Resource Name (URN) of the resource. Set it with SetUrn(). |
Description | *string | No | A longer description of what the resource represents in your system. Set it with SetDescription(). |
Actions | *map[string]models.ActionBlockEditable | No | The actions users can perform on the resource, keyed by action key. Set them with SetActions(). |
Attributes | *map[string]models.AttributeBlockEditable | No | The 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:
ErrorCode | Cause |
|---|---|
NotFound | HTTP 404: no resource 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. |