2016-01-29 20:53:56 +01:00
|
|
|
package godo
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/digitalocean/godo/context"
|
|
|
|
)
|
2016-01-29 20:53:56 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
actionsBasePath = "v2/actions"
|
|
|
|
|
|
|
|
// ActionInProgress is an in progress action status
|
|
|
|
ActionInProgress = "in-progress"
|
|
|
|
|
|
|
|
//ActionCompleted is a completed action status
|
|
|
|
ActionCompleted = "completed"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ActionsService handles communction with action related methods of the
|
|
|
|
// DigitalOcean API: https://developers.digitalocean.com/documentation/v2#actions
|
|
|
|
type ActionsService interface {
|
2017-05-15 15:54:16 +02:00
|
|
|
List(context.Context, *ListOptions) ([]Action, *Response, error)
|
|
|
|
Get(context.Context, int) (*Action, *Response, error)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActionsServiceOp handles communition with the image action related methods of the
|
|
|
|
// DigitalOcean API.
|
|
|
|
type ActionsServiceOp struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ActionsService = &ActionsServiceOp{}
|
|
|
|
|
|
|
|
type actionsRoot struct {
|
|
|
|
Actions []Action `json:"actions"`
|
|
|
|
Links *Links `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type actionRoot struct {
|
2017-02-15 15:35:41 +01:00
|
|
|
Event *Action `json:"action"`
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Action represents a DigitalOcean Action
|
|
|
|
type Action struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
StartedAt *Timestamp `json:"started_at"`
|
|
|
|
CompletedAt *Timestamp `json:"completed_at"`
|
|
|
|
ResourceID int `json:"resource_id"`
|
|
|
|
ResourceType string `json:"resource_type"`
|
|
|
|
Region *Region `json:"region,omitempty"`
|
|
|
|
RegionSlug string `json:"region_slug,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// List all actions
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *ActionsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Action, *Response, error) {
|
2016-01-29 20:53:56 +01:00
|
|
|
path := actionsBasePath
|
|
|
|
path, err := addOptions(path, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
req, err := s.client.NewRequest(ctx, "GET", path, nil)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(actionsRoot)
|
2017-05-15 15:54:16 +02:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
if l := root.Links; l != nil {
|
|
|
|
resp.Links = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Actions, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an action by ID.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *ActionsServiceOp) Get(ctx context.Context, id int) (*Action, *Response, error) {
|
2016-01-29 20:53:56 +01:00
|
|
|
if id < 1 {
|
|
|
|
return nil, nil, NewArgError("id", "cannot be less than 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/%d", actionsBasePath, id)
|
2017-05-15 15:54:16 +02:00
|
|
|
req, err := s.client.NewRequest(ctx, "GET", path, nil)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(actionRoot)
|
2017-05-15 15:54:16 +02:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2017-02-15 15:35:41 +01:00
|
|
|
return root.Event, resp, err
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a Action) String() string {
|
|
|
|
return Stringify(a)
|
|
|
|
}
|