/
table_scaleway_instance_image.go
269 lines (234 loc) · 6.9 KB
/
table_scaleway_instance_image.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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 tableScalewayInstanceImage(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "scaleway_instance_image",
Description: "Images are backups of your instances.",
GetMatrixItemFunc: BuildZoneList,
List: &plugin.ListConfig{
Hydrate: listInstanceImages,
KeyColumns: []*plugin.KeyColumn{
{
Name: "name",
Require: plugin.Optional,
},
{
Name: "public",
Require: plugin.Optional,
Operators: []string{"<>", "="},
},
{
Name: "zone",
Require: plugin.Optional,
},
},
},
Get: &plugin.GetConfig{
Hydrate: getInstanceImage,
KeyColumns: plugin.AllColumns([]string{"id", "zone"}),
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "The user-defined name of the image.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "An unique identifier of the image.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID"),
},
{
Name: "state",
Description: "The current state of the image.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("State").Transform(transform.ToString),
},
{
Name: "public",
Description: "Indicates whether the image is public, or not.",
Type: proto.ColumnType_BOOL,
},
{
Name: "creation_date",
Description: "The time when the image was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "arch",
Description: "The architecture the image is compatible with. Possible values are 'x86_64' and 'arm'.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Arch").Transform(transform.ToString),
},
{
Name: "from_server",
Description: "The ID of the server the image if based from.",
Type: proto.ColumnType_STRING,
},
{
Name: "default_boot_script",
Description: "Describes the default bootscript for this image.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("DefaultBootscript"),
},
{
Name: "extra_volumes",
Description: "Describes the extra volumes for this image.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("DefaultBootscript"),
},
{
Name: "root_volume",
Description: "Describes the root volume in this image.",
Type: proto.ColumnType_JSON,
},
// Scaleway standard columns
{
Name: "zone",
Description: "Specifies the zone where the image resides.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Zone").Transform(transform.ToString),
},
{
Name: "project",
Description: "The ID of the project where the image resides.",
Type: proto.ColumnType_STRING,
},
{
Name: "organization",
Description: "The ID of the organization where the image 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 listInstanceImages(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_image.listInstanceImages", "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_image.listInstanceImages", "connection_error", err)
return nil, err
}
// Create SDK objects for Scaleway Instance product
instanceApi := instance.NewAPI(client)
req := &instance.ListImagesRequest{
Zone: parseZoneData,
Page: scw.Int32Ptr(1),
}
// Additional filters
if quals["name"] != nil {
req.Name = scw.StringPtr(quals["name"].GetStringValue())
}
if d.EqualsQuals["public"] != nil {
req.Public = scw.BoolPtr(d.EqualsQuals["public"].GetBoolValue())
}
// Non-Equals Qual Map handling
if d.Quals["public"] != nil {
for _, q := range d.Quals["public"].Quals {
value := q.Value.GetBoolValue()
if q.Operator == "<>" {
req.Public = scw.BoolPtr(false)
if !value {
req.Public = scw.BoolPtr(true)
}
}
}
}
// Retrieve the list of images
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.ListImages(req)
if err != nil {
plugin.Logger(ctx).Error("scaleway_instance_image.listInstanceImages", "query_error", err)
return nil, err
}
for _, image := range resp.Images {
d.StreamListItem(ctx, image)
// 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 getInstanceImage(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_image.getInstanceImage", "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_image.getInstanceImage", "connection_error", err)
return nil, err
}
// Create SDK objects for Scaleway Instance product
instanceApi := instance.NewAPI(client)
id := d.EqualsQuals["id"].GetStringValue()
imageZone := d.EqualsQuals["zone"].GetStringValue()
// No inputs
if id == "" && imageZone == "" {
return nil, nil
}
data, err := instanceApi.GetImage(&instance.GetImageRequest{
ImageID: id,
Zone: parseZoneData,
})
if err != nil {
plugin.Logger(ctx).Error("scaleway_instance_image.getInstanceImage", "query_error", err)
if is404Error(err) {
return nil, nil
}
return nil, err
}
return data.Image, nil
}