Skip to main content

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

ParameterTypeRequiredDescription
resourceDataResourceCreateYesThe 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.

FieldBuilder methodTypeRequiredDescription
keywithKey()StringYesA URL-friendly name (slug) of the resource. Use the key instead of the resource ID (UUID) in later calls.
namewithName()StringYesThe display name of the resource.
actionswithActions()HashMap<String, ActionBlockEditable>YesThe actions a user can perform on the resource, keyed by action key (for example read or update).
urnwithUrn()StringNoThe URN (Uniform Resource Name) of the resource.
descriptionwithDescription()StringNoA longer description of what the resource represents in your system.
attributeswithAttributes()HashMap<String, AttributeBlockEditable>NoAttributes that each resource of this type defines, for use in attribute-based access control (ABAC) policies.
roleswithRoles()HashMap<String, RoleBlockEditable>NoResource roles, keyed by role key. Each value holds the role's granted permissions and base roles.
relationswithRelations()HashMap<String, String>NoRelations 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.

ExceptionThrown when
IOExceptionThe HTTP request to the Permit API fails, for example because of a network error.
PermitApiErrorThe Permit API returns an error status code. getResponseCode() returns the HTTP status code and getRawResponse() returns the response body.
PermitContextErrorThe SDK context does not include an environment, for example because the API key is scoped to an organization or project.