Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for all go sdk operations #27

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ if err != nil {
}
```

Please refer [examples](https://github.com/openfaas/go-sdk/tree/master/examples) folder for code examples of each operation

## License

License: MIT
17 changes: 17 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Examples
Folder containes examples about how to use [openfaas](https://www.openfaas.com/) `go-sdk` API for different supported operations. Each subfolder contains a `main.go` file which can be executed independently. Before running them, please expose relevant environment variables such as `OPENFAAS_USERNAME`, `OPENFAAS_PASSWORD` and `OPENFAAS_GATEWAY_URL`. Do note, these are not standard environment variables. You can change them in your usecase.

Below is list of examples
1. [Deploy Function](./deploy-function/main.go)
2. [Update Function](./update-function/main.go)
3. [Scale Function](./scale-function/main.go)
4. [Get All Functions Of A Namespace](./get-functions/main.go)
5. [Create Namespace](./create-namespace/main.go)
6. [Update Namespace](./update-namespace/main.go)
7. [Get All Namespaces](./get-namepsaces/main.go)
8. [Create Secret](./create-secret/main.go)
9. [Update Secret](./update-secret/main.go)
10. [Get Logs Of A Function](./logs/main.go)



60 changes: 60 additions & 0 deletions example/create-namespace/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"time"

"github.com/openfaas/faas-provider/types"
"github.com/openfaas/go-sdk"
)

func main() {
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
username := os.Getenv("OPENFAAS_USERNAME")
password := os.Getenv("OPENFAAS_PASSWORD")

gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
auth := &sdk.BasicAuth{
Username: username,
Password: password,
}

client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)

status, err := client.CreateNamespace(context.Background(), types.FunctionNamespace{
Name: "test-namespace",
Labels: map[string]string{
"env": "dev",
},
Annotations: map[string]string{
"imageregistry": "https://hub.docker.com/",
},
})
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Status: %d Create Failed: %s", status, err)
os.Exit(1)
}

fmt.Println("Wait for 15 seconds....")
fmt.Println("Get Namespace")
time.Sleep(15 * time.Second)
ns, err := client.GetNamespace(context.Background(), "test-namespace")
if err != nil {
fmt.Fprintf(os.Stderr, "Get Failed: %s", err)
os.Exit(1)
}
fmt.Printf("Namespace: %v \n", ns)

// delete namespace
err = client.DeleteNamespace(context.Background(), "test-namespace")
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Delete Failed: %s", err)
os.Exit(1)
}
}
59 changes: 59 additions & 0 deletions example/create-secret/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"

"github.com/openfaas/faas-provider/types"
"github.com/openfaas/go-sdk"
)

func main() {
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
username := os.Getenv("OPENFAAS_USERNAME")
password := os.Getenv("OPENFAAS_PASSWORD")

gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
auth := &sdk.BasicAuth{
Username: username,
Password: password,
}

client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)

status, err := client.CreateSecret(context.Background(), types.Secret{
Name: "env-store-test",
Namespace: "openfaas-fn",
// secret support both binary and string values
// Use Value field to store string values
RawValue: []byte("this is secret"),
})
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Status: %d Create Failed: %s", status, err)
os.Exit(1)
}

// Get Secrets
secrets, err := client.GetSecrets(context.Background(), "openfaas-fn")
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Get Failed: %s", err)
os.Exit(1)
}

for _, s := range secrets {
fmt.Printf("Secret: %v \n", s)
}

err = client.DeleteSecret(context.Background(), "env-store-test", "openfaas-fn")
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Delete Failed: %s", err)
os.Exit(1)
}

}
60 changes: 60 additions & 0 deletions example/deploy-function/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"time"

"github.com/openfaas/faas-provider/types"
"github.com/openfaas/go-sdk"
)

func main() {
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
username := os.Getenv("OPENFAAS_USERNAME")
password := os.Getenv("OPENFAAS_PASSWORD")

gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
auth := &sdk.BasicAuth{
Username: username,
Password: password,
}

client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)

status, err := client.Deploy(context.Background(), types.FunctionDeployment{
Service: "env-store-test",
Image: "ghcr.io/openfaas/alpine:latest",
Namespace: "openfaas-fn",
EnvProcess: "env",
Labels: &map[string]string{
"purpose": "test",
},
})
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Status: %d Deploy Failed: %s", status, err)
os.Exit(1)
}

fmt.Println("Wait for 15 seconds....")
fmt.Println("Get Function")
time.Sleep(15 * time.Second)
fn, err := client.GetFunction(context.Background(), "env-store-test", "openfaas-fn")
if err != nil {
fmt.Fprintf(os.Stderr, "Get Failed: %s", err)
os.Exit(1)
}
fmt.Printf("Function: %v \n", fn)

// delete function
err = client.DeleteFunction(context.Background(), "env-store-test", "openfaas-fn")
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Delete Failed: %s", err)
os.Exit(1)
}
}
62 changes: 62 additions & 0 deletions example/get-functions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"

"github.com/openfaas/faas-provider/types"
"github.com/openfaas/go-sdk"
)

func main() {
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
username := os.Getenv("OPENFAAS_USERNAME")
password := os.Getenv("OPENFAAS_PASSWORD")

gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
auth := &sdk.BasicAuth{
Username: username,
Password: password,
}

client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)

fns, err := client.GetFunctions(context.Background(), "openfaas-fn")
if err != nil {
fmt.Fprintf(os.Stderr, "Get Failed: %s", err)
os.Exit(1)
}
fmt.Printf("No Of Functions: %d\n", len(fns))

status, err := client.Deploy(context.Background(), types.FunctionDeployment{
Service: "env-store-test",
Image: "ghcr.io/openfaas/alpine:latest",
Namespace: "openfaas-fn",
EnvProcess: "env",
Labels: &map[string]string{
"purpose": "test",
},
})
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Status: %d Deploy Failed: %s", status, err)
os.Exit(1)
}

fns, err = client.GetFunctions(context.Background(), "openfaas-fn")
if err != nil {
fmt.Fprintf(os.Stderr, "Get Failed: %s", err)
os.Exit(1)
}
fmt.Printf("No Of Functions: %d\n", len(fns))

err = client.DeleteFunction(context.Background(), "env-store-test", "openfaas-fn")
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Delete Failed: %s", err)
os.Exit(1)
}
}
57 changes: 57 additions & 0 deletions example/get-namepsaces/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"

"github.com/openfaas/faas-provider/types"
"github.com/openfaas/go-sdk"
)

func main() {
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
username := os.Getenv("OPENFAAS_USERNAME")
password := os.Getenv("OPENFAAS_PASSWORD")

gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
auth := &sdk.BasicAuth{
Username: username,
Password: password,
}

client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)

ns, err := client.GetNamespaces(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "Get Failed: %s", err)
os.Exit(1)
}
fmt.Printf("No Of Namespaces: %d\n", len(ns))

status, err := client.CreateNamespace(context.Background(), types.FunctionNamespace{
Name: "test-namespace",
})
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Status: %d Create Failed: %s", status, err)
os.Exit(1)
}

ns, err = client.GetNamespaces(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "Get Failed: %s", err)
os.Exit(1)
}
fmt.Printf("No Of Namespaces: %d\n", len(ns))

// delete namespace
err = client.DeleteNamespace(context.Background(), "test-namespace")
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Delete Failed: %s", err)
os.Exit(1)
}
}
58 changes: 58 additions & 0 deletions example/logs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"time"

"github.com/openfaas/faas-provider/types"
"github.com/openfaas/go-sdk"
)

func main() {
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
username := os.Getenv("OPENFAAS_USERNAME")
password := os.Getenv("OPENFAAS_PASSWORD")

gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
auth := &sdk.BasicAuth{
Username: username,
Password: password,
}

client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)

// Deploy function
status, err := client.Deploy(context.Background(), types.FunctionDeployment{
Service: "env-store-test",
Image: "ghcr.io/openfaas/alpine:latest",
Namespace: "openfaas-fn",
EnvProcess: "env",
})
// non 200 status value will have some error
if err != nil {
fmt.Fprintf(os.Stderr, "Status: %d Deploy Failed: %s", status, err)
os.Exit(1)
}

// Follow is allows the user to request a stream of logs until the timeout
follow := false
// Tail sets the maximum number of log messages to return, <=0 means unlimited
tail := 5
// Since is the optional datetime value to start the logs from
since := time.Now().Add(-30 * time.Second)

logsChan, err := client.GetLogs(context.Background(), "env-store-test", "openfaas-fn", follow, tail, &since)
if err != nil {
fmt.Fprintf(os.Stderr, "Get Logs Failed: %s", err)
os.Exit(1)
}

fmt.Println("Logs Received....")
for line := range logsChan {
fmt.Println(line)
}
}
Loading
Loading