-
Notifications
You must be signed in to change notification settings - Fork 2
/
table_databricks_sql_alert.go
148 lines (131 loc) · 3.77 KB
/
table_databricks_sql_alert.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
package databricks
import (
"context"
"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 tableDatabricksSQLAlert(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "databricks_sql_alert",
Description: "Gets a list of alerts.",
List: &plugin.ListConfig{
Hydrate: listSQLAlerts,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AnyColumn([]string{"id"}),
ShouldIgnoreError: isNotFoundError([]string{"400"}),
Hydrate: getSQLAlert,
},
Columns: databricksAccountColumns([]*plugin.Column{
{
Name: "id",
Description: "Databricks alert ID.",
Type: proto.ColumnType_STRING,
},
{
Name: "name",
Description: "Name of the alert.",
Type: proto.ColumnType_STRING,
},
{
Name: "created_at",
Description: "Timestamp when the alert was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "last_triggered_at",
Description: "Timestamp when the alert was last triggered.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "parent",
Description: "The identifier of the workspace folder containing the object.",
Type: proto.ColumnType_STRING,
},
{
Name: "rearm",
Description: "Number of seconds after being triggered before the alert rearms itself and can be triggered again. If `null`, alert will never be triggered again.",
Type: proto.ColumnType_INT,
},
{
Name: "state",
Description: "State of the alert.",
Type: proto.ColumnType_STRING,
},
{
Name: "updated_at",
Description: "Timestamp when the alert was last updated.",
Type: proto.ColumnType_TIMESTAMP,
},
// JSON fields
{
Name: "options",
Description: "Alert configuration options",
Type: proto.ColumnType_JSON,
},
{
Name: "query",
Description: "Query associated with the alert.",
Type: proto.ColumnType_JSON,
},
{
Name: "user",
Description: "User associated with the alert.",
Type: proto.ColumnType_JSON,
},
// Standard Steampipe columns
{
Name: "title",
Description: "The title of the resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
}),
}
}
//// LIST FUNCTION
func listSQLAlerts(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
// Create client
client, err := getWorkspaceClient(ctx, d)
if err != nil {
logger.Error("databricks_sql_alert.listSQLAlerts", "connection_error", err)
return nil, err
}
alerts, err := client.Alerts.List(ctx)
if err != nil {
logger.Error("databricks_sql_alert.listSQLAlerts", "api_error", err)
return nil, err
}
for _, item := range alerts {
d.StreamListItem(ctx, item)
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getSQLAlert(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
id := d.EqualsQualString("id")
// Return nil, if no input provided
if id == "" {
return nil, nil
}
// Create client
client, err := getWorkspaceClient(ctx, d)
if err != nil {
logger.Error("databricks_sql_alert.getSQLAlert", "connection_error", err)
return nil, err
}
alert, err := client.Alerts.GetByAlertId(ctx, id)
if err != nil {
logger.Error("databricks_sql_alert.getSQLAlert", "api_error", err)
return nil, err
}
return alert, nil
}