-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvolumes.go
More file actions
154 lines (143 loc) · 4.38 KB
/
volumes.go
File metadata and controls
154 lines (143 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Package volumes provides methods for managing volumes of apps.
package volumes
import (
"context"
"encoding/json"
"fmt"
"time"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// List list an app's volumes.
func List(c *drycc.Client, appID string, results int) (api.Volumes, int, error) {
u := fmt.Sprintf("/v2/apps/%s/volumes/", appID)
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.Volume{}, -1, reqErr
}
var volumes []api.Volume
if err := json.Unmarshal([]byte(body), &volumes); err != nil {
return []api.Volume{}, -1, err
}
return volumes, count, reqErr
}
// Get an app's volume.
func Get(c *drycc.Client, appID string, name string) (api.Volume, error) {
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/", appID, name)
res, reqErr := c.Request("GET", u, nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.Volume{}, reqErr
}
defer res.Body.Close()
volume := api.Volume{}
if err := json.NewDecoder(res.Body).Decode(&volume); err != nil {
return volume, err
}
return volume, nil
}
// Create create an app's Volume.
func Create(c *drycc.Client, appID string, volume api.Volume) (api.Volume, error) {
body, err := json.Marshal(volume)
if err != nil {
return api.Volume{}, err
}
u := fmt.Sprintf("/v2/apps/%s/volumes/", appID)
res, reqErr := c.Request("POST", u, body)
if reqErr != nil {
return api.Volume{}, reqErr
}
defer res.Body.Close()
newVolume := api.Volume{}
if err = json.NewDecoder(res.Body).Decode(&newVolume); err != nil {
return api.Volume{}, err
}
return newVolume, reqErr
}
// Expand create an app's Volume.
func Expand(c *drycc.Client, appID string, volume api.Volume) (api.Volume, error) {
body, err := json.Marshal(volume)
if err != nil {
return api.Volume{}, err
}
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/", appID, volume.Name)
res, reqErr := c.Request("PATCH", u, body)
if reqErr != nil {
return api.Volume{}, reqErr
}
defer res.Body.Close()
newVolume := api.Volume{}
if err = json.NewDecoder(res.Body).Decode(&newVolume); err != nil {
return api.Volume{}, err
}
return newVolume, reqErr
}
// Serve serves an app's volume.
func Serve(parent context.Context, c *drycc.Client, appID, name string) (context.Context, map[string]string, error) {
basePath := fmt.Sprintf("/v2/apps/%s/volumes/%s/filer", appID, name)
res, err := c.Request("POST", fmt.Sprintf("%s/_/bind", basePath), nil)
if err != nil {
return nil, nil, err
}
defer res.Body.Close()
var filer map[string]string
if err = json.NewDecoder(res.Body).Decode(&filer); err != nil {
return nil, nil, err
}
ctx, cancel := context.WithCancel(parent)
go func() {
defer cancel()
for {
select {
case <-parent.Done():
return
default:
res, err := c.Request("GET", fmt.Sprintf("%s/_/ping", basePath), nil)
if err != nil {
return
}
if err := res.Body.Close(); err != nil {
return
}
time.Sleep(time.Second * 30)
}
}
}()
filer["endpoint"] = fmt.Sprintf("%s%s/webdav/", c.ControllerURL, basePath)
return ctx, filer, nil
}
// Delete delete an app's Volume.
func Delete(c *drycc.Client, appID string, name string) error {
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/", appID, name)
res, err := c.Request("DELETE", u, nil)
if err == nil {
res.Body.Close()
}
return err
}
// Mount mount an app's volume and creates a new release.
// This is a patching operation, which means when you call Mount() with an api.Volumes:
//
// - If the variable does not exist, it will be set.
// - If the variable exists, it will be overwritten.
// - If the variable is set to nil, it will be unmount.
// - If the variable was ignored in the api.Volumes, it will remain unchanged.
//
// Calling Mount() with an empty api.Volume will return a drycc.ErrConflict.
// Trying to Unmount a key that does not exist returns a drycc.ErrUnprocessable.
func Mount(c *drycc.Client, appID string, name string, volume api.Volume) (api.Volume, error) {
body, err := json.Marshal(volume)
if err != nil {
return api.Volume{}, err
}
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/path/", appID, name)
res, reqErr := c.Request("PATCH", u, body)
if reqErr != nil {
return api.Volume{}, reqErr
}
defer res.Body.Close()
newVolume := api.Volume{}
if err = json.NewDecoder(res.Body).Decode(&newVolume); err != nil {
return api.Volume{}, err
}
return newVolume, reqErr
}