Class: Warrant::Tenant

Inherits:
Object
  • Object
show all
Includes:
WarrantObject
Defined in:
lib/warrant/models/tenant.rb

Constant Summary collapse

OBJECT_TYPE =
"tenant"

Instance Attribute Summary

Attributes inherited from Object

#created_at, #meta, #object_id, #object_type

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.batch_create(tenants, options = {}) ⇒ Array<Tenant>

Batch creates multiple tenants with given parameters

Examples:

Create two new tenants with tenant ids “test-tenant-1” and “test-tenant-2”

Warrant::Tenant.batch_create([{ tenant_id: "test-tenant-1" }, { tenant_id: "test-tenant-2" }])

Parameters:

  • tenants (Array<Hash>)

    Array of tenants to create.

Options Hash (tenants):

  • :tenant_id (String)

    User defined string identifier for this tenant. If not provided, Warrant will create an id for the tenant and return it. In this case, you should store the id in your system for future reference. Note that tenantIds in Warrant must be composed of alphanumeric chars and/or ‘-’, ‘_’, and ‘@’. (optional)

  • :meta (Hash)

    A JSON object containing additional information about the tenant (e.g. name/description, etc.) to be persisted to Warrant. (optional)

Returns:

  • (Array<Tenant>)

    all created tenants

Raises:



54
55
56
57
# File 'lib/warrant/models/tenant.rb', line 54

def self.batch_create(tenants, options = {})
    res = Object.batch_create(tenants.map{ |tenant| { object_type: OBJECT_TYPE, object_id: tenant[:tenant_id], meta: tenant[:meta] }}, options)
    return res.map{ |obj| Tenant.new(obj.object_id, obj.meta, obj.created_at)}
end

.batch_delete(tenants, options = {}) ⇒ nil

Batch deletes multiple tenants with given parameters

Examples:

Delete two tenants with ids “test-tenant-1” and “test-tenant-2”

Warrant::Tenant.batch_delete([{ tenant_id: "test-tenant-1" }, { tenant_id: "test-tenant-2" }])

Parameters:

  • tenants (Array<Hash, Tenant>)

    Array of tenants to delete.

Options Hash (tenants):

  • :tenant_id (String)

    Customer defined string identifier for this tenant.

Returns:

  • (nil)

    if delete was successful

Raises:



89
90
91
92
93
94
95
96
97
# File 'lib/warrant/models/tenant.rb', line 89

def self.batch_delete(tenants, options = {})
    return Object.batch_delete(tenants.map{ |tenant|
        if tenant.instance_of? Tenant
            { object_type: OBJECT_TYPE, object_id: tenant.object_id }
        else
            { object_type: OBJECT_TYPE, object_id: tenant[:tenant_id] }
        end
    }, options)
end

.create(params = {}, options = {}) ⇒ Tenant

Creates a tenant with the given parameters

Examples:

Create a new Tenant with the tenant id “test-customer”

Warrant::Tenant.create(tenant_id: "test-customer")

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :tenant_id (String)

    User defined string identifier for this tenant. If not provided, Warrant will create an id for the tenant and return it. In this case, you should store the id in your system for future reference. Note that tenantIds in Warrant must be composed of alphanumeric chars and/or ‘-’, ‘_’, and ‘@’. (optional)

  • :meta (Hash)

    A JSON object containing additional information about this tenant (e.g. name/description, etc.) to be persisted to Warrant. (optional)

Returns:

Raises:



32
33
34
35
# File 'lib/warrant/models/tenant.rb', line 32

def self.create(params = {}, options = {})
    object = Object.create({ object_type: OBJECT_TYPE, object_id: params[:tenant_id], meta: params[:meta] }, options)
    return Tenant.new(object.object_id, object.meta, object.created_at)
end

.delete(tenant_id, options = {}) ⇒ nil

Deletes a tenant with given tenant id

Examples:

Delete a Tenant with the tenant id “test-customer”

Warrant::Tenant.delete("test-customer")

Parameters:

  • tenant_id (String)

    User defined string identifier for this tenant.

Returns:

  • (nil)

    if delete was successful

Raises:



