Introduction
In this blog post, we will learn How to Connect to Cloud Services From CAP Application Locally
Though our SAP
CAP Official Document is clearly explained
hybrid-testing, I would like to illustrate with an use-case “Connect to Northwind Cloud Service Locally in CAP Node JS Application using Function Import”
In General Service can be from any external remote system or SAP System like S/4 HANA
For Example Business Partner OData service in SAP S/4HANA
Reference :
Connect to Remote Services from Local
I assume you already aware how to integrate any Remote Service on BTP
- If it is Cloud Service for example (S/4 HANA Cloud ) then you need to use BTP Destination service
- If it is On Premise System which requires VPN to connect to then you need to use BTP BTP Connectivity service & SAP Cloud Connector
Reference :
CAP BTP Connection - Destination
Target Audience
Developers
Prerequisites
I assume you already configured required services for CAP APP
Destination
Authorization and Trust Management Service XSUAA Service
Sample Source Code
Below is sample CAP Node JS Source code to connect to BTP Destination
this.on("getRemoteNWData", async req => {
letresponse;
constcpqservice = awaitcds.connect.to('ext_northwind')
try {
LOG.info("start demo Northwind connecting")
response = awaitcpqservice.send({
query:'GET /v2/northwind/northwind.svc/Products',
headers: {
'Content-Type':'application/json'
}
})
LOG.info("end Northwind connection ")
returnresponse;
} catch (error) {
req.error(500, error.message)
}
});
...
I hope you know further code & how to prepare CAP Application to communicate to BTP cloud services
Scenario : Use Local Application Destinations (Non BTP)
For Testing ,If you don’t want to use SAP BTP
destinations, you can also define
destinations, which means the URL, authentication type, and additional configuration properties, in your application configuration or code.
As of now only Basic Type authentication Destination works with Local CAP Destination
Refer : https://cap.cloud.sap/docs/guides/using-services?q=destination#app-defined-destinations
Scenario : On-Premise Connectivity
Few. SAP systems are located within a company's internal network ,Example : S/4HANA On-Premise , SAP ERP (CRM) ... . They are not exposed to the internet. The SAP Cloud SDK provides helpful features to allow connecting to on-premise systems.
Reference : https://sap.github.io/cloud-sdk/docs/js/features/connectivity/on-premise
Consuming Services in CAP APP
You can easily test your CAP application using a local database and mock ups. But at some point, you’re going to want to test with real cloud services. Of course, you can also deploy your application to the cloud & test but if you want to connect to Real Service locally before deploying to the cloud then you may want to run some ad-hoc tests,CAP Provides below options
Approach 1
- Create default-env.json
- Copy VCAP_SERVICES Values from BTP Environment Variable of your application
- run cds watch from terminal
Note :
https://cap.cloud.sap/docs/node.js/cds-env?q=default-env.json#in-default-envjson
Recently The use of
default-env.json is deprecated
Approach 2 [Hybrid Testing]
A new approach to bind BTP services locally is to use
cds bind
Execute below command with your service instance name ,service key as shown below from Terminal
cds bind -2 servicename:keyname
#1) cds bind -2 sap-organickart-destination-instance:sap-organickart-srv-key
#2) cds bind -2 sap-organickart-auth:sap-organickart-auth-key
Alternatively you can also bind Multiple BTP Service Instances with One Command as shown below in shortcut by comma separated service instances
cds bind -2 servicename1,servicename2
Note : This implies that a service key exists with the suffix
-key
. In this example: sap-organickart-srv
-key.
Example :
cds bind -2 sap-organickart-destination-instance,sap-organickart-auth
After cds bind execution
,it will generate cdsrc-private.json file in project
as shown below
Unlike Default-env.json which is un secure to maintain service sensitive data ,Running the "cds watch" in hybrid mode, which allows you to retrieve service credentials from BTP CF/Kyma on runtime
#3)
cds watch --profile hybrid
cds serve all --with-mocks --in-memory? --profile hybrid
watching: cds,csn,csv,ts,mjs,cjs,js,json,properties,edmx,xml,env,css,gif,html,jpg,png,svg...
live reload enabled for browsers
[bind] - resolving cloud service bindings...
[bind] - bound auth to Cloud Foundry service sap-organickart-auth key sap-organickart-auth-key
[bind] - bound destinations to Cloud Foundry service sap-organickart-destination-instance key sap-organickart-srv-key
Now you can browse your function import using local host port which fetch Products from NorthWind Remote Services
Example :
https://port4004-workspaces-ws-x.eu10.applicationstudio.cloud.sap/auth/getRemoteNWProducts()
Reference
Consuming Services
You can also use cds bind to set up testing to any BTP Service Instance
For Example :
Thank you for reading! If you enjoyed this post, please consider giving it a ‘'Like' ,'Share' it with your friends and followers, and 'Follow' me for more content like this. Your support means a lot to me!
Happy Testing
🙂 !