Skip to content

Commit ec002af

Browse files
viveksynghalexellis
authored andcommitted
Update GetSystemInfo API return type
This updates the GetSystemInfo API to return typed struct rather than `map[string]interface{}` It also removes support for legacy vesion info response from gateway which are available prior to version 0.8.4 Signed-off-by: Vivek Singh <[email protected]>
1 parent a1394b0 commit ec002af

File tree

8 files changed

+393
-95
lines changed

8 files changed

+393
-95
lines changed

commands/version.go

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,20 @@ func printServerVersions() error {
104104
if err != nil {
105105
return err
106106
}
107-
info, err := cliClient.GetSystemInfo(context.Background())
107+
gatewayInfo, err := cliClient.GetSystemInfo(context.Background())
108108
if err != nil {
109109
return err
110110
}
111111

112-
version, sha := getGatewayDetails(info)
112+
printGatewayDetails(gatewayAddress, gatewayInfo.Version.Release, gatewayInfo.Version.SHA)
113113

114-
printGatewayDetails(gatewayAddress, version, sha)
115-
116-
name, orchestration, sha, version := getProviderDetails(info)
117114
fmt.Printf(`
118115
Provider
119116
name: %s
120117
orchestration: %s
121118
version: %s
122119
sha: %s
123-
`, name, orchestration, version, sha)
120+
`, gatewayInfo.Provider.Name, gatewayInfo.Provider.Orchestration, gatewayInfo.Provider.Version.Release, gatewayInfo.Provider.Version.SHA)
124121
return nil
125122
}
126123

@@ -148,66 +145,6 @@ func printLogo() {
148145
fmt.Printf(figletColoured)
149146
}
150147

151-
func getGatewayDetails(m map[string]interface{}) (version, sha string) {
152-
if _, ok := m["orchestration"]; !ok {
153-
v := m["version"].(map[string]interface{})
154-
version, ok = v["release"].(string)
155-
if !ok {
156-
version = ""
157-
}
158-
sha, ok = v["sha"].(string)
159-
if !ok {
160-
sha = ""
161-
}
162-
}
163-
164-
return
165-
}
166-
167-
func getProviderDetails(m map[string]interface{}) (name, orchestration, sha, version string) {
168-
if k, ok := m["provider"]; ok {
169-
if kv, ok := k.(map[string]interface{}); ok {
170-
name, orchestration, sha, version = getProviderDetailsCurrent(kv)
171-
} else {
172-
name, orchestration, sha, version = getProviderDetailsLegacy(m)
173-
}
174-
}
175-
176-
return
177-
}
178-
179-
func getProviderDetailsLegacy(m map[string]interface{}) (name, orchestration, sha, version string) {
180-
name = m["provider"].(string)
181-
orchestration = m["orchestration"].(string)
182-
v := m["version"].(map[string]interface{})
183-
version = v["release"].(string)
184-
sha = v["sha"].(string)
185-
186-
return
187-
}
188-
189-
func getProviderDetailsCurrent(m map[string]interface{}) (name, orchestration, sha, version string) {
190-
v := m["version"].(map[string]interface{})
191-
version, ok := v["release"].(string)
192-
if !ok {
193-
version = ""
194-
}
195-
sha, ok = v["sha"].(string)
196-
if !ok {
197-
sha = ""
198-
}
199-
name, ok = m["provider"].(string)
200-
if !ok {
201-
name = ""
202-
}
203-
orchestration, ok = m["orchestration"].(string)
204-
if !ok {
205-
orchestration = ""
206-
}
207-
208-
return
209-
}
210-
211148
const figletStr = ` ___ _____ ____
212149
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
213150
| | | | '_ \ / _ \ '_ \| |_ / _` + "`" + ` |/ _` + "`" + ` \___ \

commands/version_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,6 @@ func Test_gateway_and_provider_information(t *testing.T) {
112112
{"provider sha", "sha: "},
113113
},
114114
},
115-
{
116-
responseBody: gateway_response_prior_to_0_8_4,
117-
params: []struct {
118-
name string
119-
value string
120-
}{
121-
{"provider name", "name: faas-swarm"},
122-
{"provider orchestration", "orchestration: swarm"},
123-
{"provider version", "version: provider-0.3.3"},
124-
{"provider sha", "sha: c890cba302d059de8edbef3f3de7fe15444b1ecf"},
125-
},
126-
},
127115
}
128116

