Skip to content

Commit 65af8fd

Browse files
committed
feat(gateway): feat(gateway): unify gateway/route API fields
1 parent 7d751e6 commit 65af8fd

6 files changed

Lines changed: 187 additions & 315 deletions

File tree

api/gateways.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,10 @@ type Gateway struct {
1010
Updated string `json:"updated,omitempty"`
1111
// UUID is a unique string reflecting the application settings in its current state.
1212
// It changes every time the application settings is changed and cannot be updated.
13-
UUID string `json:"uuid,omitempty"`
14-
Name string `json:"name,omitempty"`
15-
Listeners []Listener `json:"listeners,omitempty"`
16-
Addresses []Address `json:"addresses,omitempty"`
17-
}
18-
19-
// Listener represents a gateway listener configuration.
20-
type Listener struct {
21-
Name string `json:"name,omitempty"`
22-
Port int `json:"port,omitempty"`
23-
Protocol string `json:"protocol,omitempty"`
24-
AllowedRoutes any `json:"allowedRoutes,omitempty"`
13+
UUID string `json:"uuid,omitempty"`
14+
Name string `json:"name,omitempty"`
15+
Ports []GatewayPort `json:"ports,omitempty"`
16+
Addresses []Address `json:"addresses,omitempty"`
2517
}
2618

