-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathopenfaas.go
53 lines (46 loc) · 1.62 KB
/
openfaas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gofaas
import (
"encoding/base64"
"fmt"
"os"
"strings"
)
// GetRequestDefinition will return request definition with given method, path and URL
func GetRequestDefinition(cli *OpenFaasClient, method, path string) *FaasRequestDefinition {
cli.Method = method
cli.Path = path // path expects a trailing slash
cli.URL = cli.GatewayAddress + cli.Path
return &cli.FaasRequestDefinition
}
// GetGatewayAddress will return gateway address
func GetGatewayAddress(gateway string) string {
if gateway == "" {
gateway = os.Getenv("OPENFAAS_GATEWAY_ADDR")
}
// remove leading slash if any
if gateway[len(gateway)-1:] == "/" {
gateway = strings.TrimRight(gateway, "/")
}
return gateway
}
// setClientRequestOpts will return request definition with given credentials and request headers
func setClientRequestOpts(creds *FaasGatewayCredentials) FaasRequestDefinition {
userAndPassword := creds.Username + ":" + creds.Password
encodedAuth := base64.StdEncoding.EncodeToString([]byte(userAndPassword))
requestHeaders := map[string]string{
"Accept": "application/json",
"Authorization": "Basic " + encodedAuth,
}
return FaasRequestDefinition{
GatewayAddress: GetGatewayAddress(creds.GatewayAddress),
Headers: requestHeaders,
ClusterType: creds.ClusterType,
}
}
// NewClient will return a new client with given credentials
func NewClient(creds *FaasGatewayCredentials) (*OpenFaasClient, error) {
if creds.ClusterType != "swarm" && creds.ClusterType != "kubernetes" {
return &OpenFaasClient{}, fmt.Errorf("invalid cluster type %v", creds.ClusterType)
}
return &OpenFaasClient{setClientRequestOpts(creds)}, nil
}