Create a resource with the Java SDK
permit.api.resources.create() creates a resource type, with its actions and optional attributes, in your Permit environment. This reference is for Java developers who define their authorization schema from code. The examples assume an initialized Permit client named permit, as set up in Check permissions with the Java SDK.
Signature
ResourceRead create(ResourceCreate resourceData) throws IOException, PermitApiError, PermitContextError
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
resourceData | ResourceCreate | Yes | The resource to create. The fields are listed in ResourceCreate fields. |
ResourceCreate fields
The ResourceCreate(key, name, actions) constructor sets the three required fields. Set the optional fields with the with...() builder methods.
| Field | Builder method | Type | Required | Description |
|---|---|---|---|---|
key | withKey() | String | Yes | A URL-friendly name (slug) of the resource. Use the key instead of the resource ID (UUID) in later calls. |
name | withName() | String | Yes | The display name of the resource. |
actions | withActions() | HashMap<String, ActionBlockEditable> | Yes | The actions a user can perform on the resource, keyed by action key (for example read or update). |
urn | withUrn() | String | No | The URN (Uniform Resource Name) of the resource. |
description | withDescription() | String | No | A longer description of what the resource represents in your system. |
attributes | withAttributes() | HashMap<String, AttributeBlockEditable> | No | Attributes that each resource of this type defines, for use in attribute-based access control (ABAC) policies. |
roles | withRoles() | HashMap<String, RoleBlockEditable> | No | Resource roles, keyed by role key. Each value holds the role's granted permissions and base roles. |
relations | withRelations() | HashMap<String, String> | No | Relations to other resources, keyed by relation key. Each value is the related resource key. |
Example
The example builds the actions and attributes maps, with the types shown in ResourceCreate fields, and creates a document resource.
import io.permit.sdk.openapi.models.ActionBlockEditable;
import io.permit.sdk.openapi.models.AttributeBlockEditable;
import io.permit.sdk.openapi.models.AttributeType;
import io.permit.sdk.openapi.models.ResourceRead;
import io.permit.sdk.openapi.models.ResourceCreate;
import java.util.HashMap;
// resource actions
HashMap<String, ActionBlockEditable> actions = new HashMap<>();
actions.put("create", new ActionBlockEditable());
actions.put("read", new ActionBlockEditable().withName("Read").withDescription("Read a document"));
actions.put("update", new ActionBlockEditable());
actions.put("delete", new ActionBlockEditable());
// resource attributes for attribute-based access control
HashMap<String, AttributeBlockEditable> attributes = new HashMap<>();
attributes.put("private", new AttributeBlockEditable(AttributeType.BOOL).withDescription("Whether the document is private"));
// create document resource
ResourceCreate resourceInput = ((
new ResourceCreate("document", "Document", actions)
)
.withUrn("prn:gdrive:document")
.withDescription("google drive document")
.withAttributes(attributes)
);
ResourceRead document = permit.api.resources.create(resourceInput);
Return value
create() returns a ResourceRead object (io.permit.sdk.openapi.models.ResourceRead) with the created resource, including the ID that Permit assigns to the resource.
Exceptions
All exception classes except IOException are in the io.permit.sdk.api package.
| Exception | Thrown when |
|---|---|
IOException | The HTTP request to the Permit API fails, for example because of a network error. |
PermitApiError | The Permit API returns an error status code. getResponseCode() returns the HTTP status code and getRawResponse() returns the response body. |
PermitContextError | The SDK context does not include an environment, for example because the API key is scoped to an organization or project. |