-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathcatalog.go
More file actions
87 lines (68 loc) · 2.64 KB
/
catalog.go
File metadata and controls
87 lines (68 loc) · 2.64 KB
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
package dresources
import (
"context"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/catalog"
)
type ResourceCatalog struct {
client *databricks.WorkspaceClient
}
func (*ResourceCatalog) New(client *databricks.WorkspaceClient) *ResourceCatalog {
return &ResourceCatalog{client: client}
}
func (*ResourceCatalog) PrepareState(input *resources.Catalog) *catalog.CreateCatalog {
return &input.CreateCatalog
}
// catalogRemapCopy maps CatalogInfo (remote GET response) to CreateCatalog (local state).
var catalogRemapCopy = newCopy[catalog.CatalogInfo, catalog.CreateCatalog]()
func (*ResourceCatalog) RemapState(info *catalog.CatalogInfo) *catalog.CreateCatalog {
return catalogRemapCopy.Do(info)
}
func (r *ResourceCatalog) DoRead(ctx context.Context, id string) (*catalog.CatalogInfo, error) {
return r.client.Catalogs.GetByName(ctx, id)
}
func (r *ResourceCatalog) DoCreate(ctx context.Context, config *catalog.CreateCatalog) (string, *catalog.CatalogInfo, error) {
response, err := r.client.Catalogs.Create(ctx, *config)
if err != nil || response == nil {
return "", nil, err
}
return response.Name, response, nil
}
// catalogUpdateCopy maps CreateCatalog (local state) to UpdateCatalog (API request).
var catalogUpdateCopy = newCopy[catalog.CreateCatalog, catalog.UpdateCatalog]()
// DoUpdate updates the catalog in place and returns remote state.
func (r *ResourceCatalog) DoUpdate(ctx context.Context, id string, config *catalog.CreateCatalog, _ Changes) (*catalog.CatalogInfo, error) {
updateRequest := catalogUpdateCopy.Do(config)
updateRequest.Name = id
response, err := r.client.Catalogs.Update(ctx, *updateRequest)
if err != nil {
return nil, err
}
return response, nil
}
// DoUpdateWithID updates the catalog and returns the new ID if the name changes.
func (r *ResourceCatalog) DoUpdateWithID(ctx context.Context, id string, config *catalog.CreateCatalog) (string, *catalog.CatalogInfo, error) {
updateRequest := catalogUpdateCopy.Do(config)
updateRequest.Name = id
if config.Name != id {
updateRequest.NewName = config.Name
}
response, err := r.client.Catalogs.Update(ctx, *updateRequest)
if err != nil {
return "", nil, err
}
// Return the new name as the ID if it changed, otherwise return the old ID
newID := id
if updateRequest.NewName != "" {
newID = updateRequest.NewName
}
return newID, response, nil
}
func (r *ResourceCatalog) DoDelete(ctx context.Context, id string) error {
return r.client.Catalogs.Delete(ctx, catalog.DeleteCatalogRequest{
Name: id,
Force: true,
ForceSendFields: nil,
})
}