129117
for _, testCase := range testCases {
@@ -147,14 +135,6 @@ func Test_gateway_uri(t *testing.T) {
147135
}
148136
}
149137

150-
func Test_gateway_uri_prior_to_0_8_4(t *testing.T) {
151-
stdOut, gatewayUri := executeVersionCmd(t, gateway_response_prior_to_0_8_4)
152-
153-
if found, err := regexp.MatchString(fmt.Sprintf(`(?m:uri: %s)`, gatewayUri), stdOut); err != nil || !found {
154-
t.Fatalf("Gateway uri is not as expected - want: %s, got:\n%s", gatewayUri, stdOut)
155-
}
156-
}
157-
158138
func Test_gateway_details_prior_to_0_8_4_should_not_be_displayed(t *testing.T) {
159139
stdOut, _ := executeVersionCmd(t, gateway_response_prior_to_0_8_4)
160140

proxy/version.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,46 @@ import (
99
"fmt"
1010
"io/ioutil"
1111
"net/http"
12+
13+
"github.com/openfaas/faas/gateway/types"
1214
)
1315

1416
//GetSystemInfo get system information from /system/info endpoint
15-
func (c *Client) GetSystemInfo(ctx context.Context) (map[string]interface{}, error) {
17+
func (c *Client) GetSystemInfo(ctx context.Context) (types.GatewayInfo, error) {
1618
infoEndPoint := "/system/info"
19+
var info types.GatewayInfo
20+
1721
req, err := c.newRequest(http.MethodGet, infoEndPoint, nil)
1822
if err != nil {
19-
return nil, fmt.Errorf("invalid HTTP method or invalid URL")
23+
return info, fmt.Errorf("invalid HTTP method or invalid URL")
2024
}
2125

2226
response, err := c.doRequest(ctx, req)
2327
if err != nil {
24-
return nil, fmt.Errorf("cannot connect to OpenFaaS on URL: %s", c.GatewayURL.String())
28+
return info, fmt.Errorf("cannot connect to OpenFaaS on URL: %s", c.GatewayURL.String())
2529
}
2630

2731
if response.Body != nil {
2832
defer response.Body.Close()
2933
}
3034

31-
info := make(map[string]interface{})
32-
3335
switch response.StatusCode {
3436
case http.StatusOK:
3537
bytesOut, err := ioutil.ReadAll(response.Body)
3638
if err != nil {
37-
return nil, fmt.Errorf("cannot read result from OpenFaaS on URL: %s", c.GatewayURL.String())
39+
return info, fmt.Errorf("cannot read result from OpenFaaS on URL: %s", c.GatewayURL.String())
3840
}
3941
err = json.Unmarshal(bytesOut, &info)
4042
if err != nil {
41-
return nil, fmt.Errorf("cannot parse result from OpenFaaS on URL: %s\n%s", c.GatewayURL.String(), err.Error())
43+
return info, fmt.Errorf("cannot parse result from OpenFaaS on URL: %s\n%s", c.GatewayURL.String(), err.Error())
4244
}
4345

4446
case http.StatusUnauthorized:
45-
return nil, fmt.Errorf("unauthorized access, run \"faas-cli login\" to setup authentication for this server")
47+
return info, fmt.Errorf("unauthorized access, run \"faas-cli login\" to setup authentication for this server")
4648
default:
4749
bytesOut, err := ioutil.ReadAll(response.Body)
4850
if err == nil {
49-
return nil, fmt.Errorf("server returned unexpected status code: %d - %s", response.StatusCode, string(bytesOut))
51+
return info, fmt.Errorf("server returned unexpected status code: %d - %s", response.StatusCode, string(bytesOut))
5052
}
5153
}
5254

vendor/github.com/openfaas/faas/gateway/types/handler_set.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/openfaas/faas/gateway/types/inforequest.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/openfaas/faas/gateway/types/proxy_client.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)