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" }])

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)

Raises:

Parameters:

  • Array of tenants to create.

Returns:

  • all created tenants



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" }])

Options Hash (tenants):

  • :tenant_id (String)

    Customer defined string identifier for this tenant.

Raises:

Parameters:

  • Array of tenants to delete.

Returns:

  • if delete was successful



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")

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)

Raises:

Parameters:

  • (defaults to: {})

    a customizable set of options

Returns:

  • created tenant



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")

Raises:

Parameters:

  • User defined string identifier for this tenant.

Returns:

  • if delete was successful



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

Raises:

Parameters:

  • 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:

  • retrieved tenant



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()

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)

Raises:

Parameters:

  • (defaults to: {})

    Filters to apply to result set

  • (defaults to: {})

    Options to apply on a per-request basis

Returns:

  • all tenants for your organization



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

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)

Raises:

Parameters:

  • The user_id of the user from which to fetch tenants

  • (defaults to: {})

    Filters to apply to result set

  • (defaults to: {})

    Options to apply on a per-request basis

Returns:

  • all tenants for the user



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]" })

Raises:

Parameters:

  • User defined string identifier for this tenant.

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

Returns:

  • updated tenant



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

Raises:

Parameters:

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

  • (defaults to: "member")

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

Returns:

  • assigned feature



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

Raises:

Parameters:

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

  • (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:

  • assigned pricing tier



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

Raises:

Parameters:

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

  • (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 assigning user to the tenant



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

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)

Raises:

Parameters:

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

  • (defaults to: "member")

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

  • (defaults to: {})

    a customizable set of options

Returns:

  • whether or not the tenant has the given feature



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

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)

Raises:

Parameters:

  • (defaults to: {})

    Filters to apply to result set

  • (defaults to: {})

    Options to apply on a per-request basis

Returns:

  • assigned features for the tenant



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

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)

Raises:

Parameters:

  • (defaults to: {})

    Filters to apply to result set

  • (defaults to: {})

    Options to apply on a per-request basis

Returns:

  • assigned pricing tiers for the tenant



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

Raises:

Returns:

  • all users for the tenant



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

Raises:

Parameters:

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

  • (defaults to: "member")

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

Returns:

  • if remove was successful



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

Raises:

Parameters:

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

  • (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:

  • if remove was successful



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

Raises:

Parameters:

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

  • (defaults to: "member")

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

Returns:

  • if remove was successful



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]" })

Raises:

Parameters:

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

Returns:

  • updated tenant



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