|
| 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