packer-cn/vendor/github.com/digitalocean/godo/droplet_actions.go

330 lines
12 KiB
Go
Raw Normal View History

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