2719
// Address represents a gateway address configuration.
@@ -33,16 +25,23 @@ type Address struct {
3325
// Gateways defines a collection of gateway objects.
3426
type Gateways []Gateway
3527

36-
// GatewayCreateRequest is the structure of POST /v2/app/<app id>/gateways/.
37-
type GatewayCreateRequest struct {
38-
Name string `json:"name,omitempty"`
39-
Port int `json:"port,omitempty"`
40-
Protocol string `json:"protocol,omitempty"`
28+
// GatewayUpdateRequest is the structure of PUT /v2/apps/<app id>/gateways/<name>/.
29+
type GatewayUpdateRequest struct {
30+
App string `json:"app"`
31+
Name string `json:"name"`
32+
Ports []GatewayPort `json:"ports"`
33+
}
34+
35+
// GatewayPort represents a port in a gateway apply request.
36+
type GatewayPort struct {
37+
Port int `json:"port"`
38+
Protocol string `json:"protocol"`
4139
}
4240

43-
// GatewayRemoveRequest is the structure of Delete /v2/app/<app id>/gateways/.
44-
type GatewayRemoveRequest struct {
45-
Name string `json:"name,omitempty"`
46-
Port int `json:"port,omitempty"`
47-
Protocol string `json:"protocol,omitempty"`
41+
// GatewayInfo is the structure returned by GET /v2/apps/<app id>/gateways/<name>/.
42+
type GatewayInfo struct {
43+
App string `json:"app,omitempty"`
44+
Name string `json:"name,omitempty"`
45+
Ports []GatewayPort `json:"ports"`
46+
Addresses []Address `json:"addresses,omitempty"`
4847
}

api/routes.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,27 @@ type RouteRule map[string]any
2929
// Routes defines a collection of Route objects.
3030
type Routes []Route
3131

32-
// RouteCreateRequest is the structure of POST /v2/app/<app_id>/routes/.
33-
34-
// RouteCreateRequest is the structure of POST /v2/app/<app_id>/routes/.
35-
type RouteCreateRequest struct {
36-
Name string `json:"name,omitempty"`
37-
Kind string `json:"kind,omitempty"`
38-
Rules []RequestRouteRule `json:"rules,omitempty"`
39-
}
40-
41-
// BackendRefRequest represents a backend reference in a route request.
42-
type BackendRefRequest struct {
43-
Kind string `json:"kind,omitempty"`
44-
Name string `json:"name,omitempty"`
45-
Port int32 `json:"port,omitempty"`
46-
Weight int32 `json:"weight,omitempty"`
47-
}
48-
49-
// RequestRouteRule represents a route rule in a request.
50-
type RequestRouteRule struct {
51-
BackendRefs []BackendRefRequest `json:"backendRefs,omitempty"`
32+
// RouteUpdateRequest is the structure of PUT /v2/apps/<app_id>/routes/<name>/.
33+
type RouteUpdateRequest struct {
34+
App string `json:"app"`
35+
Name string `json:"name"`
36+
Kind string `json:"kind"`
37+
ParentRefs []RouteParentRef `json:"parent_refs"`
38+
Rules []RouteRule `json:"rules"`
5239
}
5340

54-
// RouteAttachRequest is the structure of PATCH /v2/apps/(?P<id>{})/routes/(?P<name>{})/attach/?$.
55-
type RouteAttachRequest struct {
56-
Port int `json:"port,omitempty"`
57-
Gateway string `json:"gateway,omitempty"`
41+
// RouteParentRef represents a reference to a parent gateway in apply request.
42+
type RouteParentRef struct {
43+
Name string `json:"name"`
44+
Port int `json:"port"`
5845
}
5946

60-
// RouteDetachRequest is the structure of PATCH /v2/apps/(?P<id>{})/routes/(?P<name>{})/detach/?$.
61-
type RouteDetachRequest struct {
62-
Port int `json:"port,omitempty"`
63-
Gateway string `json:"gateway,omitempty"`
47+
// RouteInfo is the structure returned by GET /v2/apps/<app_id>/routes/<name>/.
48+
type RouteInfo struct {
49+
App string `json:"app,omitempty"`
50+
Name string `json:"name,omitempty"`
51+
Kind string `json:"kind"`
52+
ParentRefs []RouteParentRef `json:"parent_refs"`
53+
Rules []RouteRule `json:"rules"`
54+
Routable *bool `json:"routable,omitempty"`
6455
}

gateways/gateways.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,54 @@ func List(c *drycc.Client, appID string, results int) (api.Gateways, int, error)
2626
return gateways, count, reqErr
2727
}
2828

29-
// New adds a gateway to an app.
30-
func New(c *drycc.Client, appID string, name string, port int, protocol string) error {
31-
u := fmt.Sprintf("/v2/apps/%s/gateways/", appID)
32-
33-
req := api.GatewayCreateRequest{Name: name, Port: port, Protocol: protocol}
29+
// Apply creates or updates a gateway for an app.
30+
func Apply(c *drycc.Client, appID string, req api.GatewayUpdateRequest) (api.GatewayInfo, error) {
31+
name := req.Name
32+
req.App = appID
33+
u := fmt.Sprintf("/v2/apps/%s/gateways/%s/", appID, name)
3434

3535
body, err := json.Marshal(req)
3636
if err != nil {
37-
return err
37+
return api.GatewayInfo{}, err
3838
}
3939

40-
res, reqErr := c.Request("POST", u, body)
40+
res, reqErr := c.Request("PUT", u, body)
4141
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
42-
return reqErr
42+
return api.GatewayInfo{}, reqErr
4343
}
4444
defer res.Body.Close()
4545

46-
return reqErr
47-
}
46+
var info api.GatewayInfo
47+
if err := json.NewDecoder(res.Body).Decode(&info); err != nil {
48+
return api.GatewayInfo{}, err
49+
}
4850

49-
// Delete removes a gateway or listener of gateway from an app.
50-
func Delete(c *drycc.Client, appID string, name string, port int, protocol string) error {
51-
u := fmt.Sprintf("/v2/apps/%s/gateways/", appID)
51+
return info, reqErr
52+
}
5253

53-
req := api.GatewayRemoveRequest{Name: name, Port: port, Protocol: protocol}
54+
// Info retrieves information about a gateway.
55+
func Info(c *drycc.Client, appID string, name string) (api.GatewayInfo, error) {
56+
u := fmt.Sprintf("/v2/apps/%s/gateways/%s/", appID, name)
5457

55-
body, err := json.Marshal(req)
58+
res, err := c.Request("GET", u, nil)
5659
if err != nil {
57-
return err
60+
return api.GatewayInfo{}, err
5861
}
62+
defer res.Body.Close()
63+
64+
var info api.GatewayInfo
65+
if err := json.NewDecoder(res.Body).Decode(&info); err != nil {
66+
return api.GatewayInfo{}, err
67+
}
68+
69+
return info, nil
70+
}
71+
72+
// Delete removes a gateway from an app.
73+
func Delete(c *drycc.Client, appID string, name string) error {
74+
u := fmt.Sprintf("/v2/apps/%s/gateways/%s/", appID, name)
5975

60-
res, err := c.Request("DELETE", u, body)
76+
res, err := c.Request("DELETE", u, nil)
6177
if err == nil {
6278
res.Body.Close()
6379
}

gateways/gateways_test.go

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ const gatewaysFixture string = `
2323
"name": "example-go",
2424
"created": "2023-04-19T00:00:00UTC",
2525
"updated": "2023-04-19T00:00:00UTC",
26-
"listeners": [
26+
"ports": [
2727
{
28-
"name": "example-go-80-http",
2928
"port": 80,
30-
"protocol": "HTTP",
31-
"allowedRoutes": {"namespaces": {"from": "All"}}
29+
"protocol": "HTTP"
3230
},
3331
{
34-
"name": "example-go-443-https",
3532
"port": 443,
36-
"protocol": "HTTPS",
37-
"allowedRoutes": {"namespaces": {"from": "All"}}
33+
"protocol": "HTTPS"
3834
}
3935
],
4036
"addresses": [
@@ -48,8 +44,8 @@ const gatewaysFixture string = `
4844
}`
4945

5046
const (
51-
gatewayCreateExpected string = `{"name":"example-go","port":443,"protocol":"HTTPS"}`
52-
gatewayRemoveExpected string = `{"name":"example-go","port":443,"protocol":"HTTPS"}`
47+
gatewayApplyExpected string = `{"app":"example-go","name":"example-go","ports":[{"port":80,"protocol":"HTTP"},{"port":443,"protocol":"HTTPS"}]}`
48+
gatewayInfoResponse string = `{"name":"example-go","ports":[{"port":80,"protocol":"HTTP"},{"port":443,"protocol":"HTTPS"}]}`
5349
)
5450

5551
type fakeHTTPServer struct{}
@@ -62,37 +58,31 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
6258
return
6359
}
6460

65-
if req.URL.Path == "/v2/apps/example-go/gateways/" && req.Method == "POST" {
61+
if req.URL.Path == "/v2/apps/example-go/gateways/example-go/" && req.Method == "PUT" {
6662
body, err := io.ReadAll(req.Body)
6763
if err != nil {
6864
fmt.Println(err)
6965
res.WriteHeader(http.StatusInternalServerError)
7066
res.Write(nil)
7167
}
72-
if string(body) != gatewayCreateExpected {
73-
fmt.Printf("Expected '%s', Got '%s'\n", gatewayCreateExpected, body)
68+
if string(body) != gatewayApplyExpected {
69+
fmt.Printf("Expected '%s', Got '%s'\n", gatewayApplyExpected, body)
7470
res.WriteHeader(http.StatusInternalServerError)
7571
res.Write(nil)
7672
return
7773
}
7874

79-
res.WriteHeader(http.StatusCreated)
75+
res.WriteHeader(http.StatusOK)
76+
res.Write([]byte(gatewayInfoResponse))
8077
return
8178
}
8279

83-
if req.URL.Path == "/v2/apps/example-go/gateways/" && req.Method == "DELETE" {
84-
body, err := io.ReadAll(req.Body)
85-
if err != nil {
86-
fmt.Println(err)
87-
res.WriteHeader(http.StatusInternalServerError)
88-
res.Write(nil)
89-
}
90-
if string(body) != gatewayRemoveExpected {
91-
fmt.Printf("Expected '%s', Got '%s'\n", gatewayRemoveExpected, body)
92-
res.WriteHeader(http.StatusInternalServerError)
93-
res.Write(nil)
94-
return
95-
}
80+
if req.URL.Path == "/v2/apps/example-go/gateways/example-go/" && req.Method == "GET" {
81+
res.Write([]byte(gatewayInfoResponse))
82+
return
83+
}
84+
85+
if req.URL.Path == "/v2/apps/example-go/gateways/example-go/" && req.Method == "DELETE" {
9686
res.WriteHeader(http.StatusNoContent)
9787
return
9888
}
@@ -111,18 +101,14 @@ func TestGatewaysList(t *testing.T) {
111101
Created: "2023-04-19T00:00:00UTC",
112102
Name: "example-go",
113103
Updated: "2023-04-19T00:00:00UTC",
114-
Listeners: []api.Listener{
104+
Ports: []api.GatewayPort{
115105
{
116-
Name: "example-go-80-http",
117-
Port: 80,
118-
Protocol: "HTTP",
119-
AllowedRoutes: map[string]any{"namespaces": map[string]any{"from": "All"}},
106+
Port: 80,
107+
Protocol: "HTTP",
120108
},
121109
{
122-
Name: "example-go-443-https",
123-
Port: 443,
124-
Protocol: "HTTPS",
125-
AllowedRoutes: map[string]any{"namespaces": map[string]any{"from": "All"}},
110+
Port: 443,
111+
Protocol: "HTTPS",
126112
},
127113
},
128114
Addresses: []api.Address{
@@ -152,7 +138,37 @@ func TestGatewaysList(t *testing.T) {
152138
}
153139
}
154140

155-
func TestGatewaysAdd(t *testing.T) {
141+
func TestGatewaysApply(t *testing.T) {
142+
t.Parallel()
143+
144+
handler := fakeHTTPServer{}
145+
server := httptest.NewServer(handler)
146+
defer server.Close()
147+
148+
drycc, err := drycc.New(false, server.URL, "abc")
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
153+
req := api.GatewayUpdateRequest{
154+
Name: "example-go",
155+
Ports: []api.GatewayPort{
156+
{Port: 80, Protocol: "HTTP"},
157+
{Port: 443, Protocol: "HTTPS"},
158+
},
159+
}
160+
161+
info, err := Apply(drycc, "example-go", req)
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
166+
if len(info.Ports) != 2 {
167+
t.Fatalf("Expected 2 ports, got %d", len(info.Ports))
168+
}
169+
}
170+
171+
func TestGatewaysInfo(t *testing.T) {
156172
t.Parallel()
157173

158174
handler := fakeHTTPServer{}
@@ -164,10 +180,14 @@ func TestGatewaysAdd(t *testing.T) {
164180
t.Fatal(err)
165181
}
166182

167-
err = New(drycc, "example-go", "example-go", 443, "HTTPS")
183+
info, err := Info(drycc, "example-go", "example-go")
168184
if err != nil {
169185
t.Fatal(err)
170186
}
187+
188+
if len(info.Ports) != 2 {
189+
t.Fatalf("Expected 2 ports, got %d", len(info.Ports))
190+
}
171191
}
172192

173193
func TestGatewaysRemove(t *testing.T) {
@@ -182,7 +202,7 @@ func TestGatewaysRemove(t *testing.T) {
182202
t.Fatal(err)
183203
}
184204

185-
if err = Delete(drycc, "example-go", "example-go", 443, "HTTPS"); err != nil {
205+
if err = Delete(drycc, "example-go", "example-go"); err != nil {
186206
t.Fatal(err)
187207
}
188208
}

0 commit comments

Comments
 (0)