Skip to content

Node and Python tutorials are updated #22900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ primary_tag: programming-tool>node-js
- You have a trial or a productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
- You have created a subaccount and a space on Cloud Foundry Environment.
- [cf CLI] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/4ef907afb1254e8286882a2bdef0edf4.html) is installed locally.
- [Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally. Make sure you have the latest Node.js version. In this tutorial, we use v.18.
- [Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally. Make sure you have the latest Node.js version. In this tutorial, we use v.16.
- You have installed an integrated development environment, for example [Visual Studio Code] (https://code.visualstudio.com/).

## Intro
This tutorial will guide you through creating and setting up a simple Node.js application by using cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through another one - a web microservice (application router).
This tutorial will guide you through creating and setting up a simple Node.js application in cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through a web microservice (application router). Finally, you will set authentication checks and authorization roles to properly access and manage your web application.

### Log on to SAP BTP
First, you need to connect to the SAP BTP, Cloud Foundry environment with your productive subaccount. Your Cloud Foundry URL depends on the region where the API endpoint belongs to. To find out which one is yours, see: [Regions and API Endpoints Available for the CF Environment] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/f344a57233d34199b2123b9620d0bb41.html?version=Cloud)
Expand All @@ -49,6 +49,9 @@ In this tutorial, we use `eu20.hana.ondemand.com` as an example.


5. Choose the org name and space where you want to create your application.

> This step is skipped if you're using a trial account.



#### RESULT
Expand Down Expand Up @@ -127,7 +130,7 @@ You're going to create a simple Node.js application.
"description": "My simple Node.js app",
"main": "index.js",
"engines": {
"node": "14.x.x"
"node": "16.x.x"
},
"scripts": {
"start": "node start.js"
Expand All @@ -140,7 +143,7 @@ You're going to create a simple Node.js application.
}
```

8. Inside the `myapp` folder, create another file called `start.js` with the following content:
8. Inside the `myapp` folder, create a file `start.js` with the following content:

```JavaScript
const express = require('express');
Expand Down Expand Up @@ -199,8 +202,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
"tenant-mode" : "dedicated",
"oauth2-configuration": {
"redirect-uris": [
"https://node-1234-aaaa-5678.cfapps.eu20.hana.ondemand.com/",
"https://web-1234-aaaa-5678.cfapps.eu20.hana.ondemand.com/"
"https://*.cfapps.eu20.hana.ondemand.com/**"
]
}
}
Expand Down Expand Up @@ -380,7 +382,6 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth




### Run an Authorization Check
Authorization in the SAP BTP, Cloud Foundry environment is also provided by the XSUAA service. In the previous example, the `@sap/approuter` package was added to provide a central entry point for the business application and to enable authentication. Now to extend the example, authorization will be added through the implementation of a `users` REST service. Different authorization checks will be introduced for the GET and CREATE operations to demonstrate how authorization works. The authorization concept includes elements such as roles, scopes, and attributes provided in the security descriptor file `xs-security.json`. For more information, see: [Application Security Descriptor Configuration Syntax] (https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/517895a9612241259d6941dbf9ad81cb.html?version=Cloud)

Expand Down Expand Up @@ -431,7 +432,7 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
cf update-service nodeuaa -c xs-security.json
```

3. In the `myapp` folder, create a new file called `users.json` with the following content:
3. In the `myapp` folder, create a file `users.json` with the following content:

```JSON
[{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,35 @@
parser: v2
author_name: Gergana Tsakova
author_profile: https://github.com/Joysie
title: Create a Python Application with Cloud Foundry Python Buildpack
description: Create a simple application and enable services for it, by using the Cloud Foundry Python Buildpack and Cloud Foundry Command Line Interface (cf CLI).
auto_validation: true
time: 40
tags: [ tutorial>beginner, software-product>sap-btp--cloud-foundry-environment, software-product>sap-hana, software-product-function>sap-btp-cockpit]
primary_tag: programming-tool>python
---

# Create a Python Application via Cloud Foundry Command Line Interface
<!-- description --> Create a simple Python application in the Cloud Foundry Command Line Interface (cf CLI) and enable services for it.
## You will learn
- How to create a simple "Hello World" application in Python
- How to consume an SAP BTP service from it
- How to run authentication and authorization checks via the XSUAA service

## Prerequisites
- You have a productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
- You have a trial or productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
- You have created a subaccount and a space on Cloud Foundry Environment.
- You have created a service instance for [SAP HANA Cloud] (https://help.sap.com/docs/HANA_CLOUD/9ae9104a46f74a6583ce5182e7fb20cb/f7febb16072b41f7ac90abf5ea1d4b86.html) with `hana-free` plan.
- [Python] (https://www.python.org/downloads/) version 3.6.x or higher is installed locally. In this tutorial, we use Python **3.10.1**.
- You have created a service instance for [SAP HANA Cloud] (https://help.sap.com/docs/HANA_CLOUD/9ae9104a46f74a6583ce5182e7fb20cb/f7febb16072b41f7ac90abf5ea1d4b86.html). To learn how to set it up, watch the first half of this video: [SAP HANA Cloud Trial Setup](https://www.youtube.com/watch?v=GSNQpfxPuLU)
- [Python] (https://www.python.org/downloads/) is installed locally. To check which Python versions are supported in the current buildpack, see: [Developing Python in the Cloud Foundry Environment](https://help.sap.com/docs/btp/sap-business-technology-platform/developing-python-in-cloud-foundry-environment#buildpack-versioning). In this tutorial, we use Python **3.11.1**.
- [cf CLI] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/4ef907afb1254e8286882a2bdef0edf4.html) is installed locally.
- [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) is installed locally.
- You have installed an integrated development environment, for example [Visual Studio Code] (https://code.visualstudio.com/).
- You have installed the `virtualenv` tool. It creates a folder, which contains all the necessary executables to use the packages that your Python project would need. To install it locally, execute the following command in the Python installation path:
&nbsp;
`<Python_installation_path>\Python39\Scripts>pip install virtualenv`

`<Python_installation_path>\Python311\Scripts>pip install virtualenv`



## You will learn
- How to create a simple "Hello World" application in Python
- How to consume SAP BTP services from it
- How to run authentication checks for your app
- How to run authorization checks for your app


## Intro
This tutorial will guide you through creating and setting up a simple Python application by using cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through another one - a web microservice (application router).
This tutorial will guide you through creating and setting up a simple Python application by using cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message. This simple app will consume an SAP BTP service, and then will be invoked through a web microservice (application router). Finally, you will set authentication checks and authorization roles to properly access and manage your web application.

---

Expand All @@ -58,9 +54,15 @@ In this tutorial, we use `eu20.hana.ondemand.com` as an example.
cf login
```

4. When prompted, enter your user credentials – the email and password you have used to register your productive SAP BTP account.
4. When prompted, enter your user credentials – the email and password you have used to register your trial or productive SAP BTP account.

> **IMPORTANT**: If the authentication fails, even though you've entered correct credentials, try [logging in via single sign-on] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/e1009b4aa486462a8951c4d499ce6d4c.html?version=Cloud).


5. Choose the org name and space where you want to create your application.

> This step is skipped if you're using a trial account.

> **IMPORTANT**: If the authentication fails, even though you've entered correct credentials, try [logging in via single sign-on] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/e1009b4aa486462a8951c4d499ce6d4c.html?version=Cloud).


#### RESULT
Expand Down Expand Up @@ -102,7 +104,7 @@ You're going to create a simple Python application.
4. Specify the Python runtime version that your application will run on. To do that, create a `runtime.txt` file with the following content:

```TXT
python-3.10.1
python-3.11.1
```

5. This application will be a web server utilizing the Flask web framework. To specify Flask as an application dependency, create a `requirements.txt` file with the following content:
Expand Down Expand Up @@ -133,7 +135,7 @@ You're going to create a simple Python application.
cf push
```

> Make sure you always execute `cf push` in the folder where the `manifest.yml` file is located! In this case, that's `python-tutorial`.
> Make sure you always execute `cf push` in the directory where the `manifest.yml` file is located! In this case, that's `python-tutorial`.

8. When the staging and deployment steps are completed, the `myapp` application should be successfully started and its details displayed in the command console.

Expand All @@ -153,13 +155,7 @@ Your Python application is successfully deployed and running on the SAP BTP, Clo

You have created a service instance for SAP HANA Cloud (see **Prerequisites** at the beginning). Now you're going to make a connection to your SAP HANA database from SAP HANA Schemas & HDI Containers - a service that runs on the SAP BTP, Cloud Foundry environment - and consume this service in your application.

1. Create a `hana` service instance named `pyhana`. As a service plan, you can use `securestore` or `hdi-shared`. They both work for this scenario, so - whichever you prefer (or whichever is available in your subaccount). Execute one of the following commands, respectively:

```Bash/Shell
cf create-service hana securestore pyhana
```

or
1. Create a `hana` service instance named `pyhana` with service plan `hdi-shared`. Execute:

```Bash/Shell
cf create-service hana hdi-shared pyhana
Expand Down Expand Up @@ -258,6 +254,21 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
}
```

> **NOTE**: For trial accounts, enter the following additional `oauth2-configuration` lines in your `xs-security.json` file:


```JSON
{
"xsappname" : "myapp",
"tenant-mode" : "dedicated",
"oauth2-configuration": {
"redirect-uris": [
"https://*.cfapps.eu20.hana.ondemand.com/**"
]
}
}
```

2. Create an `xsuaa` service instance named `pyuaa` with plan `application`, by executing the following command:

```Bash
Expand Down Expand Up @@ -299,7 +310,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
</head>
<body>
<h1>Python Tutorial</h1>
<a href="/myapp/">My Application</a>
<a href="/myapp/">My Python Application</a>
</body>
</html>
```
Expand All @@ -312,7 +323,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
npm init
```

This will walk you through creating a `package.json` file in the `web` folder. Press **Enter** on every step.
Press **Enter** on every step. This process will walk you through creating a `package.json` file in the `web` folder.

8. Now you need to create a directory `web/node_modules/@sap` and install an `approuter` package in it. To do that, in the `web` directory execute:

Expand Down Expand Up @@ -376,7 +387,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth

> ### What's going on?

>As of this point of the tutorial, the URL of the `web` application will be requested instead of the `myapp` URL. It will then forward the requests to the `myapp` application.
>At this point of the tutorial, the URL of the `web` application will be requested instead of the `myapp` URL. It will then forward the requests to the `myapp` application.


13. When the staging and deployment steps are completed, the `web` application should be successfully started and its details displayed in the command console.
Expand All @@ -389,13 +400,12 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth

#### RESULT

A simple application page with title **Python Tutorial** is displayed. When you click the `My Application` link, the current SAP HANA time is displayed, in UTC time zone.

- A simple application page with title **Python Tutorial** is displayed. When you click the `My Application` link, the current SAP HANA time is displayed, in UTC time zone.

- If you directly access the `myapp` URL, it displays the same result - the current SAP HANA time in UTC time zone.


### Run an Authorization Check


Authorization in the SAP BTP, Cloud Foundry environment is also provided by the XSUAA service. In the previous example, the `@sap/approuter` package was added to provide a central entry point for the business application and to enable authentication. Now to extend the example, authorization will be added.

1. Add the `sap-xssec` security library to the `requirements.txt` file, to place restrictions on the content you serve. The file should look like this:
Expand Down Expand Up @@ -472,9 +482,6 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the

Accessing the `myapp` application results in the following:

- If you try to access it directly, a `403 Forbidden` response is displayed due to lack or permissions (lack of authorization header).
- If you try to access it directly, a `403 Forbidden` response is displayed due to lack of permissions (lack of authorization header). This is a correct and expected behavior.

- If you try to access it through the `web` application router, the current SAP HANA time is displayed (in UTC time zone) – provided that you have the `openid` scope assigned to your user. Since the OAuth 2.0 client is used, the `openid` scope is assigned to your user by default, the correct authorization header is declared, and thus you are allowed to access the `myapp` application.