Skip to content

Commit

Permalink
feat(instance): replace some update utils by generated routes (#1928)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Germain <vgermain@scaleway.com>
  • Loading branch information
Codelax and Vinzius committed Dec 4, 2023
1 parent fc27865 commit c5bd7ba
Show file tree
Hide file tree
Showing 8 changed files with 617 additions and 895 deletions.
67 changes: 0 additions & 67 deletions api/instance/v1/image_utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package instance

import (
"fmt"
"net/http"
"time"

"github.com/scaleway/scaleway-sdk-go/internal/async"
Expand Down Expand Up @@ -56,68 +54,3 @@ func (s *API) WaitForImage(req *WaitForImageRequest, opts ...scw.RequestOption)
}
return image.(*Image), nil
}

type UpdateImageRequest struct {
Zone scw.Zone `json:"zone"`
ImageID string `json:"id"`
Name *string `json:"name,omitempty"`
Arch Arch `json:"arch,omitempty"`
CreationDate *time.Time `json:"creation_date"`
ModificationDate *time.Time `json:"modification_date"`
ExtraVolumes map[string]*VolumeTemplate `json:"extra_volumes"`
FromServer string `json:"from_server,omitempty"`
Organization string `json:"organization"`
Public bool `json:"public"`
RootVolume *VolumeSummary `json:"root_volume,omitempty"`
State ImageState `json:"state"`
Project string `json:"project"`
Tags *[]string `json:"tags,omitempty"`
}

type UpdateImageResponse struct {
Image *Image
}

func (s *API) UpdateImage(req *UpdateImageRequest, opts ...scw.RequestOption) (*UpdateImageResponse, error) {
// This function is the equivalent of the private setImage function that is not usable because the json tags and
// types are not compatible with what the compute API expects

if req.Project == "" {
defaultProject, _ := s.client.GetDefaultProjectID()
req.Project = defaultProject
}
if req.Organization == "" {
defaultOrganization, _ := s.client.GetDefaultOrganizationID()
req.Organization = defaultOrganization
}
if req.Zone == "" {
defaultZone, _ := s.client.GetDefaultZone()
req.Zone = defaultZone
}
if fmt.Sprint(req.Zone) == "" {
return nil, errors.New("field Zone cannot be empty in request")
}
if fmt.Sprint(req.ImageID) == "" {
return nil, errors.New("field ID cannot be empty in request")
}

scwReq := &scw.ScalewayRequest{
Method: "PUT",
Path: "/instance/v1/zones/" + fmt.Sprint(req.Zone) + "/images/" + fmt.Sprint(req.ImageID) + "",
Headers: http.Header{},
}

err := scwReq.SetBody(req)
if err != nil {
return nil, err
}

var resp UpdateImageResponse

err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}

return &resp, nil
}
Loading

0 comments on commit c5bd7ba

Please sign in to comment.