From 5182c4849b0d4ed9cc4ac87c505437b861b9a588 Mon Sep 17 00:00:00 2001 From: Patrik Cyvoct Date: Mon, 16 Sep 2019 10:14:35 +0200 Subject: [PATCH 1/2] fix: attach volume key choice Signed-off-by: Patrik Cyvoct --- api/instance/v1/instance_utils.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/api/instance/v1/instance_utils.go b/api/instance/v1/instance_utils.go index aaf7b40d9..0b69c82c1 100644 --- a/api/instance/v1/instance_utils.go +++ b/api/instance/v1/instance_utils.go @@ -139,11 +139,25 @@ func (s *API) AttachVolume(req *AttachVolumeRequest, opts ...scw.RequestOption) newVolumes := volumesToVolumeTemplates(volumes) // add volume to volumes list - key := fmt.Sprintf("%d", len(volumes)) - newVolumes[key] = &VolumeTemplate{ - ID: req.VolumeID, - // name is ignored on this PATCH - Name: req.VolumeID, + // 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] = &VolumeTemplate{ + ID: req.VolumeID, + // name is ignored on this PATCH + Name: req.VolumeID, + } + found = true + break + } + } + + if !found { + return nil, fmt.Errorf("could not find key to attach volume %s", req.VolumeID) } // update server From 2e4bfc411e1a89e6e36fbc747c4663cc8120e4af Mon Sep 17 00:00:00 2001 From: Quentin Brosse Date: Mon, 16 Sep 2019 11:03:39 +0200 Subject: [PATCH 2/2] typo comment --- api/instance/v1/instance_utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/instance/v1/instance_utils.go b/api/instance/v1/instance_utils.go index 0b69c82c1..6da60eb5a 100644 --- a/api/instance/v1/instance_utils.go +++ b/api/instance/v1/instance_utils.go @@ -139,7 +139,7 @@ func (s *API) AttachVolume(req *AttachVolumeRequest, opts ...scw.RequestOption) newVolumes := volumesToVolumeTemplates(volumes) // add volume to volumes list - // we loop through all the possible volume keys (0 to len(volumes)) + // 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