image id: int -> string

This commit is contained in:
Kiara Grouwstra 2022-08-01 20:44:49 +02:00
parent a137f5be07
commit 58cb4f5862
6 changed files with 53 additions and 69 deletions

View File

@ -663,10 +663,10 @@ func TestDroplets_Snapshots(t *testing.T) {
fmt.Fprint(w, `{
"snapshots": [
{
"id": 1
"id": "1"
},
{
"id": 2
"id": "2"
}
],
"meta": {
@ -681,7 +681,7 @@ func TestDroplets_Snapshots(t *testing.T) {
t.Errorf("Droplets.Snapshots returned error: %v", err)
}
expectedSnapshots := []Image{{ID: 1}, {ID: 2}}
expectedSnapshots := []Image{{ID: "1"}, {ID: "2"}}
if !reflect.DeepEqual(snapshots, expectedSnapshots) {
t.Errorf("Droplets.Snapshots\nSnapshots got=%#v\nwant=%#v", snapshots, expectedSnapshots)
}
@ -700,10 +700,10 @@ func TestDroplets_Backups(t *testing.T) {
fmt.Fprint(w, `{
"backups": [
{
"id": 1
"id": "1"
},
{
"id": 2
"id": "2"
}
],
"meta": {
@ -718,7 +718,7 @@ func TestDroplets_Backups(t *testing.T) {
t.Errorf("Droplets.Backups returned error: %v", err)
}
expectedBackups := []Image{{ID: 1}, {ID: 2}}
expectedBackups := []Image{{ID: "1"}, {ID: "2"}}
if !reflect.DeepEqual(backups, expectedBackups) {
t.Errorf("Droplets.Backups\nBackups got=%#v\nwant=%#v", backups, expectedBackups)
}

View File

@ -10,9 +10,9 @@ import (
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Image-Actions
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)
Get(context.Context, string, int) (*Action, *Response, error)
Transfer(context.Context, string, *ActionRequest) (*Action, *Response, error)
Convert(context.Context, string) (*Action, *Response, error)
}
// ImageActionsServiceOp handles communication with the image action related methods of the
@ -24,16 +24,12 @@ type ImageActionsServiceOp struct {
var _ ImageActionsService = &ImageActionsServiceOp{}
// Transfer an image
func (i *ImageActionsServiceOp) Transfer(ctx context.Context, imageID int, transferRequest *ActionRequest) (*Action, *Response, error) {
if imageID < 1 {
return nil, nil, NewArgError("imageID", "cannot be less than 1")
}
func (i *ImageActionsServiceOp) Transfer(ctx context.Context, imageID string, transferRequest *ActionRequest) (*Action, *Response, error) {
if transferRequest == nil {
return nil, nil, NewArgError("transferRequest", "cannot be nil")
}
path := fmt.Sprintf("v2/images/%d/actions", imageID)
path := fmt.Sprintf("v2/images/%s/actions", imageID)
req, err := i.client.NewRequest(ctx, http.MethodPost, path, transferRequest)
if err != nil {
@ -50,12 +46,8 @@ func (i *ImageActionsServiceOp) Transfer(ctx context.Context, imageID int, trans
}
// 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)
func (i *ImageActionsServiceOp) Convert(ctx context.Context, imageID string) (*Action, *Response, error) {
path := fmt.Sprintf("v2/images/%s/actions", imageID)
convertRequest := &ActionRequest{
"type": "convert",
@ -76,16 +68,12 @@ func (i *ImageActionsServiceOp) Convert(ctx context.Context, imageID int) (*Acti
}
// 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 {
return nil, nil, NewArgError("imageID", "cannot be less than 1")
}
func (i *ImageActionsServiceOp) Get(ctx context.Context, imageID string, actionID int) (*Action, *Response, error) {
if actionID < 1 {
return nil, nil, NewArgError("actionID", "cannot be less than 1")
}
path := fmt.Sprintf("v2/images/%d/actions/%d", imageID, actionID)
path := fmt.Sprintf("v2/images/%s/actions/%d", imageID, actionID)
req, err := i.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {

View File

@ -30,7 +30,7 @@ func TestImageActions_Transfer(t *testing.T) {
})
transfer, _, err := client.ImageActions.Transfer(ctx, 12345, transferRequest)
transfer, _, err := client.ImageActions.Transfer(ctx, "12345", transferRequest)
if err != nil {
t.Errorf("ImageActions.Transfer returned error: %v", err)
}
@ -65,7 +65,7 @@ func TestImageActions_Convert(t *testing.T) {
})
transfer, _, err := client.ImageActions.Convert(ctx, 12345)
transfer, _, err := client.ImageActions.Convert(ctx, "12345")
if err != nil {
t.Errorf("ImageActions.Transfer returned error: %v", err)
}
@ -85,7 +85,7 @@ func TestImageActions_Get(t *testing.T) {
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.ImageActions.Get(ctx, 123, 456)
action, _, err := client.ImageActions.Get(ctx, "123", 456)
if err != nil {
t.Errorf("ImageActions.Get returned error: %v", err)
}

View File

@ -20,7 +20,7 @@ type ImagesService interface {
GetByID(context.Context, int) (*Image, *Response, error)
GetBySlug(context.Context, string) (*Image, *Response, error)
Create(context.Context, *CustomImageCreateRequest) (*Image, *Response, error)
Update(context.Context, int, *ImageUpdateRequest) (*Image, *Response, error)
Update(context.Context, string, *ImageUpdateRequest) (*Image, *Response, error)
Delete(context.Context, int) (*Response, error)
}
@ -34,7 +34,7 @@ var _ ImagesService = &ImagesServiceOp{}
// Image represents a DigitalOcean Image
type Image struct {
ID int `json:"id,float64,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Distribution string `json:"distribution,omitempty"`
@ -155,16 +155,12 @@ func (s *ImagesServiceOp) Create(ctx context.Context, createRequest *CustomImage
}
// Update an image name.
func (s *ImagesServiceOp) Update(ctx context.Context, imageID int, updateRequest *ImageUpdateRequest) (*Image, *Response, error) {
if imageID < 1 {
return nil, nil, NewArgError("imageID", "cannot be less than 1")
}
func (s *ImagesServiceOp) Update(ctx context.Context, imageID string, updateRequest *ImageUpdateRequest) (*Image, *Response, error) {
if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}
path := fmt.Sprintf("%s/%d", imageBasePath, imageID)
path := fmt.Sprintf("%s/%s", imageBasePath, imageID)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, updateRequest)
if err != nil {
return nil, nil, err

View File

@ -17,10 +17,10 @@ func TestImages_List(t *testing.T) {
fmt.Fprint(w, `{
"images": [
{
"id": 1
"id": "1"
},
{
"id": 2
"id": "2"
}
],
"meta": {
@ -34,7 +34,7 @@ func TestImages_List(t *testing.T) {
t.Errorf("Images.List returned error: %v", err)
}
expectedImages := []Image{{ID: 1}, {ID: 2}}
expectedImages := []Image{{ID: "1"}, {ID: "2"}}
if !reflect.DeepEqual(images, expectedImages) {
t.Errorf("Images.List returned images %+v, expected %+v", images, expectedImages)
}
@ -59,10 +59,10 @@ func TestImages_ListDistribution(t *testing.T) {
fmt.Fprint(w, `{
"images": [
{
"id": 1
"id": "1"
},
{
"id": 2
"id": "2"
}
],
"meta": {
@ -76,7 +76,7 @@ func TestImages_ListDistribution(t *testing.T) {
t.Errorf("Images.ListDistribution returned error: %v", err)
}
expectedImages := []Image{{ID: 1}, {ID: 2}}
expectedImages := []Image{{ID: "1"}, {ID: "2"}}
if !reflect.DeepEqual(images, expectedImages) {
t.Errorf("Images.ListDistribution returned images %+v, expected %+v", images, expectedImages)
}
@ -101,10 +101,10 @@ func TestImages_ListApplication(t *testing.T) {
fmt.Fprint(w, `{
"images": [
{
"id": 1
"id": "1"
},
{
"id": 2
"id": "2"
}
],
"meta": {
@ -118,7 +118,7 @@ func TestImages_ListApplication(t *testing.T) {
t.Errorf("Images.ListApplication returned error: %v", err)
}
expectedImages := []Image{{ID: 1}, {ID: 2}}
expectedImages := []Image{{ID: "1"}, {ID: "2"}}
if !reflect.DeepEqual(images, expectedImages) {
t.Errorf("Images.ListApplication returned images %+v, expected %+v", images, expectedImages)
}
@ -144,10 +144,10 @@ func TestImages_ListUser(t *testing.T) {
fmt.Fprint(w, `{
"images": [
{
"id": 1
"id": "1"
},
{
"id": 2
"id": "2"
}
],
"meta": {
@ -161,7 +161,7 @@ func TestImages_ListUser(t *testing.T) {
t.Errorf("Images.ListUser returned error: %v", err)
}
expectedImages := []Image{{ID: 1}, {ID: 2}}
expectedImages := []Image{{ID: "1"}, {ID: "2"}}
if !reflect.DeepEqual(images, expectedImages) {
t.Errorf("Images.ListUser returned images %+v, expected %+v", images, expectedImages)
}
@ -187,10 +187,10 @@ func TestImages_ListByTag(t *testing.T) {
fmt.Fprint(w, `{
"images": [
{
"id": 1
"id": "1"
},
{
"id":2
"id": "2"
}
],
"meta": {
@ -204,7 +204,7 @@ func TestImages_ListByTag(t *testing.T) {
t.Errorf("Images.ListByTag returned error: %v", err)
}
expectedImages := []Image{{ID: 1}, {ID: 2}}
expectedImages := []Image{{ID: "1"}, {ID: "2"}}
if !reflect.DeepEqual(images, expectedImages) {
t.Errorf("Images.ListByTag returned images %+v, expected %+v", images, expectedImages)
}
@ -221,7 +221,7 @@ func TestImages_ListImagesMultiplePages(t *testing.T) {
mux.HandleFunc("/v2/images", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"images": [{"id":1},{"id":2}], "links":{"pages":{"next":"http://example.com/v2/images/?page=2"}}}`)
fmt.Fprint(w, `{"images": [{"id": "1"},{"id": "2"}], "links":{"pages":{"next":"http://example.com/v2/images/?page=2"}}}`)
})
_, resp, err := client.Images.List(ctx, &ListOptions{Page: 2})
@ -237,7 +237,7 @@ func TestImages_RetrievePageByNumber(t *testing.T) {
jBlob := `
{
"images": [{"id":1},{"id":2}],
"images": [{"id": "1"},{"id": "2"}],
"links":{
"pages":{
"next":"http://example.com/v2/images/?page=3",
@ -268,7 +268,7 @@ func TestImages_GetImageByID(t *testing.T) {
mux.HandleFunc("/v2/images/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"image":{"id":12345}}`)
fmt.Fprint(w, `{"image":{"id": "12345"}}`)
})
images, _, err := client.Images.GetByID(ctx, 12345)
@ -276,7 +276,7 @@ func TestImages_GetImageByID(t *testing.T) {
t.Errorf("Image.GetByID returned error: %v", err)
}
expected := &Image{ID: 12345}
expected := &Image{ID: "12345"}
if !reflect.DeepEqual(images, expected) {
t.Errorf("Images.GetByID returned %+v, expected %+v", images, expected)
}
@ -288,7 +288,7 @@ func TestImages_GetImageBySlug(t *testing.T) {
mux.HandleFunc("/v2/images/ubuntu", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"image":{"id":12345}}`)
fmt.Fprint(w, `{"image":{"id": "12345"}}`)
})
images, _, err := client.Images.GetBySlug(ctx, "ubuntu")
@ -296,7 +296,7 @@ func TestImages_GetImageBySlug(t *testing.T) {
t.Errorf("Image.GetBySlug returned error: %v", err)
}
expected := &Image{ID: 12345}
expected := &Image{ID: "12345"}
if !reflect.DeepEqual(images, expected) {
t.Errorf("Images.Get returned %+v, expected %+v", images, expected)
}
@ -335,7 +335,7 @@ func TestImages_Create(t *testing.T) {
t.Errorf("Request body\n got=%#v\nwant=%#v", v, expected)
}
fmt.Fprintf(w, `{"image": {"id": 1,"created_at": "2018-09-20T19:28:00Z","description": "A custom image","distribution": "Ubuntu","error_message": "","regions": [],"type": "custom","tags":["foo","bar"],"status": "NEW"}}`)
fmt.Fprintf(w, `{"image": {"id": "1","created_at": "2018-09-20T19:28:00Z","description": "A custom image","distribution": "Ubuntu","error_message": "","regions": [],"type": "custom","tags":["foo","bar"],"status": "NEW"}}`)
})
image, _, err := client.Images.Create(ctx, createRequest)
@ -343,8 +343,8 @@ func TestImages_Create(t *testing.T) {
t.Errorf("Images.Create returned error: %v", err)
}
if id := image.ID; id != 1 {
t.Errorf("expected id '%d', received '%d'", 1, id)
if id := image.ID; id != "1" {
t.Errorf("expected id '%s', received '%s'", "1", id)
}
}
@ -375,15 +375,15 @@ func TestImages_Update(t *testing.T) {
t.Errorf("Request body = %#v, expected %#v", v, expected)
}
fmt.Fprintf(w, `{"image":{"id":1}}`)
fmt.Fprintf(w, `{"image":{"id": "1"}}`)
})
image, _, err := client.Images.Update(ctx, 12345, updateRequest)
image, _, err := client.Images.Update(ctx, "12345", updateRequest)
if err != nil {
t.Errorf("Images.Update returned error: %v", err)
} else {
if id := image.ID; id != 1 {
t.Errorf("expected id '%d', received '%d'", 1, id)
if id := image.ID; id != "1" {
t.Errorf("expected id '%s', received '%s'", "1", id)
}
}
}
@ -404,7 +404,7 @@ func TestImages_Destroy(t *testing.T) {
func TestImage_String(t *testing.T) {
image := &Image{
ID: 1,
ID: "1",
Name: "Image",
Type: "snapshot",
Distribution: "Ubuntu",
@ -417,7 +417,7 @@ func TestImage_String(t *testing.T) {
}
stringified := image.String()
expected := `godo.Image{ID:1, Name:"Image", Type:"snapshot", Distribution:"Ubuntu", Slug:"image", Public:true, Regions:["one" "two"], MinDiskSize:20, SizeGigaBytes:2.36, Created:"2013-11-27T09:24:55Z", Description:"", Status:"", ErrorMessage:""}`
expected := `godo.Image{ID:"1", Name:"Image", Type:"snapshot", Distribution:"Ubuntu", Slug:"image", Public:true, Regions:["one" "two"], MinDiskSize:20, SizeGigaBytes:2.36, Created:"2013-11-27T09:24:55Z", Description:"", Status:"", ErrorMessage:""}`
if expected != stringified {
t.Errorf("Image.String returned %+v, expected %+v", stringified, expected)
}

View File

@ -140,7 +140,7 @@ var (
]
},
"image": {
"id": 119192817,
"id": "119192817",
"name": "Ubuntu 13.04",
"distribution": "ubuntu",
"slug": "ubuntu1304",