Question

I am considering a multi-tenant environment where I can have each tenant access a different subdomain and then potentially allocate a namespace based on that domain.

For instance,

tenantA.mydomain.com
tenantB.mydomain.com

Then I would want to have namespace tenantA for all tenantA data and tenantB for all tenantB data.

From the docs, it sounds like I would accomplish this in my appengine_config.py file and do something like this:

from google.appengine.api import namespace_manager

def namespace_manager_default_namespace_for_request():

    this_namespace = get_namespace_from_subdomain()
    return this_namespace

First question, is this a reasonable/good approach?

Second question, it's unclear what variables are available in this scope - any pointers on how to implement the get_namepsace_from_subdomain() function?

Finally, if there were some functionality I wanted to provide that would cross namespaces could this still be achieved with a global namespace? For instance, say a user has an account in multiple tenants and I want to give a view of his activity across all tenants.

Était-ce utile?

La solution 2

I don't think this is the best approach to take. The problem with this approach is that you're tightly coupling you application with the infrastructure in a way. Domain and subdomain are just an easier way to access a machine bound to a specific ip address. I would classify domain names to be part of the infrastructure, not really part of the application. If you go with the above mentioned approach you're introducing some knowledge about the infrastructure into the application and thus making your application less flexible. What happens if you, for some reason, sometime in the future decide that your client A should use clientA.mydomain.com? or how about keyClientA.myotherdomain.com? Or how about you want to allow your client A to use their domain name, i.e. support.clientA.com? If your application does not know anything about domains and infrastructure setup then it's a lot easier to just reconfigure DNS server and get the portability.

If I had this scenario, I would have some kind of mapping of URLs to a tenant id, and then use that tenant id as a namespace name. This way you can easily map different URLs to a tenant id. You can even map multiple URLs to the same tenant id and expose the same application on multiple URLs. Your mapping can be stored in a simple config file or even in the AppEngine datastore itself within global namespace. If the config is stored in the AppEngine datastore, you can have your admin section of the application (or even another AppEngine module) which you can use to update config in real time.

Autres conseils

It is possible and reasonable to use multi-tenancy in your app based on sub-domains, though from my experience you should also allow overriding the namespace by using a url param.

e.g.

tenantB.mydomain.com/?tenant=tenantA => namespace=tenantA

This will make you life a lot easier and will enable you to test your newest appengine versions on *.appspot.com before moving them to production (especially if you are planning on SSL access).

Once you set the namespace than only the entities under that namespace will be available, you can change the namespace via code whenever you want - the scope doesn't matter. for the sub-domain - you can parse it out from the one of the client's request headers.

you can write whatever you want to the global namespace and access it whenever you want via code. for the scenario you described, you need to save the user activity on the global namespace.

also, take a loot at the official python example for using namespaces from the GAE team. https://github.com/GoogleCloudPlatform/appengine-guestbook-namespaces-python It gives you everything you need to get started.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top