Skip to content

feat(instance): use new attach and detach routes in volume utils #2077

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

Merged
merged 3 commits into from
May 21, 2024
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
86 changes: 19 additions & 67 deletions api/instance/v1/instance_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ func (s *API) DetachIP(req *DetachIPRequest, opts ...scw.RequestOption) (*Detach
}

// AttachVolumeRequest contains the parameters to attach a volume to a server
// Deprecated by AttachServerVolumeRequest
type AttachVolumeRequest struct {
Zone scw.Zone `json:"-"`
ServerID string `json:"-"`
VolumeID string `json:"-"`
}

// AttachVolumeResponse contains the updated server after attaching a volume
// Deprecated by AttachServerVolumeResponse
type AttachVolumeResponse struct {
Server *Server `json:"-"`
}
Expand Down Expand Up @@ -129,69 +131,35 @@ func volumesToVolumeTemplates(volumes map[string]*VolumeServer) map[string]*Volu
// AttachVolume attaches a volume to a server
//
// Note: Implementation is thread-safe.
// Deprecated by AttachServerVolume provided by instance API
func (s *API) AttachVolume(req *AttachVolumeRequest, opts ...scw.RequestOption) (*AttachVolumeResponse, error) {
defer lockServer(req.Zone, req.ServerID).Unlock()
// check where the volume comes from
volume, err := s.getUnknownVolume(&getUnknownVolumeRequest{
Zone: req.Zone,
VolumeID: req.VolumeID,
})
}, opts...)
if err != nil {
return nil, err
}

// get server with volumes
getServerResponse, err := s.GetServer(&GetServerRequest{
Zone: req.Zone,
ServerID: req.ServerID,
})
if err != nil {
return nil, err
attachServerVolumeReq := &AttachServerVolumeRequest{
Zone: req.Zone,
ServerID: req.ServerID,
VolumeID: req.VolumeID,
VolumeType: AttachServerVolumeRequestVolumeType(volume.Type),
}
volumes := getServerResponse.Server.Volumes

newVolumes := volumesToVolumeTemplates(volumes)

// add volume to volumes list
// We loop through all the possible volume keys (0 to len(volumes))
// to find a non existing key and assign it to the requested volume.
// A key should always be found. However we return an error if no keys were found.
found := false
for i := 0; i <= len(volumes); i++ {
key := fmt.Sprintf("%d", i)
if _, ok := newVolumes[key]; !ok {
newVolumes[key] = &VolumeServerTemplate{
ID: &req.VolumeID,
}
if volume.Type == VolumeVolumeTypeSbsVolume {
newVolumes[key].VolumeType = VolumeVolumeTypeSbsVolume
} else {
newVolumes[key].Name = &req.VolumeID
}

found = true
break
}
}

if !found {
return nil, fmt.Errorf("could not find key to attach volume %s", req.VolumeID)
}

// update server
updateServerResponse, err := s.updateServer(&UpdateServerRequest{
Zone: req.Zone,
ServerID: req.ServerID,
Volumes: &newVolumes,
})
resp, err := s.AttachServerVolume(attachServerVolumeReq, opts...)
if err != nil {
return nil, err
}

return &AttachVolumeResponse{Server: updateServerResponse.Server}, nil
return &AttachVolumeResponse{Server: resp.Server}, nil
}

// DetachVolumeRequest contains the parameters to detach a volume from a server
// Deprecated by DetachServerVolumeRequest
type DetachVolumeRequest struct {
Zone scw.Zone `json:"-"`
VolumeID string `json:"-"`
Expand All @@ -202,18 +170,20 @@ type DetachVolumeRequest struct {
}

// DetachVolumeResponse contains the updated server after detaching a volume
// Deprecated by DetachServerVolumeResponse
type DetachVolumeResponse struct {
Server *Server `json:"-"`
}

// DetachVolume detaches a volume from a server
//
// Note: Implementation is thread-safe.
// Deprecated by DetachServerVolume provided by instance API
func (s *API) DetachVolume(req *DetachVolumeRequest, opts ...scw.RequestOption) (*DetachVolumeResponse, error) {
volume, err := s.getUnknownVolume(&getUnknownVolumeRequest{
Zone: req.Zone,
VolumeID: req.VolumeID,
})
}, opts...)
if err != nil {
return nil, err
}
Expand All @@ -223,35 +193,17 @@ func (s *API) DetachVolume(req *DetachVolumeRequest, opts ...scw.RequestOption)
}

defer lockServer(req.Zone, *volume.ServerID).Unlock()
// get server with volumes
getServerResponse, err := s.GetServer(&GetServerRequest{
Zone: req.Zone,
ServerID: *volume.ServerID,
})
if err != nil {
return nil, err
}
volumes := getServerResponse.Server.Volumes
// remove volume from volumes list
for key, volume := range volumes {
if volume.ID == req.VolumeID {
delete(volumes, key)
}
}

newVolumes := volumesToVolumeTemplates(volumes)

// update server
updateServerResponse, err := s.updateServer(&UpdateServerRequest{
resp, err := s.DetachServerVolume(&DetachServerVolumeRequest{
Zone: req.Zone,
ServerID: *volume.ServerID,
Volumes: &newVolumes,
})
VolumeID: volume.ID,
}, opts...)
if err != nil {
return nil, err
}

return &DetachVolumeResponse{Server: updateServerResponse.Server}, nil
return &DetachVolumeResponse{Server: resp.Server}, nil
}

// UnsafeSetTotalCount should not be used
Expand Down
Loading
Loading