Skip to content

Commit 5c7d8ad

Browse files
authored
direct: refactor: Group resources settings in a single struct (#3334)
## Why The number of settings have grown and will continue to grow, this give better overview.
1 parent 83797ec commit 5c7d8ad

File tree

2 files changed

+55
-52
lines changed

2 files changed

+55
-52
lines changed

bundle/terranova/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func (d *Deployer) Update(ctx context.Context, resource tnresources.IResource, o
258258
return fmt.Errorf("saving state id=%s: %w", oldID, err)
259259
}
260260

261-
if oldID != newID && !tnresources.UpdateableIDResource[d.group] {
261+
if oldID != newID && !tnresources.SupportedResources[d.group].UpdateUpdatesID {
262262
return fmt.Errorf("internal error, unexpected change of ID from %#v to %#v", oldID, newID)
263263
}
264264

bundle/terranova/tnresources/resource.go

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,60 @@ import (
1111
"github.com/databricks/databricks-sdk-go"
1212
)
1313

14-
const (
15-
_jobs = "jobs"
16-
_pipelines = "pipelines"
17-
_schemas = "schemas"
18-
_volumes = "volumes"
19-
_apps = "apps"
20-
_sql_warehouses = "sql_warehouses"
21-
)
14+
type DeleteResourceFN = func(ctx context.Context, client *databricks.WorkspaceClient, oldID string) error
2215

23-
var supportedResources = map[string]reflect.Value{
24-
_jobs: reflect.ValueOf(NewResourceJob),
25-
_pipelines: reflect.ValueOf(NewResourcePipeline),
26-
_schemas: reflect.ValueOf(NewResourceSchema),
27-
_volumes: reflect.ValueOf(NewResourceVolume),
28-
_apps: reflect.ValueOf(NewResourceApp),
29-
_sql_warehouses: reflect.ValueOf(NewResourceSqlWarehouse),
30-
}
16+
type ResourceSettings struct {
17+
// Method to call to create new resource
18+
// First argument must be client* databricks.Workspace and second argument is *resource.<Resource> from bundle config
19+
// where Resource is appropriate resource e.g. resource.Job.
20+
New reflect.Value
3121

32-
// This types matches what Config() returns and should match 'config' field in the resource struct
33-
var supportedResourcesTypes = map[string]reflect.Type{
34-
_jobs: reflect.TypeOf(ResourceJob{}.config),
35-
_pipelines: reflect.TypeOf(ResourcePipeline{}.config),
36-
_schemas: reflect.TypeOf(ResourceSchema{}.config),
37-
_volumes: reflect.TypeOf(ResourceVolume{}.config),
38-
_apps: reflect.TypeOf(ResourceApp{}.config),
39-
_sql_warehouses: reflect.TypeOf(ResourceSqlWarehouse{}.config),
40-
}
22+
// Type of the stored config state
23+
ConfigType reflect.Type
4124

42-
type DeleteResourceFN = func(ctx context.Context, client *databricks.WorkspaceClient, oldID string) error
25+
// Function to delete a resource of this type
26+
DeleteFN DeleteResourceFN
4327

44-
var deletableResources = map[string]DeleteResourceFN{
45-
_jobs: DeleteJob,
46-
_pipelines: DeletePipeline,
47-
_schemas: DeleteSchema,
48-
_volumes: DeleteVolume,
49-
_apps: DeleteApp,
50-
_sql_warehouses: DeleteSqlWarehouse,
28+
// true if Update() method can return a different ID than that was passed in
29+
// If ID changes during Update and UpdateUpdatesID is false, deployment of that resource will fail with internal error.
30+
// This allows to make assumptions about references stability (${resources.jobs.foo.id}) when we see that
31+
// operation is going to be "update" & ID is guarantee not to change.
32+
UpdateUpdatesID bool
5133
}
5234

53-
// UpdateableIDResource configures whether the resource is allowed to change ID in Update operation. Default is false.
54-
// If ID changes during Update and it is not allowed, deployment of that resource will fail with internal error.
55-
// This allows to make assumptions about references stability (${resources.jobs.foo.id}) when we see that
56-
// operation is going to be "update" & ID is guarantee not to change.
57-
var UpdateableIDResource = map[string]bool{
58-
_schemas: true,
59-
_volumes: true,
35+
var SupportedResources = map[string]ResourceSettings{
36+
"jobs": {
37+
New: reflect.ValueOf(NewResourceJob),
38+
ConfigType: reflect.TypeOf(ResourceJob{}.config),
39+
DeleteFN: DeleteJob,
40+
},
41+
"pipelines": {
42+
New: reflect.ValueOf(NewResourcePipeline),
43+
ConfigType: reflect.TypeOf(ResourcePipeline{}.config),
44+
DeleteFN: DeletePipeline,
45+
},
46+
"schemas": {
47+
New: reflect.ValueOf(NewResourceSchema),
48+
ConfigType: reflect.TypeOf(ResourceSchema{}.config),
49+
DeleteFN: DeleteSchema,
50+
UpdateUpdatesID: true,
51+
},
52+
"volumes": {
53+
New: reflect.ValueOf(NewResourceVolume),
54+
ConfigType: reflect.TypeOf(ResourceVolume{}.config),
55+
DeleteFN: DeleteVolume,
56+
UpdateUpdatesID: true,
57+
},
58+
"apps": {
59+
New: reflect.ValueOf(NewResourceApp),
60+
ConfigType: reflect.TypeOf(ResourceApp{}.config),
61+
DeleteFN: DeleteApp,
62+
},
63+
"sql_warehouses": {
64+
New: reflect.ValueOf(NewResourceSqlWarehouse),
65+
ConfigType: reflect.TypeOf(ResourceSqlWarehouse{}.config),
66+
DeleteFN: DeleteSqlWarehouse,
67+
},
6068
}
6169

6270
type IResource interface {
@@ -67,7 +75,7 @@ type IResource interface {
6775

6876
// Update the resource. Returns id of the resource.
6977
// Usually returns the same id as oldId but can also return a different one (e.g. schemas and volumes when certain fields are changed)
70-
// Note, UpdateableIDResource[group] must be true for this group if ID can be changed. Otherwise function must return the same ID.
78+
// Note, SupportedResources[group].UpdateUpdatesID must be true for this group if ID can be changed. Otherwise function must return the same ID.
7179
DoUpdate(ctx context.Context, oldID string) (string, error)
7280

7381
WaitAfterCreate(ctx context.Context) error
@@ -114,12 +122,7 @@ func invokeConstructor(ctor reflect.Value, client *databricks.WorkspaceClient, c
114122
}
115123

116124
func New(client *databricks.WorkspaceClient, group, name string, config any) (IResource, reflect.Type, error) {
117-
ctor, ok := supportedResources[group]
118-
if !ok {
119-
return nil, nil, fmt.Errorf("unsupported resource type: %s", group)
120-
}
121-
122-
cfgType, ok := supportedResourcesTypes[group]
125+
settings, ok := SupportedResources[group]
123126
if !ok {
124127
return nil, nil, fmt.Errorf("unsupported resource type: %s", group)
125128
}
@@ -139,18 +142,18 @@ func New(client *databricks.WorkspaceClient, group, name string, config any) (IR
139142
}
140143
}
141144

142-
result, err := invokeConstructor(ctor, client, config)
145+
result, err := invokeConstructor(settings.New, client, config)
143146
if err != nil {
144147
return nil, nil, err
145148
}
146149

147-
return result, cfgType, nil
150+
return result, settings.ConfigType, nil
148151
}
149152

150153
func DeleteResource(ctx context.Context, client *databricks.WorkspaceClient, group, id string) error {
151-
fn, ok := deletableResources[group]
154+
settings, ok := SupportedResources[group]
152155
if !ok {
153156
return fmt.Errorf("cannot delete %s", group)
154157
}
155-
return fn(ctx, client, id)
158+
return settings.DeleteFN(ctx, client, id)
156159
}

0 commit comments

Comments
 (0)