Skip to content

Commit 8c6903c

Browse files
committed
Add Info endpoint
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 4b4ac8d commit 8c6903c

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

client.go

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"net/url"
1111
"path/filepath"
@@ -54,7 +54,7 @@ func (s *Client) GetNamespaces() ([]string, error) {
5454
defer res.Body.Close()
5555
}
5656

57-
bytesOut, err := ioutil.ReadAll(res.Body)
57+
bytesOut, err := io.ReadAll(res.Body)
5858
if err != nil {
5959
return namespaces, err
6060
}
@@ -104,7 +104,7 @@ func (s *Client) GetFunctions(namespace string) ([]types.FunctionStatus, error)
104104
defer res.Body.Close()
105105
}
106106

107-
body, _ := ioutil.ReadAll(res.Body)
107+
body, _ := io.ReadAll(res.Body)
108108

109109
functions := []types.FunctionStatus{}
110110
if err := json.Unmarshal(body, &functions); err != nil {
@@ -115,6 +115,40 @@ func (s *Client) GetFunctions(namespace string) ([]types.FunctionStatus, error)
115115
return functions, nil
116116
}
117117

118+
func (s *Client) GetInfo() (SystemInfo, error) {
119+
u := s.GatewayURL
120+
121+
u.Path = "/system/info"
122+
123+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
124+
if err != nil {
125+
return SystemInfo{}, fmt.Errorf("unable to create request for %s, error: %w", u.String(), err)
126+
}
127+
128+
if s.Credentials != nil {
129+
req.SetBasicAuth(s.Credentials.User, s.Credentials.Password)
130+
}
131+
132+
res, err := s.Client.Do(req)
133+
if err != nil {
134+
return SystemInfo{}, fmt.Errorf("unable to make HTTP request: %w", err)
135+
}
136+
137+
if res.Body != nil {
138+
defer res.Body.Close()
139+
}
140+
141+
body, _ := io.ReadAll(res.Body)
142+
143+
info := SystemInfo{}
144+
if err := json.Unmarshal(body, &info); err != nil {
145+
return SystemInfo{},
146+
fmt.Errorf("unable to unmarshal value: %q, error: %w", string(body), err)
147+
}
148+
149+
return info, nil
150+
}
151+
118152
// GetFunction gives a richer payload than GetFunctions, but for a specific function
119153
func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment, error) {
120154
u := s.GatewayURL
@@ -145,7 +179,7 @@ func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment,
145179
defer res.Body.Close()
146180
}
147181

148-
body, _ := ioutil.ReadAll(res.Body)
182+
body, _ := io.ReadAll(res.Body)
149183

150184
functions := types.FunctionDeployment{}
151185
if err := json.Unmarshal(body, &functions); err != nil {
@@ -157,6 +191,15 @@ func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment,
157191
}
158192

159193
func (s *Client) Deploy(spec types.FunctionDeployment) (int, error) {
194+
return s.deploy(http.MethodPost, spec)
195+
196+
}
197+
198+
func (s *Client) Update(spec types.FunctionDeployment) (int, error) {
199+
return s.deploy(http.MethodPut, spec)
200+
}
201+
202+
func (s *Client) deploy(method string, spec types.FunctionDeployment) (int, error) {
160203

161204
bodyBytes, err := json.Marshal(spec)
162205
if err != nil {
@@ -168,7 +211,7 @@ func (s *Client) Deploy(spec types.FunctionDeployment) (int, error) {
168211
u := s.GatewayURL
169212
u.Path = "/system/functions"
170213

171-
req, err := http.NewRequest(http.MethodPost, u.String(), bodyReader)
214+
req, err := http.NewRequest(method, u.String(), bodyReader)
172215
if err != nil {
173216
return http.StatusBadGateway, err
174217
}
@@ -185,7 +228,7 @@ func (s *Client) Deploy(spec types.FunctionDeployment) (int, error) {
185228
var body []byte
186229
if res.Body != nil {
187230
defer res.Body.Close()
188-
body, _ = ioutil.ReadAll(res.Body)
231+
body, _ = io.ReadAll(res.Body)
189232
}
190233

191234
if res.StatusCode != http.StatusAccepted {
@@ -249,7 +292,7 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
249292

250293
default:
251294
var err error
252-
bytesOut, err := ioutil.ReadAll(res.Body)
295+
bytesOut, err := io.ReadAll(res.Body)
253296
if err != nil {
254297
return err
255298
}

types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package sdk
2+
3+
import "github.com/openfaas/faas-provider/types"
4+
5+
type SystemInfo struct {
6+
Arch string `json:"arch,omitempty"`
7+
Provider Provider `json:"provider,omitempty"`
8+
Version types.VersionInfo `json:"version,omitempty"`
9+
}
10+
11+
type Provider struct {
12+
Provider string `json:"provider,omitempty"`
13+
Version types.VersionInfo `json:"version,omitempty"`
14+
Orchestration string `json:"orchestration,omitempty"`
15+
}

0 commit comments

Comments
 (0)