Create a resource with the Go SDK
Permit.Api.Resources.Create() creates a resource, the type of object you protect with permissions (such as document), in the environment that your API key belongs to. This reference is for Go developers who define their policy schema from backend code. After you create a resource, grant its actions to roles with Roles.Create or Roles.Update.
Resources.Create signature
func (r *Resources) Create(ctx context.Context, resourceCreate models.ResourceCreate) (*models.ResourceRead, error)
Resources.Create parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
resourceCreate | models.ResourceCreate | Yes | The resource to create. Build it with models.NewResourceCreate(key, name, actions). |
ResourceCreate fields
| Field | Type | Required | Description |
|---|---|---|---|
Key | string | Yes | A URL-friendly name of the resource (a slug). You use the key to refer to the resource in later calls, and in permissions such as document:read, instead of the resource ID (a UUID). |
Name | string | Yes | The name of the resource. |
Actions | map[string]models.ActionBlockEditable | Yes | The actions users can perform on the resource. Each map key is an action key, such as edit. Each value is an ActionBlockEditable built with models.NewActionBlockEditable(), with an optional Name, Description, and Attributes. |
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(). |
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 ResourceCreate struct has no fields for resource roles or relations.
Example: create a resource with Resources.Create
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 an edit action, and pass it in the actions map of a new ResourceCreate struct with the key resource-key. The final SetName("document") call replaces the name resource-name that the constructor set:
action := models.NewActionBlockEditable()
action.SetName("edit")
resourceCreate := models.NewResourceCreate("resource-key", "resource-name", map[string]models.ActionBlockEditable{"edit": *action})
resourceCreate.SetName("document")
Pass the struct to Resources.Create:
resource, err := Permit.Api.Resources.Create(ctx, *resourceCreate)
Resources.Create return value and errors
On success, Resources.Create returns a *models.ResourceRead with the resource's Key, Id, Name, Actions, and Attributes.
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 |
|---|---|
UnprocessableEntityError | HTTP 422: a field failed validation, for example a missing Actions map. |
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. |