|
| 1 | +# Golang MS SQL database API |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This sample provides a Golang API endpoint for communication with the MS SQL databases provided in the `database-mssql` and `database-azure-mssql` directories. You can also enable an Event Trigger for both examples. |
| 6 | + |
| 7 | +## MS SQL database example |
| 8 | + |
| 9 | +For the `database-mssql` example, use the `deployment.yaml` file. It provides the Deployment definition as well as an APIRule to expose the Function without authentication. The Deployment also contains a ConfigMap and a Secret with the following parameters for the `database-mssql` example that you can configure to modify the default options: |
| 10 | + |
| 11 | +| Parameter | Value | |
| 12 | +| ------------ | ----------------------------- | |
| 13 | +| **database** | `DemoDB` | |
| 14 | +| **host** | `mssql.dev.svc.cluster.local` | |
| 15 | +| **password** | `Yukon900` | |
| 16 | +| **username** | `sa` | |
| 17 | +| **port** | `1433` | |
| 18 | + |
| 19 | +This sample demonstrates how to: |
| 20 | + |
| 21 | +- Create a development Namespace in the Kyma runtime. |
| 22 | +- Deploy the following Kubernetes resources: |
| 23 | + - API deployment written in GO |
| 24 | + - API Rule |
| 25 | + - Service |
| 26 | + - Secret |
| 27 | + |
| 28 | +## Azure MS SQL database example |
| 29 | + |
| 30 | +For the `database-azure-mssql` example, use the `deployment-servicebinding.yaml` file. It defines the Deployment definition as well as an APIRule to expose the Function without authentication. It also defines a ServiceBinding and ServiceBindingUsage that configure the Function to use the `database-azure-mssql` ServiceInstance. |
| 31 | + |
| 32 | +This sample demonstrates how to: |
| 33 | + |
| 34 | +- Create a development Namespace in the Kyma runtime. |
| 35 | +- Deploy the following Kubernetes resources: |
| 36 | + - API deployment written in GO |
| 37 | + - API Rule |
| 38 | + - Service |
| 39 | + - Event Subscription |
| 40 | + - ServiceBinding |
| 41 | + - ServiceBindingUsage |
| 42 | + |
| 43 | +## Prerequisites |
| 44 | + |
| 45 | +- SAP BTP, Kyma runtime instance |
| 46 | +- [Docker](https://www.docker.com/) |
| 47 | +- [Go](https://golang.org/doc/install) |
| 48 | +- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) configured to use the `KUBECONFIG` file downloaded from the Kyma runtime |
| 49 | + |
| 50 | +## Steps |
| 51 | + |
| 52 | +### Run the API locally |
| 53 | + |
| 54 | +1. Set the environment variables required to connect with the database: |
| 55 | + |
| 56 | + ```shell script |
| 57 | + export MYAPP_username=sa |
| 58 | + export MYAPP_password=Yukon900 |
| 59 | + export MYAPP_database=DemoDB |
| 60 | + export MYAPP_host=localhost |
| 61 | + export MYAPP_port=1433 |
| 62 | + ``` |
| 63 | + |
| 64 | +2. Run the application: |
| 65 | + |
| 66 | + ```shell script |
| 67 | + go run ./cmd/api |
| 68 | + ``` |
| 69 | + |
| 70 | +### Build the Docker image |
| 71 | + |
| 72 | +1. Build and push the image to your Docker repository: |
| 73 | + |
| 74 | + ```shell script |
| 75 | + docker build -t {your-docker-account}/api-mssql-go -f docker/Dockerfile . |
| 76 | + docker push {your-docker-account}/api-mssql-go |
| 77 | + ``` |
| 78 | + |
| 79 | +2. To run the image locally, run: |
| 80 | + |
| 81 | + ```shell script |
| 82 | + docker run -p 8000:8000 -d {your-docker-account}/api-mssql-go:latest |
| 83 | + ``` |
| 84 | + |
| 85 | +### Deploy the API - MS SQL database example |
| 86 | + |
| 87 | +1. Create a new `dev` Namespace: |
| 88 | + |
| 89 | + ```shell script |
| 90 | + kubectl create namespace dev |
| 91 | + kubectl label namespaces dev istio-injection=enabled |
| 92 | + ``` |
| 93 | + |
| 94 | +2. Apply the ConfigMap: |
| 95 | + |
| 96 | + ```shell script |
| 97 | + kubectl -n dev apply -f ./k8s/configmap.yaml |
| 98 | + ``` |
| 99 | + |
| 100 | +3. Apply the Secret: |
| 101 | + |
| 102 | + ```shell script |
| 103 | + kubectl -n dev apply -f ./k8s/secret.yaml |
| 104 | + ``` |
| 105 | + |
| 106 | +4. Apply the Deployment: |
| 107 | + |
| 108 | + ```shell script |
| 109 | + kubectl -n dev apply -f ./k8s/deployment.yaml |
| 110 | + ``` |
| 111 | + |
| 112 | +5. Apply the APIRule: |
| 113 | + |
| 114 | + ```shell script |
| 115 | + kubectl -n dev apply -f ./k8s/apirule.yaml |
| 116 | + ``` |
| 117 | + |
| 118 | +6. Verify that the Deployment is up and running: |
| 119 | + |
| 120 | + ```shell script |
| 121 | + kubectl -n dev get deployment api-mssql-go |
| 122 | + ``` |
| 123 | + |
| 124 | +7. Use the APIRule: |
| 125 | + |
| 126 | + - `https://api-mssql-go.{cluster-domain}/orders` |
| 127 | + - `https://api-mssql-go.{cluster-domain}/orders/10000001` |
| 128 | + |
| 129 | +### Deploy the API - Azure MS SQL database example |
| 130 | + |
| 131 | +1. Create a new `dev` Namespace: |
| 132 | + |
| 133 | + ```shell script |
| 134 | + kubectl create namespace dev |
| 135 | + ``` |
| 136 | + |
| 137 | +2. Get the name of the ServiceInstance: |
| 138 | + |
| 139 | + ```shell script |
| 140 | + kubectl -n dev get serviceinstances |
| 141 | + ``` |
| 142 | + |
| 143 | + For example: |
| 144 | + |
| 145 | + | NAME | CLASS | PLAN | STATUS | AGE | |
| 146 | + | ------------------------------------- | --------------------------- | ----- | ------ | --- | |
| 147 | + | **_azure-sql-12-0-unkempt-entrance_** | ServiceClass/azure-sql-12-0 | basic | Ready | 63m | |
| 148 | + |
| 149 | +3. Within the `deployment-servicebinding.yaml`, adjust the name of the **instanceRef** property of the corresponding ServiceBinding: |
| 150 | + |
| 151 | + ```yaml |
| 152 | + apiVersion: servicecatalog.k8s.io/v1beta1 |
| 153 | + kind: ServiceBinding |
| 154 | + metadata: |
| 155 | + name: azure-sql |
| 156 | + spec: |
| 157 | + instanceRef: name:<b>azure-sql-12-0-unkempt-entrance</b> |
| 158 | + ``` |
| 159 | +
|
| 160 | +4. Apply the Deployment: |
| 161 | +
|
| 162 | + ```shell script |
| 163 | + kubectl -n dev apply -f ./k8s/deployment-servicebinding.yaml |
| 164 | + ``` |
| 165 | + |
| 166 | +### Deploy the Event Subscription |
| 167 | + |
| 168 | +The Event Subscription works for both samples. It expects that either SAP Commerce Cloud or the Commerce Mock application is connected and configured within the Namespace. You can find a blog post with details on the Commerce Mock setup [here](https://blogs.sap.com/2020/06/17/sap-cloud-platform-extension-factory-kyma-runtime-commerce-mock-events-and-apis/). |
| 169 | + |
| 170 | +The subscription and code within the Golang application are set up for the `order.created` event. Before you deploy the subscription, verify that the value of `spec.filter.filters.eventType.value` is correct for the name of your application. |
| 171 | + |
| 172 | +1. Apply the Deployment: |
| 173 | + |
| 174 | + ```shell script |
| 175 | + kubectl -n dev apply -f ./k8s/event.yaml |
| 176 | + ``` |
| 177 | + |
| 178 | +2. Within the mock application, submit the `order.created` event. This populates the database with the submitted order code and the `order received from event` notification. |
0 commit comments