Skip to content

Commit de65d8c

Browse files
committed
feat(controller-sdk-go): add volume expand support
1 parent b9fc879 commit de65d8c

3 files changed

Lines changed: 108 additions & 16 deletions

File tree

resources/resources.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// Services is list all available resource services
1313
func Services(c *drycc.Client, results int) (api.ResourceServices, int, error) {
14-
u := fmt.Sprintf("/v2/resources/services/")
14+
u := "/v2/resources/services/"
1515
body, count, reqErr := c.LimitedRequest(u, results)
1616
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
1717
return []api.ResourceService{}, -1, reqErr

volumes/volumes.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,30 @@ func Create(c *drycc.Client, appID string, volume api.Volume) (api.Volume, error
4242
return newVolume, reqErr
4343
}
4444

45+
// Expand create an app's Volume.
46+
func Expand(c *drycc.Client, appID string, volume api.Volume) (api.Volume, error) {
47+
body, err := json.Marshal(volume)
48+
if err != nil {
49+
return api.Volume{}, err
50+
}
51+
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/", appID, volume.Name)
52+
res, reqErr := c.Request("PUT", u, body)
53+
if reqErr != nil {
54+
return api.Volume{}, reqErr
55+
}
56+
defer res.Body.Close()
57+
newVolume := api.Volume{}
58+
if err = json.NewDecoder(res.Body).Decode(&newVolume); err != nil {
59+
return api.Volume{}, err
60+
}
61+
return newVolume, reqErr
62+
}
63+
4564
// Delete delete an app's Volume.
4665
func Delete(c *drycc.Client, appID string, name string) error {
4766
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/", appID, name)
4867
res, err := c.Request("DELETE", u, nil)
49-
if err == nil {
50-
res.Body.Close()
51-
}
68+
defer res.Body.Close()
5269
return err
5370
}
5471

volumes/volumes_test.go

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,40 @@ package volumes
22

33
import (
44
"fmt"
5-
drycc "github.com/drycc/controller-sdk-go"
6-
"github.com/drycc/controller-sdk-go/api"
75
"io/ioutil"
86
"net/http"
97
"net/http/httptest"
108
"reflect"
119
"testing"
10+
11+
drycc "github.com/drycc/controller-sdk-go"
12+
"github.com/drycc/controller-sdk-go/api"
1213
)
1314

14-
const volumeCreateExpected string = `{"name":"myvolume","size":"500M"}`
15+
const volumeCreateExpected string = `{"name":"myvolume","size":"500G"}`
1516

1617
const volumeCreateFixture string = `
1718
{
1819
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
1920
"owner": "test",
2021
"app": "example-go",
2122
"name": "myvolume",
22-
"size": "500M",
23+
"size": "500G",
24+
"path": {},
25+
"created": "2020-08-26T00:00:00UTC",
26+
"updated": "2020-08-26T00:00:00UTC"
27+
}
28+
`
29+
30+
const volumeExpandExpected string = `{"name":"myvolume","size":"500G"}`
31+
32+
const volumeExpandFixture string = `
33+
{
34+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
35+
"owner": "test",
36+
"app": "example-go",
37+
"name": "myvolume",
38+
"size": "500G",
2339
"path": {},
2440
"created": "2020-08-26T00:00:00UTC",
2541
"updated": "2020-08-26T00:00:00UTC"
@@ -37,7 +53,7 @@ const volumesFixture string = `
3753
"owner": "test",
3854
"app": "example-go",
3955
"name": "myvolume",
40-
"size": "500M",
56+
"size": "500G",
4157
"path": {},
4258
"created": "2020-08-26T00:00:00UTC",
4359
"updated": "2020-08-26T00:00:00UTC"
@@ -52,7 +68,7 @@ const volumeMountFixture string = `
5268
"owner": "test",
5369
"app": "example-go",
5470
"name": "myvolume",
55-
"size": "500M",
71+
"size": "500G",
5672
"path": {
5773
"cmd": "/data/cmd1",
5874
"web": "/data/web1"
@@ -68,7 +84,7 @@ const volumeUnmountFixture string = `
6884
"owner": "test",
6985
"app": "unmount-test",
7086
"name": "myvolume",
71-
"size": "500M",
87+
"size": "500G",
7288
"path": {},
7389
"created": "2020-08-26T00:00:00UTC",
7490
"updated": "2020-08-26T00:00:00UTC"
@@ -105,6 +121,28 @@ func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
105121
return
106122
}
107123

