Skip to content

Commit eb34e0b

Browse files
committed
feat(workflow-cli): add resources services and plans api
1 parent f5eae01 commit eb34e0b

7 files changed

Lines changed: 226 additions & 9 deletions

File tree

cmd/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ type Commander interface {
102102
VolumesList(string, int) error
103103
VolumesMount(string, string, []string) error
104104
VolumesUnmount(string, string, []string) error
105+
ResourcesServices(int) error
106+
ResourcesPlans(string, int) error
105107
ResourcesCreate(string, string, string, []string) error
106108
ResourcesList(string, int) error
107109
ResourceDelete(string, string) error

cmd/resources.go

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,77 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/drycc/controller-sdk-go/api"
6-
"github.com/drycc/controller-sdk-go/resources"
7-
"github.com/drycc/pkg/prettyprint"
85
"io"
96
"regexp"
7+
"strconv"
108
"strings"
9+
10+
"github.com/olekukonko/tablewriter"
11+
12+
"github.com/drycc/controller-sdk-go/api"
13+
"github.com/drycc/controller-sdk-go/resources"
14+
"github.com/drycc/pkg/prettyprint"
15+
"github.com/drycc/workflow-cli/settings"
1116
)
1217

18+
// ResourceServices list resource service
19+
func (d *DryccCmd) ResourcesServices(results int) error {
20+
s, err := settings.Load(d.ConfigFile)
21+
22+
if err != nil {
23+
return err
24+
}
25+
26+
if results == defaultLimit {
27+
results = s.Limit
28+
}
29+
services, count, err := resources.Services(s.Client, results)
30+
if d.checkAPICompatibility(s.Client, err) != nil {
31+
return err
32+
}
33+
34+
if count == 0 {
35+
d.Println("Could not find any services")
36+
} else {
37+
table := tablewriter.NewWriter(d.WOut)
38+
table.SetHeader([]string{"Name", "Updateable"})
39+
for _, service := range services {
40+
table.Append([]string{service.Name, strconv.FormatBool(service.Updateable)})
41+
}
42+
table.Render()
43+
}
44+
return nil
45+
}
46+
47+
// ResourcePlans list resource plans
48+
func (d *DryccCmd) ResourcesPlans(serviceName string, results int) error {
49+
s, err := settings.Load(d.ConfigFile)
50+
51+
if err != nil {
52+
return err
53+
}
54+
55+
if results == defaultLimit {
56+
results = s.Limit
57+
}
58+
plans, count, err := resources.Plans(s.Client, serviceName, results)
59+
if d.checkAPICompatibility(s.Client, err) != nil {
60+
return err
61+
}
62+
63+
if count == 0 {
64+
d.Println("Could not find any services")
65+
} else {
66+
table := tablewriter.NewWriter(d.WOut)
67+
table.SetHeader([]string{"Name", "Description"})
68+
for _, plan := range plans {
69+
table.Append([]string{plan.Name, plan.Description})
70+
}
71+
table.Render()
72+
}
73+
return nil
74+
}
75+
1376
// ResourcesCreate create a resource for the application
1477
func (d *DryccCmd) ResourcesCreate(appID, plan string, name string, params []string) error {
1578
s, appID, err := load(d.ConfigFile, appID)
@@ -150,7 +213,7 @@ func (d *DryccCmd) ResourceBind(appID string, name string) error {
150213
d.Print("Binding resource... ")
151214

152215
quit := progress(d.WOut)
153-
bindAction := api.Binding{BindAction: "bind"}
216+
bindAction := api.ResourceBinding{BindAction: "bind"}
154217
_, err = resources.Binding(s.Client, appID, name, bindAction)
155218
quit <- true
156219
<-quit
@@ -174,7 +237,7 @@ func (d *DryccCmd) ResourceUnbind(appID string, name string) error {
174237
d.Print("Unbinding resource... ")
175238

176239
quit := progress(d.WOut)
177-
bindAction := api.Binding{BindAction: "unbind"}
240+
bindAction := api.ResourceBinding{BindAction: "unbind"}
178241
_, err = resources.Binding(s.Client, appID, name, bindAction)
179242
quit <- true
180243
<-quit

cmd/resources_test.go

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,84 @@ package cmd
33
import (
44
"bytes"
55
"fmt"
6-
"github.com/drycc/controller-sdk-go/api"
76
"net/http"
87
"testing"
98

9+
"github.com/drycc/controller-sdk-go/api"
10+
1011
"github.com/arschles/assert"
1112
"github.com/drycc/workflow-cli/pkg/testutil"
1213
)
1314

15+
func TestResourcesServices(t *testing.T) {
16+
t.Parallel()
17+
cf, server, err := testutil.NewTestServerAndClient()
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
defer server.Close()
22+
var b bytes.Buffer
23+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
24+
25+
server.Mux.HandleFunc("/v2/resources/services/", func(w http.ResponseWriter, r *http.Request) {
26+
testutil.SetHeaders(w)
27+
fmt.Fprintf(w, `{
28+
"results": [
29+
{
30+
"id": "332588e0-6c2c-4f56-a6af-a56fd01ec4b4",
31+
"name": "mysql",
32+
"updateable": true
33+
}
34+
],
35+
"count": 1
36+
}`)
37+
})
38+
39+
err = cmdr.ResourcesServices(100)
40+
assert.NoErr(t, err)
41+
42+
assert.Equal(t, b.String(), `+-------+------------+
43+
| NAME | UPDATEABLE |
44+
+-------+------------+
45+
| mysql | true |
46+
+-------+------------+
47+
`, "output")
48+
}
49+
50+
func TestResourcesPlans(t *testing.T) {
51+
t.Parallel()
52+
cf, server, err := testutil.NewTestServerAndClient()
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
defer server.Close()
57+
var b bytes.Buffer
58+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
59+
60+
server.Mux.HandleFunc("/v2/resources/services/mysql/plans/", func(w http.ResponseWriter, r *http.Request) {
61+
testutil.SetHeaders(w)
62+
fmt.Fprintf(w, `{
63+
"results": [{
64+
"id": "4d1dbd33-201b-45bc-9abb-757584ef7ab8",
65+
"name": "standard-1600",
66+
"description": "mysql standard-1600 plan which limit persistence size 1600Gi."
67+
}],
68+
"count": 1
69+
}`)
70+
})
71+
72+
err = cmdr.ResourcesPlans("mysql", 100)
73+
assert.NoErr(t, err)
74+
75+
assert.Equal(t, b.String(), `+---------------+--------------------------------+
76+
| NAME | DESCRIPTION |
77+
+---------------+--------------------------------+
78+
| standard-1600 | mysql standard-1600 plan which |
79+
| | limit persistence size 1600Gi. |
80+
+---------------+--------------------------------+
81+
`, "output")
82+
}
83+
1484
func TestResourcesCreate(t *testing.T) {
1585
t.Parallel()
1686
cf, server, err := testutil.NewTestServerAndClient()
@@ -178,7 +248,7 @@ func TestResourceBind(t *testing.T) {
178248
server.Mux.HandleFunc("/v2/apps/example-go/resources/mysql/binding/", func(w http.ResponseWriter, r *http.Request) {
179249
testutil.SetHeaders(w)
180250
if r.Method == "PATCH" {
181-
testutil.AssertBody(t, api.Binding{
251+
testutil.AssertBody(t, api.ResourceBinding{
182252
BindAction: "bind",
183253
}, r)
184254
}
@@ -218,7 +288,7 @@ func TestResourceUnbind(t *testing.T) {
218288
server.Mux.HandleFunc("/v2/apps/example-go/resources/mysql/binding/", func(w http.ResponseWriter, r *http.Request) {
219289
testutil.SetHeaders(w)
220290
if r.Method == "PATCH" {
221-
testutil.AssertBody(t, api.Binding{
291+
testutil.AssertBody(t, api.ResourceBinding{
222292
BindAction: "unbind",
223293
}, r)
224294
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.17
55
require (
66
github.com/arschles/assert v1.0.1-0.20191213221312-71f210f9375a
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20211124095117-dbf56da8c5a8
8+
github.com/drycc/controller-sdk-go v0.0.0-20211213072304-8a9f2f28d524
99
github.com/drycc/pkg v0.0.0-20210826011456-c60b87108840
1010
github.com/olekukonko/tablewriter v0.0.5
1111
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ github.com/drycc/controller-sdk-go v0.0.0-20210826011521-d57f3c75935a h1:TKz7m1c
5353
github.com/drycc/controller-sdk-go v0.0.0-20210826011521-d57f3c75935a/go.mod h1:DmVcssrb6yJMWI8sGT+8Xy3caVR5kSlsctD3a7Zn6FA=
5454
github.com/drycc/controller-sdk-go v0.0.0-20211124095117-dbf56da8c5a8 h1:cddNthWLhOdgvpF5S78Fc7j8I5ESDRUtTZhHIFTLcC0=
5555
github.com/drycc/controller-sdk-go v0.0.0-20211124095117-dbf56da8c5a8/go.mod h1:aGxL2AkmmG7d5afWiUGy93/+UPDnIxgsWSwxuznjteE=
56+
github.com/drycc/controller-sdk-go v0.0.0-20211213072304-8a9f2f28d524 h1:45Ufw74Ye6AXR3X5IzwJvSeByFiaV1nw3eh++mvLQbE=
57+
github.com/drycc/controller-sdk-go v0.0.0-20211213072304-8a9f2f28d524/go.mod h1:aGxL2AkmmG7d5afWiUGy93/+UPDnIxgsWSwxuznjteE=
5658
github.com/drycc/pkg v0.0.0-20210826011456-c60b87108840 h1:0OhP9AQ0mh3q0TMxK4PJTPSFwD/wj0xugiaZ3lnLcNA=
5759
github.com/drycc/pkg v0.0.0-20210826011456-c60b87108840/go.mod h1:KX1FLm7Fq6FLCsjjRsgfI/bMQuHZXqYf1ZXU9fzJhDw=
5860
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=

parser/resources.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ func Resources(argv []string, cmdr cmd.Commander) error {
1010
usage := `
1111
Valid commands for resources:
1212
13+
resources:services list all available resource services
14+
resources:plans list all available plans for an resource services
1315
resources:create create a resource for the application
1416
resources:list list resources in the application
1517
resources:describe get a resource detail info in the application
@@ -22,6 +24,10 @@ Use 'drycc help [command]' to learn more.
2224
`
2325

2426
switch argv[0] {
27+
case "resources:services":
28+
return resourcesServices(argv, cmdr)
29+
case "resources:plans":
30+
return resourcesPlans(argv, cmdr)
2531
case "resources:create":
2632
return resourcesCreate(argv, cmdr)
2733
case "resources:list":
@@ -51,6 +57,64 @@ Use 'drycc help [command]' to learn more.
5157
}
5258
}
5359

60+
func resourcesServices(argv []string, cmdr cmd.Commander) error {
61+
usage := `
62+
List all available resource services.
63+
64+
Usage: drycc resources:services [options]
65+
66+
Options:
67+
-l --limit=<num>
68+
the maximum number of results to display, defaults to config setting
69+
`
70+
71+
args, err := docopt.Parse(usage, argv, true, "", false, true)
72+
73+
if err != nil {
74+
return err
75+
}
76+
77+
results, err := responseLimit(safeGetValue(args, "--limit"))
78+
79+
if err != nil {
80+
return err
81+
}
82+
83+
return cmdr.ResourcesServices(results)
84+
}
85+
86+
func resourcesPlans(argv []string, cmdr cmd.Commander) error {
87+
usage := `
88+
List all available plans for an resource services.
89+
90+
Usage: drycc resources:plans <service> [options]
91+
92+
Arguments:
93+
<service>
94+
the service name for plans.
95+
96+
Options:
97+
-l --limit=<num>
98+
the maximum number of results to display, defaults to config setting
99+
`
100+
101+
args, err := docopt.Parse(usage, argv, true, "", false, true)
102+
103+
if err != nil {
104+
return err
105+
}
106+
107+
service := safeGetValue(args, "<service>")
108+
109+
results, err := responseLimit(safeGetValue(args, "--limit"))
110+
111+
if err != nil {
112+
return err
113+
}
114+
115+
return cmdr.ResourcesPlans(service, results)
116+
}
117+
54118
func resourcesCreate(argv []string, cmdr cmd.Commander) error {
55119
usage := `
56120
Create a resource for the application.

parser/resources_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
// Create fake implementations of each method that return the argument
1313
// we expect to have called the function (as an error to satisfy the interface).
1414

15+
func (d FakeDryccCmd) ResourcesServices(int) error {
16+
return errors.New("resources:services")
17+
}
18+
19+
func (d FakeDryccCmd) ResourcesPlans(string, int) error {
20+
return errors.New("resources:plans")
21+
}
22+
1523
func (d FakeDryccCmd) ResourcesCreate(string, string, string, []string) error {
1624
return errors.New("resources:create")
1725
}
@@ -57,6 +65,14 @@ func TestResources(t *testing.T) {
5765
args []string
5866
expected string
5967
}{
68+
{
69+
args: []string{"resources:services"},
70+
expected: "",
71+
},
72+
{
73+
args: []string{"resources:plans", "mysql"},
74+
expected: "",
75+
},
6076
{
6177
args: []string{"resources:create", "mysql:5.6", "mysql", "key1=value1"},
6278
expected: "",

0 commit comments

Comments
 (0)