Multitenant Business Application with PostgreSQL – Part 1
Our goal is to create and deploy a sample project using CAP, multitenancy and postgreSQL to SAP Cloud Foundry environment (BTP). However, getting the lego pieces of several different development frameworks can sometimes be tricky. Let’s take a quick look at the three frameworks we’ll be combining in our sample project.
- The Cloud Application Programming Model (CAP) framework relies on SAP Core Data Services (@sap/cds) to quickly build and ship full stack business applications.
- SAP Hana academy has published an amazing youtube playlist showing how to bootstrap CAP projects to as Multitenant SaaS solutions. Most of what we’ll be doing in part 1 of this tutorial series comes from from SAP Hana Academy.
- Gregor Wolf, Mike Zaschka and Volker Buzek created the cds-pg & cds-dbm open-sourced libraries which make it possible to run CAP projects with a postgreSQL database.
Before digging in, you’ll want to make sure you have a BTP trial and a Github account.
I’ll be using VS Code and Docker throughout the project, let’s get those installed too. Once you’ve got your accounts and software set-up, grab some coffee and meet me back here.
We’ll bootstrap our sample project using SAP Hana Academy’s SaaS yoeman generator so get those installed globally:
npm install -g yo npm i -g generator-saphanaacademy-saas
I like to start off projects from an existing github repo and I’m also hungry, so I’ll be creating a multitenant tacoshop (mt-tacoshop). Create you’re own project and make it yours, what kind business will your SaaS cater to?
From the terminal clone and change director to the new project.
git clone https://github.com/austinkloske22/mt-tacoshop.git cd mt-tacoshop
Launch the Saphanaacademy SaaS generator. Below our the values for my sample project for reference.
This generator can also be run with: yo saphanaacademy-saas ? What project name would you like? mt-tacoshop ? Would you like to create a new directory for this project? No ? What is the display name of your app? Multitenant Tacoshop ? What is the description of your app? Multitenant Tacoshop SaaS ? What is the category of your app? Demo ? Would you like to include an example of using the SaaS API (view subscriptions)? Yes ? Would you like to include HANA persistence (schema separation)? No ? Will you be using a wildcard custom domain (eg: myappsaas.acme.com)? If so please enter it here - or simply press enter for none. ? Would you like to include creation/deletion of tenant routes on subscribe/unsubscribe (using the CF API)? NB: This is not necessary when using a wildcard custom domain. Yes ? Would you like to include an example of using the destination reuse service? Yes
Awesome! With a few terminal cmds and the help of SAP academy we already have a large part of an enterprise level business application out-of-the-box. Before adding the postgres adapters, and connecting to our database, let’s make sure our projects is deploying and subscribe / subsubscribing correctly.
login to CF and target your trial BTP account.
cf login -a https://api.cf.us10.hana.ondemand.com/
Build and deploy the project to SAP BTP
mbt build cf deploy ./mta_archives/mt-tacoshop_0.0.1.mtar
If you enable creation/deletion of tenant routes on bootstrap, we’ll also need to set up some environment variables that process routes during the subscribe/unsubscribe process.
cf set-env mt-tacoshop-srv cf_api_user '<BTP email>' cf set-env mt-tacoshop-srv cf_api_password '<BTP password>' cf restage mt-tacoshop-srv
If the deployment worked, well done! Let’s move on to BTP cockpit configuration.
BTP Trial set-up
By default BTP trial comes with on CF enabled account. That’s where we deployed our SaaS application to. Well want to create a 2 subaccounts to properly demonstrate multitenancy.
The main difference between our original ‘trial’ acount the the two newly created subaccounts is that CF is enabled in our trial account. Applications are deployed and run in CF enabled spaces. ‘street-food’ & ‘tacos-and-tequilla’ are consumer tenants this our SaaS solution.
From either tenant, navigate to Service Marketplace -> <Your SaaS> and create an instance of the service. If all goes well, you’ll see a green subscribed box and have the option to ‘Go to Application’.
If you enabled route creation / deletion, you should automatiically be taken to a page looking something like this. If not, routes can be manually created in the CF enabled account.
Any of the links above will initially give a 403 forbidden error. Use the BTP cockpit to add authentication & authorization to your login. (Hint: role, and role collections already exist from the xs-security.json that was created on bootstrap. You can assign role-collections after a tenant subscription is successful)
If you haven’t already, make sure to commit back to github. In part 2 well deploy to post postgreSQL locally and on AWS rds.
looking forward for the next part of this series. But please let's not forget another driver for cds-pg which is Volker Buzek!
Good catch, i've updated the references to give a shout out to Volker. Part 2 published today and Working on Part 3 but still have some issues to iron out of cds-pg, cds-dbm.