-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtable_gcp_compute_disk.go
452 lines (410 loc) · 14.7 KB
/
table_gcp_compute_disk.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package gcp
import (
"context"
"strings"
"github.com/turbot/go-kit/types"
"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"
"google.golang.org/api/compute/v1"
)
func tableGcpComputeDisk(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_compute_disk",
Description: "GCP Compute Disk",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
Hydrate: getComputeDisk,
},
List: &plugin.ListConfig{
Hydrate: listComputeDisk,
KeyColumns: plugin.KeyColumnSlice{
// String columns
{Name: "name", Require: plugin.Optional, Operators: []string{"<>", "="}},
{Name: "status", Require: plugin.Optional, Operators: []string{"<>", "="}},
},
},
Columns: []*plugin.Column{
// commonly used columns
{
Name: "name",
Description: "Name of the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "The unique identifier for the resource. This identifier is defined by the server.",
Type: proto.ColumnType_INT,
},
{
Name: "creation_timestamp",
Description: "Timestamp when the disk was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "size_gb",
Description: "Size, in GB, of the persistent disk.",
Type: proto.ColumnType_DOUBLE,
},
{
Name: "status",
Description: "The status of disk creation. CREATING: Disk is provisioning. RESTORING: Source data is being copied into the disk. FAILED: Disk creation failed. READY: Disk is ready for use. DELETING: Disk is deleting.",
Type: proto.ColumnType_STRING,
},
{
Name: "description",
Description: "An optional description of this resource. Provide this property when you create the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "disk_encryption_key",
Description: "Specifies the encryption configuration used to encrypt stored data.",
Type: proto.ColumnType_JSON,
},
{
Name: "disk_encryption_key_type",
Description: "The type of encryption key used to encrypt storage data. Valid values are Google managed | Customer managed | Customer supplied.",
Type: proto.ColumnType_STRING,
Transform: transform.From(diskEncryptionKeyType),
},
{
Name: "kind",
Description: "Type of the resource. Always compute#disk for disks.",
Type: proto.ColumnType_STRING,
},
{
Name: "last_attach_timestamp",
Description: "Timestamp when the disk was last attached.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfZero(),
},
{
Name: "last_detach_timestamp",
Description: "Timestamp when the disk was last detached.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfZero(),
},
{
Name: "physical_block_size_bytes",
Description: "Physical block size of the persistent disk, in bytes. If not present in a request, a default value is used.",
Type: proto.ColumnType_INT,
},
{
Name: "self_link",
Description: "Server-defined fully-qualified URL for this resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_disk",
Description: "The source disk used to create this disk. You can provide this as a partial or full URL to the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_disk_id",
Description: "The unique ID of the disk used to create this disk. This value identifies the exact disk that was used to create this persistent disk.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_image",
Description: "The source image used to create this disk. If the source image is deleted, this field will not be set.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_image_id",
Description: "The ID value of the image used to create this disk. This value identifies the exact image that was used to create this persistent disk.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_snapshot",
Description: "The source snapshot used to create this disk.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_snapshot_id",
Description: "The unique ID of the snapshot used to create this disk. This value identifies the exact snapshot that was used to create this persistent disk.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_image_encryption_key",
Description: "The customer-supplied encryption key of the source image. Required if the source image is protected by a customer-supplied encryption key.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_snapshot_encryption_key",
Description: "The customer-supplied encryption key of the source snapshot. Required if the source snapshot is protected by a customer-supplied encryption key.",
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "URL of the disk type resource describing which disk type to use to create the disk. Provide this when creating the disk. For example: projects/project/zones/zone/diskTypes/pd-standard or pd-ssd",
Type: proto.ColumnType_STRING,
},
// type_name is a simpler view of the type, without the full path
{
Name: "type_name",
Description: "Type of the disk. For example: pd-standard or pd-ssd",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Type").Transform(lastPathElement),
},
{
Name: "location_type",
Description: "Location type where the disk resides.",
Type: proto.ColumnType_STRING,
Transform: transform.FromP(diskLocation, "Type"),
},
{
Name: "region",
Description: "URL of the region where the disk resides. Only applicable for regional resources.",
Type: proto.ColumnType_STRING,
},
// region_name is a simpler view of the region, without the full path
{
Name: "region_name",
Description: "Name of the region where the disk resides. Only applicable for regional resources.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Region").Transform(lastPathElement),
},
{
Name: "zone",
Description: "URL of the zone where the disk resides.",
Type: proto.ColumnType_STRING,
},
// zone_name is a simpler view of the zone, without the full path
{
Name: "zone_name",
Description: "The zone name in which the disk resides.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Zone").Transform(lastPathElement),
},
{
Name: "guest_os_features",
Description: "A list of features to enable on the guest operating system. Applicable only for bootable images.",
Type: proto.ColumnType_JSON,
},
{
Name: "labels",
Description: "A map of labels assigned to bucket",
Type: proto.ColumnType_JSON,
},
{
Name: "licenses",
Description: "A list of publicly visible licenses.",
Type: proto.ColumnType_JSON,
},
{
Name: "license_codes",
Description: "Integer license codes indicating which licenses are attached to this disk.",
Type: proto.ColumnType_JSON,
},
{
Name: "replica_zones",
Description: "URLs of the zones where the disk should be replicated to. Only applicable for regional resources.",
Type: proto.ColumnType_JSON,
},
{
Name: "resource_policies",
Description: "Resource policies applied to this disk for automatic snapshot creations.",
Type: proto.ColumnType_JSON,
},
{
Name: "users",
Description: "Links to the users of the disk (attached instances) in form: projects/project/zones/zone/instances/instance",
Type: proto.ColumnType_JSON,
},
{
Name: "iam_policy",
Description: "An Identity and Access Management (IAM) policy, which specifies access controls for Google Cloud resources. A `Policy` is a collection of `bindings`. A `binding` binds one or more `members` to a single `role`. Members can be user accounts, service accounts, Google groups, and domains (such as G Suite). A `role` is a named list of permissions; each `role` can be an IAM predefined role or a user-created custom role. For some types of Google Cloud resources, a `binding` can also specify a `condition`, which is a logical expression that allows access to a resource only if the expression evaluates to `true`.",
Type: proto.ColumnType_JSON,
Hydrate: getComputeDiskIamPolicy,
Transform: transform.FromValue(),
},
// standard steampipe columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Labels"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.From(diskAka),
},
// standard gcp columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(diskLocation, "Location"),
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(diskLocation, "Project"),
},
},
}
}
//// LIST FUNCTIONS
func listComputeDisk(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("listComputeDisk")
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
return nil, err
}
filterQuals := []filterQualMap{
{"name", "name", "string"},
{"status", "status", "string"},
}
filters := buildQueryFilterFromQuals(filterQuals, d.Quals)
filterString := ""
if len(filters) > 0 {
filterString = strings.Join(filters, " ")
}
// Max limit is set as per documentation
// https://pkg.go.dev/google.golang.org/[email protected]/compute/v1?utm_source=gopls#DisksAggregatedListCall.MaxResults
pageSize := types.Int64(500)
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < *pageSize {
pageSize = limit
}
}
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
return nil, err
}
project := projectId.(string)
resp := service.Disks.AggregatedList(project).Filter(filterString).MaxResults(*pageSize)
if err := resp.Pages(ctx, func(page *compute.DiskAggregatedList) error {
for _, item := range page.Items {
for _, disk := range item.Disks {
d.StreamListItem(ctx, disk)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
page.NextPageToken = ""
return nil
}
}
}
return nil
}); err != nil {
return nil, err
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getComputeDisk(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getComputeDisk")
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
return nil, err
}
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
return nil, err
}
project := projectId.(string)
var disk compute.Disk
name := d.EqualsQuals["name"].GetStringValue()
resp := service.Disks.AggregatedList(project).Filter("name=" + name)
if err := resp.Pages(ctx, func(page *compute.DiskAggregatedList) error {
for _, item := range page.Items {
for _, i := range item.Disks {
disk = *i
}
}
return nil
},
); err != nil {
return nil, err
}
// If the specified resource is not present, API does not return any not found errors
if len(disk.Name) < 1 {
return nil, nil
}
return &disk, nil
}
func getComputeDiskIamPolicy(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
disk := h.Item.(*compute.Disk)
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
return nil, err
}
var resp *compute.Policy
project := strings.Split(disk.SelfLink, "/")[6]
zoneName := getLastPathElement(types.SafeString(disk.Zone))
// disk can be regional or zonal
if zoneName == "" {
regionName := getLastPathElement(types.SafeString(disk.Region))
// regional disk get iam policy
resp, err = service.RegionDisks.GetIamPolicy(project, regionName, disk.Name).Do()
if err != nil {
return nil, err
}
return resp, nil
}
// zonal disk get iam policy
resp, err = service.Disks.GetIamPolicy(project, zoneName, disk.Name).Do()
if err != nil {
return nil, err
}
return resp, nil
}
//// TRANSFORM FUNCTIONS
func diskAka(_ context.Context, d *transform.TransformData) (interface{}, error) {
i := d.HydrateItem.(*compute.Disk)
zoneName := getLastPathElement(types.SafeString(i.Zone))
regionName := getLastPathElement(types.SafeString(i.Region))
project := strings.Split(i.SelfLink, "/")[6]
diskName := types.SafeString(i.Name)
akas := []string{"gcp://compute.googleapis.com/projects/" + project + "/zones/" + zoneName + "/disks/" + diskName}
if zoneName == "" {
akas = []string{"gcp://compute.googleapis.com/projects/" + project + "/regions/" + regionName + "/disks/" + diskName}
}
return akas, nil
}
func diskLocation(_ context.Context, d *transform.TransformData) (interface{}, error) {
i := d.HydrateItem.(*compute.Disk)
param := d.Param.(string)
zoneName := getLastPathElement(types.SafeString(i.Zone))
regionName := getLastPathElement(types.SafeString(i.Region))
project := strings.Split(i.SelfLink, "/")[6]
locationData := map[string]string{
"Type": "ZONAL",
"Location": zoneName,
"Project": project,
}
if zoneName == "" {
locationData["Type"] = "REGIONAL"
locationData["Location"] = regionName
}
return locationData[param], nil
}
func diskEncryptionKeyType(_ context.Context, d *transform.TransformData) (interface{}, error) {
i := d.HydrateItem.(*compute.Disk)
if i.DiskEncryptionKey == nil {
return "Google managed", nil
} else if len(i.DiskEncryptionKey.KmsKeyName) > 0 {
return "Customer managed", nil
} else if len(i.DiskEncryptionKey.Sha256) > 0 {
return "Customer supplied", nil
}
return nil, nil
}