# Google Cloud Run functions

KPN Things: Configure a Google Cloud Run functions destination

### Name

Give your Google Cloud Run functions Destination a name and optionally a description.

* **Destination name** (required) - the administrative name of your Destination.

  Technical name: `name`

  Example value: *Tenant Z*
* **Description** - an administrative description of your Destination.

  Technical name: `description`

  Example value: Send my data to Google Cloud for further data processing

### Connection details

First you need to create a Google Cloud account on[ ](https://aws.amazon.com/free)<https://cloud.google.com>, or use an existing Google Cloud account. \
Then follow the instructions below to create an integration in your Google Cloud account. Keep your KPN Things Destination configuration screen open, as you will need to copy information from and to it later.

***

## Google Cloud: Create an integration

{% hint style="info" %}
This is an example to integrate directly with a Google Cloud Run functions.&#x20;

There are other alternatives.&#x20;
{% endhint %}

Log in to your Google Cloud account and go to Consol&#x65;*.* \
Then select the project of your choice and from there select the Cloud Run Product.

<figure><img src="https://1453626848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl6RrePMSAjRvOgcHjMBZ%2Fuploads%2FMdGFMnv0t0vYQXMSitdY%2Fimage.png?alt=media&#x26;token=35b8071a-b86b-453f-9814-da9e8bbcd154" alt=""><figcaption><p>Creating a Google Cloud Run function</p></figcaption></figure>

From the Cloud Run menu select the option Write a Function

<figure><img src="https://1453626848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl6RrePMSAjRvOgcHjMBZ%2Fuploads%2F7d5kstNfOqNfJ76TLjD1%2Fimage.png?alt=media&#x26;token=f63cc815-dabc-478a-a8e3-dfced9cc520a" alt=""><figcaption></figcaption></figure>

The following form is presented.

<figure><img src="https://1453626848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl6RrePMSAjRvOgcHjMBZ%2Fuploads%2Fz05vf0ZInOhT6hvcaLjR%2FGCR1-1.png?alt=media&#x26;token=e9a85c5e-8df7-40ec-ad1c-64f699a98b05" alt=""><figcaption></figcaption></figure>

#### 1. Basic information

In the first step, enter the following information.

* **Service name**\
  Enter a name that describes the purpose of your function.
* **Runtime**\
  Choose the language to use to write your function. For this example Python 3.10 is chosen.
* **Authentication**

  Choose the authentication type. For this example, Allow unauthenticated invocations.

All other setting can be left to default.

Click *Create* .

#### 2. Code

Select *the Source tab to define your code. The screenshot below contains example code.*

<figure><img src="https://1453626848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl6RrePMSAjRvOgcHjMBZ%2Fuploads%2FZv39GsGIssYqxhXsuG36%2FGCR2-1.png?alt=media&#x26;token=6b0a26f4-a2e4-46f5-87f6-ad5e8a14bd52" alt=""><figcaption></figcaption></figure>

Click Save and Redeploy

## KPN Things: Continue with Configure a Google Cloud Run functions destination

In this step, you need to copy/paste information from Google Cloud to Things. So pay attention, please.&#x20;

Go back to the KPN Things Destination configuration screen and fill the remain fields.

* **HTTP endpoint URL**\
  Copy the *Function URL* from Google Cloud into the KPN Things Destination configuration.
* **Shared secret**\
  With the shared secret you have the possibility to verify that Things has sent the data and not someone else, because with the shared secret a unique Things Message Token is calculated for each message. Within the Google Cloud Run function you can repeat the calculation and check whether the calculated Things-Message-Token is the same as the received Things-Message-Token.  Below you can find an example of a calculation and a check in Python.

````
import functions_framework
import hashlib
import json

@functions_framework.http
def hello_http(request):
    # This is an example.
    # Make sure you store the sharedSecret in a safe place,
    
    requestBody = request.get_json(silent=True)
    sharedSecret='[Fill in your shared secret]'
    thingsMessageToken = request.headers.get('Things-Message-Token')
    input = json.dumps(requestBody) + sharedSecret
 
    def calculate_sha256(data):
    # Convert data to bytes if it’s not already
        if isinstance(data, str):
            data = data.encode()
        # Calculate SHA-256 hash
        sha256_hash = hashlib.sha256(data).hexdigest()
        return sha256_hash

    if thingsMessageToken == calculate_sha256(input):
    # Define further action
        return {
            'statusCode': 201,
        }
    else:
        return {
            'statusCode': 401,

        }

```
````

Finally, go back to the KPN Things Destination configuration screen and press the *Add Google Cloud Run functions destination* button to save the new destination in KPN Things.&#x20;

Now you are finished and good to go!