124+
// Expand
125+
if req.URL.Path == "/v2/apps/example-go/volumes/myvolume/" && req.Method == "PUT" {
126+
body, err := ioutil.ReadAll(req.Body)
127+
128+
if err != nil {
129+
fmt.Println(err)
130+
res.WriteHeader(http.StatusInternalServerError)
131+
res.Write(nil)
132+
}
133+
134+
if string(body) != volumeExpandExpected {
135+
fmt.Printf("Expected '%s', Got '%s'\n", volumeExpandExpected, body)
136+
res.WriteHeader(http.StatusInternalServerError)
137+
res.Write(nil)
138+
return
139+
}
140+
141+
res.WriteHeader(http.StatusOK)
142+
res.Write([]byte(volumeExpandFixture))
143+
return
144+
}
145+
108146
// Delete
109147
if req.URL.Path == "/v2/apps/example-go/volumes/myvolume/" && req.Method == "DELETE" {
110148
res.WriteHeader(http.StatusNoContent)
@@ -171,7 +209,7 @@ func TestVolumesCreate(t *testing.T) {
171209
Owner: "test",
172210
App: "example-go",
173211
Name: "myvolume",
174-
Size: "500M",
212+
Size: "500G",
175213
Path: map[string]interface{}{},
176214
Created: "2020-08-26T00:00:00UTC",
177215
Updated: "2020-08-26T00:00:00UTC",
@@ -187,7 +225,7 @@ func TestVolumesCreate(t *testing.T) {
187225
}
188226
volume := api.Volume{
189227
Name: "myvolume",
190-
Size: "500M",
228+
Size: "500G",
191229
}
192230
actual, err := Create(drycc, "example-go", volume)
193231

@@ -200,6 +238,43 @@ func TestVolumesCreate(t *testing.T) {
200238
}
201239
}
202240

241+
func TestVolumesExpand(t *testing.T) {
242+
t.Parallel()
243+
244+
expected := api.Volume{
245+
UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
246+
Owner: "test",
247+
App: "example-go",
248+
Name: "myvolume",
249+
Size: "500G",
250+
Path: map[string]interface{}{},
251+
Created: "2020-08-26T00:00:00UTC",
252+
Updated: "2020-08-26T00:00:00UTC",
253+
}
254+
255+
handler := fakeHTTPServer{}
256+
server := httptest.NewServer(&handler)
257+
defer server.Close()
258+
259+
drycc, err := drycc.New(false, server.URL, "abc")
260+
if err != nil {
261+
t.Fatal(err)
262+
}
263+
volume := api.Volume{
264+
Name: "myvolume",
265+
Size: "500G",
266+
}
267+
actual, err := Expand(drycc, "example-go", volume)
268+
269+
if err != nil {
270+
t.Fatal(err)
271+
}
272+
273+
if !reflect.DeepEqual(expected, actual) {
274+
t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual))
275+
}
276+
}
277+
203278
func TestVolumesDelete(t *testing.T) {
204279
t.Parallel()
205280

@@ -227,7 +302,7 @@ func TestVolumesList(t *testing.T) {
227302
Owner: "test",
228303
Name: "myvolume",
229304
Path: map[string]interface{}{},
230-
Size: "500M",
305+
Size: "500G",
231306
Created: "2020-08-26T00:00:00UTC",
232307
Updated: "2020-08-26T00:00:00UTC",
233308
},
@@ -273,7 +348,7 @@ func TestVolumeMount(t *testing.T) {
273348
"cmd": "/data/cmd1",
274349
"web": "/data/web1",
275350
},
276-
Size: "500M",
351+
Size: "500G",
277352
Created: "2020-08-26T00:00:00UTC",
278353
Updated: "2020-08-26T00:00:00UTC",
279354
UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
@@ -313,7 +388,7 @@ func TestVolumeUnmount(t *testing.T) {
313388
Owner: "test",
314389
App: "unmount-test",
315390
Path: map[string]interface{}{},
316-
Size: "500M",
391+
Size: "500G",
317392
Created: "2020-08-26T00:00:00UTC",
318393
Updated: "2020-08-26T00:00:00UTC",
319394
UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",

0 commit comments

Comments
 (0)