Skip to content

Commit 4eaf84d

Browse files
impel rest-client/controllers
1 parent fdc81e0 commit 4eaf84d

20 files changed

+446
-387
lines changed
Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package client
22

3-
43
import (
5-
"testing"
6-
"regexp"
7-
"strings"
4+
"encoding/json"
85
"net/http"
96
"net/http/httptest"
10-
"encoding/json"
117
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
1211

1312
"github.com/google/go-cmp/cmp"
1413
)
@@ -23,41 +22,41 @@ func TestAlerts(t *testing.T) {
2322
json.Unmarshal(ropen, &aopen)
2423
json.Unmarshal(rall, &aall)
2524
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26-
urlall := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/alerts$`)
27-
urlopen := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/alerts\?filter=state%3D%27open%27$`)
28-
if r.URL.Path == "/api/api_version" {
29-
w.Header().Set("Content-Type", "application/json")
30-
w.WriteHeader(http.StatusOK)
25+
urlall := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/alerts$`)
26+
urlopen := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/alerts\?filter=state%3D%27open%27$`)
27+
if r.URL.Path == "/api/api_version" {
28+
w.Header().Set("Content-Type", "application/json")
29+
w.WriteHeader(http.StatusOK)
3130
w.Write([]byte(vers))
32-
} else if urlopen.MatchString(r.URL.Path + "?" + r.URL.RawQuery) {
31+
} else if urlopen.MatchString(r.URL.Path + "?" + r.URL.RawQuery) {
3332
w.Header().Set("x-auth-token", "faketoken")
3433
w.Header().Set("Content-Type", "application/json")
3534
w.WriteHeader(http.StatusOK)
3635
w.Write([]byte(ropen))
37-
} else if urlall.MatchString(r.URL.Path) {
36+
} else if urlall.MatchString(r.URL.Path) {
3837
w.Header().Set("x-auth-token", "faketoken")
3938
w.Header().Set("Content-Type", "application/json")
4039
w.WriteHeader(http.StatusOK)
4140
w.Write([]byte(rall))
4241
}
43-
}))
44-
endp := strings.Split(server.URL, "/")
45-
e := endp[len(endp)-1]
46-
t.Run("alerts_open", func(t *testing.T) {
47-
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
48-
al := c.GetAlerts("state='open'")
49-
if diff := cmp.Diff(al.Items, aopen.Items); diff != "" {
50-
t.Errorf("Mismatch (-want +got):\n%s", diff)
51-
server.Close()
52-
}
53-
})
54-
t.Run("alerts_all", func(t *testing.T) {
55-
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
56-
al := c.GetAlerts("")
57-
if diff := cmp.Diff(al.Items, aall.Items); diff != "" {
58-
t.Errorf("Mismatch (-want +got):\n%s", diff)
59-
server.Close()
60-
}
61-
})
62-
server.Close()
42+
}))
43+
endp := strings.Split(server.URL, "/")
44+
e := endp[len(endp)-1]
45+
t.Run("alerts_open", func(t *testing.T) {
46+
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
47+
al := c.GetAlerts("state='open'")
48+
if diff := cmp.Diff(al.Items, aopen.Items); diff != "" {
49+
t.Errorf("Mismatch (-want +got):\n%s", diff)
50+
server.Close()
51+
}
52+
})
53+
t.Run("alerts_all", func(t *testing.T) {
54+
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
55+
al := c.GetAlerts("")
56+
if diff := cmp.Diff(al.Items, aall.Items); diff != "" {
57+
t.Errorf("Mismatch (-want +got):\n%s", diff)
58+
server.Close()
59+
}
60+
})
61+
server.Close()
6362
}
Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package client
22

