-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathexternal_location.go
More file actions
82 lines (64 loc) · 2.97 KB
/
external_location.go
File metadata and controls
82 lines (64 loc) · 2.97 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
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 ResourceExternalLocation struct {
client *databricks.WorkspaceClient
}
func (*ResourceExternalLocation) New(client *databricks.WorkspaceClient) *ResourceExternalLocation {
return &ResourceExternalLocation{client: client}
}
func (*ResourceExternalLocation) PrepareState(input *resources.ExternalLocation) *catalog.CreateExternalLocation {
return &input.CreateExternalLocation
}
// externalLocationRemapCopy maps ExternalLocationInfo (remote GET response) to CreateExternalLocation (local state).
var externalLocationRemapCopy = newCopy[catalog.ExternalLocationInfo, catalog.CreateExternalLocation]()
func (*ResourceExternalLocation) RemapState(info *catalog.ExternalLocationInfo) *catalog.CreateExternalLocation {
return externalLocationRemapCopy.Do(info)
}
func (r *ResourceExternalLocation) DoRead(ctx context.Context, id string) (*catalog.ExternalLocationInfo, error) {
return r.client.ExternalLocations.GetByName(ctx, id)
}
func (r *ResourceExternalLocation) DoCreate(ctx context.Context, config *catalog.CreateExternalLocation) (string, *catalog.ExternalLocationInfo, error) {
response, err := r.client.ExternalLocations.Create(ctx, *config)
if err != nil || response == nil {
return "", nil, err
}
return response.Name, response, nil
}
// externalLocationUpdateCopy maps CreateExternalLocation (local state) to UpdateExternalLocation (API request).
var externalLocationUpdateCopy = newCopy[catalog.CreateExternalLocation, catalog.UpdateExternalLocation]()
// DoUpdate updates the external location in place and returns remote state.
func (r *ResourceExternalLocation) DoUpdate(ctx context.Context, id string, config *catalog.CreateExternalLocation, _ Changes) (*catalog.ExternalLocationInfo, error) {
updateRequest := externalLocationUpdateCopy.Do(config)
updateRequest.Name = id
return r.client.ExternalLocations.Update(ctx, *updateRequest)
}
// DoUpdateWithID updates the external location and returns the new ID if the name changes.
func (r *ResourceExternalLocation) DoUpdateWithID(ctx context.Context, id string, config *catalog.CreateExternalLocation) (string, *catalog.ExternalLocationInfo, error) {
updateRequest := externalLocationUpdateCopy.Do(config)
updateRequest.Name = id
if config.Name != id {
updateRequest.NewName = config.Name
}
response, err := r.client.ExternalLocations.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 *ResourceExternalLocation) DoDelete(ctx context.Context, id string) error {
return r.client.ExternalLocations.Delete(ctx, catalog.DeleteExternalLocationRequest{
Name: id,
Force: true,
ForceSendFields: nil,
})
}