/
list_volumes.go
97 lines (84 loc) · 3.45 KB
/
list_volumes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Copyright 2020 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package provider
import (
"fmt"
"github.com/IBM/ibmcloud-storage-volume-lib/lib/metrics"
"github.com/IBM/ibmcloud-storage-volume-lib/lib/provider"
userError "github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/messages"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/models"
"go.uber.org/zap"
"strings"
"time"
)
const (
maxLimit = 100
startVolumeIDNotFoundMsg = "start parameter is not valid"
)
// ListVolumes list all volumes
func (vpcs *VPCSession) ListVolumes(limit int, start string, tags map[string]string) (*provider.VolumeList, error) {
vpcs.Logger.Info("Entry ListVolumes", zap.Reflect("start", start), zap.Reflect("filters", tags))
defer vpcs.Logger.Info("Exit ListVolumes", zap.Reflect("start", start), zap.Reflect("filters", tags))
defer metrics.UpdateDurationFromStart(vpcs.Logger, "ListVolumes", time.Now())
if limit < 0 {
return nil, userError.GetUserError("InvalidListVolumesLimit", nil, limit)
}
if limit > maxLimit {
vpcs.Logger.Warn(fmt.Sprintf("listVolumes requested max entries of %v, supports values <= %v so defaulting value back to %v", limit, maxLimit, maxLimit))
limit = maxLimit
}
filters := &models.ListVolumeFilters{
// Tag: tags["tag"],
ResourceGroupID: tags["resource_group.id"],
ZoneName: tags["zone.name"],
VolumeName: tags["name"],
}
vpcs.Logger.Info("Getting volumes list from VPC provider...", zap.Reflect("start", start), zap.Reflect("filters", filters))
var volumes *models.VolumeList
var err error
err = retry(vpcs.Logger, func() error {
volumes, err = vpcs.Apiclient.VolumeService().ListVolumes(limit, start, filters, vpcs.Logger)
return err
})
if err != nil {
if strings.Contains(err.Error(), startVolumeIDNotFoundMsg) {
return nil, userError.GetUserError("StartVolumeIDNotFound", err, start)
}
return nil, userError.GetUserError("ListVolumesFailed", err)
}
vpcs.Logger.Info("Successfully retrieved volumes list from VPC backend", zap.Reflect("VolumesList", volumes))
var respVolumesList = &provider.VolumeList{}
if volumes != nil {
if volumes.Next != nil {
var next string
// "Next":{"href":"https://eu-gb.iaas.cloud.ibm.com/v1/volumes?start=3e898aa7-ac71-4323-952d-a8d741c65a68\u0026limit=1\u0026zone.name=eu-gb-1"}
if strings.Contains(volumes.Next.Href, "start=") {
next = strings.Split(strings.Split(volumes.Next.Href, "start=")[1], "\u0026")[0]
} else {
vpcs.Logger.Warn("Volumes.Next.Href is not in expected format", zap.Reflect("volumes.Next.Href", volumes.Next.Href))
}
respVolumesList.Next = next
}
volumeslist := volumes.Volumes
if volumeslist != nil && len(volumeslist) > 0 {
for _, volItem := range volumeslist {
volumeResponse := FromProviderToLibVolume(volItem, vpcs.Logger)
respVolumesList.Volumes = append(respVolumesList.Volumes, volumeResponse)
}
}
}
return respVolumesList, err
}