3-
43
import (
5-
"testing"
6-
"regexp"
7-
"strings"
4+
"encoding/json"
85
"net/http"
96
"net/http/httptest"
10-
"encoding/json"
11-
"os"
7+
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
1211

1312
"github.com/google/go-cmp/cmp"
1413
)
@@ -17,29 +16,29 @@ func TestArraysPerformance(t *testing.T) {
1716

1817
res, _ := os.ReadFile("../../test/data/arrays_performance.json")
1918
vers, _ := os.ReadFile("../../test/data/versions.json")
20-
var arrsp ArraysPerformanceList
21-
json.Unmarshal(res, &arrsp)
19+
var arrsp ArraysPerformanceList
20+
json.Unmarshal(res, &arrsp)
2221
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23-
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/arrays/performance$`)
24-
if r.URL.Path == "/api/api_version" {
25-
w.Header().Set("Content-Type", "application/json")
26-
w.WriteHeader(http.StatusOK)
27-
w.Write([]byte(vers))
28-
} else if valid.MatchString(r.URL.Path) {
22+
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/arrays/performance$`)
23+
if r.URL.Path == "/api/api_version" {
24+
w.Header().Set("Content-Type", "application/json")
25+
w.WriteHeader(http.StatusOK)
26+
w.Write([]byte(vers))
27+
} else if valid.MatchString(r.URL.Path) {
2928
w.Header().Set("x-auth-token", "faketoken")
3029
w.Header().Set("Content-Type", "application/json")
3130
w.WriteHeader(http.StatusOK)
3231
w.Write([]byte(res))
3332
}
34-
}))
35-
endp := strings.Split(server.URL, "/")
36-
e := endp[len(endp)-1]
37-
t.Run("arrays_performance_1", func(t *testing.T) {
38-
defer server.Close()
39-
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
40-
apl := c.GetArraysPerformance()
41-
if diff := cmp.Diff(apl.Items[0], arrsp.Items[0]); diff != "" {
42-
t.Errorf("Mismatch (-want +got):\n%s", diff)
43-
}
44-
})
33+
}))
34+
endp := strings.Split(server.URL, "/")
35+
e := endp[len(endp)-1]
36+
t.Run("arrays_performance_1", func(t *testing.T) {
37+
defer server.Close()
38+
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
39+
apl := c.GetArraysPerformance()
40+
if diff := cmp.Diff(apl.Items[0], arrsp.Items[0]); diff != "" {
41+
t.Errorf("Mismatch (-want +got):\n%s", diff)
42+
}
43+
})
4544
}
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package client
22

