Skip to content

Commit 4715492

Browse files
authored
feat(instance): use new attach and detach routes in volume utils (#2077)
1 parent 2e50ecb commit 4715492

File tree

4 files changed

+295
-584
lines changed

4 files changed

+295
-584
lines changed

api/instance/v1/instance_utils.go

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ func (s *API) DetachIP(req *DetachIPRequest, opts ...scw.RequestOption) (*Detach
9393
}
9494

9595
// AttachVolumeRequest contains the parameters to attach a volume to a server
96+
// Deprecated by AttachServerVolumeRequest
9697
type AttachVolumeRequest struct {
9798
Zone scw.Zone `json:"-"`
9899
ServerID string `json:"-"`
99100
VolumeID string `json:"-"`
100101
}
101102

102103
// AttachVolumeResponse contains the updated server after attaching a volume
104+
// Deprecated by AttachServerVolumeResponse
103105
type AttachVolumeResponse struct {
104106
Server *Server `json:"-"`
105107
}
@@ -129,69 +131,35 @@ func volumesToVolumeTemplates(volumes map[string]*VolumeServer) map[string]*Volu
129131
// AttachVolume attaches a volume to a server
130132
//
131133
// Note: Implementation is thread-safe.
134+
// Deprecated by AttachServerVolume provided by instance API
132135
func (s *API) AttachVolume(req *AttachVolumeRequest, opts ...scw.RequestOption) (*AttachVolumeResponse, error) {
133136
defer lockServer(req.Zone, req.ServerID).Unlock()
134137
// check where the volume comes from
135138
volume, err := s.getUnknownVolume(&getUnknownVolumeRequest{
136139
Zone: req.Zone,
137140
VolumeID: req.VolumeID,
138-
})
141+
}, opts...)
139142
if err != nil {
140143
return nil, err
141144
}
142145

143-
// get server with volumes
144-
getServerResponse, err := s.GetServer(&GetServerRequest{
145-
Zone: req.Zone,
146-
ServerID: req.ServerID,
147-
})
148-
if err != nil {
149-
return nil, err
146+
attachServerVolumeReq := &AttachServerVolumeRequest{
147+
Zone: req.Zone,
148+
ServerID: req.ServerID,
149+
VolumeID: req.VolumeID,
150+
VolumeType: AttachServerVolumeRequestVolumeType(volume.Type),
150151
}
151-
volumes := getServerResponse.Server.Volumes
152-
153-
newVolumes := volumesToVolumeTemplates(volumes)
154-
155-
// add volume to volumes list
156-
// We loop through all the possible volume keys (0 to len(volumes))
157-
// to find a non existing key and assign it to the requested volume.
158-
// A key should always be found. However we return an error if no keys were found.
159-
found := false
160-
for i := 0; i <= len(volumes); i++ {
161-
key := fmt.Sprintf("%d", i)
162-
if _, ok := newVolumes[key]; !ok {
163-
newVolumes[key] = &VolumeServerTemplate{
164-
ID: &req.VolumeID,
165-
}
166-
if volume.Type == VolumeVolumeTypeSbsVolume {
167-
newVolumes[key].VolumeType = VolumeVolumeTypeSbsVolume
168-
} else {
169-
newVolumes[key].Name = &req.VolumeID
170-
}
171152

172-
found = true
173-
break
174-
}
175-
}
176-
177-
if !found {
178-
return nil, fmt.Errorf("could not find key to attach volume %s", req.VolumeID)
179-
}
180-
181-
// update server
182-
updateServerResponse, err := s.updateServer(&UpdateServerRequest{
183-
Zone: req.Zone,
184-
ServerID: req.ServerID,
185-
Volumes: &newVolumes,
186-
})
153+
resp, err := s.AttachServerVolume(attachServerVolumeReq, opts...)
187154
if err != nil {
188155
return nil, err
189156
}
190157

191-
return &AttachVolumeResponse{Server: updateServerResponse.Server}, nil
158+
return &AttachVolumeResponse{Server: resp.Server}, nil
192159
}
193160

194161
// DetachVolumeRequest contains the parameters to detach a volume from a server
162+
// Deprecated by DetachServerVolumeRequest
195163
type DetachVolumeRequest struct {
196164
Zone scw.Zone `json:"-"`
197165
VolumeID string `json:"-"`
@@ -202,18 +170,20 @@ type DetachVolumeRequest struct {
202170
}
203171

204172
// DetachVolumeResponse contains the updated server after detaching a volume
173+
// Deprecated by DetachServerVolumeResponse
205174
type DetachVolumeResponse struct {
206175
Server *Server `json:"-"`
207176
}
208177

209178
// DetachVolume detaches a volume from a server
210179
//
211180
// Note: Implementation is thread-safe.
181+
// Deprecated by DetachServerVolume provided by instance API
212182
func (s *API) DetachVolume(req *DetachVolumeRequest, opts ...scw.RequestOption) (*DetachVolumeResponse, error) {
213183
volume, err := s.getUnknownVolume(&getUnknownVolumeRequest{
214184
Zone: req.Zone,
215185
VolumeID: req.VolumeID,
216-
})
186+
}, opts...)
217187
if err != nil {
218188
return nil, err
219189
}
@@ -223,35 +193,17 @@ func (s *API) DetachVolume(req *DetachVolumeRequest, opts ...scw.RequestOption)
223193
}
224194

225195
defer lockServer(req.Zone, *volume.ServerID).Unlock()
226-
// get server with volumes
227-
getServerResponse, err := s.GetServer(&GetServerRequest{
228-
Zone: req.Zone,
229-
ServerID: *volume.ServerID,
230-
})
231-
if err != nil {
232-
return nil, err
233-
}
234-
volumes := getServerResponse.Server.Volumes
235-
// remove volume from volumes list
236-
for key, volume := range volumes {
237-
if volume.ID == req.VolumeID {
238-
delete(volumes, key)
239-
}
240-
}
241-
242-
newVolumes := volumesToVolumeTemplates(volumes)
243196

244-
// update server
245-
updateServerResponse, err := s.updateServer(&UpdateServerRequest{
197+
resp, err := s.DetachServerVolume(&DetachServerVolumeRequest{
246198
Zone: req.Zone,
247199
ServerID: *volume.ServerID,
248-
Volumes: &newVolumes,
249-
})
200+
VolumeID: volume.ID,
201+
}, opts...)
250202
if err != nil {
251203
return nil, err
252204
}
253205

254-
return &DetachVolumeResponse{Server: updateServerResponse.Server}, nil
206+
return &DetachVolumeResponse{Server: resp.Server}, nil
255207
}
256208

257209
// UnsafeSetTotalCount should not be used

0 commit comments

Comments
 (0)