Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.3k stars 1.96k forks source link

[FEATURE REQ] Allow in CosmosFactory overrideContainerName to pass the original container name #36632

Closed fernyettheplant closed 11 months ago

fernyettheplant commented 1 year ago

Is your feature request related to a problem? Please describe.

I'm working at the moment to enable multi-tenancy capabilities to a Java Project with CosmosDB and we were planning to go tenant per container. Our project requires that a single tenant is owner of multiple containers but i have not found yet any example on how to enable it for such nor the capabilities to append the tenant instead of just having a single container per tenant

example, currently the tenant per container works with Spring Data works like this: Container Name
Tenant01
Tenant02

However, we need to be able to do something like this:

Container Name
EventStore-Tenant01
Projection-Tenant01
EventStore-Tenant02
Projection-Tenant02

Describe the solution you'd like

I was checking the changes introduced in https://github.com/Azure/azure-sdk-for-java/pull/33400 and one improvement that would solve this problem is to allow the method overrideContainerName in CosmosFactory to pass the original container name so it can be manipulated where the tenant id can be appended.

An example of the usage would be like this:

public class MultiTenantContainerCosmosFactory extends CosmosFactory {
    private final TenantStorage tenantStorage;

    public MultiTenantContainerCosmosFactory(
            CosmosAsyncClient cosmosAsyncClient, String databaseName, TenantStorage tenantStorage) {
        super(cosmosAsyncClient, databaseName);
        this.tenantStorage = tenantStorage;
    }

    @Override
    public String overrideContainerName(String originalContainerName) {
        String tenantId = tenantStorage.getCurrentTenant();
        if (tenantId != null) {
            this.tenantId = tenantId;
            return String.format("%s-%s", originalContainerName, tenantId);
        } else {
            return originalContainerName;
        }
    }
}

Describe alternatives you've considered

I was thinking on implement something similar from the ground up using the Azure CosmosDB SDK to allow this but honestly Spring Data solves this issue relatively easy,

Additional context

The only issue that i see with this proposition is that it would introduce breaking changes

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

trande4884 commented 1 year ago

@fernyettheplant

What you are describing here is still just 4 separate tenants, naming them this way does not make them part of the same tenant. I believe what you want is database per tenant.

Container Name

EventStore-Tenant01 Projection-Tenant01 EventStore-Tenant02 Projection-Tenant02

With database per tenant you would do:

Database Name

Tenant01 (with containers EventStore and Projection) Tenant02 (with containers EventStore and Projection)

trande4884 commented 1 year ago

However to achieve your goal by extending CosmosTemplate and overriding getContainerNameOverride like below, adding "containerName + ". image

trande4884 commented 11 months ago

Closing due to inactivity.