Golang MS SQL database API
Overview
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.
MS SQL database example
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:
Parameter |
Value |
database |
DemoDB |
host |
mssql.dev.svc.cluster.local |
password |
Yukon900 |
username |
sa |
port |
1433 |
This sample demonstrates how to:
- Create a development Namespace in the Kyma runtime.
- Deploy the following Kubernetes resources:
- API deployment written in GO
- API Rule
- Service
- Secret
Azure MS SQL database example
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.
This sample demonstrates how to:
- Create a development Namespace in the Kyma runtime.
- Deploy the following Kubernetes resources:
- API deployment written in GO
- API Rule
- Service
- Event Subscription
- ServiceBinding
- ServiceBindingUsage
Prerequisites
- SAP BTP, Kyma runtime instance
- Docker
- Go
- kubectl configured to use the
KUBECONFIG
file downloaded from the Kyma runtime
Steps
Run the API locally
-
Set the environment variables required to connect with the database:
export MYAPP_username=sa
export MYAPP_password=Yukon900
export MYAPP_database=DemoDB
export MYAPP_host=localhost
export MYAPP_port=1433
-
Run the application:
go run ./cmd/api
Build the Docker image
-
Build and push the image to your Docker repository:
docker build -t {your-docker-account}/api-mssql-go -f docker/Dockerfile .
docker push {your-docker-account}/api-mssql-go
-
To run the image locally, run:
docker run -p 8000:8000 -d {your-docker-account}/api-mssql-go:latest
Deploy the API - MS SQL database example
-
Create a new dev
Namespace:
kubectl create namespace dev
kubectl label namespaces dev istio-injection=enabled
-
Apply the ConfigMap:
kubectl -n dev apply -f ./k8s/configmap.yaml
-
Apply the Secret:
kubectl -n dev apply -f ./k8s/secret.yaml
-
Apply the Deployment:
kubectl -n dev apply -f ./k8s/deployment.yaml
-
Apply the APIRule:
kubectl -n dev apply -f ./k8s/apirule.yaml
-
Verify that the Deployment is up and running:
kubectl -n dev get deployment api-mssql-go
-
Use the APIRule:
https://api-mssql-go.{cluster-domain}/orders
https://api-mssql-go.{cluster-domain}/orders/10000001
Deploy the API - Azure MS SQL database example
-
Create a new dev
Namespace:
kubectl create namespace dev
-
Get the name of the ServiceInstance:
kubectl -n dev get serviceinstances
For example:
NAME |
CLASS |
PLAN |
STATUS |
AGE |
azure-sql-12-0-unkempt-entrance |
ServiceClass/azure-sql-12-0 |
basic |
Ready |
63m |
-
Within the deployment-servicebinding.yaml
, adjust the name of the instanceRef property of the corresponding ServiceBinding:
apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceBinding
metadata:
name: azure-sql
spec:
instanceRef: name:<b>azure-sql-12-0-unkempt-entrance</b>
-
Apply the Deployment:
kubectl -n dev apply -f ./k8s/deployment-servicebinding.yaml
Deploy the Event Subscription
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.
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.
-
Apply the Deployment:
kubectl -n dev apply -f ./k8s/event.yaml
-
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.