Skip to content

Commit c87224b

Browse files
authored
Remove IResource.GetType() method (#3120)
## Why There is no need for this to be a method, since type is static. Better to track it centrally, since it defines schema in resources.json.
1 parent 388f37d commit c87224b

File tree

8 files changed

+33
-43
lines changed

8 files changed

+33
-43
lines changed

bundle/terranova/apply.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (d *Deployer) destroy(ctx context.Context, inputConfig any) error {
120120
return nil
121121
}
122122

123-
resource, err := tnresources.New(d.client, d.section, d.resourceName, inputConfig)
123+
resource, _, err := tnresources.New(d.client, d.section, d.resourceName, inputConfig)
124124
if err != nil {
125125
return err
126126
}
@@ -140,7 +140,7 @@ func (d *Deployer) destroy(ctx context.Context, inputConfig any) error {
140140
func (d *Deployer) deploy(ctx context.Context, inputConfig any, actionType deployplan.ActionType) error {
141141
entry, hasEntry := d.db.GetResourceEntry(d.section, d.resourceName)
142142

143-
resource, err := tnresources.New(d.client, d.section, d.resourceName, inputConfig)
143+
resource, cfgType, err := tnresources.New(d.client, d.section, d.resourceName, inputConfig)
144144
if err != nil {
145145
return err
146146
}
@@ -156,7 +156,7 @@ func (d *Deployer) deploy(ctx context.Context, inputConfig any, actionType deplo
156156
return errors.New("invalid state: empty id")
157157
}
158158

159-
savedState, err := typeConvert(resource.GetType(), entry.State)
159+
savedState, err := typeConvert(cfgType, entry.State)
160160
if err != nil {
161161
return fmt.Errorf("interpreting state: %w", err)
162162
}
@@ -218,7 +218,7 @@ func (d *Deployer) Recreate(ctx context.Context, oldResource tnresources.IResour
218218
return fmt.Errorf("deleting state: %w", err)
219219
}
220220

221-
newResource, err := tnresources.New(d.client, d.section, d.resourceName, config)
221+
newResource, _, err := tnresources.New(d.client, d.section, d.resourceName, config)
222222
if err != nil {
223223
return fmt.Errorf("initializing: %w", err)
224224
}

bundle/terranova/plan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Planner struct {
2525
func (d *Planner) Plan(ctx context.Context, inputConfig any) (deployplan.ActionType, error) {
2626
entry, hasEntry := d.db.GetResourceEntry(d.section, d.resourceName)
2727

28-
resource, err := tnresources.New(d.client, d.section, d.resourceName, inputConfig)
28+
resource, cfgType, err := tnresources.New(d.client, d.section, d.resourceName, inputConfig)
2929
if err != nil {
3030
return "", err
3131
}
@@ -41,7 +41,7 @@ func (d *Planner) Plan(ctx context.Context, inputConfig any) (deployplan.ActionT
4141
return "", errors.New("invalid state: empty id")
4242
}
4343

44-
savedState, err := typeConvert(resource.GetType(), entry.State)
44+
savedState, err := typeConvert(cfgType, entry.State)
4545
if err != nil {
4646
return "", fmt.Errorf("interpreting state: %w", err)
4747
}

bundle/terranova/tnresources/app.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tnresources
22

33
import (
44
"context"
5-
"reflect"
65

76
"github.com/databricks/cli/bundle/config/resources"
87
"github.com/databricks/cli/bundle/deployplan"
@@ -74,9 +73,3 @@ func (r *ResourceApp) ClassifyChanges(changes []structdiff.Change) deployplan.Ac
7473
// TODO: changing name is recreation
7574
return deployplan.ActionTypeUpdate
7675
}
77-
78-
var appType = reflect.TypeOf(ResourceApp{}.config)
79-
80-
func (r *ResourceApp) GetType() reflect.Type {
81-
return appType
82-
}

bundle/terranova/tnresources/job.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tnresources
22

33
import (
44
"context"
5-
"reflect"
65
"strconv"
76

87
"github.com/databricks/cli/bundle/config/resources"
@@ -87,12 +86,6 @@ func makeCreateJob(config jobs.JobSettings) (jobs.CreateJob, error) {
8786
return result, err
8887
}
8988

90-
var jobSettingsType = reflect.TypeOf(jobs.JobSettings{})
91-
92-
func (r *ResourceJob) GetType() reflect.Type {
93-
return jobSettingsType
94-
}
95-
9689
func makeResetJob(config jobs.JobSettings, id string) (jobs.ResetJob, error) {
9790
idInt, err := strconv.ParseInt(id, 10, 64)
9891
if err != nil {

bundle/terranova/tnresources/pipeline.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tnresources
22

33
import (
44
"context"
5-
"reflect"
65

76
"github.com/databricks/cli/bundle/config/resources"
87
"github.com/databricks/cli/bundle/deployplan"
@@ -74,9 +73,3 @@ func (r *ResourcePipeline) WaitAfterUpdate(ctx context.Context) error {
7473
func (r *ResourcePipeline) ClassifyChanges(changes []structdiff.Change) deployplan.ActionType {
7574
return deployplan.ActionTypeUpdate
7675
}
77-
78-
var pipelineType = reflect.TypeOf(ResourcePipeline{}.config)
79-
80-
func (r *ResourcePipeline) GetType() reflect.Type {
81-
return pipelineType
82-
}

bundle/terranova/tnresources/resource.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ var supportedResources = map[string]reflect.Value{
1818
"apps": reflect.ValueOf(NewResourceApp),
1919
}
2020

21+
// This types matches what Config() returns and should match 'config' field in the resource struct
22+
var supportedResourcesTypes = map[string]reflect.Type{
23+
"jobs": reflect.TypeOf(ResourceJob{}.config),
24+
"pipelines": reflect.TypeOf(ResourcePipeline{}.config),
25+
"schemas": reflect.TypeOf(ResourceSchema{}.config),
26+
"apps": reflect.TypeOf(ResourceApp{}.config),
27+
}
28+
2129
type IResource interface {
2230
Config() any
2331

@@ -33,9 +41,6 @@ type IResource interface {
3341
WaitAfterCreate(ctx context.Context) error
3442
WaitAfterUpdate(ctx context.Context) error
3543

36-
// Get type of the struct that stores the state
37-
GetType() reflect.Type
38-
3944
ClassifyChanges(changes []structdiff.Change) deployplan.ActionType
4045
}
4146

@@ -78,15 +83,20 @@ func invokeConstructor(ctor reflect.Value, client *databricks.WorkspaceClient, c
7883
return res, nil
7984
}
8085

81-
func New(client *databricks.WorkspaceClient, section, name string, config any) (IResource, error) {
86+
func New(client *databricks.WorkspaceClient, section, name string, config any) (IResource, reflect.Type, error) {
8287
ctor, ok := supportedResources[section]
8388
if !ok {
84-
return nil, fmt.Errorf("unsupported resource type: %s", section)
89+
return nil, nil, fmt.Errorf("unsupported resource type: %s", section)
90+
}
91+
92+
cfgType, ok := supportedResourcesTypes[section]
93+
if !ok {
94+
return nil, nil, fmt.Errorf("unsupported resource type: %s", section)
8595
}
8696

8797
// Disallow nil configs (including typed nil pointers).
8898
if config == nil {
89-
return nil, fmt.Errorf("unexpected nil in config: %s.%s", section, name)
99+
return nil, nil, fmt.Errorf("unexpected nil in config: %s.%s", section, name)
90100
}
91101

92102
// If the supplied config is a pointer value, dereference it so that we pass
@@ -95,9 +105,14 @@ func New(client *databricks.WorkspaceClient, section, name string, config any) (
95105
v := reflect.ValueOf(config)
96106
if v.Kind() == reflect.Ptr {
97107
if v.IsNil() {
98-
return nil, fmt.Errorf("unexpected nil in config: %s.%s", section, name)
108+
return nil, nil, fmt.Errorf("unexpected nil in config: %s.%s", section, name)
99109
}
100110
}
101111

102-
return invokeConstructor(ctor, client, config)
112+
result, err := invokeConstructor(ctor, client, config)
113+
if err != nil {
114+
return nil, nil, err
115+
}
116+
117+
return result, cfgType, nil
103118
}

bundle/terranova/tnresources/resource_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tnresources
22

33
import (
4+
"reflect"
45
"testing"
56

67
"github.com/databricks/cli/bundle/config/resources"
@@ -18,12 +19,14 @@ func TestNewJobResource(t *testing.T) {
1819
},
1920
}
2021

21-
res, err := New(client, "jobs", "test-job", cfg)
22+
res, cfgType, err := New(client, "jobs", "test-job", cfg)
2223
require.NoError(t, err)
2324
require.NotNil(t, res)
2425

2526
// Ensure we received the correct resource type.
2627
require.IsType(t, &ResourceJob{}, res)
28+
require.IsType(t, reflect.TypeOf(ResourceJob{}.config), cfgType)
29+
require.IsType(t, reflect.TypeOf(jobs.JobSettings{}), cfgType)
2730

2831
// The underlying config should match what we passed in.
2932
r := res.(*ResourceJob)

bundle/terranova/tnresources/schema.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tnresources
22

33
import (
44
"context"
5-
"reflect"
65

76
"github.com/databricks/cli/bundle/config/resources"
87
"github.com/databricks/cli/bundle/deployplan"
@@ -70,9 +69,3 @@ func (r *ResourceSchema) WaitAfterUpdate(ctx context.Context) error {
7069
func (r *ResourceSchema) ClassifyChanges(changes []structdiff.Change) deployplan.ActionType {
7170
return deployplan.ActionTypeUpdate
7271
}
73-
74-
var schemaType = reflect.TypeOf(ResourceSchema{}.config)
75-
76-
func (r *ResourceSchema) GetType() reflect.Type {
77-
return schemaType
78-
}

0 commit comments

Comments
 (0)