71
72
73
# File 'lib/warrant/models/tenant.rb', line 71

def self.delete(tenant_id, options = {})
    return Object.delete(OBJECT_TYPE, tenant_id, options)
end

.get(tenant_id, options = {}) ⇒ Tenant

Get a tenant with the given tenant_id

Parameters:

  • tenant_id (String)

    User defined string identifier for this tenant. If not provided, Warrant will create an id for the tenant and return it. In this case, you should store the id in your system for future reference. Note that tenantIds in Warrant must be composed of alphanumeric chars and/or ‘-’, ‘_’, and ‘@’.

Returns:

  • (Tenant)

    retrieved tenant

Raises:



135
136
137
138
# File 'lib/warrant/models/tenant.rb', line 135

def self.get(tenant_id, options = {})
    object = Object.get(OBJECT_TYPE, tenant_id, options)
    return Tenant.new(object.object_id, object.meta, object.created_at)
end

.list(filters = {}, options = {}) ⇒ Array<Tenant>

Lists all tenants for your organization

Examples:

List all tenants

Warrant::Tenant.list()

Parameters:

  • filters (Hash) (defaults to: {})

    Filters to apply to result set

  • options (Hash) (defaults to: {})

    Options to apply on a per-request basis

Options Hash (filters):

  • :limit (Integer)

    A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)

  • :prev_cursor (String)

    A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)

  • :next_cursor (String)

    A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)

  • :sort_by (String)

    The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are object_type, object_id, and created_at (optional)

  • :sort_order (String)

    The order in which to sort the result by. Valid values are ASC and DESC. Defaults to ASC. (optional)

Options Hash (options):

  • :warrant_token (String)

    A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)

Returns:

  • (Array<Tenant>)

    all tenants for your organization

Raises:



118
119
120
121
122
123
# File 'lib/warrant/models/tenant.rb', line 118

def self.list(filters = {}, options = {})
    filters.merge({ object_type: "tenant" })
    list_response = Object.list(filters, options)
    tenants = list_response.results.map{ |object| Tenant.new(object.object_id, object.meta, object.created_at)}
    return ListResponse.new(tenants, list_response.prev_cursor, list_response.next_cursor)
end

.list_for_user(user_id, filters = {}, options = {}) ⇒ Array<Tenant>

List all tenants for a user

Parameters:

  • user_id (String)

    The user_id of the user from which to fetch tenants

  • filters (Hash) (defaults to: {})

    Filters to apply to result set

  • options (Hash) (defaults to: {})

    Options to apply on a per-request basis

