gogreen/droplet_actions.go

330 lines
12 KiB
Go
Raw Normal View History

2014-09-03 14:03:30 +00:00
package godo
import (
"context"
2014-09-03 14:03:30 +00:00
"fmt"
"net/http"
2014-09-03 14:03:30 +00:00
"net/url"
)
// ActionRequest represents DigitalOcean Action Request
type ActionRequest map[string]interface{}
2016-12-21 00:00:17 +00:00
// DropletActionsService is an interface for interfacing with the Droplet actions
2015-09-18 13:10:59 +00:00
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Droplet-Actions
type DropletActionsService interface {
2016-11-29 02:42:23 +00:00
Shutdown(context.Context, int) (*Action, *Response, error)
ShutdownByTag(context.Context, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
PowerOff(context.Context, int) (*Action, *Response, error)
PowerOffByTag(context.Context, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
PowerOn(context.Context, int) (*Action, *Response, error)
PowerOnByTag(context.Context, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
PowerCycle(context.Context, int) (*Action, *Response, error)
PowerCycleByTag(context.Context, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
Reboot(context.Context, int) (*Action, *Response, error)
Restore(context.Context, int, int) (*Action, *Response, error)
Resize(context.Context, int, string, bool) (*Action, *Response, error)
Rename(context.Context, int, string) (*Action, *Response, error)
Snapshot(context.Context, int, string) (*Action, *Response, error)
SnapshotByTag(context.Context, string, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
EnableBackups(context.Context, int) (*Action, *Response, error)
EnableBackupsByTag(context.Context, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
DisableBackups(context.Context, int) (*Action, *Response, error)
DisableBackupsByTag(context.Context, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
PasswordReset(context.Context, int) (*Action, *Response, error)
RebuildByImageID(context.Context, int, int) (*Action, *Response, error)
RebuildByImageSlug(context.Context, int, string) (*Action, *Response, error)
ChangeKernel(context.Context, int, int) (*Action, *Response, error)
EnableIPv6(context.Context, int) (*Action, *Response, error)
EnableIPv6ByTag(context.Context, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
EnablePrivateNetworking(context.Context, int) (*Action, *Response, error)
EnablePrivateNetworkingByTag(context.Context, string) ([]Action, *Response, error)
2016-11-29 02:42:23 +00:00
Get(context.Context, int, int) (*Action, *Response, error)
GetByURI(context.Context, string) (*Action, *Response, error)
}
2016-12-21 00:00:17 +00:00
// DropletActionsServiceOp handles communication with the Droplet action related
2014-09-03 14:03:30 +00:00
// methods of the DigitalOcean API.
type DropletActionsServiceOp struct {
2014-09-03 14:03:30 +00:00
client *Client
}
var _ DropletActionsService = &DropletActionsServiceOp{}
2014-09-03 14:03:30 +00:00
// Shutdown a Droplet
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) Shutdown(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "shutdown"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
2014-09-03 14:03:30 +00:00
}
2016-12-21 00:00:17 +00:00
// ShutdownByTag shuts down Droplets matched by a Tag.
func (s *DropletActionsServiceOp) ShutdownByTag(ctx context.Context, tag string) ([]Action, *Response, error) {
request := &ActionRequest{"type": "shutdown"}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
2014-09-03 14:03:30 +00:00
// PowerOff a Droplet
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) PowerOff(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_off"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
2014-09-03 14:03:30 +00:00
}
2016-12-21 00:00:17 +00:00
// PowerOffByTag powers off Droplets matched by a Tag.
func (s *DropletActionsServiceOp) PowerOffByTag(ctx context.Context, tag string) ([]Action, *Response, error) {
request := &ActionRequest{"type": "power_off"}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
// PowerOn a Droplet
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) PowerOn(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_on"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// PowerOnByTag powers on Droplets matched by a Tag.
func (s *DropletActionsServiceOp) PowerOnByTag(ctx context.Context, tag string) ([]Action, *Response, error) {
request := &ActionRequest{"type": "power_on"}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
2014-09-03 14:03:30 +00:00
// PowerCycle a Droplet
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) PowerCycle(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_cycle"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
2014-09-03 14:03:30 +00:00
}
2016-12-21 00:00:17 +00:00
// PowerCycleByTag power cycles Droplets matched by a Tag.
func (s *DropletActionsServiceOp) PowerCycleByTag(ctx context.Context, tag string) ([]Action, *Response, error) {
request := &ActionRequest{"type": "power_cycle"}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
2014-09-03 14:03:30 +00:00
// Reboot a Droplet
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) Reboot(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "reboot"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
2014-09-03 14:03:30 +00:00
}
// Restore an image to a Droplet
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) Restore(ctx context.Context, id, imageID int) (*Action, *Response, error) {
2014-09-03 14:03:30 +00:00
requestType := "restore"
request := &ActionRequest{
"type": requestType,
"image": float64(imageID),
2014-09-03 14:03:30 +00:00
}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
2014-09-03 14:03:30 +00:00
}
// Resize a Droplet
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) Resize(ctx context.Context, id int, sizeSlug string, resizeDisk bool) (*Action, *Response, error) {
2014-09-03 14:03:30 +00:00
requestType := "resize"
request := &ActionRequest{
"type": requestType,
"size": sizeSlug,
"disk": resizeDisk,
2014-09-03 14:03:30 +00:00
}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
2014-09-03 14:03:30 +00:00
}
// Rename a Droplet
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) Rename(ctx context.Context, id int, name string) (*Action, *Response, error) {
2014-09-03 14:03:30 +00:00
requestType := "rename"
request := &ActionRequest{
"type": requestType,
"name": name,
2014-09-03 14:03:30 +00:00
}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
2014-09-03 14:03:30 +00:00
}
2015-06-01 14:44:30 +00:00
// Snapshot a Droplet.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) Snapshot(ctx context.Context, id int, name string) (*Action, *Response, error) {
requestType := "snapshot"
request := &ActionRequest{
"type": requestType,
"name": name,
}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// SnapshotByTag snapshots Droplets matched by a Tag.
func (s *DropletActionsServiceOp) SnapshotByTag(ctx context.Context, tag string, name string) ([]Action, *Response, error) {
requestType := "snapshot"
request := &ActionRequest{
"type": requestType,
"name": name,
}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
2016-12-21 00:00:17 +00:00
// EnableBackups enables backups for a Droplet.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) EnableBackups(ctx context.Context, id int) (*Action, *Response, error) {
2015-11-06 00:02:24 +00:00
request := &ActionRequest{"type": "enable_backups"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
2015-11-06 00:02:24 +00:00
}
2016-12-21 00:00:17 +00:00
// EnableBackupsByTag enables backups for Droplets matched by a Tag.
func (s *DropletActionsServiceOp) EnableBackupsByTag(ctx context.Context, tag string) ([]Action, *Response, error) {
request := &ActionRequest{"type": "enable_backups"}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
2016-12-21 00:00:17 +00:00
// DisableBackups disables backups for a Droplet.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) DisableBackups(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "disable_backups"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// DisableBackupsByTag disables backups for Droplet matched by a Tag.
func (s *DropletActionsServiceOp) DisableBackupsByTag(ctx context.Context, tag string) ([]Action, *Response, error) {
request := &ActionRequest{"type": "disable_backups"}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
2016-12-21 00:00:17 +00:00
// PasswordReset resets the password for a Droplet.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) PasswordReset(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "password_reset"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// RebuildByImageID rebuilds a Droplet from an image with a given id.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) RebuildByImageID(ctx context.Context, id, imageID int) (*Action, *Response, error) {
request := &ActionRequest{"type": "rebuild", "image": imageID}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// RebuildByImageSlug rebuilds a Droplet from an Image matched by a given Slug.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) RebuildByImageSlug(ctx context.Context, id int, slug string) (*Action, *Response, error) {
request := &ActionRequest{"type": "rebuild", "image": slug}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// ChangeKernel changes the kernel for a Droplet.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) ChangeKernel(ctx context.Context, id, kernelID int) (*Action, *Response, error) {
request := &ActionRequest{"type": "change_kernel", "kernel": kernelID}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// EnableIPv6 enables IPv6 for a Droplet.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) EnableIPv6(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_ipv6"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// EnableIPv6ByTag enables IPv6 for Droplets matched by a Tag.
func (s *DropletActionsServiceOp) EnableIPv6ByTag(ctx context.Context, tag string) ([]Action, *Response, error) {
request := &ActionRequest{"type": "enable_ipv6"}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
2016-12-21 00:00:17 +00:00
// EnablePrivateNetworking enables private networking for a Droplet.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) EnablePrivateNetworking(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_private_networking"}
2016-11-29 02:42:23 +00:00
return s.doAction(ctx, id, request)
}
2016-12-21 00:00:17 +00:00
// EnablePrivateNetworkingByTag enables private networking for Droplets matched by a Tag.
func (s *DropletActionsServiceOp) EnablePrivateNetworkingByTag(ctx context.Context, tag string) ([]Action, *Response, error) {
request := &ActionRequest{"type": "enable_private_networking"}
2016-11-29 02:42:23 +00:00
return s.doActionByTag(ctx, tag, request)
}
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) doAction(ctx context.Context, id int, request *ActionRequest) (*Action, *Response, error) {
if id < 1 {
return nil, nil, NewArgError("id", "cannot be less than 1")
}
if request == nil {
return nil, nil, NewArgError("request", "request can't be nil")
}
2014-09-03 14:03:30 +00:00
path := dropletActionPath(id)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, request)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
2014-09-03 14:03:30 +00:00
}
func (s *DropletActionsServiceOp) doActionByTag(ctx context.Context, tag string, request *ActionRequest) ([]Action, *Response, error) {
if tag == "" {
return nil, nil, NewArgError("tag", "cannot be empty")
}
if request == nil {
return nil, nil, NewArgError("request", "request can't be nil")
}
path := dropletActionPathByTag(tag)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, request)
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
}
return root.Actions, resp, err
}
2016-12-21 00:00:17 +00:00
// Get an action for a particular Droplet by id.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) Get(ctx context.Context, dropletID, actionID int) (*Action, *Response, error) {
if dropletID < 1 {
return nil, nil, NewArgError("dropletID", "cannot be less than 1")
}
if actionID < 1 {
return nil, nil, NewArgError("actionID", "cannot be less than 1")
}
2014-09-03 14:03:30 +00:00
path := fmt.Sprintf("%s/%d", dropletActionPath(dropletID), actionID)
2016-11-29 02:42:23 +00:00
return s.get(ctx, path)
2014-09-03 14:03:30 +00:00
}
2016-12-21 00:00:17 +00:00
// GetByURI gets an action for a particular Droplet by id.
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) GetByURI(ctx context.Context, rawurl string) (*Action, *Response, error) {
2014-09-03 14:03:30 +00:00
u, err := url.Parse(rawurl)
if err != nil {
return nil, nil, err
}
2016-11-29 02:42:23 +00:00
return s.get(ctx, u.Path)
2014-09-03 14:03:30 +00:00
}
2016-11-29 02:42:23 +00:00
func (s *DropletActionsServiceOp) get(ctx context.Context, path string) (*Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
2014-09-03 14:03:30 +00:00
}
func dropletActionPath(dropletID int) string {
return fmt.Sprintf("v2/droplets/%d/actions", dropletID)
}
func dropletActionPathByTag(tag string) string {
return fmt.Sprintf("v2/droplets/actions?tag_name=%s", tag)
}