Skip to content

Commit 710c2f2

Browse files
authored
Merge pull request #569 from grego952/brin-back-two-tutorials
Bring back api-mssgql and database-mssql
2 parents a62fc70 + b05ee32 commit 710c2f2

25 files changed

Lines changed: 1044 additions & 0 deletions

api-mssql-go/README.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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.

api-mssql-go/cmd/api/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/gorilla/mux"
8+
9+
"github.com/SAP-samples/kyma-runtime-extension-samples/api-mssql-go/internal/api"
10+
)
11+
12+
func main() {
13+
router := mux.NewRouter().StrictSlash(true)
14+
15+
apiServer := api.InitAPIServer()
16+
17+
router.HandleFunc("/orders", apiServer.GetOrders).Methods("GET")
18+
router.HandleFunc("/orders/{id}", apiServer.GetOrder).Methods("GET")
19+
router.HandleFunc("/orders/{id}", apiServer.DeleteOrder).Methods("DELETE")
20+
router.HandleFunc("/orders/{id}", apiServer.EditOrder).Methods("PUT")
21+
router.HandleFunc("/orders", apiServer.AddOrder).Methods("POST")
22+
23+
router.HandleFunc("/orderCodeEvent", apiServer.ConsumeOrderCode).Methods("POST")
24+
25+
log.Fatal(http.ListenAndServe(":8000", router))
26+
}

api-mssql-go/docker/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM golang:1.14 as builder
2+
3+
ENV GO111MODULE=on
4+
5+
WORKDIR /app
6+
COPY go.mod .
7+
COPY go.sum .
8+
9+
RUN go mod download
10+
11+
COPY cmd ./cmd
12+
COPY internal ./internal
13+
14+
RUN ls /app/
15+
RUN CGO_ENABLED=0 GOOS=linux go build -v -a -o api-mssql-go ./cmd/api
16+
17+
FROM scratch
18+
WORKDIR /app
19+
COPY --from=builder /app/api-mssql-go /app/
20+
21+
EXPOSE 8000
22+
ENTRYPOINT ["/app/api-mssql-go"]

api-mssql-go/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/SAP-samples/kyma-runtime-extension-samples/api-mssql-go
2+
3+
go 1.14
4+
5+
require (
6+
github.com/denisenkom/go-mssqldb v0.0.0-20200910202707-1e08a3fab204
7+
github.com/gorilla/mux v1.8.0
8+
github.com/vrischmann/envconfig v1.3.0
9+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
10+
)

api-mssql-go/go.sum

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/denisenkom/go-mssqldb v0.0.0-20200910202707-1e08a3fab204 h1:tI48fqaIkxxYuIylVv1tdDfBp6836GKSfmmzgSyP1CY=
6+
github.com/denisenkom/go-mssqldb v0.0.0-20200910202707-1e08a3fab204/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
7+
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
8+
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
9+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
10+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
11+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
12+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
13+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
14+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
15+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
16+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
17+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
18+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
21+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
22+
github.com/vrischmann/envconfig v1.3.0 h1:4XIvQTXznxmWMnjouj0ST5lFo/WAYf5Exgl3x82crEk=
23+
github.com/vrischmann/envconfig v1.3.0/go.mod h1:bbvxFYJdRSpXrhS63mBFtKJzkDiNkyArOLXtY6q0kuI=
24+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
25+
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI=
26+
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
27+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
28+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
29+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
30+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
31+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
32+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
33+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
34+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
35+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
36+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
37+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
38+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
)
7+
8+
type orderCode struct {
9+
OrderCode string `json:"orderCode"`
10+
}
11+
12+
func (s *server) ConsumeOrderCode(w http.ResponseWriter, r *http.Request) {
13+
var order orderCode
14+
15+
defer r.Body.Close()
16+
err := json.NewDecoder(r.Body).Decode(&order)
17+
18+
if err != nil {
19+
http.Error(w, err.Error(), http.StatusInternalServerError)
20+
return
21+
}
22+
23+
_, err = s.db.AddOrder(order.OrderCode, "order received from event")
24+
25+
if err != nil {
26+
http.Error(w, err.Error(), http.StatusInternalServerError)
27+
return
28+
}
29+
30+
w.WriteHeader(http.StatusOK)
31+
}

0 commit comments

Comments
 (0)