-
Notifications
You must be signed in to change notification settings - Fork 389
/
resource_catalog.go
240 lines (218 loc) · 7.88 KB
/
resource_catalog.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
package catalog
import (
"context"
"fmt"
"log"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ucDirectoryPathSlashOnlySuppressDiff(k, old, new string, d *schema.ResourceData) bool {
if (new == (old + "/")) || (old == (new + "/")) {
log.Printf("[DEBUG] Ignoring configuration drift from %s to %s", old, new)
return true
}
return false
}
func ucDirectoryPathSlashAndEmptySuppressDiff(k, old, new string, d *schema.ResourceData) bool {
if (new == (old + "/")) || (old == (new + "/")) || (new == "" && old != "") {
log.Printf("[DEBUG] Ignoring configuration drift from %s to %s", old, new)
return true
}
return false
}
type CatalogInfo struct {
Name string `json:"name"`
Comment string `json:"comment,omitempty"`
StorageRoot string `json:"storage_root,omitempty" tf:"force_new"`
ProviderName string `json:"provider_name,omitempty" tf:"force_new,conflicts:storage_root"`
ShareName string `json:"share_name,omitempty" tf:"force_new,conflicts:storage_root"`
ConnectionName string `json:"connection_name,omitempty" tf:"force_new,conflicts:storage_root"`
EnablePredictiveOptimization string `json:"enable_predictive_optimization,omitempty" tf:"computed"`
Options map[string]string `json:"options,omitempty" tf:"force_new"`
Properties map[string]string `json:"properties,omitempty"`
Owner string `json:"owner,omitempty" tf:"computed"`
IsolationMode string `json:"isolation_mode,omitempty" tf:"computed"`
MetastoreID string `json:"metastore_id,omitempty" tf:"computed"`
}
func ResourceCatalog() common.Resource {
catalogSchema := common.StructToSchema(CatalogInfo{},
func(s map[string]*schema.Schema) map[string]*schema.Schema {
s["force_destroy"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
}
common.CustomizeSchemaPath(s, "storage_root").SetCustomSuppressDiff(ucDirectoryPathSlashOnlySuppressDiff)
common.CustomizeSchemaPath(s, "name").SetCustomSuppressDiff(common.EqualFoldDiffSuppress)
common.CustomizeSchemaPath(s, "enable_predictive_optimization").SetValidateFunc(
validation.StringInSlice([]string{"DISABLE", "ENABLE", "INHERIT"}, false),
)
return s
})
return common.Resource{
Schema: catalogSchema,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore_id").(string))
if err != nil {
return err
}
var createCatalogRequest catalog.CreateCatalog
common.DataToStructPointer(d, catalogSchema, &createCatalogRequest)
ci, err := w.Catalogs.Create(ctx, createCatalogRequest)
if err != nil {
return err
}
// only remove catalog default schema for standard catalog (e.g. non-Delta Sharing, non-foreign)
if ci.ShareName == "" && ci.ConnectionName == "" {
if err := w.Schemas.DeleteByFullName(ctx, ci.Name+".default"); err != nil {
return fmt.Errorf("cannot remove new catalog default schema: %w", err)
}
}
d.SetId(ci.Name)
// Update owner, isolation mode or predictive optimization if it is provided
updateRequired := false
for _, key := range []string{"owner", "isolation_mode", "enable_predictive_optimization"} {
if d.Get(key) != "" {
updateRequired = true
break
}
}
if !updateRequired {
return nil
}
var updateCatalogRequest catalog.UpdateCatalog
common.DataToStructPointer(d, catalogSchema, &updateCatalogRequest)
updateCatalogRequest.Name = d.Id()
_, err = w.Catalogs.Update(ctx, updateCatalogRequest)
if err != nil {
return err
}
if d.Get("isolation_mode") != "ISOLATED" {
return nil
}
// Bind the current workspace if the catalog is isolated, otherwise the read will fail
currentMetastoreAssignment, err := w.Metastores.Current(ctx)
if err != nil {
return err
}
_, err = w.WorkspaceBindings.UpdateBindings(ctx, catalog.UpdateWorkspaceBindingsParameters{
SecurableName: ci.Name,
SecurableType: "catalog",
Add: []catalog.WorkspaceBinding{
{
BindingType: catalog.WorkspaceBindingBindingTypeBindingTypeReadWrite,
WorkspaceId: currentMetastoreAssignment.WorkspaceId,
},
},
})
return err
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
ci, err := w.Catalogs.GetByName(ctx, d.Id())
if err != nil {
return err
}
return common.StructToData(ci, catalogSchema, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore_id").(string))
if err != nil {
return err
}
var updateCatalogRequest catalog.UpdateCatalog
common.DataToStructPointer(d, catalogSchema, &updateCatalogRequest)
updateCatalogRequest.Name = d.Id()
if d.HasChange("owner") {
_, err = w.Catalogs.Update(ctx, catalog.UpdateCatalog{
Name: updateCatalogRequest.Name,
Owner: updateCatalogRequest.Owner,
})
if err != nil {
return err
}
}
if !d.HasChangeExcept("owner") {
return nil
}
updateCatalogRequest.Owner = ""
ci, err := w.Catalogs.Update(ctx, updateCatalogRequest)
if err != nil {
if d.HasChange("owner") {
// Rollback
old, new := d.GetChange("owner")
_, rollbackErr := w.Catalogs.Update(ctx, catalog.UpdateCatalog{
Name: updateCatalogRequest.Name,
Owner: old.(string),
})
if rollbackErr != nil {
return common.OwnerRollbackError(err, rollbackErr, old.(string), new.(string))
}
}
return err
}
// We need to update the resource data because Name is updatable
// So if we don't update the field then the requests would be made to old Name which doesn't exists.
d.SetId(ci.Name)
if d.Get("isolation_mode") != "ISOLATED" {
return nil
}
// Bind the current workspace if the catalog is isolated, otherwise the read will fail
currentMetastoreAssignment, err := w.Metastores.Current(ctx)
if err != nil {
return err
}
_, err = w.WorkspaceBindings.UpdateBindings(ctx, catalog.UpdateWorkspaceBindingsParameters{
SecurableName: ci.Name,
SecurableType: "catalog",
Add: []catalog.WorkspaceBinding{
{
BindingType: catalog.WorkspaceBindingBindingTypeBindingTypeReadWrite,
WorkspaceId: currentMetastoreAssignment.WorkspaceId,
},
},
})
return err
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore_id").(string))
if err != nil {
return err
}
force := d.Get("force_destroy").(bool)
// If the workspace has isolation mode ISOLATED, we need to add the current workspace to its
// bindings before deleting.
if d.Get("isolation_mode").(string) == "ISOLATED" {
currentMetastoreAssignment, err := w.Metastores.Current(ctx)
if err != nil {
return err
}
_, err = w.WorkspaceBindings.Update(ctx, catalog.UpdateWorkspaceBindings{
Name: d.Id(),
AssignWorkspaces: []int64{currentMetastoreAssignment.WorkspaceId},
})
if err != nil {
return err
}
}
return w.Catalogs.Delete(ctx, catalog.DeleteCatalogRequest{Force: force, Name: d.Id()})
},
}
}