-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvolumes.go
More file actions
100 lines (93 loc) · 3 KB
/
volumes.go
File metadata and controls
100 lines (93 loc) · 3 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
// Package config provides methods for managing configuration of apps.
package volumes
import (
"encoding/json"
"fmt"
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
}
// 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
}
// 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
}