-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservices.go
More file actions
87 lines (66 loc) · 2.32 KB
/
services.go
File metadata and controls
87 lines (66 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Package services provides methods for managing an app's services.
package services
import (
"encoding/json"
"fmt"
"io"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// List services registered with an app.
func List(c *drycc.Client, appID string) (api.Services, error) {
u := fmt.Sprintf("/v2/apps/%s/services/", appID)
res, reqErr := c.Request("GET", u, nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.Service{}, reqErr
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return []api.Service{}, err
}
r := make(map[string]interface{})
if err = json.Unmarshal([]byte(body), &r); err != nil {
return []api.Service{}, err
}
out, err := json.Marshal(r["services"].([]interface{}))
if err != nil {
return []api.Service{}, err
}
var services []api.Service
if err := json.Unmarshal([]byte(out), &services); err != nil {
return []api.Service{}, err
}
return services, reqErr
}
// New adds a new service to an app. App should already exists.
// Service is the way to route some traffic matching given URL pattern to worker different than `web`
// Ptype - name of the process in Procfile (i.e. <process type> from the `<process type>: <command>`), e.g. `webhooks`
// for more about Procfile see this https://devcenter.heroku.com/articles/procfile
// Ptype and pathPattern are mandatory and should have valid values.
func New(c *drycc.Client, appID string, Ptype string, port int, protocol string, targetPort int) error {
u := fmt.Sprintf("/v2/apps/%s/services/", appID)
req := api.ServiceCreateUpdateRequest{Ptype: Ptype, Port: port, Protocol: protocol, TargetPort: targetPort}
body, err := json.Marshal(req)
if err != nil {
return err
}
res, reqErr := c.Request("POST", u, body)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return reqErr
}
defer res.Body.Close()
return reqErr
}
// Delete service from app
// If given service for the app doesn't exists then error returned
func Delete(c *drycc.Client, appID string, Ptype string, protocol string, port int) error {
u := fmt.Sprintf("/v2/apps/%s/services/", appID)
req := api.ServiceDeleteRequest{Ptype: Ptype, Protocol: protocol, Port: port}
body, err := json.Marshal(req)
if err != nil {
return err
}
_, err = c.Request("DELETE", u, body)
return err
}