Skip to content

Commit ad5ede6

Browse files
committed
new
1 parent 1e0e0b1 commit ad5ede6

24 files changed

Lines changed: 1297 additions & 329 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DEV_ENV_WORK_DIR := /opt/drycc/go/src/${REPO_PATH}
66

77
DIST_DIR ?= _dist
88

9-
DEV_ENV_CMD := docker run --rm -v ${CURDIR}:${DEV_ENV_WORK_DIR} -w ${DEV_ENV_WORK_DIR} ${DEV_ENV_IMAGE}
9+
DEV_ENV_CMD := docker run --rm -e GOPROXY=https://goproxy.cn -v ${CURDIR}:${DEV_ENV_WORK_DIR} -w ${DEV_ENV_WORK_DIR} ${DEV_ENV_IMAGE}
1010

1111
bootstrap:
1212
${DEV_ENV_CMD} go mod vendor

cmd/cmd.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,18 @@ type Commander interface {
4141
DomainsAdd(string, string) error
4242
DomainsRemove(string, string) error
4343
ServicesList(string) error
44-
ServicesAdd(string, string, string) error
45-
ServicesRemove(string, string) error
44+
ServicesAdd(string, string, string, string) error
45+
ServicesRemove(string, string, string, int) error
46+
GatewaysAdd(string, string, int, string) error
47+
GatewaysList(string, int) error
48+
GatewaysRemove(string, string, int, string) error
49+
RoutesCreate(string, string, string, string, int) error
50+
RoutesList(string, int) error
51+
RoutesGet(string, string) error
52+
RoutesSet(string, string, string) error
53+
RoutesAttach(string, string, int, string) error
54+
RoutesDetach(string, string, int, string) error
55+
RoutesRemove(string, string) error
4656
GitRemote(string, string, bool) error
4757
GitRemove(string) error
4858
HealthchecksList(string, string) error
@@ -85,12 +95,10 @@ type Commander interface {
8595
TLSForceDisable(string) error
8696
TLSAutoEnable(string) error
8797
TLSAutoDisable(string) error
98+
TLSAutoIssuer(string, string, string, string, string) error
8899
UsersList(results int) error
89100
UsersEnable(string) error
90101
UsersDisable(string) error
91-
AllowlistAdd(string, string) error
92-
AllowlistList(string) error
93-
AllowlistRemove(string, string) error
94102
Println(...interface{}) (int, error)
95103
Print(...interface{}) (int, error)
96104
Printf(string, ...interface{}) (int, error)

cmd/gateways.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/drycc/controller-sdk-go/gateways"
8+
"github.com/olekukonko/tablewriter"
9+
)
10+
11+
// GatewaysList lists gateways for the app
12+
func (d *DryccCmd) GatewaysList(appID string, results int) error {
13+
s, appID, err := load(d.ConfigFile, appID)
14+
15+
if err != nil {
16+
return err
17+
}
18+
if results == defaultLimit {
19+
results = s.Limit
20+
}
21+
22+
gateways, count, err := gateways.List(s.Client, appID, results)
23+
if d.checkAPICompatibility(s.Client, err) != nil {
24+
return err
25+
}
26+
d.Printf("=== %s Gateways\n", appID)
27+
if count == 0 {
28+
d.Println("Could not find any gateway")
29+
} else {
30+
table := tablewriter.NewWriter(d.WOut)
31+
table.SetHeader([]string{"Name", "Lisenter", "Port", "Protocol", "allowedRoutes"})
32+
for _, gateway := range gateways {
33+
for _, listener := range gateway.Listeners {
34+
allowedRoutes, _ := json.Marshal(listener.AllowedRoutes)
35+
table.Append([]string{gateway.Name, listener.Name, fmt.Sprint(listener.Port), listener.Protocol, string(allowedRoutes)})
36+
}
37+
}
38+
table.SetAutoMergeCellsByColumnIndex([]int{0})
39+
table.SetRowLine(true)
40+
table.Render()
41+
}
42+
return nil
43+
}
44+
45+
// GatewaysAdd adds a gateway to an app.
46+
func (d *DryccCmd) GatewaysAdd(appID, name string, port int, protocol string) error {
47+
s, appID, err := load(d.ConfigFile, appID)
48+
49+
if err != nil {
50+
return err
51+
}
52+
d.Printf("Adding gateway %s to %s... ", name, appID)
53+
54+
quit := progress(d.WOut)
55+
err = gateways.New(s.Client, appID, name, port, protocol)
56+
quit <- true
57+
<-quit
58+
if d.checkAPICompatibility(s.Client, err) != nil {
59+
return err
60+
}
61+
62+
d.Println("done")
63+
return nil
64+
}
65+
66+
// GatewaysRemove removes a gateway registered with an app.
67+
func (d *DryccCmd) GatewaysRemove(appID, name string, port int, protocol string) error {
68+
s, appID, err := load(d.ConfigFile, appID)
69+
70+
if err != nil {
71+
return err
72+
}
73+
d.Printf("Removing gateway %s to %s... ", name, appID)
74+
75+
quit := progress(d.WOut)
76+
err = gateways.Delete(s.Client, appID, name, port, protocol)
77+
quit <- true
78+
<-quit
79+
if d.checkAPICompatibility(s.Client, err) != nil {
80+
return err
81+
}
82+
83+
d.Println("done")
84+
return nil
85+
}

cmd/gateways_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/drycc/controller-sdk-go/api"
10+
"github.com/drycc/workflow-cli/pkg/testutil"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestGatewaysList(t *testing.T) {
15+
t.Parallel()
16+
cf, server, err := testutil.NewTestServerAndClient()
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
defer server.Close()
21+
var b bytes.Buffer
22+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
23+
24+
server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, r *http.Request) {
25+
testutil.SetHeaders(w)
26+
fmt.Fprintf(w, `{
27+
"count": 1,
28+
"next": null,
29+
"previous": null,
30+
"results": [
31+
{
32+
"app": "foo",
33+
"name": "foo",
34+
"created": "2023-04-19T00:00:00UTC",
35+
"owner": "test",
36+
"updated": "2023-04-19T00:00:00UTC",
37+
"listeners": [
38+
{
39+
"name": "foo-80-http",
40+
"port": 80,
41+
"protocol": "HTTP",
42+
"allowedRoutes": {"namespaces": {"from": "All"}}
43+
},
44+
{
45+
"name": "foo-443-https",
46+
"port": 443,
47+
"protocol": "HTTPS",
48+
"allowedRoutes": {"namespaces": {"from": "All"}}
49+
}
50+
]
51+
}
52+
]
53+
}`)
54+
})
55+
56+
err = cmdr.GatewaysList("foo", -1)
57+
assert.NoError(t, err)
58+
59+
assert.Equal(t, b.String(), `=== foo Gateways
60+
+------+---------------+------+----------+-------------------------------+
61+
| NAME | LISENTER | PORT | PROTOCOL | ALLOWEDROUTES |
62+
+------+---------------+------+----------+-------------------------------+
63+
| foo | foo-80-http | 80 | HTTP | {"namespaces":{"from":"All"}} |
64+
+ +---------------+------+----------+-------------------------------+
65+
| | foo-443-https | 443 | HTTPS | {"namespaces":{"from":"All"}} |
66+
+------+---------------+------+----------+-------------------------------+
67+
`, "output")
68+
}
69+
70+
func TestGatewaysAdd(t *testing.T) {
71+
t.Parallel()
72+
cf, server, err := testutil.NewTestServerAndClient()
73+
if err != nil {
74+
t.Fatal(err)
75+
}
76+
defer server.Close()
77+
var b bytes.Buffer
78+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
79+
80+
server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, r *http.Request) {
81+
testutil.AssertBody(t, api.GatewayCreateRequest{Name: "example-go", Port: 443, Protocol: "HTTPS"}, r)
82+
testutil.SetHeaders(w)
83+
w.WriteHeader(http.StatusCreated)
84+
// Body isn't used by CLI, so it isn't set.
85+
w.Write([]byte("{}"))
86+
})
87+
88+
err = cmdr.GatewaysAdd("foo", "example-go", 443, "HTTPS")
89+
assert.NoError(t, err)
90+
91+
assert.Equal(t, testutil.StripProgress(b.String()), "Adding gateway example-go to foo... done\n", "output")
92+
}
93+
94+
func TestGatewaysDelete(t *testing.T) {
95+
t.Parallel()
96+
cf, server, err := testutil.NewTestServerAndClient()
97+
if err != nil {
98+
t.Fatal(err)
99+
}
100+
defer server.Close()
101+
var b bytes.Buffer
102+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
103+
104+
server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, r *http.Request) {
105+
testutil.SetHeaders(w)
106+
w.WriteHeader(http.StatusNoContent)
107+
})
108+
109+
err = cmdr.GatewaysRemove("foo", "example-go", 443, "HTTPS")
110+
assert.NoError(t, err)
111+
112+
assert.Equal(t, testutil.StripProgress(b.String()), "Removing gateway example-go to foo... done\n", "output")
113+
}

0 commit comments

Comments
 (0)