Skip to content

Commit dce01c6

Browse files
jianxiaoguoduanhongyi
authored andcommitted
parent eea7ecc
author jianxiaoguo <lijianguo1991@outlook.com> 1682477903 +0800 committer duanhongyi <duanhongyi@doopai.com> 1687661385 +0800 feat(controller-sdk-go): add gateway api
1 parent eea7ecc commit dce01c6

33 files changed

Lines changed: 1176 additions & 127 deletions

allowlist/allowlist_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package allowlist
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88
"reflect"
@@ -30,7 +30,7 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
3030
}
3131

3232
if req.URL.Path == "/v2/apps/example-go/allowlist/" && req.Method == "POST" {
33-
body, err := ioutil.ReadAll(req.Body)
33+
body, err := io.ReadAll(req.Body)
3434

3535
if err != nil {
3636
fmt.Println(err)

api/appsettings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type AppSettings struct {
2323
Allowlist []string `json:"allowlist,omitempty"`
2424
Autoscale map[string]*Autoscale `json:"autoscale,omitempty"`
2525
Label Labels `json:"label,omitempty"`
26+
Canaries []string `json:"canaries,omitempty"`
2627
}
2728

2829
// NewRoutable returns a default value for the AppSettings.Routable field.

api/gateways.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package api
2+
3+
// Gateway is the structure of an app's gateways.
4+
type Gateway struct {
5+
// Owner is the app owner. It cannot be updated with AppSettings.Set(). See app.Transfer().
6+
Owner string `json:"owner,omitempty"`
7+
// App is the app name. It cannot be updated at all right now.
8+
App string `json:"app,omitempty"`
9+
// Created is the time that the application settings was created and cannot be updated.
10+
Created string `json:"created,omitempty"`
11+
// Updated is the last time the application settings was changed and cannot be updated.
12+
Updated string `json:"updated,omitempty"`
13+
// UUID is a unique string reflecting the application settings in its current state.
14+
// It changes every time the application settings is changed and cannot be updated.
15+
UUID string `json:"uuid,omitempty"`
16+
Name string `json:"name,omitempty"`
17+
Listeners []Listener `json:"listeners,omitempty"`
18+
}
19+
20+
type Listener struct {
21+
Name string `json:"name,omitempty"`
22+
Port int `json:"port,omitempty"`
23+
Protocol string `json:"protocol,omitempty"`
24+
AllowedRoutes interface{} `json:"allowedRoutes,omitempty"`
25+
}
26+
27+
// Gateways defines a collection of gateway objects.
28+
type Gateways []Gateway
29+
30+
// GatewayCreateRequest is the structure of POST /v2/app/<app id>/gateways/.
31+
type GatewayCreateRequest struct {
32+
Name string `json:"name,omitempty"`
33+
Port int `json:"port,omitempty"`
34+
Protocol string `json:"protocol,omitempty"`
35+
}
36+
37+
// GatewayRemoteRequest is the structure of Delete /v2/app/<app id>/gateways/.
38+
type GatewayRemoveRequest struct {
39+
Name string `json:"name,omitempty"`
40+
Port int `json:"port,omitempty"`
41+
Protocol string `json:"protocol,omitempty"`
42+
}

api/routes.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package api
2+
3+
// Route is the structure of an app's route.
4+
type Route struct {
5+
// Owner is the app owner. It cannot be updated with AppSettings.Set(). See app.Transfer().
6+
Owner string `json:"owner,omitempty"`
7+
// App is the app name. It cannot be updated at all right now.
8+
App string `json:"app,omitempty"`
9+
// Created is the time that the application settings was created and cannot be updated.
10+
Created string `json:"created,omitempty"`
11+
// Updated is the last time the application settings was changed and cannot be updated.
12+
Updated string `json:"updated,omitempty"`
13+
// UUID is a unique string reflecting the application settings in its current state.
14+
// It changes every time the application settings is changed and cannot be updated.
15+
UUID string `json:"uuid,omitempty"`
16+
Name string `json:"name,omitempty"`
17+
Type string `json:"procfile_type,omitempty"`
18+
Kind string `json:"kind,omitempty"`
19+
Port int `json:"port,omitempty"`
20+
ParentRefs []ParentRef `json:"parent_refs,omitempty"`
21+
}
22+
23+
type ParentRef struct {
24+
Name string `json:"name,omitempty"`
25+
Port int `json:"port,omitempty"`
26+
}
27+
28+
// // Routes defines a collection of Route objects.
29+
type Routes []Route
30+
31+
// RouteCreateRequest is the structure of POST /v2/app/<app id>/routes/.
32+
type RouteCreateRequest struct {
33+
Name string `json:"name,omitempty"`
34+
Type string `json:"procfile_type,omitempty"`
35+
Port int `json:"port,omitempty"`
36+
Kind string `json:"kind,omitempty"`
37+
}
38+
39+
// RouteAttackRequest is the structure of PATCH /v2/apps/(?P<id>{})/routes/(?P<name>{})/attach/?$.
40+
type RouteAttackRequest struct {
41+
Port int `json:"port,omitempty"`
42+
Gateway string `json:"gateway,omitempty"`
43+
}
44+
45+
// RouteDetackRequest is the structure of PATCH /v2/apps/(?P<id>{})/routes/(?P<name>{})/detach/?$.
46+
type RouteDetackRequest struct {
47+
Port int `json:"port,omitempty"`
48+
Gateway string `json:"gateway,omitempty"`
49+
}
50+
51+
// RouteRule is the structure of GET RESPONSE /v2/apps/(?P<id>{})/routes/(?P<name>{})/rules/?$.
52+
type RouteRule struct {
53+
Name string `json:"name,omitempty"`
54+
Type string `json:"procfile_type,omitempty"`
55+
Kind string `json:"kind,omitempty"`
56+
Rules interface{} `json:"rules,omitempty"`
57+
}

api/services.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ package api
22

33
// Service is the structure of the service object.
44
type Service struct {
5+
Domain string `json:"domain"`
56
ProcfileType string `json:"procfile_type"`
6-
Port int `json:"port"`
7-
Protocol string `json:"protocol"`
8-
TargetPort int `json:"target_port"`
7+
Ports []Port `json:"ports"`
8+
}
9+
10+
type Port struct {
11+
Name string `json:"name"`
12+
Port int `json:"port"`
13+
Protocol string `json:"protocol"`
14+
TargetPort int `json:"targetPort"`
915
}
1016

1117
// Services defines a collection of service objects.
@@ -26,4 +32,6 @@ type ServiceCreateUpdateRequest struct {
2632
// ServiceDeleteRequest is the structure of DELETE /v2/app/<app id>/services/.
2733
type ServiceDeleteRequest struct {
2834
ProcfileType string `json:"procfile_type"`
35+
Port int `json:"port"`
36+
Protocol string `json:"protocol"`
2937
}

api/tls.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ type TLS struct {
2020
//HTTPSEnforced determines if the router should enable or disable https-only requests.
2121
HTTPSEnforced *bool `json:"https_enforced,omitempty"`
2222
//Use ACME to automatically generate certificates if CertsAuto enable
23-
CertsAutoEnabled *bool `json:"certs_auto_enabled,omitempty"`
23+
CertsAutoEnabled *bool `json:"certs_auto_enabled,omitempty"`
24+
Issuer *Issuer `json:"issuer,omitempty"`
25+
}
26+
27+
// Issuer is the structure of POST /v2/app/<app id>/tls/.
28+
type Issuer struct {
29+
Email string `json:"email"`
30+
Server string `json:"server"`
31+
KeyID string `json:"key_id"`
32+
KeySecret string `json:"key_secret"`
2433
}
2534

2635
// NewTLS creates a new TLS object with fields properly zeroed
@@ -32,8 +41,15 @@ func NewTLS() *TLS {
3241
}
3342

3443
func (t TLS) String() string {
35-
tpl := `HTTPS Enforced: %s
36-
Certs Auto: %s`
44+
tpl := `--- HTTPS Enforced: %s
45+
--- Certs Auto: %s
46+
--- Issuer: %s`
47+
issuerTpl := `
48+
email: %s
49+
server: %s
50+
key-id: %s
51+
key-secret: %s
52+
`
3753
httpsEnforced := "not set"
3854
if t.HTTPSEnforced != nil {
3955
httpsEnforced = fmt.Sprintf("%t", *(t.HTTPSEnforced))
@@ -42,5 +58,9 @@ Certs Auto: %s`
4258
if t.CertsAutoEnabled != nil {
4359
certsAutoEnabled = fmt.Sprintf("%t", *(t.CertsAutoEnabled))
4460
}
45-
return fmt.Sprintf(tpl, httpsEnforced, certsAutoEnabled)
61+
issuer := "not set"
62+
if t.Issuer != nil {
63+
issuer = fmt.Sprintf(issuerTpl, t.Issuer.Email, t.Issuer.Server, t.Issuer.KeyID, t.Issuer.KeySecret)
64+
}
65+
return fmt.Sprintf(tpl, httpsEnforced, certsAutoEnabled, issuer)
4666
}

api/tls_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
func TestTLSString(t *testing.T) {
99
tls := &TLS{}
1010

11-
expected := "HTTPS Enforced: not set\nCerts Auto: not set"
11+
expected := "--- HTTPS Enforced: not set\n--- Certs Auto: not set\n--- Issuer: not set"
1212

1313
if strings.TrimSpace(tls.String()) != expected {
1414
t.Errorf("Expected:\n\n%s\n\nGot:\n\n%s", expected, tls.String())
1515
}
1616

1717
tls = NewTLS()
1818

19-
expected = "HTTPS Enforced: false\nCerts Auto: false"
19+
expected = "--- HTTPS Enforced: false\n--- Certs Auto: false\n--- Issuer: not set"
2020

2121
if strings.TrimSpace(tls.String()) != expected {
2222
t.Errorf("Expected:\n\n%s\n\nGot:\n\n%s", expected, tls.String())
@@ -25,7 +25,27 @@ func TestTLSString(t *testing.T) {
2525
b := true
2626
tls.HTTPSEnforced = &b
2727

28-
expected = "HTTPS Enforced: true\nCerts Auto: false"
28+
expected = "--- HTTPS Enforced: true\n--- Certs Auto: false\n--- Issuer: not set"
29+
30+
if strings.TrimSpace(tls.String()) != expected {
31+
t.Errorf("Expected:\n\n%s\n\nGot:\n\n%s", expected, tls.String())
32+
}
33+
34+
issuer := Issuer{
35+
Email: "anonymous@cert-manager.io",
36+
Server: "https://acme-v02.api.letsencrypt.org/directory",
37+
KeyID: "",
38+
KeySecret: "",
39+
}
40+
tls.Issuer = &issuer
41+
42+
expected = `--- HTTPS Enforced: true
43+
--- Certs Auto: false
44+
--- Issuer:
45+
email: anonymous@cert-manager.io
46+
server: https://acme-v02.api.letsencrypt.org/directory
47+
key-id:
48+
key-secret:`
2949

3050
if strings.TrimSpace(tls.String()) != expected {
3151
t.Errorf("Expected:\n\n%s\n\nGot:\n\n%s", expected, tls.String())

apps/apps_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package apps
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"log"
88
"net/http"
99
"net/http/httptest"
@@ -63,7 +63,7 @@ func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
6363
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
6464

6565
if req.URL.Path == "/v2/apps/" && req.Method == "POST" {
66-
body, err := ioutil.ReadAll(req.Body)
66+
body, err := io.ReadAll(req.Body)
6767

6868
if err != nil {
6969
fmt.Println(err)
@@ -128,7 +128,7 @@ func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
128128
}
129129

130130
if req.URL.Path == "/v2/apps/example-go/run" && req.Method == "POST" {
131-
body, err := ioutil.ReadAll(req.Body)
131+
body, err := io.ReadAll(req.Body)
132132

133133
if err != nil {
134134
fmt.Println(err)
@@ -148,7 +148,7 @@ func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
148148
}
149149

150150
if req.URL.Path == "/v2/apps/example-go/" && req.Method == "POST" {
151-
body, err := ioutil.ReadAll(req.Body)
151+
body, err := io.ReadAll(req.Body)
152152

153153
if err != nil {
154154
fmt.Println(err)

appsettings/appsettings.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,39 @@ func Set(c *drycc.Client, app string, appSettings api.AppSettings) (api.AppSetti
5858

5959
return newAppSettings, reqErr
6060
}
61+
62+
// CanaryDelete remove an app's canary settings.
63+
func CanaryRemove(c *drycc.Client, app string, appSettings api.AppSettings) error {
64+
body, err := json.Marshal(appSettings)
65+
66+
if err != nil {
67+
return err
68+
}
69+
70+
u := fmt.Sprintf("/v2/apps/%s/settings/", app)
71+
res, err := c.Request("DELETE", u, body)
72+
if err == nil {
73+
res.Body.Close()
74+
}
75+
return err
76+
}
77+
78+
// CanaryRelease release an app's canary settings.
79+
func CanaryRelease(c *drycc.Client, app string) error {
80+
u := fmt.Sprintf("/v2/apps/%s/canary/release/", app)
81+
res, err := c.Request("POST", u, nil)
82+
if err == nil {
83+
res.Body.Close()
84+
}
85+
return nil
86+
}
87+
88+
// CanaryRollback rollback an app's canary settings.
89+
func CanaryRollback(c *drycc.Client, app string) error {
90+
u := fmt.Sprintf("/v2/apps/%s/canary/rollback/", app)
91+
res, err := c.Request("POST", u, nil)
92+
if err == nil {
93+
res.Body.Close()
94+
}
95+
return nil
96+
}

0 commit comments

Comments
 (0)