Skip to content

Commit

Permalink
Add prefix column as key column to gcp_storage_object table (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
villers authored Aug 3, 2023
1 parent 73930c7 commit c84b76f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
17 changes: 17 additions & 0 deletions docs/tables/gcp_storage_object.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ where
and name = 'test/logs/2021/03/01/12/abc.txt';
```

### List storage objects with a prefix in a bucket

```sql
select
id,
name,
bucket,
size,
storage_class,
time_created
from
gcp_storage_object
where
bucket = 'steampipe-test'
and prefix = 'test/logs/2021/03/01/12';
```

### List storage objects encrypted with customer managed keys

```sql
Expand Down
16 changes: 13 additions & 3 deletions gcp/table_gcp_storage_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ func tableGcpStorageObject(_ context.Context) *plugin.Table {
Hydrate: getStorageObject,
},
List: &plugin.ListConfig{
KeyColumns: plugin.SingleColumn("bucket"),
Hydrate: listStorageObjects,
KeyColumns: []*plugin.KeyColumn{
{Name: "bucket", Require: plugin.Required, CacheMatch: "exact"},
{Name: "prefix", Require: plugin.Optional},
},
Hydrate: listStorageObjects,
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "The name of the object.",
Type: proto.ColumnType_STRING,
},
{
Name: "prefix",
Description: "The prefix of the key of the object.",
Type: proto.ColumnType_STRING,
Transform: transform.FromQual("prefix"),
},
{
Name: "id",
Description: "The ID of the object, including the bucket name, object name, and generation number.",
Expand Down Expand Up @@ -233,6 +242,7 @@ func tableGcpStorageObject(_ context.Context) *plugin.Table {

func listStorageObjects(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
bucket := d.EqualsQualString("bucket")
prefix := d.EqualsQualString("prefix")

// The bucket name should not be empty
if bucket == "" {
Expand All @@ -255,7 +265,7 @@ func listStorageObjects(ctx context.Context, d *plugin.QueryData, h *plugin.Hydr
}
}

resp := service.Objects.List(bucket).Projection("full").MaxResults(*maxResults)
resp := service.Objects.List(bucket).Prefix(prefix).Projection("full").MaxResults(*maxResults)
if err := resp.Pages(ctx, func(page *storage.Objects) error {
for _, object := range page.Items {
d.StreamListItem(ctx, object)
Expand Down

0 comments on commit c84b76f

Please sign in to comment.