Options Hash (filters):

  • :object_type (String)

    Only return objects with an ‘objectType` matching this value

  • :limit (Integer)

    A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)

  • :prev_cursor (String)

    A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)

  • :next_cursor (String)

    A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)

  • :sort_by (String)

    The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are object_type, object_id, and created_at (optional)

  • :sort_order (String)

    The order in which to sort the result by. Valid values are ASC and DESC. Defaults to ASC. (optional)

Options Hash (options):

  • :warrant_token (String)

    A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)

Returns:

  • (Array<Tenant>)

    all tenants for the user

Raises:



227
228
229
230
231
# File 'lib/warrant/models/tenant.rb', line 227

def self.list_for_user(user_id, filters = {}, options = {})
    query_response = Warrant.query("select tenant where user:#{user_id} is *", filters: filters, options: options)
    tenants = query_response.results.map{ |result| Tenant.new(result.object_id, result.meta) }
    return ListResponse.new(tenants, query_response.prev_cursor, query_response.next_cursor)
end

.update(tenant_id, meta, options = {}) ⇒ Tenant

Updates a tenant with the given tenant_id

Examples:

Update tenant “test-tenant”‘s email

Warrant::Tenant.update("test-tenant", { email: "[email protected]" })

Parameters:

  • tenant_id (String)

    User defined string identifier for this tenant.

  • meta (Hash)

    A JSON object containing additional information about this tenant (e.g. name/description, etc.) to be persisted to Warrant.

Returns:

Raises:



155
156
157
158
# File 'lib/warrant/models/tenant.rb', line 155

def self.update(tenant_id, meta, options = {})
    object = Object.update(OBJECT_TYPE, tenant_id, meta, options)
    return Tenant.new(object.object_id, object.meta, object.created_at)
end

Instance Method Details

#assign_feature(feature_id, relation: "member", options: {}) ⇒ Feature

Assign a feature to a tenant

Parameters:

  • feature_id (String)

    The feature_id of the feature you want to assign to the tenant.

  • relation (String) (defaults to: "member")

    The relation for this feature to tenant association. The relation must be valid as per the feature object type definition.

Returns:

Raises:



331
332
333
# File 'lib/warrant/models/tenant.rb', line 331

def assign_feature(feature_id, relation: "member", options: {})
    return Feature.assign_to_tenant(tenant_id, feature_id, relation: relation, options: options)
end

#assign_pricing_tier(pricing_tier_id, relation: "member", options: {}) ⇒ PricingTier

Assign a pricing tier to a tenant

Parameters:

  • pricing_tier_id (String)

    The pricing_tier_id of the pricing tier you want to assign to the tenant.

  • relation (String) (defaults to: "member")

    The relation for this pricing tier to tenant association. The relation must be valid as per the pricing tier object type definition.

Returns:

Raises:



278
279
280
# File 'lib/warrant/models/tenant.rb', line 278

def assign_pricing_tier(pricing_tier_id, relation: "member", options: {})
    return PricingTier.assign_to_tenant(tenant_id, pricing_tier_id, relation: relation, options: options)
end

#assign_user(user_id, relation: "member", options: {}) ⇒ Warrant

Add a user to a tenant

Parameters:

  • user_id (String)

    The user_id of the user you want to add to the tenant.

  • relation (String) (defaults to: "member")

    The relation for this tenant to user association. The relation must be valid as per the tenant object type definition.

Returns:

  • (Warrant)

    warrant assigning user to the tenant

Raises:



190
191
192
# File 'lib/warrant/models/tenant.rb', line 190

def assign_user(user_id, relation: "member", options: {})
    return User.assign_to_tenant(tenant_id, user_id, relation: relation, options: options)
end

#has_feature?(feature_id, relation: "member", options: {}) ⇒ Boolean

Check whether a tenant has a given feature

Parameters:

  • feature_id (String)

    The feature_id of the feature to check whether the tenant has access to.

  • relation (String) (defaults to: "member")

    The relation for this feature to tenant association. The relation must be valid as per the feature object type definition.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options:):

  • :context (Hash)

    Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)

  • :debug (Boolean)

    Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)

Returns:

  • (Boolean)

    whether or not the tenant has the given feature

Raises:



364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/warrant/models/tenant.rb', line 364

def has_feature?(feature_id, relation: "member", options: {})
    return Warrant.has_feature?({
        feature_id: feature_id,
        relation: relation,
        subject: {
            object_type: "tenant",
            object_id: tenant_id
        },
        context: options[:context],
        debug: options[:debug]
    }, options)
end

#list_features(filters = {}, options = {}) ⇒ Array<Feature>

List features for a tenant

Parameters:

  • filters (Hash) (defaults to: {})

    Filters to apply to result set

  • options (Hash) (defaults to: {})

    Options to apply on a per-request basis

Options Hash (filters):

  • :object_type (String)

    Only return objects with an ‘objectType` matching this value

  • :limit (Integer)

    A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)

  • :prev_cursor (String)

    A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)

  • :next_cursor (String)

    A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)

  • :sort_by (String)

    The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are object_type, object_id, and created_at (optional)

  • :sort_order (String)

    The order in which to sort the result by. Valid values are ASC and DESC. Defaults to ASC. (optional)

Options Hash (options):

  • :warrant_token (String)

    A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)

Returns:

  • (Array<Feature>)

    assigned features for the tenant

Raises:



315
316
317
# File 'lib/warrant/models/tenant.rb', line 315

def list_features(filters = {}, options = {})
    return Feature.list_for_tenant(tenant_id, filters, options)
end

#list_pricing_tiers(filters = {}, options = {}) ⇒ Array<Feature>

List pricing tiers for a tenant

