This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_turbot_notification.go
616 lines (566 loc) · 25.1 KB
/
table_turbot_notification.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package turbot
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strconv"
"time"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe-plugin-sdk/v3/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v3/plugin"
"github.com/turbot/steampipe-plugin-sdk/v3/plugin/transform"
)
func tableTurbotNotification(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "turbot_notification",
Description: "Notifications from the Turbot CMDB.",
List: &plugin.ListConfig{
Hydrate: listNotification,
KeyColumns: plugin.KeyColumnSlice{
{Name: "id", Require: plugin.Optional},
{Name: "notification_type", Require: plugin.Optional},
{Name: "control_id", Require: plugin.Optional},
{Name: "control_type_id", Require: plugin.Optional},
{Name: "control_type_uri", Require: plugin.Optional},
{Name: "resource_id", Require: plugin.Optional},
{Name: "resource_type_id", Require: plugin.Optional},
{Name: "resource_type_uri", Require: plugin.Optional},
{Name: "policy_setting_type_id", Require: plugin.Optional},
{Name: "policy_setting_type_uri", Require: plugin.Optional},
{Name: "actor_identity_id", Require: plugin.Optional},
{Name: "create_timestamp", Require: plugin.Optional, Operators: []string{">", ">=", "=", "<", "<="}},
{Name: "filter", Require: plugin.Optional},
},
},
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("id"),
Hydrate: getNotification,
},
Columns: []*plugin.Column{
// Top columns
{Name: "id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.ID"), Description: "Unique identifier of the notification."},
{Name: "process_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.ProcessID"), Description: "ID of the process that created this notification."},
{Name: "icon", Type: proto.ColumnType_STRING, Description: "Icon for this notification type."},
{Name: "message", Type: proto.ColumnType_STRING, Description: "Message for the notification."},
{Name: "notification_type", Type: proto.ColumnType_STRING, Description: "Type of the notification: resource, action, policySetting, control, grant, activeGrant."},
{Name: "create_timestamp", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("Turbot.CreateTimestamp"), Description: "When the resource was first discovered by Turbot. (It may have been created earlier.)"},
{Name: "filter", Type: proto.ColumnType_STRING, Transform: transform.FromQual("filter"), Description: "Filter used to search for notifications."},
// Actor info for the notification
{Name: "actor_identity_trunk_title", Type: proto.ColumnType_STRING, Transform: transform.FromField("Actor.Identity.Trunk.Title").NullIfZero(), Description: "Title hierarchy of the actor from the root down to the actor of this event."},
{Name: "actor_identity_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Actor.Identity.Turbot.ID").NullIfZero(), Description: "Identity ID of the actor that performed this event."},
// Resource info for notification
{Name: "resource_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.ResourceID").NullIfZero(), Description: "ID of the resource for this notification."},
{Name: "resource_trunk_title", Type: proto.ColumnType_STRING, Transform: transform.FromField("Resource.Trunk.Title"), Description: "Title of the resource hierarchy from the root down to this resource."},
{Name: "resource_title", Type: proto.ColumnType_STRING, Transform: transform.FromField("Resource.Turbot.Title"), Description: "Title of the resource."},
{Name: "resource_new_version_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.ResourceNewVersionID"), Description: "Version ID of the resource after the event."},
{Name: "resource_old_version_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.ResourceOldVersionID"), Description: "Version ID of the resource before the event."},
{Name: "resource_type_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Resource.Type.Turbot.ID").NullIfZero(), Description: "ID of the resource type for this notification."},
{Name: "resource_type_uri", Type: proto.ColumnType_STRING, Transform: transform.FromField("Resource.Type.URI"), Description: "URI of the resource type for this notification."},
{Name: "resource_type_trunk_title", Type: proto.ColumnType_STRING, Transform: transform.FromField("Resource.Type.Trunk.Title"), Description: "Title of the resource type hierarchy from the root down to this resource."},
{Name: "resource_data", Type: proto.ColumnType_JSON, Transform: transform.FromField("Resource.Data"), Description: "The data for this resource"},
{Name: "resource_akas", Type: proto.ColumnType_JSON, Transform: transform.FromField("Resource.Turbot.Akas"), Description: "The globally-unique akas for this resource."},
{Name: "resource_parent_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Resource.Turbot.ParentID").NullIfZero(), Description: "The id of the parent resource of this resource."},
{Name: "resource_path", Type: proto.ColumnType_STRING, Transform: transform.FromField("Resource.Turbot.Path"), Description: "The string of resource ids separated by \".\" from root down to this resource."},
{Name: "resource_tags", Type: proto.ColumnType_JSON, Transform: transform.FromField("Resource.Turbot.Tags"), Description: "Tags attached to this resource."},
// Policy settings notification details
{Name: "policy_setting_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.PolicySettingID"), Description: "ID of the policy setting for this notification."},
{Name: "policy_setting_new_version_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.PolicySettingNewVersionID"), Description: "Version ID of the policy setting after the event."},
{Name: "policy_setting_old_version_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.PolicySettingOldVersionID"), Description: "Version ID of the policy setting before the event."},
{Name: "policy_setting_default_template", Type: proto.ColumnType_STRING, Transform: transform.FromField("PolicySetting.DefaultTemplate"), Description: "The Nunjucks template if this setting is for a calculated value."},
{Name: "policy_setting_default_template_input", Type: proto.ColumnType_STRING, Transform: transform.FromField("PolicySetting.DefaultTemplateInput").Transform(formatPolicyFieldsValue), Description: "The GraphQL Input query if this setting is for a calculated value."},
{Name: "policy_setting_is_calculated", Type: proto.ColumnType_BOOL, Transform: transform.FromField("PolicySetting.isCalculated"), Description: "If true this setting contains calculated inputs e.g. templateInput and template."},
{Name: "policy_setting_type_id", Type: proto.ColumnType_INT, Transform: transform.FromField("PolicySetting.Type.Turbot.ID").NullIfZero(), Description: "ID of the policy setting type for this notification."},
{Name: "policy_setting_type_read_only", Type: proto.ColumnType_BOOL, Transform: transform.FromField("PolicySetting.Type.ReadOnly"), Description: "If true user-defined policy settings are blocked from being created."},
{Name: "policy_setting_type_secret", Type: proto.ColumnType_BOOL, Transform: transform.FromField("PolicySetting.Type.Secret"), Description: "If true policy value will be encrypted."},
{Name: "policy_setting_type_trunk_title", Type: proto.ColumnType_STRING, Transform: transform.FromField("PolicySetting.Type.Trunk.Title"), Description: "This is the title of hierarchy from the root down to this policy type."},
{Name: "policy_setting_type_uri", Type: proto.ColumnType_STRING, Transform: transform.FromField("PolicySetting.Type.URI"), Description: "URI of the policy setting type for this notification."},
{Name: "policy_setting_value", Type: proto.ColumnType_STRING, Transform: transform.FromField("PolicySetting.Value").Transform(formatPolicyFieldsValue), Description: "The value of the policy setting after this event."},
// Controls notification details
{Name: "control_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.ControlID"), Description: "ID of the control for this notification."},
{Name: "control_new_version_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.ControlNewVersionID"), Description: "Version ID of the control after the event."},
{Name: "control_old_version_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Turbot.ControlOldVersionID"), Description: "Version ID of the control before the event."},
{Name: "control_details", Type: proto.ColumnType_JSON, Transform: transform.FromField("Control.Details"), Description: "Optional details provided at the last state update of this control."},
{Name: "control_reason", Type: proto.ColumnType_STRING, Transform: transform.FromField("Control.Reason"), Description: "Optional reason provided at the last state update of this control."},
{Name: "control_state", Type: proto.ColumnType_STRING, Transform: transform.FromField("Control.State"), Description: "The current state of the control."},
{Name: "control_type_id", Type: proto.ColumnType_INT, Transform: transform.FromField("Control.Type.Turbot.ID"), Description: "ID of the control type for this control."},
{Name: "control_type_trunk_title", Type: proto.ColumnType_STRING, Transform: transform.FromField("Control.Type.Trunk.Title"), Description: "This is the title of hierarchy from the root down to this control type."},
{Name: "control_type_uri", Type: proto.ColumnType_STRING, Transform: transform.FromField("Control.Type.URI"), Description: "URI of the control type for this control."},
// ActiveGrants notification details
{Name: "active_grant_id", Type: proto.ColumnType_INT, Transform: fromField("Turbot.ActiveGrantsID"), Description: "Active grant ID for this notification."},
{Name: "active_grant_new_version_id", Type: proto.ColumnType_INT, Transform: fromField("Turbot.ActiveGrantsNewVersionID"), Description: "Active grant version ID of the grant after the notification."},
{Name: "active_grant_old_version_id", Type: proto.ColumnType_INT, Transform: fromField("Turbot.ActiveGrantsOldVersionID"), Description: "Version ID of the active grant before the event."},
{Name: "active_grant_valid_to_timestamp", Type: proto.ColumnType_TIMESTAMP, Transform: fromField("ActiveGrant.Grant.ValidToTimestamp"), Description: "Optional end date for the active grant to expire."},
{Name: "active_grant_identity_profile_id", Type: proto.ColumnType_STRING, Transform: fromField("ActiveGrant.Grant.Identity.ProfileID"), Description: "The identity of profile id for this active grant."},
{Name: "active_grant_identity_trunk_title", Type: proto.ColumnType_STRING, Transform: fromField("ActiveGrant.Grant.Identity.Trunk.Title"), Description: "This is the title of hierarchy from the root down to this identity (i.e. Identity whoes access got revoked/permiited) for this active grant."},
{Name: "active_grant_level_title", Type: proto.ColumnType_STRING, Transform: fromField("ActiveGrant.Grant.Level.Title"), Description: "The name of the active grant level."},
{Name: "active_grant_permission_level_id", Type: proto.ColumnType_INT, Transform: fromField("ActiveGrant.Grant.PermissionLevelId"), Description: "The unique identifier for the active grant permission level."},
{Name: "active_grant_permission_type_id", Type: proto.ColumnType_INT, Transform: fromField("ActiveGrant.Grant.PermissionTypeID"), Description: "The unique identifier for the active grant permission type."},
{Name: "active_grant_role_name", Type: proto.ColumnType_STRING, Transform: fromField("ActiveGrant.Grant.RoleName"), Description: "Optional custom roleName for this active grant, when using existing roles rather than Turbot-managed ones."},
{Name: "active_grant_type_title", Type: proto.ColumnType_STRING, Transform: fromField("ActiveGrant.Grant.Type.Title"), Description: "The name of the active grant type."},
// Grants notification details
{Name: "grant_id", Type: proto.ColumnType_INT, Transform: fromField("Turbot.GrantID"), Description: "ID of the grant for this notification."},
{Name: "grant_new_version_id", Type: proto.ColumnType_INT, Transform: fromField("Turbot.GrantNewVersionID"), Description: "Version ID of the grant after the event."},
{Name: "grant_old_version_id", Type: proto.ColumnType_INT, Transform: fromField("Turbot.GrantOldVersionID"), Description: "Version ID of the grant before the event."},
{Name: "grant_valid_to_timestamp", Type: proto.ColumnType_TIMESTAMP, Transform: fromField("Grant.ValidToTimestamp"), Description: "Optional end date for the grant."},
{Name: "grant_identity_profile_id", Type: proto.ColumnType_STRING, Transform: fromField("Grant.Identity.ProfileID"), Description: "The identity profile id for this grant."},
{Name: "grant_identity_trunk_title", Type: proto.ColumnType_STRING, Transform: fromField("Grant.Identity.Trunk.Title"), Description: "This is the title of hierarchy from the root down to this identity (i.e. Identity whoes access got revoked/permiited) for this grant."},
{Name: "grant_level_title", Type: proto.ColumnType_STRING, Transform: fromField("Grant.Level.Title"), Description: "The name of the permission level."},
{Name: "grant_permission_level_id", Type: proto.ColumnType_INT, Transform: fromField("Grant.PermissionLevelId"), Description: "The unique identifier for the permission level."},
{Name: "grant_permission_type_id", Type: proto.ColumnType_INT, Transform: fromField("Grant.PermissionTypeID"), Description: "The unique identifier for the permission type."},
{Name: "grant_role_name", Type: proto.ColumnType_STRING, Transform: fromField("Grant.RoleName"), Description: "Optional custom roleName for this grant, when using existing roles rather than Turbot-managed ones."},
{Name: "grant_type_title", Type: proto.ColumnType_STRING, Transform: fromField("Grant.Type.Title"), Description: "The name of the permission type."},
},
}
}
const (
queryNotificationList = `
query notificationList($filter: [String!], $next_token: String) {
notifications(filter: $filter, paging: $next_token) {
items {
icon
message
notificationType
data
actor {
identity {
trunk { title }
turbot {
title
id
actorIdentityId
}
}
}
control {
state
reason
details
type {
uri
trunk {
title
}
turbot {
id
}
}
}
resource {
data
metadata
trunk {
title
}
turbot {
akas
parentId
path
tags
title
}
type {
uri
trunk {
title
}
turbot {
id
}
}
}
policySetting {
isCalculated
type {
uri
readOnly
defaultTemplate
defaultTemplateInput
secret
trunk {
title
}
turbot {
id
}
}
value
}
grant {
roleName
permissionTypeId
permissionLevelId
validToTimestamp
validFromTimestamp
level {
title
}
type {
title
}
identity {
trunk { title }
profileId: get(path: "profileId")
}
}
activeGrant {
grant {
roleName
permissionTypeId
permissionLevelId
validToTimestamp
validFromTimestamp
level {
title
}
type {
title
}
identity {
trunk { title }
profileId: get(path: "profileId")
}
}
}
turbot {
controlId
controlNewVersionId
controlOldVersionId
createTimestamp
grantId
grantNewVersionId
grantOldVersionId
id
policySettingId
policySettingNewVersionId
policySettingOldVersionId
processId
resourceId
resourceNewVersionId
resourceOldVersionId
grantId
grantNewVersionId
grantOldVersionId
activeGrantsId
activeGrantsNewVersionId
activeGrantsOldVersionId
type
}
}
paging {
next
}
}
}`
queryNotificationGet = `
query notificationGet($id: ID!) {
notification(id: $id) {
icon
message
notificationType
data
actor {
identity {
trunk {
title
}
turbot {
title
id
actorIdentityId
}
}
}
control {
state
reason
details
type {
uri
trunk {
title
}
turbot {
id
}
}
}
resource {
data
metadata
trunk {
title
}
turbot {
akas
parentId
path
tags
title
}
type {
uri
trunk {
title
}
turbot {
id
}
}
}
policySetting {
isCalculated
type {
uri
readOnly
defaultTemplate
defaultTemplateInput
secret
trunk {
title
}
turbot {
id
}
}
value
}
grant {
roleName
permissionTypeId
permissionLevelId
validToTimestamp
validFromTimestamp
level {
title
}
type {
title
}
identity {
trunk {
title
}
profileId: get(path: "profileId")
}
}
activeGrant {
grant {
roleName
permissionTypeId
permissionLevelId
validToTimestamp
validFromTimestamp
level {
title
}
type {
title
}
identity {
trunk {
title
}
profileId: get(path: "profileId")
}
}
}
turbot {
controlId
controlNewVersionId
controlOldVersionId
createTimestamp
grantId
grantNewVersionId
grantOldVersionId
id
policySettingId
policySettingNewVersionId
policySettingOldVersionId
processId
resourceId
resourceNewVersionId
resourceOldVersionId
grantId
grantNewVersionId
grantOldVersionId
activeGrantsId
activeGrantsNewVersionId
activeGrantsOldVersionId
type
}
}
}`
)
func listNotification(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
conn, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("turbot_notification.listNotification", "connection_error", err)
return nil, err
}
filters := []string{}
quals := d.KeyColumnQuals
allQuals := d.Quals
filter := ""
if quals["filter"] != nil {
filter = quals["filter"].GetStringValue()
filters = append(filters, filter)
}
if quals["id"] != nil {
filters = append(filters, fmt.Sprintf("id:%s", getQualListValues(ctx, quals, "id", "int64")))
}
if quals["notification_type"] != nil {
filters = append(filters, fmt.Sprintf("notificationType:%s", getQualListValues(ctx, quals, "notification_type", "string")))
}
if quals["actor_identity_id"] != nil {
filters = append(filters, fmt.Sprintf("actorIdentityId:%s", getQualListValues(ctx, quals, "actor_identity_id", "int64")))
}
if quals["resource_id"] != nil {
filters = append(filters, fmt.Sprintf("resourceId:%s", getQualListValues(ctx, quals, "resource_id", "int64")))
}
if quals["resource_type_id"] != nil {
filters = append(filters, fmt.Sprintf("resourceTypeId:%s resourceTypeLevel:self", getQualListValues(ctx, quals, "resource_type_id", "int64")))
}
if quals["resource_type_uri"] != nil {
filters = append(filters, fmt.Sprintf("resourceTypeId:%s resourceTypeLevel:self", getQualListValues(ctx, quals, "resource_type_uri", "string")))
}
if quals["control_type_id"] != nil {
filters = append(filters, fmt.Sprintf("controlTypeId:%s controlTypeLevel:self", getQualListValues(ctx, quals, "control_type_id", "int64")))
}
if quals["control_type_uri"] != nil {
filters = append(filters, fmt.Sprintf("controlTypeId:%s controlTypeLevel:self", getQualListValues(ctx, quals, "control_type_uri", "string")))
}
if quals["policy_type_id"] != nil {
filters = append(filters, fmt.Sprintf("policyTypeId:%s policyTypeLevel:self", getQualListValues(ctx, quals, "policy_type_id", "int64")))
}
if quals["policy_type_uri"] != nil {
filters = append(filters, fmt.Sprintf("policyTypeId:%s policyTypeLevel:self", getQualListValues(ctx, quals, "policy_type_uri", "string")))
}
if allQuals["create_timestamp"] != nil {
for _, q := range allQuals["create_timestamp"].Quals {
// Subtracted 1 minute to FilterFrom time and Added 1 minute to FilterTo time to miss any results due to time conersions in steampipe
switch q.Operator {
case "=":
filters = append(filters, fmt.Sprintf("createTimestamp:'%s'", q.Value.GetTimestampValue().AsTime().Format(filterTimeFormat)))
case ">=", ">":
filters = append(filters, fmt.Sprintf("createTimestamp:>='%s'", q.Value.GetTimestampValue().AsTime().Add(-1*time.Minute).Format(filterTimeFormat)))
case "<", "<=":
filters = append(filters, fmt.Sprintf("createTimestamp:<='%s'", q.Value.GetTimestampValue().AsTime().Add(1*time.Minute).Format(filterTimeFormat)))
}
}
}
// Default to a very large page size. Page sizes earlier in the filter string
// win, so this is only used as a fallback.
pageResults := false
// Add a limit if they haven't given one in the filter field
re := regexp.MustCompile(`(^|\s)limit:[0-9]+($|\s)`)
if !re.MatchString(filter) {
// The caller did not specify a limit, so set a high limit and page all
// results.
pageResults = true
var pageLimit int64 = 5000
// Adjust page limit, if less than default value
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < pageLimit {
pageLimit = *limit
}
}
filters = append(filters, fmt.Sprintf("limit:%s", strconv.Itoa(int(pageLimit))))
}
plugin.Logger(ctx).Warn("turbot_notification.listNotification", "filters", filters)
nextToken := ""
for {
result := &NotificationsResponse{}
err = conn.DoRequest(queryNotificationList, map[string]interface{}{"filter": filters, "next_token": nextToken}, result)
if err != nil {
plugin.Logger(ctx).Error("turbot_notification.listNotification", "query_error", err)
// Not returning for function in case of errors because of resources/policies/controls referred might be deleted and
// graphql queries may fail to retrieve few properties for such items
// return nil, err
}
for _, r := range result.Notifications.Items {
d.StreamListItem(ctx, r)
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.QueryStatus.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
if !pageResults || result.Notifications.Paging.Next == "" {
break
}
nextToken = result.Notifications.Paging.Next
}
return nil, nil
}
func getNotification(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
conn, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("turbot_notification.getNotification", "connection_error", err)
return nil, err
}
id := d.KeyColumnQuals["id"].GetInt64Value()
result := &NotificationsGetResponse{}
err = conn.DoRequest(queryNotificationGet, map[string]interface{}{"id": id}, result)
if err != nil {
plugin.Logger(ctx).Error("turbot_notification.getNotification", "query_error", err)
return nil, err
}
return result.Notification, nil
}
//// TRANFORM FUNCTION
// formatPolicyValue:: Policy value can be a string, hcl or a json.
// It will transform the raw value from api into a string if a hcl or json
func formatPolicyFieldsValue(_ context.Context, d *transform.TransformData) (interface{}, error) {
var item = d.HydrateItem.(Notification)
columnName := d.ColumnName
var value interface{}
if item.PolicySetting != nil {
if columnName == "policy_template_input" {
value = item.PolicySetting.Type.DefaultTemplateInput
} else {
value = item.PolicySetting.Value
}
}
if value != nil {
switch val := value.(type) {
case string:
return val, nil
case []string, map[string]interface{}, interface{}:
data, err := json.Marshal(val)
if err != nil {
return nil, err
}
return string(data), nil
}
}
return nil, nil
}
// fromField:: generates a value by retrieving a field or a set of fields from the source item
func fromField(fieldNames ...string) *transform.ColumnTransforms {
var fieldNameArray []string
fieldNameArray = append(fieldNameArray, fieldNames...)
return &transform.ColumnTransforms{Transforms: []*transform.TransformCall{{Transform: fieldValue, Param: fieldNameArray}}}
}
// fieldValue function is intended for the start of a transform chain.
// This returns a field value of either the hydrate call result (if present) or the root item if not
// the field name is in the 'Param'
func fieldValue(ctx context.Context, d *transform.TransformData) (interface{}, error) {
var item = d.HydrateItem
var fieldNames []string
switch p := d.Param.(type) {
case []string:
fieldNames = p
case string:
fieldNames = []string{p}
default:
return nil, fmt.Errorf("'FieldValue' requires one or more string parameters containing property path but received %v", d.Param)
}
for _, propertyPath := range fieldNames {
fieldValue, ok := helpers.GetNestedFieldValueFromInterface(item, propertyPath)
if ok && !helpers.IsNil(fieldValue) {
return fieldValue, nil
}
}
return nil, nil
}