gogreen/storage_actions.go

133 lines
4.0 KiB
Go
Raw Normal View History

2016-04-18 09:38:29 +00:00
package godo
2016-11-29 02:42:23 +00:00
import (
"context"
2016-11-29 02:42:23 +00:00
"fmt"
"net/http"
2016-11-29 02:42:23 +00:00
)
2016-04-18 09:38:29 +00:00
// StorageActionsService is an interface for interfacing with the
// storage actions endpoints of the Digital Ocean API.
// See: https://developers.digitalocean.com/documentation/v2#storage-actions
type StorageActionsService interface {
2016-11-29 02:42:23 +00:00
Attach(ctx context.Context, volumeID string, dropletID int) (*Action, *Response, error)
DetachByDropletID(ctx context.Context, volumeID string, dropletID int) (*Action, *Response, error)
Get(ctx context.Context, volumeID string, actionID int) (*Action, *Response, error)
List(ctx context.Context, volumeID string, opt *ListOptions) ([]Action, *Response, error)
Resize(ctx context.Context, volumeID string, sizeGigabytes int, regionSlug string) (*Action, *Response, error)
2016-04-18 09:38:29 +00:00
}
// StorageActionsServiceOp handles communication with the storage volumes
2016-04-18 09:38:29 +00:00
// action related methods of the DigitalOcean API.
type StorageActionsServiceOp struct {
client *Client
}
// StorageAttachment represents the attachement of a block storage
2016-12-21 00:00:17 +00:00
// volume to a specific Droplet under the device name.
2016-04-18 09:38:29 +00:00
type StorageAttachment struct {
DropletID int `json:"droplet_id"`
}
2016-12-21 00:00:17 +00:00
// Attach a storage volume to a Droplet.
2016-11-29 02:42:23 +00:00
func (s *StorageActionsServiceOp) Attach(ctx context.Context, volumeID string, dropletID int) (*Action, *Response, error) {
2016-04-18 09:38:29 +00:00
request := &ActionRequest{
"type": "attach",
"droplet_id": dropletID,
}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, volumeID, request)
2016-04-18 09:38:29 +00:00
}
2016-12-21 00:00:17 +00:00
// DetachByDropletID a storage volume from a Droplet by Droplet ID.
2016-11-29 02:42:23 +00:00
func (s *StorageActionsServiceOp) DetachByDropletID(ctx context.Context, volumeID string, dropletID int) (*Action, *Response, error) {
2016-11-07 16:55:44 +00:00
request := &ActionRequest{
"type": "detach",
"droplet_id": dropletID,
}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, volumeID, request)
2016-11-07 16:55:44 +00:00
}
// Get an action for a particular storage volume by id.
2016-11-29 02:42:23 +00:00
func (s *StorageActionsServiceOp) Get(ctx context.Context, volumeID string, actionID int) (*Action, *Response, error) {
path := fmt.Sprintf("%s/%d", storageAllocationActionPath(volumeID), actionID)
2016-11-29 02:42:23 +00:00
return s.get(ctx, path)
}
// List the actions for a particular storage volume.
2016-11-29 02:42:23 +00:00
func (s *StorageActionsServiceOp) List(ctx context.Context, volumeID string, opt *ListOptions) ([]Action, *Response, error) {
path := storageAllocationActionPath(volumeID)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
2016-11-29 02:42:23 +00:00
return s.list(ctx, path)
}
2016-07-17 01:40:32 +00:00
// Resize a storage volume.
2016-11-29 02:42:23 +00:00
func (s *StorageActionsServiceOp) Resize(ctx context.Context, volumeID string, sizeGigabytes int, regionSlug string) (*Action, *Response, error) {
2016-07-17 01:40:32 +00:00
request := &ActionRequest{
"type": "resize",
"size_gigabytes": sizeGigabytes,
"region": regionSlug,
}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, volumeID, request)
2016-07-17 01:40:32 +00:00
}
2016-11-29 02:42:23 +00:00
func (s *StorageActionsServiceOp) doAction(ctx context.Context, volumeID string, request *ActionRequest) (*Action, *Response, error) {
2016-04-18 09:38:29 +00:00
path := storageAllocationActionPath(volumeID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, request)
2016-04-18 09:38:29 +00:00
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
2016-04-18 09:38:29 +00:00
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
2016-04-18 09:38:29 +00:00
}
2016-11-29 02:42:23 +00:00
func (s *StorageActionsServiceOp) get(ctx context.Context, path string) (*Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
}
2016-11-29 02:42:23 +00:00
func (s *StorageActionsServiceOp) list(ctx context.Context, path string) ([]Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(actionsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Actions, resp, err
}
2016-04-18 09:38:29 +00:00
func storageAllocationActionPath(volumeID string) string {
return fmt.Sprintf("%s/%s/actions", storageAllocPath, volumeID)
}