|
| 1 | +package main |
| 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 main() { |
| 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 | + fns, err := client.GetFunctions(context.Background(), "openfaas-fn") |
| 29 | + if err != nil { |
| 30 | + log.Printf("Get Failed: %s", err) |
| 31 | + } |
| 32 | + fmt.Printf("No Of Functions: %d\n", len(fns)) |
| 33 | + |
| 34 | + status, err := client.Deploy(context.Background(), types.FunctionDeployment{ |
| 35 | + Service: "env-store-test", |
| 36 | + Image: "ghcr.io/openfaas/alpine:latest", |
| 37 | + Namespace: "openfaas-fn", |
| 38 | + EnvProcess: "env", |
| 39 | + Labels: &map[string]string{ |
| 40 | + "purpose": "test", |
| 41 | + }, |
| 42 | + }) |
| 43 | + // non 200 status value will have some error |
| 44 | + if err != nil { |
| 45 | + log.Printf("Status: %d Deploy Failed: %s", status, err) |
| 46 | + } |
| 47 | + |
| 48 | + fns, err = client.GetFunctions(context.Background(), "openfaas-fn") |
| 49 | + if err != nil { |
| 50 | + log.Printf("Get Failed: %s", err) |
| 51 | + } |
| 52 | + fmt.Printf("No Of Functions: %d\n", len(fns)) |
| 53 | + |
| 54 | + err = client.DeleteFunction(context.Background(), "env-store-test", "openfaas-fn") |
| 55 | + // non 200 status value will have some error |
| 56 | + if err != nil { |
| 57 | + log.Printf("Delete Failed: %s", err) |
| 58 | + } |
| 59 | +} |
0 commit comments