-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtable_gcp_bigquery_table.go
362 lines (335 loc) · 11.4 KB
/
table_gcp_bigquery_table.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
package gcp
import (
"context"
"strings"
"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
"google.golang.org/api/bigquery/v2"
)
//// TABLE DEFINITION
func tableGcpBigqueryTable(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_bigquery_table",
Description: "GCP Bigquery Table",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"dataset_id", "table_id"}),
Hydrate: getBigqueryTable,
},
List: &plugin.ListConfig{
ParentHydrate: listBigQueryDatasets,
Hydrate: listBigqueryTables,
ShouldIgnoreError: isIgnorableError([]string{"403"}),
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "A descriptive name for this table, if one exists.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("FriendlyName"),
},
{
Name: "table_id",
Description: "The ID of the table resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("TableReference.TableId"),
},
{
Name: "dataset_id",
Description: "The ID of the dataset containing this table.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("TableReference.DatasetId"),
},
{
Name: "id",
Description: "An opaque ID uniquely identifying the table.",
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "The type of table. Possible values are: TABLE, VIEW.",
Type: proto.ColumnType_STRING,
},
{
Name: "self_link",
Description: "A URL that can be used to access this resource again.",
Type: proto.ColumnType_STRING,
Hydrate: getBigqueryTable,
},
{
Name: "creation_time",
Description: "The time when this table was created, in milliseconds since the epoch.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromField("CreationTime").Transform(transform.UnixMsToTimestamp),
},
{
Name: "description",
Description: "A user-friendly description of this table.",
Type: proto.ColumnType_STRING,
Hydrate: getBigqueryTable,
},
{
Name: "etag",
Description: "A hash of the table metadata, used to ensure there were no concurrent modifications to the resource when attempting an update.",
Type: proto.ColumnType_STRING,
Hydrate: getBigqueryTable,
},
{
Name: "expiration_time",
Description: "The time when this table expires, in milliseconds since the epoch. If not present, the table will persist indefinitely. Expired tables will be deleted and their storage reclaimed. The defaultTableExpirationMs property of the encapsulating dataset can be used to set a default expirationTime on newly created tables.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromField("ExpirationTime").Transform(transform.UnixMsToTimestamp),
},
{
Name: "kind",
Description: "The type of resource ID.",
Type: proto.ColumnType_STRING,
},
{
Name: "kms_key_name",
Description: "Describes the Cloud KMS encryption key that will be used to protect destination BigQuery table.",
Type: proto.ColumnType_STRING,
Hydrate: getBigqueryTable,
Transform: transform.FromField("EncryptionConfiguration.KmsKeyName"),
},
{
Name: "last_modified_time",
Description: "The time when this table was last modified.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromField("LastModifiedTime").Transform(transform.UnixMsToTimestamp),
Hydrate: getBigqueryTable,
},
{
Name: "num_bytes",
Description: "The size of this table in bytes, excluding any data in the streaming buffer.",
Type: proto.ColumnType_INT,
Hydrate: getBigqueryTable,
},
{
Name: "num_long_term_bytes",
Description: "The number of bytes in the table that are considered 'long-term storage'.",
Type: proto.ColumnType_INT,
Hydrate: getBigqueryTable,
},
{
Name: "num_physical_bytes",
Description: "The physical size of this table in bytes, excluding any data in the streaming buffer.",
Type: proto.ColumnType_INT,
Hydrate: getBigqueryTable,
},
{
Name: "num_rows",
Description: "The number of rows of data in this table, excluding any data in the streaming buffer.",
Type: proto.ColumnType_INT,
Hydrate: getBigqueryTable,
},
{
Name: "require_partition_filter",
Description: "If set to true, queries over this table require a partition filter that can be used for partition elimination to be specified.",
Type: proto.ColumnType_BOOL,
Hydrate: getBigqueryTable,
},
{
Name: "snapshot_time",
Description: "The time at which the base table was snapshot.",
Type: proto.ColumnType_TIMESTAMP,
Hydrate: getBigqueryTable,
Transform: transform.FromField("SnapshotDefinition.SnapshotTime"),
},
{
Name: "view_query",
Description: "A query that BigQuery executes when the view is referenced.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("View.Query"),
Hydrate: getBigqueryTable,
},
{
Name: "view_use_legacy_sql",
Description: "True if view is defined in legacy SQL dialect, false if in standard SQL.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromField("View.UseLegacySql"),
},
{
Name: "clustering_fields",
Description: "One or more fields on which data should be clustered.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Clustering.Fields"),
},
{
Name: "external_data_configuration",
Description: "Describes the data format, location, and other properties of a table stored outside of BigQuery.",
Type: proto.ColumnType_JSON,
Hydrate: getBigqueryTable,
},
{
Name: "labels",
Description: "The labels associated with this table. You can use these to organize and group your tables. Label keys and values can be no longer than 63 characters, can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. Label values are optional. Label keys must start with a letter and each label in the list must have a different key.",
Type: proto.ColumnType_JSON,
},
{
Name: "materialized_view",
Description: "Describes materialized view definition.",
Type: proto.ColumnType_JSON,
Hydrate: getBigqueryTable,
},
{
Name: "range_partitioning",
Description: "If specified, configures range partitioning for this table.",
Type: proto.ColumnType_JSON,
},
{
Name: "schema_fields",
Description: "Describes the fields in a table.",
Type: proto.ColumnType_JSON,
Hydrate: getBigqueryTable,
Transform: transform.FromField("Schema.Fields"),
},
{
Name: "streaming_buffer",
Description: "Contains information regarding this table's streaming buffer, if one is present.",
Type: proto.ColumnType_JSON,
Hydrate: getBigqueryTable,
},
{
Name: "time_partitioning",
Description: "If specified, configures time-based partitioning for this table.",
Type: proto.ColumnType_JSON,
},
{
Name: "view",
Description: "dditional details for a view.",
Type: proto.ColumnType_JSON,
},
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.From(bigQueryTableTitle),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Labels"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.From(bigqueryTableAkas),
},
// GCP standard columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Hydrate: getBigqueryTable,
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("TableReference.ProjectId"),
},
},
}
}
//// LIST FUNCTION
func listBigqueryTables(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("ListBigqueryTables")
// Get a details of Cloud Dataset
dataset := h.Item.(*bigquery.DatasetListDatasets)
// Create Service Connection
service, err := BigQueryService(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)
resp := service.Tables.List(project, dataset.DatasetReference.DatasetId)
if err := resp.Pages(ctx, func(page *bigquery.TableList) error {
for _, table := range page.Tables {
d.StreamListItem(ctx, table)
}
return nil
}); err != nil {
return nil, err
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getBigqueryTable(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getBigqueryTable")
// Create Service Connection
service, err := BigQueryService(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 datasetID, id string
if h.Item != nil {
data := h.Item.(*bigquery.TableListTables)
datasetID = data.TableReference.DatasetId
id = data.TableReference.TableId
} else {
datasetID = d.KeyColumnQuals["dataset_id"].GetStringValue()
id = d.KeyColumnQuals["table_id"].GetStringValue()
}
// Empty Check
if id == "" || datasetID == ""{
return nil, nil
}
resp, err := service.Tables.Get(project, datasetID, id).Do()
if err != nil {
return nil, err
}
return resp, err
}
//// TRANSFORM FUNCTIONS
func bigqueryTableAkas(_ context.Context, h *transform.TransformData) (interface{}, error) {
data := tableID(h.HydrateItem)
projectID := strings.Split(data, ":")[0]
id := strings.Split(data, ":")[1]
datasetId := strings.Split(id, ".")[0]
id = strings.Split(id, ".")[1]
akas := []string{"gcp://bigquery.googleapis.com/projects/" + projectID + "/datasets/" + datasetId + "/tables/" + id}
return akas, nil
}
func bigQueryTableTitle(_ context.Context, h *transform.TransformData) (interface{}, error) {
data := tableID(h.HydrateItem)
name := tableName(h.HydrateItem)
if len(name) > 0 {
return name, nil
}
return strings.Split(strings.Split(data, ":")[1], ".")[1], nil
}
func tableID(item interface{}) string {
switch item := item.(type) {
case *bigquery.TableListTables:
return item.Id
case *bigquery.Table:
return item.Id
}
return ""
}
func tableName(item interface{}) string {
switch item := item.(type) {
case *bigquery.TableListTables:
return item.FriendlyName
case *bigquery.Table:
return item.FriendlyName
}
return ""
}