Hello and welcome to this second blog entry around APIs on SAP Cloud Platform!
Hopefully you successfully completed
part 1 and you are ready for part 2.
In this part, we will upload our application to SAP Cloud Platform Cloud Foundry and protect it through API Management.
Let's roll...
Setup SAP Cloud Platform Cloud Foundry trial (if not yet done)
Note that all steps that relate to configuring SAP Cloud Platform Cloud Foundry are documented in depth
here. The following is kept very simple.
First of all, we'll need a trial account on SAP Cloud Platform. If you have no trial account, go ahead and
create one.
Login and you will see a "
Cloud Foundry Trial"
button.
Click it and it will provision a 30-day Trial Access on SAP Cloud Platform Cloud Foundry.
Once the provisioning is done, it is important that you note down the details above. Then, you can go to your space.
On the space page, click on "
trial" on the breadcrumb navigation. This will allow you to get the URL of the CF API used later. It is a good idea to store it somewhere in a text file for now.
In my case, it is:
https://api.cf.eu10.hana.ondemand.com
Before going any further, we will need to install the Cloud Foundry Command Line Interface on our machine.
Install and configure the Cloud Foundry CLI
The CF CLI will let you manage your CF environment from your machine. In order to install it, download the CF CLI from here:
https://github.com/cloudfoundry/cli#downloads
Install the CLI by running
cf_installer.exe.
In order to simplify the usage of the CLI, you may want to update your environment variables and add the CF CLI folder to your PATH user variable.
Now you can start using the CLI: open a command prompt (with Admin rights for Windows users), and connect to your environment with the right connection information.
Type in the following commands, and replace the API endpoint with your own (see end of the first step above).
cf api https://api.cf.eu10.hana.ondemand.com
Now you can login:
cf login
Enter your SAP Cloud Platform mail and password.
When you first use the CLI, you need to define the targeted
org and
space.
To do so, use the following command:
cf target -o <your SCP user ID>trial_trial -s dev
Here is the complete flow for my user:
In case you are behind a proxy, you will need to update variables before doing the connection. You can use the following script to do so:
set http_proxy=your_proxy
set HTTP_PROXY=your_proxy
set https_proxy=your_proxy
set HTTPS_PROXY=your_proxy
set ftp_proxy=your_proxy
set FTP_PROXY=your_proxy
set all_proxy=your_proxy
set ALL_PROXY=your_proxy
With this, you should have the connection up and running between your machine and your Cloud Foundry environment.
Upload the PartnerBankingInformation application
Once you are connected to SAP Cloud Platform Cloud Foundry over the CLI, you can start uploading applications to your org and space.
To do so, you will use the following command:
cf push
In our case, there are 2 relevant parameters to add to the command:
- the name of the application that we want to create on SAP CP: make sure to suffix your application with your user ID so it is unique
- the path of the application on your machine
For instance:
cf push PartnerBankingApplication<UserID> - p "c:\PartnerBankingInformation"
After a while (be patient), your application will ahve been uploaded and started:
In case you get the following error, make sure the application name is unique when pushing it.
If you now go back to your SAP Cloud Platform Cloud Foundry cockpit, you can see that the application has been created.
If you click on the name of the application, you can see its details, especially the URL over which it is acsessible.
To use the application, you need to suffix the URL with the right path and resources:
/v1/PartnerBankingInformation/partnerBankingInformation
You will also need to add the
API key parameter:
?apiKey=123
The URL to call in your browser should look something like this:
https://partnerbankinginformation<your ID>.cfapps.eu10.hana.ondemand.com/v1/PartnerBankingInformation/partnerBankingInformation?apiKey=123445
When calling URL in a browser, you will see the following result:
Note that the response is a standard response that is generated by the API Designer, and you can now adapt it to your needs in the
controllers/DefaultService.js file of your project.
Voila! Your API implementation is available on SAP Cloud Foundry! In the last step, we'll see how to protect it through SAP API Management!
Securing your API implementation with SAP API Management
As you may know by now, SAP API Management is a service from the SAP Cloud Platform which let's you centrally secure, document and publish all of your REST APIs.
One of the added-values of SAP API Management is that it is tightly integrated with other services of the SAP Cloud Platform, as explained
here.
In this last step, we will bind the API implementation to SAP API management, to include it in the central API management service of the SAP Cloud Platform.
First we need to create an API management service instance on our Cloud Foundry trial environment.
Create API management instance on Cloud Foundry
To do so go to your SAP Cloud Platform Cloud Foundry Cockpit "
dev" space. On the left side menu, click on "
Service Marketplace" and select "
API Management".
On the left side menu, click on "Instances" to create an API Management service instance on SAP Cloud Platform Cloud Foundry.
We have now 4 steps to go through.
- Step1: select the service plan lite
- Step 2: specifiy the connection information to your SAP Cloud Platform API Management tenant via a JSON structure.
{
"account_id": "<your account ID on SAP Cloud Platform - Pxxxxxtrial>",
"apiportal_admin": "<your user ID on SAP Cloud Platform - Pxxxxx>",
"apiportal_password": "your password on SAP Cloud Platform",
"consent": true
}
- Account ID: is the account name you have been allocated on your SAP Cloud Platform environment. For instance Pxxxxxtrial. Do not use any capital letters in the account id.
- APIPortal_admin: the user that has admin right on the API Management API Portal. For instance Pxxxxx. Do not use any capital letters in the admin.
- APIPortal_password: the password of the user above.
- Step 3: leave this field empty, we will do the binding in the next step.
- Step 4: give your instance a meaningful name, for instance "APIM".
Click on "
Finish" when done.
Bind your application to the APIM Service
Here is the last step of our little tutorial: binding the application to the API Management service.
To perform the binding, login to your SAP Cloud Platform Cloud Foundry environment. Use the "
cf login" command to do so.
Once logged in, you will use the "
cf bind" command, that requires a couple of parameters:
- the URL of your apps' endpoint, for instance cfapps.eu10.hana.ondemand.com
- the name of your API Management service instance, for instance APIM
- the name of the application to bind, for instance PartnerBankingInformation
- optional: apiname, representing the name of the API proxy to be generated, in the following format: '{"api_name" : ”custom_api_proxy_name”}'
For example:
cf bind-route-service cfapps.eu10.hana.ondemand.com APIM --hostname PartnerBankingInformation -c '{\"api_name\":\"PartnerBankingInformation\"}'
Execute the command once it fits your environment, and the binding should take place within seconds.
Let's now verify that the API proxy has been created in SAP API Management.
To do so, go to the API Management API Portal. You can use a shortcut as depicted in the screenshot below:
In the API Portal, you should see the API Proxy created with the name you gave it in the CF CLI.
Now you can start implementing security, traffic management, mediation, as well as publish the API on your API Developer Portal through the SAP API Management.
If you want to do a quick test (and I will assume you are acquainted with SAP API Management), simply add a quota policy in the incoming request, in the pre-flow. Set it to 5 requests per second and test your API.
Call your API through either the proxy URL or directly trhough the CF URL more than 5 times, and here is the result:
Conclusion
First of all, I hope you have been successful at testing this neat feature.
As you could see by yourself, it is quite simple to build an API from scratch, following a design-first approach, and have it executed in a Cloud Foundry tenant of SAP Cloud Platform. Here you can leverage the full power of the "bring-your-own-technology" of CF, as well as awesome features like
route services.
Overall, SAP brings you the best of two worlds: the SAP Cloud Platform and its awesome business and technical services (PaaS) as well as a SAP Cloud Foundry environment for maximum flexibility in your choices, both seamlessly integrated .