Parameters:

  • filters (Hash) (defaults to: {})

    Filters to apply to result set

  • options (Hash) (defaults to: {})

    Options to apply on a per-request basis

Options Hash (filters):

  • :object_type (String)

    Only return objects with an ‘objectType` matching this value

  • :limit (Integer)

    A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)

  • :prev_cursor (String)

    A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)

  • :next_cursor (String)

    A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)

  • :sort_by (String)

    The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are object_type, object_id, and created_at (optional)

  • :sort_order (String)

    The order in which to sort the result by. Valid values are ASC and DESC. Defaults to ASC. (optional)

Options Hash (options):

  • :warrant_token (String)

    A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)

Returns:

  • (Array<Feature>)

    assigned pricing tiers for the tenant

Raises:



262
263
264
# File 'lib/warrant/models/tenant.rb', line 262

def list_pricing_tiers(filters = {}, options = {})
    return PricingTier.list_for_tenant(tenant_id, filters, options)
end

#list_users(filters = {}, options = {}) ⇒ Array<User>

List all users for a tenant

Returns:

  • (Array<User>)

    all users for the tenant

Raises:



241
242
243
# File 'lib/warrant/models/tenant.rb', line 241

def list_users(filters = {}, options = {})
    return User.list_for_tenant(tenant_id, filters, options)
end

#remove_feature(feature_id, relation: "member", options: {}) ⇒ nil

Remove a feature from a tenant

Parameters:

  • feature_id (String)

    The feature_id of the feature you want to remove from the tenant.

  • relation (String) (defaults to: "member")

    The relation for this feature to tenant association. The relation must be valid as per the feature object type definition.

Returns:

  • (nil)

    if remove was successful

Raises:



347
348
349
# File 'lib/warrant/models/tenant.rb', line 347

def remove_feature(feature_id, relation: "member", options: {})
    return Feature.remove_from_tenant(tenant_id, feature_id, relation: relation, options: options)
end

#remove_pricing_tier(pricing_tier_id, relation: "member", options: {}) ⇒ nil

Remove a pricing_tier from a tenant

Parameters:

  • pricing_tier_id (String)

    The pricing_tier_id of the pricing_tier you want to remove from the tenant.

  • relation (String) (defaults to: "member")

    The relation for this pricing tier to tenant association. The relation must be valid as per the pricing tier object type definition.

Returns:

  • (nil)

    if remove was successful

Raises:



294
295
296
# File 'lib/warrant/models/tenant.rb', line 294

def remove_pricing_tier(pricing_tier_id, relation: "member", options: {})
    return PricingTier.remove_from_tenant(tenant_id, pricing_tier_id, relation: relation, options: options)
end

#remove_user(user_id, relation: "member", options: {}) ⇒ nil

Remove a user from a tenant

Parameters:

  • user_id (String)

    The user_id of the user you want to remove from the tenant.

  • relation (String) (defaults to: "member")

    The relation for this tenant to user association. The relation must be valid as per the tenant object type definition.

Returns:

  • (nil)

    if remove was successful

Raises:



205
206
207
# File 'lib/warrant/models/tenant.rb', line 205

def remove_user(user_id, relation: "member", options: {})
    return User.remove_from_tenant(tenant_id, user_id, relation: relation, options: options)
end

#update(meta, options = {}) ⇒ Tenant

Updates the tenant with the given params

Examples:

Update tenant “test-tenant”‘s name

tenant = Warrant::Tenant.get("test-tenant")
tenant.update({ name: "[email protected]" })

Parameters:

  • meta (Hash)

    A JSON object containing additional information about this tenant (e.g. name/description, etc.) to be persisted to Warrant.

Returns:

Raises:



174
175
176
# File 'lib/warrant/models/tenant.rb', line 174

def update(meta, options = {})
    return Tenant.update(tenant_id, meta, options)
end

#warrant_object_idObject



381
382
383
# File 'lib/warrant/models/tenant.rb', line 381

def warrant_object_id
    tenant_id
end

#warrant_object_typeObject



377
378
379
# File 'lib/warrant/models/tenant.rb', line 377

def warrant_object_type
    "tenant"
end