2014-09-03 14:03:30 +00:00
|
|
|
package godo
|
|
|
|
|
2016-11-29 02:42:23 +00:00
|
|
|
import (
|
2018-05-08 20:24:29 +00:00
|
|
|
"context"
|
2016-11-29 02:42:23 +00:00
|
|
|
"fmt"
|
2017-07-02 11:55:56 +00:00
|
|
|
"net/http"
|
2016-11-29 02:42:23 +00:00
|
|
|
)
|
2014-09-03 14:03:30 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
actionsBasePath = "v2/actions"
|
|
|
|
|
|
|
|
// ActionInProgress is an in progress action status
|
|
|
|
ActionInProgress = "in-progress"
|
|
|
|
|
|
|
|
//ActionCompleted is a completed action status
|
|
|
|
ActionCompleted = "completed"
|
|
|
|
)
|
|
|
|
|
2014-09-04 19:53:41 +00:00
|
|
|
// ActionsService handles communction with action related methods of the
|
2015-03-07 19:55:07 +00:00
|
|
|
// DigitalOcean API: https://developers.digitalocean.com/documentation/v2#actions
|
2014-09-04 19:53:41 +00:00
|
|
|
type ActionsService interface {
|
2016-11-29 02:42:23 +00:00
|
|
|
List(context.Context, *ListOptions) ([]Action, *Response, error)
|
|
|
|
Get(context.Context, int) (*Action, *Response, error)
|
2014-09-04 19:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActionsServiceOp handles communition with the image action related methods of the
|
2014-09-03 14:03:30 +00:00
|
|
|
// DigitalOcean API.
|
2014-09-04 19:53:41 +00:00
|
|
|
type ActionsServiceOp struct {
|
2014-09-03 14:03:30 +00:00
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:42:48 +00:00
|
|
|
var _ ActionsService = &ActionsServiceOp{}
|
|
|
|
|
2014-09-03 14:03:30 +00:00
|
|
|
type actionsRoot struct {
|
|
|
|
Actions []Action `json:"actions"`
|
2014-09-06 21:56:23 +00:00
|
|
|
Links *Links `json:"links"`
|
2019-12-11 19:52:04 +00:00
|
|
|
Meta *Meta `json:"meta"`
|
2014-09-03 14:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type actionRoot struct {
|
2016-11-18 17:50:47 +00:00
|
|
|
Event *Action `json:"action"`
|
2014-09-03 14:03:30 +00: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"`
|
2015-03-10 13:12:55 +00:00
|
|
|
Region *Region `json:"region,omitempty"`
|
|
|
|
RegionSlug string `json:"region_slug,omitempty"`
|
2014-09-03 14:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all actions
|
2016-11-29 02:42:23 +00:00
|
|
|
func (s *ActionsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Action, *Response, error) {
|
2014-09-03 14:03:30 +00:00
|
|
|
path := actionsBasePath
|
2014-09-06 21:56:23 +00:00
|
|
|
path, err := addOptions(path, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2014-09-03 14:03:30 +00:00
|
|
|
|
2017-07-02 11:55:56 +00:00
|
|
|
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(actionsRoot)
|
2017-04-07 20:32:33 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2014-09-03 14:03:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2014-09-06 21:56:23 +00:00
|
|
|
if l := root.Links; l != nil {
|
|
|
|
resp.Links = l
|
|
|
|
}
|
2019-12-11 19:52:04 +00:00
|
|
|
if m := root.Meta; m != nil {
|
|
|
|
resp.Meta = m
|
|
|
|
}
|
2014-09-03 14:03:30 +00:00
|
|
|
|
|
|
|
return root.Actions, resp, err
|
|
|
|
}
|
|
|
|
|
2015-09-25 19:44:20 +00:00
|
|
|
// Get an action by ID.
|
2016-11-29 02:42:23 +00:00
|
|
|
func (s *ActionsServiceOp) Get(ctx context.Context, id int) (*Action, *Response, error) {
|
2015-09-25 19:44:20 +00:00
|
|
|
if id < 1 {
|
|
|
|
return nil, nil, NewArgError("id", "cannot be less than 1")
|
|
|
|
}
|
|
|
|
|
2014-09-03 14:03:30 +00:00
|
|
|
path := fmt.Sprintf("%s/%d", actionsBasePath, id)
|
2017-07-02 11:55:56 +00:00
|
|
|
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)
|
2017-04-07 20:32:33 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2014-09-03 14:03:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2016-11-18 17:50:47 +00:00
|
|
|
return root.Event, resp, err
|
2014-09-03 14:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a Action) String() string {
|
|
|
|
return Stringify(a)
|
|
|
|
}
|