Skip to content

Commit

Permalink
Add CreateWarrantOptions and DeleteWarrantOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyphu committed Aug 14, 2024
1 parent 4f489bd commit 0bcb0ab
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.workos.fga.builders

import com.workos.fga.models.Subject
import com.workos.fga.types.CreateWarrantOptions

/**
* Builder for options when performing a warrant create.
*
* @param resourceType The type of the resource. Must be one of your system's existing resource types.
* @param resourceId The unique ID of the resource.
* @param relation The relation for this resource to subject association. The relation must be valid per the resource type definition.
* @param subject (see [Subject]) The resource with the specified `relation` to the resource provided by resourceType and resourceId.
* @param policy A boolean expression that must evaluate to true for this warrant to apply. The expression can reference variables provided in the context attribute of access check requests.
*/
class CreateWarrantOptionsBuilder @JvmOverloads constructor(
private var resourceType: String,
private var resourceId: String,
private var relation: String,
private var subject: Subject,
private var policy: String? = null,
) {
fun resourceType(value: String) = apply { resourceType = value }

fun resourceId(value: String) = apply { resourceId = value }

fun relation(value: String) = apply { relation = value }

fun subject(value: Subject) = apply { subject = value }

fun policy(value: String) = apply { policy = value }

fun build(): CreateWarrantOptions {
return CreateWarrantOptions(
resourceType = this.resourceType,
resourceId = this.resourceId,
relation = this.relation,
subject = this.subject,
policy = this.policy,
)
}

companion object {
@JvmStatic
fun create(resourceType: String, resourceId: String, relation: String, subject: Subject): CreateWarrantOptionsBuilder {
return CreateWarrantOptionsBuilder(resourceType, resourceId, relation, subject)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.workos.fga.builders

import com.workos.fga.models.Subject
import com.workos.fga.types.DeleteWarrantOptions

/**
* Builder for options when performing a warrant delete.
*
* @param resourceType The type of the resource. Must be one of your system's existing resource types.
* @param resourceId The unique ID of the resource.
* @param relation The relation for this resource to subject association. The relation must be valid per the resource type definition.
* @param subject (see [Subject]) The resource with the specified `relation` to the resource provided by resourceType and resourceId.
* @param policy A boolean expression that must evaluate to true for this warrant to apply. The expression can reference variables provided in the context attribute of access check requests.
*/
class DeleteWarrantOptionsBuilder @JvmOverloads constructor(
private var resourceType: String,
private var resourceId: String,
private var relation: String,
private var subject: Subject,
private var policy: String? = null,
) {
fun resourceType(value: String) = apply { resourceType = value }

fun resourceId(value: String) = apply { resourceId = value }

fun relation(value: String) = apply { relation = value }

fun subject(value: Subject) = apply { subject = value }

fun policy(value: String) = apply { policy = value }

fun build(): DeleteWarrantOptions {
return DeleteWarrantOptions(
resourceType = this.resourceType,
resourceId = this.resourceId,
relation = this.relation,
subject = this.subject,
policy = this.policy,
)
}

companion object {
@JvmStatic
fun create(resourceType: String, resourceId: String, relation: String, subject: Subject): DeleteWarrantOptionsBuilder {
return DeleteWarrantOptionsBuilder(resourceType, resourceId, relation, subject)
}
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/com/workos/fga/types/CreateWarrantOptions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.workos.fga.types

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.workos.fga.models.Subject

@JsonInclude(JsonInclude.Include.NON_NULL)
class CreateWarrantOptions @JvmOverloads constructor(
/**
* The type of the resource.
*/
@JsonProperty("resource_type")
resourceType: String,

/**
* The unique ID of the resource.
*/
@JsonProperty("resource_id")
resourceId: String,

/**
* The relation for this resource to subject association. The relation must be valid per the resource type definition.
*/
@JsonProperty("relation")
relation: String,

/**
* The resource with the specified `relation` to the resource provided by resourceType and resourceId.
*/
@JsonProperty("subject")
subject: Subject,

/**
* A boolean expression that must evaluate to true for this warrant to apply. The expression can reference variables provided in the context attribute of access check requests.
*/
@JsonProperty("policy")
policy: String? = null,
) : WriteWarrantOptions("create", resourceType, resourceId, relation, subject, policy)
38 changes: 38 additions & 0 deletions src/main/kotlin/com/workos/fga/types/DeleteWarrantOptions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.workos.fga.types

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.workos.fga.models.Subject

@JsonInclude(JsonInclude.Include.NON_NULL)
class DeleteWarrantOptions @JvmOverloads constructor(
/**
* The type of the resource.
*/
@JsonProperty("resource_type")
resourceType: String,

/**
* The unique ID of the resource.
*/
@JsonProperty("resource_id")
resourceId: String,

/**
* The relation for this resource to subject association. The relation must be valid per the resource type definition.
*/
@JsonProperty("relation")
relation: String,

/**
* The resource with the specified `relation` to the resource provided by resourceType and resourceId.
*/
@JsonProperty("subject")
subject: Subject,

/**
* A boolean expression that must evaluate to true for this warrant to apply. The expression can reference variables provided in the context attribute of access check requests.
*/
@JsonProperty("policy")
policy: String? = null,
) : WriteWarrantOptions("delete", resourceType, resourceId, relation, subject, policy)
3 changes: 2 additions & 1 deletion src/main/kotlin/com/workos/fga/types/WriteWarrantOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
import com.workos.fga.models.Subject

@JsonInclude(JsonInclude.Include.NON_NULL)
class WriteWarrantOptions @JvmOverloads constructor(
open class WriteWarrantOptions @JvmOverloads constructor(
/**
* Operation to perform for the given warrant (create/delete).
*/
Expand Down Expand Up @@ -43,6 +43,7 @@ class WriteWarrantOptions @JvmOverloads constructor(
val policy: String? = null,
) {
init {
require(op.isNotBlank()) { "Op is required" }
require(resourceType.isNotBlank()) { "Resource type is required" }
require(resourceId.isNotBlank()) { "Resource id is required" }
require(relation.isNotBlank()) { "Relation is required" }
Expand Down
26 changes: 25 additions & 1 deletion src/test/kotlin/com/workos/test/fga/FgaApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.workos.common.models.Order
import com.workos.fga.builders.CheckBatchOptionsBuilder
import com.workos.fga.builders.CheckOptionsBuilder
import com.workos.fga.builders.CreateResourceOptionsBuilder
import com.workos.fga.builders.CreateWarrantOptionsBuilder
import com.workos.fga.builders.DeleteWarrantOptionsBuilder
import com.workos.fga.builders.ListResourcesOptionsBuilder
import com.workos.fga.builders.ListWarrantsOptionsBuilder
import com.workos.fga.builders.QueryOptionsBuilder
Expand Down Expand Up @@ -593,6 +595,16 @@ class FgaApiTest : TestBase() {
"resource_id": "tony-stark"
}
},
{
"op": "delete",
"resource_type": "tenant",
"resource_id": "avengers",
"relation": "member",
"subject": {
"resource_type": "user",
"resource_id": "tony-stark"
}
},
{
"op": "create",
"resource_type": "role",
Expand All @@ -602,13 +614,25 @@ class FgaApiTest : TestBase() {
"resource_type": "user",
"resource_id": "tony-stark"
}
},
{
"op": "create",
"resource_type": "tenant",
"resource_id": "stark-industries",
"relation": "member",
"subject": {
"resource_type": "user",
"resource_id": "tony-stark"
}
}
]"""
)

val options = listOf(
WriteWarrantOptionsBuilder("delete", "role", "editor", "member", Subject("user", "tony-stark")).build(),
WriteWarrantOptionsBuilder("create", "role", "admin", "member", Subject("user", "tony-stark")).build()
DeleteWarrantOptionsBuilder("tenant", "avengers", "member", Subject("user", "tony-stark")).build(),
WriteWarrantOptionsBuilder("create", "role", "admin", "member", Subject("user", "tony-stark")).build(),
CreateWarrantOptionsBuilder("tenant", "stark-industries", "member", Subject("user", "tony-stark")).build()
)

val warrantResponse = workos.fga.batchWriteWarrants(options)
Expand Down

0 comments on commit 0bcb0ab

Please sign in to comment.