Skip to content

Commit 69ce9b0

Browse files
authored
internal: move resource.go from tnresources to terranova (#3347)
## Changes Move resource.go from bundle/terranova/tnresources to bundle/terranova. No functional changes. ## Why - resource.go is more framework-related while the rest of tnresources is focussed on implementing specific resources. - I want to apply more linters on tnresources (specifically exhauststruct to ensure structs are copied fully).
1 parent 4556111 commit 69ce9b0

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

bundle/terranova/apply.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/databricks/cli/bundle"
1212
"github.com/databricks/cli/bundle/deployplan"
13-
"github.com/databricks/cli/bundle/terranova/tnresources"
1413
"github.com/databricks/cli/bundle/terranova/tnstate"
1514
"github.com/databricks/cli/libs/dagrun"
1615
"github.com/databricks/cli/libs/diag"
@@ -52,7 +51,7 @@ func (m *terranovaApplyMutator) Apply(ctx context.Context, b *bundle.Bundle) dia
5251
// TODO: if a given node fails, all downstream nodes should not be run. We should report those nodes.
5352
// TODO: ensure that config for this node is fully resolved at this point.
5453

55-
settings, ok := tnresources.SupportedResources[node.Group]
54+
settings, ok := SupportedResources[node.Group]
5655
if !ok {
5756
return
5857
}
@@ -105,7 +104,7 @@ type Deployer struct {
105104
db *tnstate.TerranovaState
106105
group string
107106
resourceName string
108-
settings tnresources.ResourceSettings
107+
settings ResourceSettings
109108
}
110109

111110
func (d *Deployer) Deploy(ctx context.Context, inputConfig any, actionType deployplan.ActionType) error {
@@ -146,7 +145,7 @@ func (d *Deployer) destroy(ctx context.Context, inputConfig any) error {
146145
func (d *Deployer) deploy(ctx context.Context, inputConfig any, actionType deployplan.ActionType) error {
147146
entry, hasEntry := d.db.GetResourceEntry(d.group, d.resourceName)
148147

149-
resource, cfgType, err := tnresources.New(d.client, d.group, d.resourceName, inputConfig)
148+
resource, cfgType, err := New(d.client, d.group, d.resourceName, inputConfig)
150149
if err != nil {
151150
return err
152151
}
@@ -186,7 +185,7 @@ func (d *Deployer) deploy(ctx context.Context, inputConfig any, actionType deplo
186185
return nil
187186
}
188187

189-
func (d *Deployer) Create(ctx context.Context, resource tnresources.IResource, config any) error {
188+
func (d *Deployer) Create(ctx context.Context, resource IResource, config any) error {
190189
newID, err := resource.DoCreate(ctx)
191190
if err != nil {
192191
return fmt.Errorf("creating: %w", err)
@@ -207,8 +206,8 @@ func (d *Deployer) Create(ctx context.Context, resource tnresources.IResource, c
207206
return nil
208207
}
209208

210-
func (d *Deployer) Recreate(ctx context.Context, resource tnresources.IResource, oldID string, config any) error {
211-
err := tnresources.DeleteResource(ctx, d.client, d.group, oldID)
209+
func (d *Deployer) Recreate(ctx context.Context, resource IResource, oldID string, config any) error {
210+
err := DeleteResource(ctx, d.client, d.group, oldID)
212211
if err != nil {
213212
return fmt.Errorf("deleting old id=%s: %w", oldID, err)
214213
}
@@ -239,7 +238,7 @@ func (d *Deployer) Recreate(ctx context.Context, resource tnresources.IResource,
239238
return nil
240239
}
241240

242-
func (d *Deployer) Update(ctx context.Context, resource tnresources.IResource, oldID string, config any) error {
241+
func (d *Deployer) Update(ctx context.Context, resource IResource, oldID string, config any) error {
243242
newID, err := resource.DoUpdate(ctx, oldID)
244243
if err != nil {
245244
return fmt.Errorf("updating id=%s: %w", oldID, err)
@@ -269,7 +268,7 @@ func (d *Deployer) Update(ctx context.Context, resource tnresources.IResource, o
269268

270269
func (d *Deployer) Delete(ctx context.Context, oldID string) error {
271270
// TODO: recognize 404 and 403 as "deleted" and proceed to removing state
272-
err := tnresources.DeleteResource(ctx, d.client, d.group, oldID)
271+
err := DeleteResource(ctx, d.client, d.group, oldID)
273272
if err != nil {
274273
return fmt.Errorf("deleting id=%s: %w", oldID, err)
275274
}
@@ -298,7 +297,7 @@ func typeConvert(destType reflect.Type, src any) (any, error) {
298297
return reflect.ValueOf(destPtr).Elem().Interface(), nil
299298
}
300299

301-
func calcDiff(settings tnresources.ResourceSettings, resource tnresources.IResource, savedState, config any) (deployplan.ActionType, error) {
300+
func calcDiff(settings ResourceSettings, resource IResource, savedState, config any) (deployplan.ActionType, error) {
302301
localDiff, err := structdiff.GetStructDiff(savedState, config)
303302
if err != nil {
304303
return "", err

bundle/terranova/plan.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/databricks/cli/bundle"
99
"github.com/databricks/cli/bundle/deployplan"
10-
"github.com/databricks/cli/bundle/terranova/tnresources"
1110
"github.com/databricks/cli/bundle/terranova/tnstate"
1211
"github.com/databricks/cli/libs/dyn"
1312
"github.com/databricks/cli/libs/utils"
@@ -19,13 +18,13 @@ type Planner struct {
1918
db *tnstate.TerranovaState
2019
group string
2120
resourceName string
22-
settings tnresources.ResourceSettings
21+
settings ResourceSettings
2322
}
2423

2524
func (d *Planner) Plan(ctx context.Context, inputConfig any) (deployplan.ActionType, error) {
2625
entry, hasEntry := d.db.GetResourceEntry(d.group, d.resourceName)
2726

28-
resource, cfgType, err := tnresources.New(d.client, d.group, d.resourceName, inputConfig)
27+
resource, cfgType, err := New(d.client, d.group, d.resourceName, inputConfig)
2928
if err != nil {
3029
return "", err
3130
}
@@ -75,7 +74,7 @@ func CalculateDeployActions(ctx context.Context, b *bundle.Bundle) ([]deployplan
7574
group := p[1].Key()
7675
name := p[2].Key()
7776

78-
settings, ok := tnresources.SupportedResources[group]
77+
settings, ok := SupportedResources[group]
7978
if !ok {
8079
return v, nil
8180
}
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tnresources
1+
package terranova
22

33
import (
44
"context"
@@ -7,6 +7,7 @@ import (
77
"reflect"
88

99
"github.com/databricks/cli/bundle/deployplan"
10+
"github.com/databricks/cli/bundle/terranova/tnresources"
1011
"github.com/databricks/cli/libs/structdiff"
1112
"github.com/databricks/databricks-sdk-go"
1213
)
@@ -57,16 +58,21 @@ func (s *ResourceSettings) MustRecreate(changes []structdiff.Change) bool {
5758
return false
5859
}
5960

61+
// TypeOfConfig returns the reflect.Type of the configuration returned by the resource's Config() method.
62+
func TypeOfConfig(resource IResource) reflect.Type {
63+
return reflect.TypeOf(resource.Config())
64+
}
65+
6066
var SupportedResources = map[string]ResourceSettings{
6167
"jobs": {
62-
New: reflect.ValueOf(NewResourceJob),
63-
ConfigType: reflect.TypeOf(ResourceJob{}.config),
64-
DeleteFN: DeleteJob,
68+
New: reflect.ValueOf(tnresources.NewResourceJob),
69+
ConfigType: TypeOfConfig(&tnresources.ResourceJob{}),
70+
DeleteFN: tnresources.DeleteJob,
6571
},
6672
"pipelines": {
67-
New: reflect.ValueOf(NewResourcePipeline),
68-
ConfigType: reflect.TypeOf(ResourcePipeline{}.config),
69-
DeleteFN: DeletePipeline,
73+
New: reflect.ValueOf(tnresources.NewResourcePipeline),
74+
ConfigType: TypeOfConfig(&tnresources.ResourcePipeline{}),
75+
DeleteFN: tnresources.DeletePipeline,
7076
// See TF's ForceNew fields:
7177
// https://github.com/databricks/terraform-provider-databricks/blob/8ae24ac/pipelines/resource_pipeline.go#L207
7278
RecreateFields: mkMap(
@@ -77,9 +83,9 @@ var SupportedResources = map[string]ResourceSettings{
7783
),
7884
},
7985
"schemas": {
80-
New: reflect.ValueOf(NewResourceSchema),
81-
ConfigType: reflect.TypeOf(ResourceSchema{}.config),
82-
DeleteFN: DeleteSchema,
86+
New: reflect.ValueOf(tnresources.NewResourceSchema),
87+
ConfigType: TypeOfConfig(&tnresources.ResourceSchema{}),
88+
DeleteFN: tnresources.DeleteSchema,
8389
// TF: https://github.com/databricks/terraform-provider-databricks/blob/03a2515/catalog/resource_schema.go#L14
8490
RecreateFields: mkMap(
8591
".name",
@@ -88,9 +94,9 @@ var SupportedResources = map[string]ResourceSettings{
8894
),
8995
},
9096
"volumes": {
91-
New: reflect.ValueOf(NewResourceVolume),
92-
ConfigType: reflect.TypeOf(ResourceVolume{}.config),
93-
DeleteFN: DeleteVolume,
97+
New: reflect.ValueOf(tnresources.NewResourceVolume),
98+
ConfigType: TypeOfConfig(&tnresources.ResourceVolume{}),
99+
DeleteFN: tnresources.DeleteVolume,
94100
// TF: https://github.com/databricks/terraform-provider-databricks/blob/f5fce0f/catalog/resource_volume.go#L19
95101
RecreateFields: mkMap(
96102
".catalog_name",
@@ -100,14 +106,14 @@ var SupportedResources = map[string]ResourceSettings{
100106
),
101107
},
102108
"apps": {
103-
New: reflect.ValueOf(NewResourceApp),
104-
ConfigType: reflect.TypeOf(ResourceApp{}.config),
105-
DeleteFN: DeleteApp,
109+
New: reflect.ValueOf(tnresources.NewResourceApp),
110+
ConfigType: TypeOfConfig(&tnresources.ResourceApp{}),
111+
DeleteFN: tnresources.DeleteApp,
106112
},
107113
"sql_warehouses": {
108-
New: reflect.ValueOf(NewResourceSqlWarehouse),
109-
ConfigType: reflect.TypeOf(ResourceSqlWarehouse{}.config),
110-
DeleteFN: DeleteSqlWarehouse,
114+
New: reflect.ValueOf(tnresources.NewResourceSqlWarehouse),
115+
ConfigType: TypeOfConfig(&tnresources.ResourceSqlWarehouse{}),
116+
DeleteFN: tnresources.DeleteSqlWarehouse,
111117
},
112118
}
113119

bundle/terranova/tnresources/resource_test.go renamed to bundle/terranova/resource_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package tnresources
1+
package terranova
22

33
import (
44
"reflect"
55
"testing"
66

77
"github.com/databricks/cli/bundle/config/resources"
8+
"github.com/databricks/cli/bundle/terranova/tnresources"
89
"github.com/databricks/cli/libs/structdiff/structpath"
910
"github.com/databricks/cli/libs/structwalk"
1011
"github.com/databricks/databricks-sdk-go"
@@ -26,13 +27,12 @@ func TestNewJobResource(t *testing.T) {
2627
require.NotNil(t, res)
2728

2829
// Ensure we received the correct resource type.
29-
require.IsType(t, &ResourceJob{}, res)
30-
require.IsType(t, reflect.TypeOf(ResourceJob{}.config), cfgType)
30+
require.IsType(t, &tnresources.ResourceJob{}, res)
3131
require.IsType(t, reflect.TypeOf(jobs.JobSettings{}), cfgType)
3232

3333
// The underlying config should match what we passed in.
34-
r := res.(*ResourceJob)
35-
require.Equal(t, cfg.JobSettings, r.config)
34+
r := res.(*tnresources.ResourceJob)
35+
require.Equal(t, cfg.JobSettings, r.Config())
3636
}
3737

3838
// validateFields uses structwalk to generate all valid field paths and checks membership.

0 commit comments

Comments
 (0)