2014-09-03 14:03:30 +00:00
|
|
|
package godo
|
|
|
|
|
2016-11-29 02:42:23 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-04-07 20:32:33 +00:00
|
|
|
|
|
|
|
"github.com/digitalocean/godo/context"
|
2016-11-29 02:42:23 +00:00
|
|
|
)
|
2014-09-03 14:03:30 +00:00
|
|
|
|
2014-09-04 19:53:41 +00:00
|
|
|
// ImageActionsService is an interface for interfacing with the image actions
|
2015-09-18 13:10:59 +00:00
|
|
|
// endpoints of the DigitalOcean API
|
2015-03-07 19:55:07 +00:00
|
|
|
// See: https://developers.digitalocean.com/documentation/v2#image-actions
|
2014-09-04 19:53:41 +00:00
|
|
|
type ImageActionsService interface {
|
2016-11-29 02:42:23 +00:00
|
|
|
Get(context.Context, int, int) (*Action, *Response, error)
|
|
|
|
Transfer(context.Context, int, *ActionRequest) (*Action, *Response, error)
|
2017-03-04 19:25:10 +00:00
|
|
|
Convert(context.Context, int) (*Action, *Response, error)
|
2014-09-04 19:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImageActionsServiceOp 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 ImageActionsServiceOp struct {
|
2014-09-03 14:03:30 +00:00
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:42:48 +00:00
|
|
|
var _ ImageActionsService = &ImageActionsServiceOp{}
|
|
|
|
|
2014-09-03 14:03:30 +00:00
|
|
|
// Transfer an image
|
2016-11-29 02:42:23 +00:00
|
|
|
func (i *ImageActionsServiceOp) Transfer(ctx context.Context, imageID int, transferRequest *ActionRequest) (*Action, *Response, error) {
|
2015-09-25 19:44:20 +00:00
|
|
|
if imageID < 1 {
|
|
|
|
return nil, nil, NewArgError("imageID", "cannot be less than 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if transferRequest == nil {
|
|
|
|
return nil, nil, NewArgError("transferRequest", "cannot be nil")
|
|
|
|
}
|
|
|
|
|
2014-09-03 14:03:30 +00:00
|
|
|
path := fmt.Sprintf("v2/images/%d/actions", imageID)
|
|
|
|
|
2016-11-29 02:42:23 +00:00
|
|
|
req, err := i.client.NewRequest(ctx, "POST", path, transferRequest)
|
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 := i.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
|
|
|
}
|
|
|
|
|
2017-03-04 19:25:10 +00:00
|
|
|
// Convert an image to a snapshot
|
|
|
|
func (i *ImageActionsServiceOp) Convert(ctx context.Context, imageID int) (*Action, *Response, error) {
|
|
|
|
if imageID < 1 {
|
|
|
|
return nil, nil, NewArgError("imageID", "cannont be less than 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("v2/images/%d/actions", imageID)
|
|
|
|
|
|
|
|
convertRequest := &ActionRequest{
|
|
|
|
"type": "convert",
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := i.client.NewRequest(ctx, "POST", path, convertRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(actionRoot)
|
2017-04-07 20:32:33 +00:00
|
|
|
resp, err := i.client.Do(ctx, req, root)
|
2017-03-04 19:25:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Event, resp, err
|
|
|
|
}
|
|
|
|
|
2014-09-03 14:03:30 +00:00
|
|
|
// Get an action for a particular image by id.
|
2016-11-29 02:42:23 +00:00
|
|
|
func (i *ImageActionsServiceOp) Get(ctx context.Context, imageID, actionID int) (*Action, *Response, error) {
|
2015-09-25 19:44:20 +00:00
|
|
|
if imageID < 1 {
|
|
|
|
return nil, nil, NewArgError("imageID", "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("v2/images/%d/actions/%d", imageID, actionID)
|
|
|
|
|
2016-11-29 02:42:23 +00:00
|
|
|
req, err := i.client.NewRequest(ctx, "GET", 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 := i.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
|
|
|
}
|