-
Notifications
You must be signed in to change notification settings - Fork 367
/
resource_online_table.go
120 lines (110 loc) · 4.32 KB
/
resource_online_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
package catalog
import (
"context"
"errors"
"fmt"
"time"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/apierr"
"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 onlineTableDefaultProvisionTimeout = 45 * time.Minute
func waitForOnlineTableCreation(w *databricks.WorkspaceClient, ctx context.Context, onlineTableName string) error {
return retry.RetryContext(ctx, onlineTableDefaultProvisionTimeout, func() *retry.RetryError {
endpoint, err := w.OnlineTables.GetByName(ctx, onlineTableName)
if err != nil {
return retry.NonRetryableError(err)
}
if endpoint.Status == nil {
return retry.RetryableError(fmt.Errorf("online table status is not available yet"))
}
switch endpoint.Status.DetailedState {
case catalog.OnlineTableStateOnline, catalog.OnlineTableStateOnlineContinuousUpdate,
catalog.OnlineTableStateOnlineNoPendingUpdate, catalog.OnlineTableStateOnlineTriggeredUpdate:
return nil
// does catalog.OnlineTableStateOffline means that it's failed?
case catalog.OnlineTableStateOfflineFailed, catalog.OnlineTableStateOnlinePipelineFailed:
return retry.NonRetryableError(fmt.Errorf("online table status returned %s for online table: %s",
endpoint.Status.DetailedState.String(), onlineTableName))
}
return retry.RetryableError(fmt.Errorf("online table %s is still pending", onlineTableName))
})
}
func waitForOnlineTableDeletion(w *databricks.WorkspaceClient, ctx context.Context, onlineTableName string) error {
return retry.RetryContext(ctx, onlineTableDefaultProvisionTimeout, func() *retry.RetryError {
_, err := w.OnlineTables.GetByName(ctx, onlineTableName)
if err == nil {
return retry.RetryableError(fmt.Errorf("online table %s is still not deleted", onlineTableName))
}
if errors.Is(err, apierr.ErrResourceDoesNotExist) || errors.Is(err, apierr.ErrNotFound) {
return nil
}
return retry.NonRetryableError(fmt.Errorf("online table status returned %w", err))
})
}
func ResourceOnlineTable() common.Resource {
s := common.StructToSchema(catalog.OnlineTable{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
m["name"].DiffSuppressFunc = common.EqualFoldDiffSuppress
common.CustomizeSchemaPath(m, "spec", "source_table_full_name").SetCustomSuppressDiff(common.EqualFoldDiffSuppress)
common.CustomizeSchemaPath(m, "name").SetRequired().SetForceNew()
common.CustomizeSchemaPath(m, "status").SetReadOnly()
common.CustomizeSchemaPath(m, "spec", "pipeline_id").SetReadOnly()
runTypes := []string{"spec.0.run_triggered", "spec.0.run_continuously"}
common.CustomizeSchemaPath(m, "spec", "run_triggered").SetAtLeastOneOf(runTypes).SetSuppressDiff()
common.CustomizeSchemaPath(m, "spec", "run_continuously").SetAtLeastOneOf(runTypes).SetSuppressDiff()
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 req catalog.CreateOnlineTableRequest
common.DataToStructPointer(d, s, &req)
res, err := w.OnlineTables.Create(ctx, req)
if err != nil {
return err
}
// this should be specified in the API Spec - filed a ticket to add it
err = waitForOnlineTableCreation(w, ctx, res.Name)
if err != nil {
return err
}
d.SetId(res.Name)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
table, err := w.OnlineTables.GetByName(ctx, d.Id())
if err != nil {
return err
}
return common.StructToData(*table, s, d)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = w.OnlineTables.DeleteByName(ctx, d.Id())
if err != nil {
return err
}
return waitForOnlineTableDeletion(w, ctx, d.Id())
},
StateUpgraders: []schema.StateUpgrader{},
Schema: s,
SchemaVersion: 0,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(onlineTableDefaultProvisionTimeout),
},
}
}