-
Notifications
You must be signed in to change notification settings - Fork 2
/
table_scaleway_instance_volume.go
243 lines (209 loc) · 6.27 KB
/
table_scaleway_instance_volume.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package scaleway
import (
"context"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
//// TABLE DEFINITION
func tableScalewayInstanceVolume(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "scaleway_instance_volume",
Description: "A volume is where you store your data inside your instance.",
GetMatrixItemFunc: BuildZoneList,
List: &plugin.ListConfig{
Hydrate: listInstanceVolumes,
KeyColumns: []*plugin.KeyColumn{
{
Name: "name",
Require: plugin.Optional,
},
{
Name: "zone",
Require: plugin.Optional,
},
},
},
Get: &plugin.GetConfig{
Hydrate: getInstanceVolume,
KeyColumns: plugin.AllColumns([]string{"id", "zone"}),
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "The user-defined name of the volume.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "An unique identifier of the volume.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID"),
},
{
Name: "state",
Description: "The current state of the volume.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("State").Transform(transform.ToString),
},
{
Name: "size",
Description: "The size of the volume disk (in bytes).",
Type: proto.ColumnType_INT,
Transform: transform.FromField("Size").Transform(transform.ToInt),
},
{
Name: "volume_type",
Description: "The type of the volume.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("VolumeType").Transform(transform.ToString),
},
{
Name: "creation_date",
Description: "The time when the volume was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "export_uri",
Description: "Specifies the volume NBD export URI.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ExportURI"),
},
{
Name: "modification_date",
Description: "The time when the volume was last modified.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "server",
Description: "Specifies the server attached to the volume.",
Type: proto.ColumnType_JSON,
},
// Scaleway standard columns
{
Name: "zone",
Description: "Specifies the zone where the volume resides.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Zone").Transform(transform.ToString),
},
{
Name: "project",
Description: "The ID of the project where the volume resides.",
Type: proto.ColumnType_STRING,
},
{
Name: "organization",
Description: "The ID of the organization where the volume resides.",
Type: proto.ColumnType_STRING,
},
// Steampipe standard columns
{
Name: "title",
Description: "Title of the resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
},
}
}
//// LIST FUNCTION
func listInstanceVolumes(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
zone := d.EqualsQualString("zone")
parseZoneData, err := scw.ParseZone(zone)
if err != nil {
plugin.Logger(ctx).Error("scaleway_instance_volume.listInstanceVolumes", "zone_parsing_error", err)
return nil, err
}
quals := d.EqualsQuals
if quals["zone"] != nil && quals["zone"].GetStringValue() != zone {
return nil, nil
}
// Create client
client, err := getSessionConfig(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("scaleway_instance_volume.listInstanceVolumes", "connection_error", err)
return nil, err
}
// Create SDK objects for Scaleway Instance product
instanceApi := instance.NewAPI(client)
req := &instance.ListVolumesRequest{
Zone: parseZoneData,
Page: scw.Int32Ptr(1),
}
// Additional filters
if quals["name"] != nil {
req.Name = scw.StringPtr(quals["name"].GetStringValue())
}
// Retrieve the list of volumes
maxResult := int64(100)
// Reduce the basic request limit down if the user has only requested a small number of rows
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < maxResult {
maxResult = *limit
}
}
req.PerPage = scw.Uint32Ptr(uint32(maxResult))
var count int
for {
resp, err := instanceApi.ListVolumes(req)
if err != nil {
plugin.Logger(ctx).Error("scaleway_instance_volume.listInstanceVolumes", "query_error", err)
return nil, err
}
for _, volume := range resp.Volumes {
d.StreamListItem(ctx, volume)
// Increase the resource count by 1
count++
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
if resp.TotalCount == uint32(count) {
break
}
req.Page = scw.Int32Ptr(*req.Page + 1)
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getInstanceVolume(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
zone := d.EqualsQualString("zone")
parseZoneData, err := scw.ParseZone(zone)
if err != nil {
plugin.Logger(ctx).Error("scaleway_instance_volume.getInstanceVolume", "zone_parsing_error", err)
return nil, err
}
if d.EqualsQuals["zone"].GetStringValue() != zone {
return nil, nil
}
// Create client
client, err := getSessionConfig(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("scaleway_instance_volume.getInstanceVolume", "connection_error", err)
return nil, err
}
// Create SDK objects for Scaleway Instance product
instanceApi := instance.NewAPI(client)
id := d.EqualsQuals["id"].GetStringValue()
volumeZone := d.EqualsQuals["zone"].GetStringValue()
// No inputs
if id == "" && volumeZone == "" {
return nil, nil
}
data, err := instanceApi.GetVolume(&instance.GetVolumeRequest{
VolumeID: id,
Zone: parseZoneData,
})
if err != nil {
plugin.Logger(ctx).Error("scaleway_instance_volume.getInstanceVolume", "query_error", err)
if is404Error(err) {
return nil, nil
}
return nil, err
}
return data.Volume, nil
}