Skip to content

Commit d5d4ea0

Browse files
Introduce deployment and multitenancy (#25) (#26)
The following was added to the bookshop example: - Configuration for single tenant deployment - Configuration for multi tenant deployment - Handlers for multi tenant subscription
1 parent bee140c commit d5d4ea0

14 files changed

Lines changed: 477 additions & 3 deletions

File tree

.cdsrc.json

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
11
{
22
"build": {
3-
"target": "."
3+
"target": ".",
4+
"tasks": [
5+
{
6+
"for": "java-cf",
7+
"src": "srv",
8+
"options": {
9+
"model": [
10+
"db",
11+
"srv",
12+
"app"
13+
]
14+
}
15+
},
16+
{
17+
"for": "mtx",
18+
"src": ".",
19+
"dest": "mtx-sidecar",
20+
"options": {
21+
"model": [
22+
"db",
23+
"srv",
24+
"app"
25+
]
26+
}
27+
},
28+
{
29+
"for": "hana",
30+
"src": "db",
31+
"options": {
32+
"model": [
33+
"db",
34+
"srv",
35+
"app"
36+
]
37+
}
38+
}
39+
]
40+
},
41+
"hana": {
42+
"deploy-format": "hdbtable"
443
},
544
"requires": {
645
"db": {

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Framework and Infrastructure-related Features:
4343
- [Application configuration](https://cap.cloud.sap/docs/java/development#application-configuration) for Spring and CDS using [application.yaml](srv/src/main/resources/application.yaml)
4444
- [Mocking users](/srv/src/main/resources/application.yaml) for local development
4545
- [Authentication & Authorization](https://cap.cloud.sap/docs/java/advanced#security) (including user-specific restrictions with `@restrict` in the [Admin Service](/srv/admin-service.cds))
46+
- [Cloud Foundry Deployment using MTA](https://cap.cloud.sap/docs/advanced/deploy-to-cloud#deploy-using-mta) with XSUAA [Service Bindings](mta-single-tenant.yaml)
47+
- Application Router configuration including authentication via the XSUAA Service. See [package.json](app/package.json), [xs-app.json](app/xs-app.json) and [cds-security.json](cds-security.json)
48+
- [Multitenancy configuration](https://cap.cloud.sap/docs/java/multitenancy) via [mta-multi-tenant.yaml](mta-multi-tenant.yaml), [.cdsrc.json](.cdsrc.json), [sidecar module](mtx-sidecar)
4649

4750
Domain Model related Features:
4851

@@ -140,7 +143,7 @@ Optionally, use the following steps to import the project to Eclipse:
140143

141144
## Database Setup and Spring Profiles
142145

143-
The application comes with three predefined profiles: `default`, `sqlite`, and `cloud` (see `src/main/resources/application.yaml`).
146+
The application comes with three predefined profiles: `default`, `sqlite`, and `cloud` (see `srv/src/main/resources/application.yaml`).
144147

145148
- The `default` profile specifies to use an in-memory SQLite database.
146149
The in-memory database is set up automatically during startup of the application.
@@ -165,6 +168,30 @@ To switch from the default in-memory SQLite database to a file-based SQLite data
165168

166169
2. Edit your Run Configuration via **Run** > **Run Configurations...** and select enter the **Profile** `sqlite` on tab **Spring** and click **Run**.
167170

171+
## Deploy to SAP Cloud Platform
172+
173+
CAP Java applications can be deployed to the SAP Cloud Platform either in single tenant or in multitenancy mode (see [Multitenancy in CAP Java](https://cap.cloud.sap/docs/java/multitenancy) for more information.
174+
175+
Prerequisites:
176+
- Install the [Cloud MTA Build Tool](https://sap.github.io/cloud-mta-build-tool/): `npm install -g mbt
177+
- Install the [Cloud Foundry Command Line Interface](https://docs.cloudfoundry.org/cf-cli/install-go-cli.html)
178+
- Get a SAP Cloud Platform account to deploy the services and applications
179+
180+
Deploy as Single Tenant Application
181+
- Rename `mta-single-tenant.yaml` to `mta.yaml`
182+
- Run `mbt build`
183+
- Run `cf login`
184+
- Run `cf deploy mta_archives/bookshop-java-public_1.0.0.mtar`
185+
186+
Deploy as Multitenant Application:
187+
- Create a SAP HANA Cloud Instance in your SAP Cloud Platform space
188+
- Rename `mta-multi-tenant.yaml` to `mta.yaml`
189+
- Run `mbt build`
190+
- Run `cf login`
191+
- Run `cf deploy mta_archives/bookshop-java-public_1.0.0.mtar`
192+
- Go to another subaccount in your global account, under subscriptions and subscribe to the application you deployed
193+
- Run `cf map-route bookshop-java-public-approuter <YOUR DOMAIN> --hostname <SUBSCRIBER TENANT>-<ORG>-<SPACE>-bookshop-java-public-approuter` or create and bind the route manually
194+
168195
# Get Support
169196

170197
Check out the documentation at [https://cap.cloud.sap](https://cap.cloud.sap).

app/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "approuter",
3+
"dependencies": {
4+
"@sap/approuter": "8.5.0"
5+
},
6+
"scripts": {
7+
"start": "node node_modules/@sap/approuter/approuter.js"
8+
}
9+
}

app/xs-app.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"welcomeFile": "/app/fiori.html",
3+
"authenticationMethod": "route",
4+
"routes": [
5+
{
6+
"source": "^/app/(.*)$",
7+
"cacheControl": "no-cache, no-store, must-revalidate",
8+
"target": "$1",
9+
"localDir": "./",
10+
"authenticationType": "xsuaa"
11+
},
12+
{
13+
"source": "^/browse/webapp/(.*)$",
14+
"localDir": "./",
15+
"authenticationType": "xsuaa"
16+
},
17+
{
18+
"source": "^/admin/webapp/(.*)$",
19+
"localDir": "./",
20+
"authenticationType": "xsuaa"
21+
},
22+
{
23+
"source": "^/orders/webapp/(.*)$",
24+
"localDir": "./",
25+
"authenticationType": "xsuaa"
26+
},
27+
{
28+
"source": "^/api/admin/(.*)",
29+
"authenticationType": "xsuaa",
30+
"destination": "backend"
31+
},
32+
{
33+
"source": "^/api/(.*)$",
34+
"authenticationType": "none",
35+
"destination": "backend"
36+
}
37+
]
38+
}

cds-security-mt.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"xsappname": "bookshop-java-public",
3+
"tenant-mode": "shared",
4+
"scopes": [
5+
{
6+
"name": "$XSAPPNAME.admin",
7+
"description": "admin"
8+
},
9+
{
10+
"name": "$XSAPPNAME.mtcallback",
11+
"description": "Multi Tenancy Callback Access",
12+
"grant-as-authority-to-apps": ["$XSAPPNAME(application,sap-provisioning,tenant-onboarding)"]
13+
},
14+
{
15+
"name": "$XSAPPNAME.mtdeployment",
16+
"description": "Scope to trigger a re-deployment of the database artifacts"
17+
}
18+
],
19+
"attributes": [],
20+
"role-templates": [
21+
{
22+
"name": "admin",
23+
"description": "generated",
24+
"scope-references": [
25+
"$XSAPPNAME.admin"
26+
],
27+
"attribute-references": []
28+
}
29+
]
30+
}

cds-security.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"xsappname": "bookshop-java-public",
3+
"tenant-mode": "dedicated",
4+
"scopes": [
5+
{
6+
"name": "$XSAPPNAME.admin",
7+
"description": "admin"
8+
}
9+
],
10+
"attributes": [],
11+
"role-templates": [
12+
{
13+
"name": "admin",
14+
"description": "generated",
15+
"scope-references": [
16+
"$XSAPPNAME.admin"
17+
],
18+
"attribute-references": []
19+
}
20+
]
21+
}

db/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "deploy",
3+
"dependencies": {
4+
"@sap/hdi-deploy": "^3"
5+
},
6+
"scripts": {
7+
"start": "node node_modules/@sap/hdi-deploy --exit"
8+
}
9+
}

mta-multi-tenant.yaml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
_schema-version: '2.1'
2+
ID: bookshop-java-public
3+
version: 1.0.0
4+
description: "Multitenant Bookshop CAP Java Project with UI"
5+
parameters:
6+
enable-parallel-deployments: true
7+
modules:
8+
# --------------------- SERVER MODULE ------------------------
9+
- name: bookshop-java-public-srv
10+
# ------------------------------------------------------------
11+
type: java
12+
path: srv
13+
parameters:
14+
memory: 1024M
15+
disk-quota: 256M
16+
buildpack: sap_java_buildpack
17+
properties:
18+
SPRING_PROFILES_ACTIVE: cloud
19+
CDS_MULTITENANCY_APPUI_TENANTSEPARATOR: "-"
20+
build-parameters:
21+
builder: custom
22+
commands:
23+
- mvn clean package -DskipTests=true
24+
build-result: target/*-exec.jar
25+
requires:
26+
- name: bookshop-java-public-service-manager
27+
- name: bookshop-java-public-uaa
28+
- name: bookshop-java-public-saas-registry
29+
- name: sidecar
30+
properties:
31+
CDS_MULTITENANCY_SIDECAR_URL: ~{url}
32+
- name: app-url
33+
properties:
34+
CDS_MULTITENANCY_APPUI_URL: ~{url}
35+
provides:
36+
- name: srv-url
37+
properties:
38+
url: '${default-url}'
39+
# --------------------- SIDECAR MODULE -----------------------
40+
- name: bookshop-java-public-sidecar
41+
# ------------------------------------------------------------
42+
type: nodejs
43+
path: mtx-sidecar
44+
parameters:
45+
memory: 256M
46+
disk-quota: 512M
47+
requires:
48+
- name: bookshop-java-public-service-manager
49+
- name: bookshop-java-public-uaa
50+
provides:
51+
- name: sidecar
52+
properties:
53+
url: ${default-url}
54+
# --------------------- APPROUTER MODULE ---------------------
55+
- name: bookshop-java-public-approuter
56+
# ------------------------------------------------------------
57+
type: nodejs
58+
path: app
59+
parameters:
60+
memory: 256M
61+
disk-quota: 512M
62+
properties:
63+
TENANT_HOST_PATTERN: ^(.*)-${default-uri}
64+
requires:
65+
- name: srv-url
66+
group: destinations
67+
properties:
68+
name: backend
69+
url: ~{url}
70+
forwardAuthToken: true
71+
strictSSL: true
72+
- name: bookshop-java-public-uaa
73+
provides:
74+
- name: app-url
75+
properties:
76+
url: '${default-url}'
77+
# --------------------- RESOURCES ---------------------
78+
resources:
79+
# -----------------------------------------------------
80+
- name: bookshop-java-public-uaa
81+
type: com.sap.xs.uaa
82+
parameters:
83+
service: xsuaa
84+
service-plan: broker
85+
path: ./cds-security-mt.json
86+
config: # override xsappname in cds-security.json, xsappname needs to be unique
87+
xsappname: bookshop-java-public-${org}-${space}
88+
- name: bookshop-java-public-service-manager
89+
type: org.cloudfoundry.managed-service
90+
parameters:
91+
service: service-manager
92+
service-plan: container
93+
- name: bookshop-java-public-saas-registry
94+
type: org.cloudfoundry.managed-service
95+
parameters:
96+
service: saas-registry
97+
service-plan: application
98+
config:
99+
appName: bookshop-java-public-${org}-${space} # this is the text on the tile
100+
xsappname: bookshop-java-public-${org}-${space} # this is the value from xsuaa.parameters.config.xsappname
101+
appUrls:
102+
getDependencies: ~{srv-url/url}/mt/v1.0/subscriptions/dependencies
103+
onSubscription: ~{srv-url/url}/mt/v1.0/subscriptions/tenants/{tenantId}
104+
requires:
105+
- name: srv-url

mta-single-tenant.yaml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
_schema-version: '2.1'
2+
ID: bookshop-java-public
3+
version: 1.0.0
4+
description: "Bookshop CAP Java Project with UI"
5+
parameters:
6+
enable-parallel-deployments: true
7+
modules:
8+
# --------------------- SERVER MODULE ------------------------
9+
- name: bookshop-java-public-srv
10+
# ------------------------------------------------------------
11+
type: java
12+
path: srv
13+
parameters:
14+
memory: 1024M
15+
disk-quota: 256M
16+
buildpack: sap_java_buildpack
17+
properties:
18+
SPRING_PROFILES_ACTIVE: cloud
19+
build-parameters:
20+
builder: custom
21+
commands:
22+
- mvn clean package -DskipTests=true
23+
build-result: target/*-exec.jar
24+
requires:
25+
- name: bookshop-java-public-hdi-container
26+
- name: bookshop-java-public-uaa
27+
provides:
28+
- name: srv-url
29+
properties:
30+
url: '${default-url}'
31+
# --------------------- DB MODULE ---------------------------
32+
- name: bookshop-java-public-db
33+
# -----------------------------------------------------------
34+
type: nodejs
35+
path: db
36+
parameters:
37+
memory: 256M
38+
disk-quota: 512M
39+
no-route: true
40+
no-start: true
41+
tasks: # execute hdi deployer on app creation/update
42+
- name: hdi-deploy
43+
command: npm run start
44+
requires:
45+
# Set user and password to secure rest API via basic authentication
46+
- name: bookshop-java-public-hdi-container
47+
# --------------------- APPROUTER MODULE ---------------------
48+
- name: bookshop-java-public-approuter
49+
# ------------------------------------------------------------
50+
type: nodejs
51+
path: app
52+
parameters:
53+
memory: 256M
54+
disk-quota: 512M
55+
requires:
56+
- name: srv-url
57+
group: destinations
58+
properties:
59+
name: backend
60+
url: ~{url}
61+
forwardAuthToken: true
62+
strictSSL: true
63+
- name: bookshop-java-public-uaa
64+
provides:
65+
- name: app-url
66+
properties:
67+
url: '${default-url}'
68+
# --------------------- RESOURCES ---------------------
69+
resources:
70+
# -----------------------------------------------------
71+
- name: bookshop-java-public-uaa
72+
type: com.sap.xs.uaa
73+
parameters:
74+
service: xsuaa
75+
service-plan: broker
76+
path: ./cds-security.json
77+
config: # override xsappname in cds-security.json, xsappname needs to be unique
78+
xsappname: bookshop-java-public-${org}-${space}
79+
- name: bookshop-java-public-hdi-container
80+
type: org.cloudfoundry.managed-service
81+
parameters:
82+
service: hanatrial
83+
service-plan: hdi-shared

0 commit comments

Comments
 (0)