Skip to content

Commit 4af1e4c

Browse files
nitishkumar71alexellis
authored andcommitted
add examples for all go sdk operations
Signed-off-by: Nitishkumar Singh <[email protected]>
1 parent 74fdcec commit 4af1e4c

File tree

5 files changed

+453
-0
lines changed

5 files changed

+453
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ if err != nil {
141141
}
142142
```
143143

144+
Please refer [examples](https://github.com/openfaas/go-sdk/tree/master/examples) folder for code examples of each operation
145+
144146
## License
145147

146148
License: MIT

examples/function_examples.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package examples
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
11+
"github.com/openfaas/faas-provider/types"
12+
"github.com/openfaas/go-sdk"
13+
)
14+
15+
func DeployFunction() {
16+
17+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
18+
username := os.Getenv("OPENFAAS_USERNAME")
19+
password := os.Getenv("OPENFAAS_PASSWORD")
20+
21+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
22+
auth := &sdk.BasicAuth{
23+
Username: username,
24+
Password: password,
25+
}
26+
27+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
28+
29+
status, err := client.Deploy(context.Background(), types.FunctionDeployment{
30+
Service: "env-store-test",
31+
Image: "ghcr.io/openfaas/alpine:latest",
32+
Namespace: "openfaas-fn",
33+
EnvProcess: "env",
34+
})
35+
36+
// non 200 status value will have some error
37+
if err != nil {
38+
log.Printf("Status: %d Deploy Failed: %s", status, err)
39+
}
40+
}
41+
42+
func GetFunction() {
43+
44+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
45+
username := os.Getenv("OPENFAAS_USERNAME")
46+
password := os.Getenv("OPENFAAS_PASSWORD")
47+
48+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
49+
auth := &sdk.BasicAuth{
50+
Username: username,
51+
Password: password,
52+
}
53+
54+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
55+
56+
fn, err := client.GetFunction(context.Background(), "env-store-test", "openfaas-fn")
57+
if err != nil {
58+
log.Printf("Get Failed: %s", err)
59+
}
60+
61+
fmt.Printf("Function: %v \n", fn)
62+
}
63+
64+
func GetFunctions() {
65+
66+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
67+
username := os.Getenv("OPENFAAS_USERNAME")
68+
password := os.Getenv("OPENFAAS_PASSWORD")
69+
70+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
71+
auth := &sdk.BasicAuth{
72+
Username: username,
73+
Password: password,
74+
}
75+
76+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
77+
78+
fns, err := client.GetFunctions(context.Background(), "openfaas-fn")
79+
if err != nil {
80+
log.Printf("Get Failed: %s", err)
81+
}
82+
83+
for _, fn := range fns {
84+
fmt.Printf("Function: %v \n", fn)
85+
}
86+
}
87+
88+
func UpdateFunction() {
89+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
90+
username := os.Getenv("OPENFAAS_USERNAME")
91+
password := os.Getenv("OPENFAAS_PASSWORD")
92+
93+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
94+
auth := &sdk.BasicAuth{
95+
Username: username,
96+
Password: password,
97+
}
98+
99+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
100+
101+
status, err := client.Update(context.Background(), types.FunctionDeployment{
102+
Service: "env-store-test",
103+
Image: "ghcr.io/openfaas/alpine:latest",
104+
Namespace: "openfaas-fn",
105+
EnvProcess: "env",
106+
})
107+
108+
// non 200 status value will have some error
109+
if err != nil {
110+
log.Printf("Status: %d Update Failed: %s", status, err)
111+
}
112+
}
113+
114+
func ScaleFunction() {
115+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
116+
username := os.Getenv("OPENFAAS_USERNAME")
117+
password := os.Getenv("OPENFAAS_PASSWORD")
118+
119+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
120+
auth := &sdk.BasicAuth{
121+
Username: username,
122+
Password: password,
123+
}
124+
125+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
126+
127+
err := client.ScaleFunction(context.Background(), "env-store-test", "openfaas-fn", uint64(2))
128+
129+
// non 200 status value will have some error
130+
if err != nil {
131+
log.Printf("Scale Failed: %s", err)
132+
}
133+
}
134+
135+
func DeleteFunction() {
136+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
137+
username := os.Getenv("OPENFAAS_USERNAME")
138+
password := os.Getenv("OPENFAAS_PASSWORD")
139+
140+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
141+
auth := &sdk.BasicAuth{
142+
Username: username,
143+
Password: password,
144+
}
145+
146+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
147+
148+
err := client.DeleteFunction(context.Background(), "env-store-test", "openfaas-fn")
149+
150+
// non 200 status value will have some error
151+
if err != nil {
152+
log.Printf("Delete Failed: %s", err)
153+
}
154+
}

examples/logs_example.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package examples
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
"time"
11+
12+
"github.com/openfaas/go-sdk"
13+
)
14+
15+
func GetLogs() {
16+
17+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
18+
username := os.Getenv("OPENFAAS_USERNAME")
19+
password := os.Getenv("OPENFAAS_PASSWORD")
20+
21+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
22+
auth := &sdk.BasicAuth{
23+
Username: username,
24+
Password: password,
25+
}
26+
27+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
28+
29+
// Follow is allows the user to request a stream of logs until the timeout
30+
follow := false
31+
// Tail sets the maximum number of log messages to return, <=0 means unlimited
32+
tail := 5
33+
// Since is the optional datetime value to start the logs from
34+
since := time.Now().Add(-30 * time.Second)
35+
36+
logsChan, err := client.GetLogs(context.Background(), "env-store-test", "openfaas-fn", follow, tail, &since)
37+
if err != nil {
38+
log.Printf("Get Logs Failed: %s", err)
39+
}
40+
41+
fmt.Println("Logs Received....")
42+
for line := range logsChan {
43+
fmt.Println(line)
44+
}
45+
46+
}

examples/namespace_examples.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package examples
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
11+
"github.com/openfaas/faas-provider/types"
12+
"github.com/openfaas/go-sdk"
13+
)
14+
15+
func CreateNamespace() {
16+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
17+
username := os.Getenv("OPENFAAS_USERNAME")
18+
password := os.Getenv("OPENFAAS_PASSWORD")
19+
20+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
21+
auth := &sdk.BasicAuth{
22+
Username: username,
23+
Password: password,
24+
}
25+
26+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
27+
28+
status, err := client.CreateNamespace(context.Background(), types.FunctionNamespace{
29+
Name: "test-namespace",
30+
Labels: map[string]string{
31+
"env": "dev",
32+
},
33+
Annotations: map[string]string{
34+
"imageregistry": "https://hub.docker.com/",
35+
},
36+
})
37+
38+
// non 200 status value will have some error
39+
if err != nil {
40+
log.Printf("Status: %d Create Failed: %s", status, err)
41+
}
42+
}
43+
44+
func UpdateNamespace() {
45+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
46+
username := os.Getenv("OPENFAAS_USERNAME")
47+
password := os.Getenv("OPENFAAS_PASSWORD")
48+
49+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
50+
auth := &sdk.BasicAuth{
51+
Username: username,
52+
Password: password,
53+
}
54+
55+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
56+
57+
status, err := client.UpdateNamespace(context.Background(), types.FunctionNamespace{
58+
Name: "test-namespace",
59+
Labels: map[string]string{
60+
"env": "dev",
61+
},
62+
Annotations: map[string]string{
63+
"imageregistry": "https://private.registry.com/",
64+
},
65+
})
66+
67+
// non 200 status value will have some error
68+
if err != nil {
69+
log.Printf("Status: %d Update Failed: %s", status, err)
70+
}
71+
}
72+
73+
func GetNamespace() {
74+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
75+
username := os.Getenv("OPENFAAS_USERNAME")
76+
password := os.Getenv("OPENFAAS_PASSWORD")
77+
78+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
79+
auth := &sdk.BasicAuth{
80+
Username: username,
81+
Password: password,
82+
}
83+
84+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
85+
86+
ns, err := client.GetNamespace(context.Background(), "test-namespace")
87+
if err != nil {
88+
log.Printf("Get Failed: %s", err)
89+
}
90+
91+
fmt.Printf("Namespace: %v \n", ns)
92+
}
93+
94+
func GetNamespaces() {
95+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
96+
username := os.Getenv("OPENFAAS_USERNAME")
97+
password := os.Getenv("OPENFAAS_PASSWORD")
98+
99+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
100+
auth := &sdk.BasicAuth{
101+
Username: username,
102+
Password: password,
103+
}
104+
105+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
106+
107+
namespaces, err := client.GetNamespaces(context.Background())
108+
if err != nil {
109+
log.Printf("Get Failed: %s", err)
110+
}
111+
112+
for _, ns := range namespaces {
113+
fmt.Printf("Namespace: %v \n", ns)
114+
}
115+
}
116+
117+
func DeleteNamespace() {
118+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
119+
username := os.Getenv("OPENFAAS_USERNAME")
120+
password := os.Getenv("OPENFAAS_PASSWORD")
121+
122+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
123+
auth := &sdk.BasicAuth{
124+
Username: username,
125+
Password: password,
126+
}
127+
128+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
129+
130+
err := client.DeleteNamespace(context.Background(), "test-namespace")
131+
132+
// non 200 status value will have some error
133+
if err != nil {
134+
log.Printf("Delete Failed: %s", err)
135+
}
136+
}

0 commit comments

Comments
 (0)