Skip to content

Commit 1c100a5

Browse files
Sh4d1QuentinBrosse
authored andcommitted
fix: attach volume key choice (#184)
1 parent 6594e40 commit 1c100a5

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

api/instance/v1/instance_utils.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,25 @@ func (s *API) AttachVolume(req *AttachVolumeRequest, opts ...scw.RequestOption)
139139
newVolumes := volumesToVolumeTemplates(volumes)
140140

141141
// add volume to volumes list
142-
key := fmt.Sprintf("%d", len(volumes))
143-
newVolumes[key] = &VolumeTemplate{
144-
ID: req.VolumeID,
145-
// name is ignored on this PATCH
146-
Name: req.VolumeID,
142+
// We loop through all the possible volume keys (0 to len(volumes))
143+
// to find a non existing key and assign it to the requested volume.
144+
// A key should always be found. However we return an error if no keys were found.
145+
found := false
146+
for i := 0; i <= len(volumes); i++ {
147+
key := fmt.Sprintf("%d", i)
148+
if _, ok := newVolumes[key]; !ok {
149+
newVolumes[key] = &VolumeTemplate{
150+
ID: req.VolumeID,
151+
// name is ignored on this PATCH
152+
Name: req.VolumeID,
153+
}
154+
found = true
155+
break
156+
}
157+
}
158+
159+
if !found {
160+
return nil, fmt.Errorf("could not find key to attach volume %s", req.VolumeID)
147161
}
148162

149163
// update server

0 commit comments

Comments
 (0)