Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Image Convert action #130

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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