Skip to content

Commit

Permalink
Implementing Image Convert action
Browse files Browse the repository at this point in the history
  • Loading branch information
xmudrii committed Mar 4, 2017
1 parent 7d7ef61 commit f8d79e7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
27 changes: 27 additions & 0 deletions image_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type ImageActionsService interface {
Get(context.Context, int, int) (*Action, *Response, error)
Transfer(context.Context, int, *ActionRequest) (*Action, *Response, error)
Convert(context.Context, int) (*Action, *Response, error)
}

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

// 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)
resp, err := i.client.Do(req, root)
if err != nil {
return nil, resp, err
}

return root.Event, resp, err
}

// Get an action for a particular image by id.
func (i *ImageActionsServiceOp) Get(ctx context.Context, imageID, actionID int) (*Action, *Response, error) {
if imageID < 1 {
Expand Down
35 changes: 35 additions & 0 deletions image_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,41 @@ func TestImageActions_Transfer(t *testing.T) {
}
}

func TestImageActions_Convert(t *testing.T) {
setup()
defer teardown()

convertRequest := &ActionRequest{
"type": "convert",
}

mux.HandleFunc("/v2/images/12345/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}

testMethod(t, r, "POST")
if !reflect.DeepEqual(v, convertRequest) {
t.Errorf("Request body = %+v, expected %+v", v, convertRequest)
}

fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)

})

transfer, _, err := client.ImageActions.Convert(ctx, 12345)
if err != nil {
t.Errorf("ImageActions.Transfer returned error: %v", err)
}

expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(transfer, expected) {
t.Errorf("ImageActions.Transfer returned %+v, expected %+v", transfer, expected)
}
}

func TestImageActions_Get(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit f8d79e7

Please sign in to comment.