Skip to content

Commit 4f82e08

Browse files
authored
refactor: Extract common resource fields into a separate embedded struct (#3571)
## Changes Extract common resource fields into a separate embedded struct `BaseResource` ## Why Based on feedback from #3448 (comment) ## Tests Existing tests pass <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. -->
1 parent ec7df41 commit 4f82e08

27 files changed

+85
-126
lines changed

bundle/apps/interpolate_variables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestAppInterpolateVariables(t *testing.T) {
3535
},
3636
Jobs: map[string]*resources.Job{
3737
"my_job": {
38-
ID: "123",
38+
BaseResource: resources.BaseResource{ID: "123"},
3939
},
4040
},
4141
},

bundle/config/mutator/initialize_urls_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,39 @@ func TestInitializeURLs(t *testing.T) {
2525
Resources: config.Resources{
2626
Jobs: map[string]*resources.Job{
2727
"job1": {
28-
ID: "1",
29-
JobSettings: jobs.JobSettings{Name: "job1"},
28+
BaseResource: resources.BaseResource{ID: "1"},
29+
JobSettings: jobs.JobSettings{Name: "job1"},
3030
},
3131
},
3232
Pipelines: map[string]*resources.Pipeline{
3333
"pipeline1": {
34-
ID: "3",
34+
BaseResource: resources.BaseResource{ID: "3"},
3535
CreatePipeline: pipelines.CreatePipeline{Name: "pipeline1"},
3636
},
3737
},
3838
Experiments: map[string]*resources.MlflowExperiment{
3939
"experiment1": {
40-
ID: "4",
41-
Experiment: ml.Experiment{Name: "experiment1"},
40+
BaseResource: resources.BaseResource{ID: "4"},
41+
Experiment: ml.Experiment{Name: "experiment1"},
4242
},
4343
},
4444
Models: map[string]*resources.MlflowModel{
4545
"model1": {
46-
ID: "a model uses its name for identifier",
46+
BaseResource: resources.BaseResource{ID: "a model uses its name for identifier"},
4747
CreateModelRequest: ml.CreateModelRequest{Name: "a model uses its name for identifier"},
4848
},
4949
},
5050
ModelServingEndpoints: map[string]*resources.ModelServingEndpoint{
5151
"servingendpoint1": {
52-
ID: "my_serving_endpoint",
52+
BaseResource: resources.BaseResource{ID: "my_serving_endpoint"},
5353
CreateServingEndpoint: serving.CreateServingEndpoint{
5454
Name: "my_serving_endpoint",
5555
},
5656
},
5757
},
5858
RegisteredModels: map[string]*resources.RegisteredModel{
5959
"registeredmodel1": {
60-
ID: "8",
60+
BaseResource: resources.BaseResource{ID: "8"},
6161
CreateRegisteredModelRequest: catalog.CreateRegisteredModelRequest{
6262
Name: "my_registered_model",
6363
},
@@ -71,23 +71,23 @@ func TestInitializeURLs(t *testing.T) {
7171
},
7272
Schemas: map[string]*resources.Schema{
7373
"schema1": {
74-
ID: "catalog.schema",
74+
BaseResource: resources.BaseResource{ID: "catalog.schema"},
7575
CreateSchema: catalog.CreateSchema{
7676
Name: "schema",
7777
},
7878
},
7979
},
8080
Clusters: map[string]*resources.Cluster{
8181
"cluster1": {
82-
ID: "1017-103929-vlr7jzcf",
82+
BaseResource: resources.BaseResource{ID: "1017-103929-vlr7jzcf"},
8383
ClusterSpec: compute.ClusterSpec{
8484
ClusterName: "cluster1",
8585
},
8686
},
8787
},
8888
Dashboards: map[string]*resources.Dashboard{
8989
"dashboard1": {
90-
ID: "01ef8d56871e1d50ae30ce7375e42478",
90+
BaseResource: resources.BaseResource{ID: "01ef8d56871e1d50ae30ce7375e42478"},
9191
DashboardConfig: resources.DashboardConfig{
9292
Dashboard: dashboards.Dashboard{
9393
DisplayName: "My special dashboard",
@@ -128,8 +128,8 @@ func TestInitializeURLsWithoutOrgId(t *testing.T) {
128128
Resources: config.Resources{
129129
Jobs: map[string]*resources.Job{
130130
"job1": {
131-
ID: "1",
132-
JobSettings: jobs.JobSettings{Name: "job1"},
131+
BaseResource: resources.BaseResource{ID: "1"},
132+
JobSettings: jobs.JobSettings{Name: "job1"},
133133
},
134134
},
135135
},

bundle/config/resources/apps.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ type AppPermission struct {
2323
}
2424

2525
type App struct {
26-
// This is app's name pulled from the state. Usually the same as Name but may be different if Name in the config
27-
// was changed but the app was not re-deployed yet.
28-
ID string `json:"id,omitempty" bundle:"readonly"`
26+
BaseResource
2927

3028
// SourceCodePath is a required field used by DABs to point to Databricks app source code
3129
// on local disk and to the corresponding workspace path during app deployment.
@@ -34,14 +32,12 @@ type App struct {
3432
// Config is an optional field which allows configuring the app following Databricks app configuration format like in app.yml.
3533
// When this field is set, DABs read the configuration set in this field and write
3634
// it to app.yml in the root of the source code folder in Databricks workspace.
37-
// If theres app.yml defined locally, DABs will raise an error.
35+
// If there's app.yml defined locally, DABs will raise an error.
3836
Config map[string]any `json:"config,omitempty"`
3937

40-
Permissions []AppPermission `json:"permissions,omitempty"`
41-
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
42-
URL string `json:"url,omitempty" bundle:"internal"`
38+
Permissions []AppPermission `json:"permissions,omitempty"`
4339

44-
apps.App
40+
apps.App // nolint App struct also defines Id and URL field with the same json tag "id" and "url"
4541
}
4642

4743
func (a *App) UnmarshalJSON(b []byte) error {

bundle/config/resources/base.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package resources
2+
3+
// BaseResource is a struct that contains the base settings for a resource.
4+
type BaseResource struct {
5+
ID string `json:"id,omitempty" bundle:"readonly"`
6+
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
7+
URL string `json:"url,omitempty" bundle:"internal"`
8+
}

bundle/config/resources/clusters.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ type ClusterPermission struct {
2323
}
2424

2525
type Cluster struct {
26-
ID string `json:"id,omitempty" bundle:"readonly"`
27-
Permissions []ClusterPermission `json:"permissions,omitempty"`
28-
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
29-
URL string `json:"url,omitempty" bundle:"internal"`
26+
BaseResource
27+
28+
Permissions []ClusterPermission `json:"permissions,omitempty"`
3029

3130
compute.ClusterSpec
3231
}

bundle/config/resources/dashboard.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ type DashboardConfig struct {
4545
}
4646

4747
type Dashboard struct {
48-
ID string `json:"id,omitempty" bundle:"readonly"`
49-
Permissions []DashboardPermission `json:"permissions,omitempty"`
50-
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
51-
URL string `json:"url,omitempty" bundle:"internal"`
48+
BaseResource
49+
Permissions []DashboardPermission `json:"permissions,omitempty"`
5250

5351
DashboardConfig
5452

bundle/config/resources/database_catalog.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import (
1111
)
1212

1313
type DatabaseCatalog struct {
14-
ID string `json:"id,omitempty" bundle:"readonly"`
15-
URL string `json:"url,omitempty" bundle:"internal"`
16-
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
14+
BaseResource
1715

1816
database.DatabaseCatalog
1917
}

bundle/config/resources/database_instance.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ type DatabaseInstancePermission struct {
2323
}
2424

2525
type DatabaseInstance struct {
26-
ID string `json:"id,omitempty" bundle:"readonly"`
27-
URL string `json:"url,omitempty" bundle:"internal"`
28-
Permissions []DatabaseInstancePermission `json:"permissions,omitempty"`
29-
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
26+
BaseResource
27+
28+
Permissions []DatabaseInstancePermission `json:"permissions,omitempty"`
3029

3130
database.DatabaseInstance
3231
}

bundle/config/resources/job.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ type JobPermission struct {
2424
}
2525

2626
type Job struct {
27-
ID string `json:"id,omitempty" bundle:"readonly"`
28-
Permissions []JobPermission `json:"permissions,omitempty"`
29-
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
30-
URL string `json:"url,omitempty" bundle:"internal"`
27+
BaseResource
28+
29+
Permissions []JobPermission `json:"permissions,omitempty"`
3130

3231
jobs.JobSettings
3332
}

bundle/config/resources/mlflow_experiment.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ type MlflowExperimentPermission struct {
2323
}
2424

2525
type MlflowExperiment struct {
26-
ID string `json:"id,omitempty" bundle:"readonly"`
27-
Permissions []MlflowExperimentPermission `json:"permissions,omitempty"`
28-
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
29-
URL string `json:"url,omitempty" bundle:"internal"`
26+
BaseResource
27+
28+
Permissions []MlflowExperimentPermission `json:"permissions,omitempty"`
3029

3130
ml.Experiment
3231
}

0 commit comments

Comments
 (0)