Skip to content

Commit eea7ecc

Browse files
committed
feat(services): fit controller services api
1 parent d5442cc commit eea7ecc

4 files changed

Lines changed: 30 additions & 16 deletions

File tree

api/services.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package api
33
// Service is the structure of the service object.
44
type Service struct {
55
ProcfileType string `json:"procfile_type"`
6-
PathPattern string `json:"path_pattern"`
6+
Port int `json:"port"`
7+
Protocol string `json:"protocol"`
8+
TargetPort int `json:"target_port"`
79
}
810

911
// Services defines a collection of service objects.
@@ -16,7 +18,9 @@ func (s Services) Less(i, j int) bool { return s[i].ProcfileType < s[j].Procfile
1618
// ServiceCreateUpdateRequest is the structure of POST /v2/app/<app id>/services/.
1719
type ServiceCreateUpdateRequest struct {
1820
ProcfileType string `json:"procfile_type"`
19-
PathPattern string `json:"path_pattern"`
21+
Port int `json:"port"`
22+
Protocol string `json:"protocol"`
23+
TargetPort int `json:"target_port"`
2024
}
2125

2226
// ServiceDeleteRequest is the structure of DELETE /v2/app/<app id>/services/.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/drycc/controller-sdk-go
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/gorilla/websocket v1.5.0

services/services.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@ func List(c *drycc.Client, appID string) (api.Services, error) {
4848
// Service is the way to route some traffic matching given URL pattern to worker different than `web`
4949
// procfileType - name of the process in Procfile (i.e. <process type> from the `<process type>: <command>`), e.g. `webhooks`
5050
// for more about Procfile see this https://devcenter.heroku.com/articles/procfile
51-
// pathPattern - one or several regexp patterns separated by comma, all request matching given regexp
52-
// are routed to the procfileType workers. E.g. `/webhooks/notify,~ ^/users/[0-9]+/.*/webhooks/notify,/webhooks/rest`
5351
// procfileType and pathPattern are mandatory and should have valid values.
54-
func New(c *drycc.Client, appID string, procfileType string, pathPattern string) (api.Service, error) {
52+
func New(c *drycc.Client, appID string, procfileType string, port int, protocol string, targetPort int) (api.Service, error) {
5553
u := fmt.Sprintf("/v2/apps/%s/services/", appID)
5654

57-
req := api.ServiceCreateUpdateRequest{ProcfileType: procfileType, PathPattern: pathPattern}
55+
req := api.ServiceCreateUpdateRequest{ProcfileType: procfileType, Port: port, Protocol: protocol, TargetPort: targetPort}
5856

5957
body, err := json.Marshal(req)
6058

@@ -68,7 +66,7 @@ func New(c *drycc.Client, appID string, procfileType string, pathPattern string)
6866
}
6967
defer res.Body.Close()
7068

71-
d := api.Service{ProcfileType: procfileType, PathPattern: pathPattern}
69+
d := api.Service{ProcfileType: procfileType, Port: port, Protocol: protocol, TargetPort: targetPort}
7270
return d, reqErr
7371
}
7472

services/services_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,28 @@ const servicesFixture string = `
1717
"services": [
1818
{
1919
"procfile_type": "web",
20-
"path_pattern": "/abc/xyz"
20+
"port": 5000,
21+
"protocol": "UDP",
22+
"target_port": 5000
2123
},
2224
{
2325
"procfile_type": "worker",
24-
"path_pattern": "/x1z/a2c"
26+
"port": 5000,
27+
"protocol": "TCP",
28+
"target_port": 5000
2529
}
2630
]
2731
}`
2832

2933
const serviceFixture string = `
3034
{
3135
"procfile_type": "web",
32-
"path_pattern": "/abc/xyz"
36+
"port": 5000,
37+
"protocol": "UDP",
38+
"target_port": 5000
3339
}`
3440

35-
const serviceCreateExpected string = `{"procfile_type":"web","path_pattern":"/abc/xyz"}`
41+
const serviceCreateExpected string = `{"procfile_type":"web","port":5000,"protocol":"UDP","target_port":5000}`
3642

3743
type fakeHTTPServer struct{}
3844

@@ -82,11 +88,15 @@ func TestServicesList(t *testing.T) {
8288
expected := api.Services{
8389
{
8490
ProcfileType: "web",
85-
PathPattern: "/abc/xyz",
91+
Port: 5000,
92+
Protocol: "UDP",
93+
TargetPort: 5000,
8694
},
8795
{
8896
ProcfileType: "worker",
89-
PathPattern: "/x1z/a2c",
97+
Port: 5000,
98+
Protocol: "TCP",
99+
TargetPort: 5000,
90100
},
91101
}
92102

@@ -115,7 +125,9 @@ func TestServicesAdd(t *testing.T) {
115125

116126
expected := api.Service{
117127
ProcfileType: "web",
118-
PathPattern: "/abc/xyz",
128+
Port: 5000,
129+
Protocol: "UDP",
130+
TargetPort: 5000,
119131
}
120132

121133
handler := fakeHTTPServer{}
@@ -127,7 +139,7 @@ func TestServicesAdd(t *testing.T) {
127139
t.Fatal(err)
128140
}
129141

130-
actual, err := New(drycc, "example-go", "web", "/abc/xyz")
142+
actual, err := New(drycc, "example-go", "web", 5000, "UDP", 5000)
131143

132144
if err != nil {
133145
t.Fatal(err)

0 commit comments

Comments
 (0)