In my previous blog post Consume External Service – Part 1, I have tested the application using mock and real data. Just by configuring the NorthWind OData service URL in the credentials.url property, I was able to connect to the external service. This approach is only applicable for local development testing. If you try to deploy this into SCP Cloud Foundry, you will encounter an error stating that this approach is not valid for production use.
The reason behind this is that external service consumption in this CDS framework is meant to use the Destination Service in Cloud Foundry. And this is the topic of this blog — deploying the Node.js project into SCP Cloud Foundry using Destination Service.
The reason behind this is that external service consumption in this CDS framework is meant to use the Destination Service in Cloud Foundry. And this is the topic of this blog — deploying the Node.js project into SCP Cloud Foundry using Destination Service.
Prerequisites
◉ SAP Business Application Studio / Visual Studio Code
◉ SAP Cloud Platform – Cloud Foundry Environment
◉ You’ve already gone through Consume External Service – Part 1
Setup Destination Configuration
First of all, why do we need to setup a destination? A simple explanation is that it is best practice to avoid hard coding the external services that you use in your application inside your code. It is better for it to be configured separately from your application because as you deploy your application into different environment like DEV, QA, or PROD, you can have a different setup of the destination per environment. Also, it keeps the sensitive information like credentials outside of your code.
1. Create a Destination Service Instance in Cloud Foundry
Service Name: demo-destination
Plan: lite
2. Configure a new destination
You can also test the connection if the destination can be reached. A successful test will yield a message like the one below:
Connection to "NorthWind" established. Response returned: "307: Temporary Redirect"
That’s it! We have completed the setup of our destination.
Deploy the application to SCP Cloud Foundry
Now let’s go back to our Node.js project — if you were able to follow through my previous blog post, then your project will look exactly like the one I have below:
https://github.com/jcailan/azure-devops/tree/cap-es-part1
The next step is to prepare the application for deployment to SCP Cloud Foundry.
1. Generate the mta.yaml file using the command below:
> cds add mta
2. Generate security descriptor file xs-security.json using the command below:
> cds compile srv/ --to xsuaa > xs-security.json
For our case, we can ignore the warning shown in the terminal.
3. Update mta.yaml file with declaration of xsuaa and destination resources and then binding it to our node module demo-srv.
modules:
# --------------------- SERVER MODULE ------------------------
- name: demo-srv
# ------------------------------------------------------------
type: nodejs
path: gen/srv
properties:
EXIT: 1 # required by deploy.js task to terminate
requires:
- name: demo-destination
- name: demo-uaa
resources:
- name: demo-destination
type: org.cloudfoundry.existing-service
parameters:
service-name: demo-destination
- name: demo-uaa
type: org.cloudfoundry.managed-service
parameters:
path: ./xs-security.json
service: xsuaa
service-plan: application
Note that in order for the destination service consumption to work, an xsuaa service is required to be bound to our application.
4. Update the package.json cds configurations to use the NorthWind destination we have configured in SCP Cloud Foundry:
"cds": {
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind",
"credentials": {
"destination": "NorthWind"
}
}
}
}
5. Build the MTA Project by using the command:
> mbt build
Make sure you have saved all the files changes we did before doing the build.
An MTA archive file will be generated in the mta_archives folder.
6. Deploy the MTA archive file into SCP Cloud Foundry using the command:
> cf deploy mta_archives/demo_1.0.0.mtar
7. Once the deployment has completed, look out for the terminal logs which states the URL of your demo-srv module. In my case, here’s the generated URL:
s0017687913trial-dev-demo-srv.cfapps.eu10.hana.ondemand.com
Test the deployed Node.js app service
1. Open the URL we got from the previous step using your favourite browser, the initial page will show, and now click on the Products entity. You should see the result below:
As you can see, it is very easy to deploy the app and use the destination service. We don’t really need to do any additional JavaScript coding, all that we did is just do a little bit of configuration.
Testing the Node.js app locally
Now that we have deployed our application into SCP Cloud Foundry, we don’t really need to change the configuration back to a hardcoded external service URL just to test the app locally. We can test the app locally while still using the destination service in SCP Cloud Foundry. In order to be able to test locally, we need to capture the environment variable assigned to our Node.js module in SCP.
1. Get the VCAP_SERVICES environment variable settings for demo-srv module
Sample data structure to capture:
{
"VCAP_SERVICES": {
"xsuaa": [
{
"label": "xsuaa",
"provider": null,
"plan": "application",
"name": "demo-uaa",
"tags": [
"xsuaa"
],
"instance_name": "demo-uaa",
"binding_name": null,
"credentials": {
"tenantmode": "dedicated",
"sburl": "https://internal-xsuaa.authentication.eu10.hana.ondemand.com",
"clientid": "<<clientid>>",
"xsappname": "demo!t48303",
"clientsecret": "<<clientsecret>>",
"url": "https://myaccount.authentication.eu10.hana.ondemand.com",
"uaadomain": "authentication.eu10.hana.ondemand.com",
"verificationkey": "<<key>>",
"apiurl": "https://api.authentication.eu10.hana.ondemand.com",
"identityzone": "myaccount",
"identityzoneid": "3779d905-0123-45f2-a66d-e5c3a78cb1c4",
"tenantid": "3779d905-0123-45f2-a66d-e5c3a78cb1c4"
},
"syslog_drain_url": null,
"volume_mounts": []
}
],
"destination": [
{
"label": "destination",
"provider": null,
"plan": "lite",
"name": "demo-destination",
"tags": [
"destination",
"conn",
"connsvc"
],
"instance_name": "demo-destination",
"binding_name": null,
"credentials": {
"uaadomain": "authentication.eu10.hana.ondemand.com",
"tenantmode": "dedicated",
"clientid": "<<clientid>>",
"instanceid": "a82d0b2d-0119-4c09-97ee-46793ad8dba2",
"verificationkey": "<<keyu>>",
"xsappname": "clonea82d0b2d01194c0997ee46793ad8dba2!b48303|destination-xsappname!b404",
"identityzone": "myaccount",
"clientsecret": "<<clientsecret>>",
"tenantid": "3779d905-0123-45f2-a66d-e5c3a78cb1c4",
"uri": "https://destination-configuration.cfapps.eu10.hana.ondemand.com",
"url": "https://myaccount.authentication.eu10.hana.ondemand.com"
},
"syslog_drain_url": null,
"volume_mounts": []
}
]
}
}
Note that the information contained in VCAP_SERVICES environment variable have credential information and shouldn’t committed in your git repository.
2. Create a file called default-env.json in the root of your project directory, and paste here the data you captured from SCP. If you are using the cds template project using the command cds init, then it has generated a .gitignore file that will automatically ignore default-env.json file we are creating here.
3. Next thing to do is start the app locally using the command:
> cds watch
4. Test the app on a browser by using the URL below:
http://localhost:4004/catalog/Products
As you can see we now can test the app locally using real data while also using the destination service of SCP Cloud Foundry.
If by any chance you still want to revert to testing the app using our mock data, you can still do so, you just need to amend the cds configuration in package.json to make the credentials section invalid. See below example:
"cds": {
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind",
"--credentials": {
"destination": "NorthWind"
}
}
}
}
Save the changes and refresh your browser, then you will see the mock data is available again.
Closing
Now you know how easy it is to consume an external service using CAP Model and use Destination Service to manage the connection configuration. We also tested our application in three different ways:
◉ Test the service directly in SCP Cloud Foundry
◉ Test the service locally while still using SCP CF Destination Service
◉ Test the service locally using mock data
No comments:
Post a Comment