-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroutes.go
More file actions
130 lines (104 loc) · 3.07 KB
/
Copy pathroutes.go
File metadata and controls
130 lines (104 loc) · 3.07 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Package routes provides methods for managing an app's routes.
package routes
import (
"encoding/json"
"fmt"
"io"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// List routes registered with an app.
func List(c *drycc.Client, appID string, results int) (api.Routes, int, error) {
u := fmt.Sprintf("/v2/apps/%s/routes/", appID)
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.Route{}, -1, reqErr
}
var routes []api.Route
if err := json.Unmarshal([]byte(body), &routes); err != nil {
return []api.Route{}, -1, err
}
return routes, count, reqErr
}
// New adds a route to an app.
func New(c *drycc.Client, appID, name, kind string, backendRefs ...api.BackendRefRequest) error {
u := fmt.Sprintf("/v2/apps/%s/routes/", appID)
req := api.RouteCreateRequest{
Name: name,
Kind: kind,
Rules: []api.RequestRouteRule{{BackendRefs: backendRefs}},
}
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
}
// AttachGateway route attach a gateway.
func AttachGateway(c *drycc.Client, appID string, name string, port int, gateway string) error {
u := fmt.Sprintf("/v2/apps/%s/routes/%s/attach/", appID, name)
req := api.RouteAttachRequest{Port: port, Gateway: gateway}
body, err := json.Marshal(req)
if err != nil {
return err
}
res, reqErr := c.Request("PATCH", u, body)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return reqErr
}
defer res.Body.Close()
return reqErr
}
// DetachGateway route attach a gateway.
func DetachGateway(c *drycc.Client, appID string, name string, port int, gateway string) error {
u := fmt.Sprintf("/v2/apps/%s/routes/%s/detach/", appID, name)
req := api.RouteDetachRequest{Port: port, Gateway: gateway}
body, err := json.Marshal(req)
if err != nil {
return err
}
res, reqErr := c.Request("PATCH", u, body)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return reqErr
}
defer res.Body.Close()
return reqErr
}
// GetRoute get info rule of a route from an app.
func GetRule(c *drycc.Client, appID string, name string) (string, error) {
u := fmt.Sprintf("/v2/apps/%s/routes/%s/rules/", appID, name)
res, err := c.Request("GET", u, nil)
if err != nil {
return "", err
}
defer res.Body.Close()
respBytes, err := io.ReadAll(res.Body)
return string(respBytes), err
}
// SetRule set rule of a route.
func SetRule(c *drycc.Client, appID string, name string, rules string) error {
u := fmt.Sprintf("/v2/apps/%s/routes/%s/rules/", appID, name)
body, err := json.Marshal(rules)
if err != nil {
return err
}
res, err := c.Request("PUT", u, body)
if err == nil {
res.Body.Close()
}
return err
}
// Delete Delete a route from an app.
func Delete(c *drycc.Client, appID string, name string) error {
u := fmt.Sprintf("/v2/apps/%s/routes/%s/", appID, name)
res, err := c.Request("DELETE", u, nil)
if err == nil {
res.Body.Close()
}
return err
}