Skip to content

Commit 51eb66f

Browse files
author
lijianguo
committed
feat(controller-sdk-go): drycc run cmd add --mount para
1 parent e73ce53 commit 51eb66f

7 files changed

Lines changed: 460 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ _testmain.go
2525

2626
vendor
2727
coverage.txt
28+
.idea/*

api/apps.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type AppUpdateRequest struct {
2929
// AppRunRequest is the definition of POST /v2/apps/<app id>/run.
3030
type AppRunRequest struct {
3131
Command string `json:"command"`
32+
Volumes map[string]interface{} `json:"volumes,omitempty"`
3233
}
3334

3435
// AppRunResponse is the definition of /v2/apps/<app id>/run.

api/volumes.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package api
2+
3+
// Mount is the definition of PATCH /v2/apps/<app_id>/volumes/<name>/path/.
4+
type Mount struct {
5+
Values map[string]string `json:"values"`
6+
}
7+
8+
// Unmount is the definition of PATCH /v2/apps/<app_id>/volumes/<name>/path/.
9+
type Unmount struct {
10+
Values map[string]interface{} `json:"values"`
11+
}
12+
13+
// Volume is the structure of an app's volume.
14+
type Volume struct {
15+
// Owner is the app owner.
16+
Owner string `json:"owner,omitempty"`
17+
// App is the app the tls settings apply to and cannot be updated.
18+
App string `json:"app,omitempty"`
19+
// Created is the time that the volume was created and cannot be updated.
20+
Created string `json:"created,omitempty"`
21+
// Updated is the last time the TLS settings was changed and cannot be updated.
22+
Updated string `json:"updated,omitempty"`
23+
// UUID is a unique string reflecting the volume in its current state.
24+
// It changes every time the volume is changed and cannot be updated.
25+
UUID string `json:"uuid,omitempty"`
26+
// Volume's name
27+
Name string `json:"name,omitempty"`
28+
//Volume's size
29+
Size string `json:"size,omitempty"`
30+
// mount application's path
31+
Path map[string]interface{} `json:"path,omitempty"`
32+
}
33+
34+
type Volumes []Volume

apps/apps.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ func Logs(c *drycc.Client, appID string, lines int) (string, error) {
112112

113113
// Run a one-time command in your app. This will start a kubernetes job with the
114114
// same container image and environment as the rest of the app.
115-
func Run(c *drycc.Client, appID string, command string) (api.AppRunResponse, error) {
116-
req := api.AppRunRequest{Command: command}
115+
func Run(c *drycc.Client, appID string, command string, volumes map[string]interface{}) (api.AppRunResponse, error) {
116+
req := api.AppRunRequest{
117+
Command: command,
118+
Volumes: volumes,
119+
}
117120
body, err := json.Marshal(req)
118121

119122
if err != nil {

apps/apps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func TestAppsRun(t *testing.T) {
252252
t.Fatal(err)
253253
}
254254

255-
actual, err := Run(drycc, "example-go", "echo hi")
255+
actual, err := Run(drycc, "example-go", "echo hi", nil)
256256

257257
if err != nil {
258258
t.Fatal(err)

volumes/volumes.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Package config provides methods for managing configuration of apps.
2+
package volumes
3+
4+
import (
5+
"encoding/json"
6+
"fmt"
7+
8+
drycc "github.com/drycc/controller-sdk-go"
9+
"github.com/drycc/controller-sdk-go/api"
10+
)
11+
12+
// List list an app's volumes.
13+
func List(c *drycc.Client, appID string, results int) (api.Volumes, int, error) {
14+
u := fmt.Sprintf("/v2/apps/%s/volumes/", appID)
15+
body, count, reqErr := c.LimitedRequest(u, results)
16+
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
17+
return []api.Volume{}, -1, reqErr
18+
}
19+
var volumes []api.Volume
20+
if err := json.Unmarshal([]byte(body), &volumes); err != nil {
21+
return []api.Volume{}, -1, err
22+
}
23+
return volumes, count, reqErr
24+
}
25+
26+
// Create create an app's Volume.
27+
func Create(c *drycc.Client, appID string, volume api.Volume) (api.Volume, error) {
28+
body, err := json.Marshal(volume)
29+
if err != nil {
30+
return api.Volume{}, err
31+
}
32+
u := fmt.Sprintf("/v2/apps/%s/volumes/", appID)
33+
res, reqErr := c.Request("POST", u, body)
34+
if reqErr != nil {
35+
return api.Volume{}, reqErr
36+
}
37+
defer res.Body.Close()
38+
newVolume := api.Volume{}
39+
if err = json.NewDecoder(res.Body).Decode(&newVolume); err != nil {
40+
return api.Volume{}, err
41+
}
42+
return newVolume, reqErr
43+
}
44+
45+
// Delete delete an app's Volume.
46+
func Delete(c *drycc.Client, appID string, name string) error {
47+
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/", appID, name)
48+
res, err := c.Request("DELETE", u, nil)
49+
if err == nil {
50+
res.Body.Close()
51+
}
52+
return err
53+
}
54+
55+
// Mount mount an app's volume and creates a new release.
56+
// This is a patching operation, which means when you call Mount() with an api.Volumes:
57+
//
58+
// - If the variable does not exist, it will be set.
59+
// - If the variable exists, it will be overwritten.
60+
// - If the variable is set to nil, it will be unmount.
61+
// - If the variable was ignored in the api.Volumes, it will remain unchanged.
62+
//
63+
// Calling Mount() with an empty api.Volume will return a drycc.ErrConflict.
64+
// Trying to Unmount a key that does not exist returns a drycc.ErrUnprocessable.
65+
func Mount(c *drycc.Client, appID string, name string, volume api.Volume) (api.Volume, error) {
66+
body, err := json.Marshal(volume)
67+
if err != nil {
68+
return api.Volume{}, err
69+
}
70+
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/path/", appID, name)
71+
res, reqErr := c.Request("PATCH", u, body)
72+
if reqErr != nil {
73+
return api.Volume{}, reqErr
74+
}
75+
defer res.Body.Close()
76+
newVolume := api.Volume{}
77+
if err = json.NewDecoder(res.Body).Decode(&newVolume); err != nil {
78+
return api.Volume{}, err
79+
}
80+
return newVolume, reqErr
81+
}

0 commit comments

Comments
 (0)