Skip to content

Commit 5a09b87

Browse files
authored
Merge pull request #130 from xmudrii/image-action-convert
Implementing Image Convert action
2 parents 7d7ef61 + f8d79e7 commit 5a09b87

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

image_actions.go

+27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type ImageActionsService interface {
1212
Get(context.Context, int, int) (*Action, *Response, error)
1313
Transfer(context.Context, int, *ActionRequest) (*Action, *Response, error)
14+
Convert(context.Context, int) (*Action, *Response, error)
1415
}
1516

1617
// ImageActionsServiceOp handles communition with the image action related methods of the
@@ -47,6 +48,32 @@ func (i *ImageActionsServiceOp) Transfer(ctx context.Context, imageID int, trans
4748
return root.Event, resp, err
4849
}
4950

51+
// Convert an image to a snapshot
52+
func (i *ImageActionsServiceOp) Convert(ctx context.Context, imageID int) (*Action, *Response, error) {
53+
if imageID < 1 {
54+
return nil, nil, NewArgError("imageID", "cannont be less than 1")
55+
}
56+
57+
path := fmt.Sprintf("v2/images/%d/actions", imageID)
58+
59+
convertRequest := &ActionRequest{
60+
"type": "convert",
61+
}
62+
63+
req, err := i.client.NewRequest(ctx, "POST", path, convertRequest)
64+
if err != nil {
65+
return nil, nil, err
66+
}
67+
68+
root := new(actionRoot)
69+
resp, err := i.client.Do(req, root)
70+
if err != nil {
71+
return nil, resp, err
72+
}
73+
74+
return root.Event, resp, err
75+
}
76+
5077
// Get an action for a particular image by id.
5178
func (i *ImageActionsServiceOp) Get(ctx context.Context, imageID, actionID int) (*Action, *Response, error) {
5279
if imageID < 1 {

image_actions_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,41 @@ func TestImageActions_Transfer(t *testing.T) {
4141
}
4242
}
4343

44+
func TestImageActions_Convert(t *testing.T) {
45+
setup()
46+
defer teardown()
47+
48+
convertRequest := &ActionRequest{
49+
"type": "convert",
50+
}
51+
52+
mux.HandleFunc("/v2/images/12345/actions", func(w http.ResponseWriter, r *http.Request) {
53+
v := new(ActionRequest)
54+
err := json.NewDecoder(r.Body).Decode(v)
55+
if err != nil {
56+
t.Fatalf("decode json: %v", err)
57+
}
58+
59+
testMethod(t, r, "POST")
60+
if !reflect.DeepEqual(v, convertRequest) {
61+
t.Errorf("Request body = %+v, expected %+v", v, convertRequest)
62+
}
63+
64+
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
65+
66+
})
67+
68+
transfer, _, err := client.ImageActions.Convert(ctx, 12345)
69+
if err != nil {
70+
t.Errorf("ImageActions.Transfer returned error: %v", err)
71+
}
72+
73+
expected := &Action{Status: "in-progress"}
74+
if !reflect.DeepEqual(transfer, expected) {
75+
t.Errorf("ImageActions.Transfer returned %+v, expected %+v", transfer, expected)
76+
}
77+
}
78+
4479
func TestImageActions_Get(t *testing.T) {
4580
setup()
4681
defer teardown()

0 commit comments

Comments
 (0)