-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathresource_quality_monitor.go
135 lines (123 loc) · 4.33 KB
/
resource_quality_monitor.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
package catalog
import (
"context"
"fmt"
"time"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const qualityMonitorDefaultProvisionTimeout = 15 * time.Minute
func WaitForMonitor(w *databricks.WorkspaceClient, ctx context.Context, monitorName string) error {
return retry.RetryContext(ctx, qualityMonitorDefaultProvisionTimeout, func() *retry.RetryError {
endpoint, err := w.QualityMonitors.GetByTableName(ctx, monitorName)
if err != nil {
return retry.NonRetryableError(err)
}
switch endpoint.Status {
case catalog.MonitorInfoStatusMonitorStatusActive:
return nil
case catalog.MonitorInfoStatusMonitorStatusError, catalog.MonitorInfoStatusMonitorStatusFailed:
return retry.NonRetryableError(fmt.Errorf("monitor status retrund %s for monitor: %s", endpoint.Status, monitorName))
}
return retry.RetryableError(fmt.Errorf("monitor %s is still pending", monitorName))
})
}
func ResourceQualityMonitor() common.Resource {
monitorSchema := common.StructToSchema(
catalog.MonitorInfo{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
common.CustomizeSchemaPath(m, "assets_dir").SetRequired()
common.CustomizeSchemaPath(m, "output_schema_name").SetRequired()
common.CustomizeSchemaPath(m, "table_name").SetRequired()
common.CustomizeSchemaPath(m).AddNewField("skip_builtin_dashboard", &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Required: false,
})
common.CustomizeSchemaPath(m).AddNewField("warehouse_id", &schema.Schema{
Type: schema.TypeString,
Optional: true,
Required: false,
})
common.CustomizeSchemaPath(m, "monitor_version").SetReadOnly()
common.CustomizeSchemaPath(m, "drift_metrics_table_name").SetReadOnly()
common.CustomizeSchemaPath(m, "profile_metrics_table_name").SetReadOnly()
common.CustomizeSchemaPath(m, "status").SetReadOnly()
common.CustomizeSchemaPath(m, "dashboard_id").SetReadOnly()
common.CustomizeSchemaPath(m, "schedule", "pause_status").SetReadOnly()
return m
},
)
return common.Resource{
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
var create catalog.CreateMonitor
common.DataToStructPointer(d, monitorSchema, &create)
create.TableName = d.Get("table_name").(string)
endpoint, err := w.QualityMonitors.Create(ctx, create)
if err != nil {
return err
}
err = WaitForMonitor(w, ctx, create.TableName)
if err != nil {
return err
}
d.SetId(endpoint.TableName)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
endpoint, err := w.QualityMonitors.GetByTableName(ctx, d.Id())
if err != nil {
return err
}
oldWarehouseId := d.Get("warehouse_id").(string)
oldSkipBuiltinDashboard := d.Get("skip_builtin_dashboard").(bool)
err = common.StructToData(endpoint, monitorSchema, d)
if err != nil {
return err
}
// we need this because Get API doesn't return that information
d.Set("warehouse_id", oldWarehouseId)
d.Set("skip_builtin_dashboard", oldSkipBuiltinDashboard)
return nil
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
var update catalog.UpdateMonitor
common.DataToStructPointer(d, monitorSchema, &update)
update.TableName = d.Get("table_name").(string)
if update.Schedule != nil {
update.Schedule.PauseStatus = ""
}
_, err = w.QualityMonitors.Update(ctx, update)
if err != nil {
return err
}
return WaitForMonitor(w, ctx, update.TableName)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
return w.QualityMonitors.DeleteByTableName(ctx, d.Id())
},
Schema: monitorSchema,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(qualityMonitorDefaultProvisionTimeout),
},
}
}