3-
43
import (
5-
"testing"
6-
"regexp"
7-
"strings"
4+
"encoding/json"
85
"net/http"
96
"net/http/httptest"
10-
"encoding/json"
117
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
1211

1312
"github.com/google/go-cmp/cmp"
1413
)
@@ -20,26 +19,26 @@ func TestConnections(t *testing.T) {
2019
var conns ConnectionsList
2120
json.Unmarshal(res, &conns)
2221
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23-
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/connections$`)
24-
if r.URL.Path == "/api/api_version" {
25-
w.Header().Set("Content-Type", "application/json")
26-
w.WriteHeader(http.StatusOK)
22+
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/connections$`)
23+
if r.URL.Path == "/api/api_version" {
24+
w.Header().Set("Content-Type", "application/json")
25+
w.WriteHeader(http.StatusOK)
2726
w.Write([]byte(vers))
28-
} else if valid.MatchString(r.URL.Path) {
27+
} else if valid.MatchString(r.URL.Path) {
2928
w.Header().Set("x-auth-token", "faketoken")
3029
w.Header().Set("Content-Type", "application/json")
3130
w.WriteHeader(http.StatusOK)
3231
w.Write([]byte(res))
3332
}
34-
}))
35-
endp := strings.Split(server.URL, "/")
36-
e := endp[len(endp)-1]
37-
t.Run("connections_1", func(t *testing.T) {
38-
defer server.Close()
39-
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
40-
cl := c.GetConnections()
41-
if diff := cmp.Diff(cl.Items, conns.Items); diff != "" {
42-
t.Errorf("Mismatch (-want +got):\n%s", diff)
43-
}
44-
})
33+
}))
34+
endp := strings.Split(server.URL, "/")
35+
e := endp[len(endp)-1]
36+
t.Run("connections_1", func(t *testing.T) {
37+
defer server.Close()
38+
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
39+
cl := c.GetConnections()
40+
if diff := cmp.Diff(cl.Items, conns.Items); diff != "" {
41+
t.Errorf("Mismatch (-want +got):\n%s", diff)
42+
}
43+
})
4544
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package client
2+
3+
type Controllers struct {
4+
Name string `json:"name"`
5+
Mode string `json:"mode"`
6+
Model string `json:"model"`
7+
Status string `json:"status"`
8+
Type string `json:"type"`
9+
Version string `json:"version"`
10+
ModeSince int64 `json:"mode_since"`
11+
}
12+
13+
type ControllersList struct {
14+
ContinuationToken string `json:"continuation_token"`
15+
TotalItemCount int32 `json:"total_item_count"`
16+
MoreItemsRemaining bool `json:"more_items_remaining"`
17+
Items []Controllers `json:"items"`
18+
}
19+
20+
func (fa *FAClient) GetControllers() *ControllersList {
21+
uri := "/controllers"
22+
result := new(ControllersList)
23+
res, _ := fa.RestClient.R().
24+
SetResult(&result).
25+
Get(uri)
26+
if res.StatusCode() == 401 {
27+
fa.RefreshSession()
28+
fa.RestClient.R().
29+
SetResult(&result).
30+
Get(uri)
31+
}
32+
return result
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
13+
)
14+
15+
func TestControllers(t *testing.T) {
16+
17+
res, _ := os.ReadFile("../../test/data/controllers.json")
18+
vers, _ := os.ReadFile("../../test/data/versions.json")
19+
var cont ControllersList
20+
json.Unmarshal(res, &cont)
21+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/controllers$`)
23+
if r.URL.Path == "/api/api_version" {
24+
w.Header().Set("Content-Type", "application/json")
25+
w.WriteHeader(http.StatusOK)
26+
w.Write([]byte(vers))
27+
} else if valid.MatchString(r.URL.Path) {
28+
w.Header().Set("x-auth-token", "faketoken")
29+
w.Header().Set("Content-Type", "application/json")
30+
w.WriteHeader(http.StatusOK)
31+
w.Write([]byte(res))
32+
}
33+
}))
34+
endp := strings.Split(server.URL, "/")
35+
e := endp[len(endp)-1]
36+
t.Run("controllers_1", func(t *testing.T) {
37+
defer server.Close()
38+
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
39+
cl := c.GetControllers()
40+
if diff := cmp.Diff(cl.Items, cont.Items); diff != "" {
41+
t.Errorf("Mismatch (-want +got):\n%s", diff)
42+
}
43+
})
44+
}
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package client
22

3-
43
import (
5-
"testing"
6-
"regexp"
7-
"strings"
4+
"encoding/json"
85
"net/http"
96
"net/http/httptest"
10-
"encoding/json"
117
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
1211

1312
"github.com/google/go-cmp/cmp"
1413
)
@@ -20,26 +19,26 @@ func TestDirectoriesPerformance(t *testing.T) {
2019
var dirsp DirectoriesPerformanceList
2120
json.Unmarshal(res, &dirsp)
2221
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23-
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/directories/performance$`)
24-
if r.URL.Path == "/api/api_version" {
25-
w.Header().Set("Content-Type", "application/json")
26-
w.WriteHeader(http.StatusOK)
22+
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/directories/performance$`)
23+
if r.URL.Path == "/api/api_version" {
24+
w.Header().Set("Content-Type", "application/json")
25+
w.WriteHeader(http.StatusOK)
2726
w.Write([]byte(vers))
28-
} else if valid.MatchString(r.URL.Path) {
27+
} else if valid.MatchString(r.URL.Path) {
2928
w.Header().Set("x-auth-token", "faketoken")
3029
w.Header().Set("Content-Type", "application/json")
3130
w.WriteHeader(http.StatusOK)
3231
w.Write([]byte(res))
3332
}
34-
}))
35-
endp := strings.Split(server.URL, "/")
36-
e := endp[len(endp)-1]
37-
t.Run("directories_performance_1", func(t *testing.T) {
38-
defer server.Close()
39-
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
40-
dpl := c.GetDirectoriesPerformance()
41-
if diff := cmp.Diff(dpl.Items, dirsp.Items); diff != "" {
42-
t.Errorf("Mismatch (-want +got):\n%s", diff)
43-
}
44-
})
33+
}))
34+
endp := strings.Split(server.URL, "/")
35+
e := endp[len(endp)-1]
36+
t.Run("directories_performance_1", func(t *testing.T) {
37+
defer server.Close()
38+
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
39+
dpl := c.GetDirectoriesPerformance()
40+
if diff := cmp.Diff(dpl.Items, dirsp.Items); diff != "" {
41+
t.Errorf("Mismatch (-want +got):\n%s", diff)
42+
}
43+
})
4544
}

0 commit comments

Comments
 (0)