Skip to content

Commit 4550b49

Browse files
authored
internal: move GetResourceConfig from /bundle to /bundle/config (#3512)
## Changes Pure move, no other changes. ## Why - Passing whole bundle is overkill of only config root is needed. - I want to break dependency of terranova package on /bundle so that I can embed terranova struct in the bundle.
1 parent 7c6eddd commit 4550b49

File tree

5 files changed

+40
-41
lines changed

5 files changed

+40
-41
lines changed

bundle/bundle.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ package bundle
88

99
import (
1010
"context"
11-
"encoding/json"
1211
"errors"
1312
"fmt"
1413
"os"
1514
"path/filepath"
16-
"reflect"
1715
"sync"
1816

1917
"github.com/databricks/cli/bundle/config"
@@ -23,7 +21,6 @@ import (
2321
"github.com/databricks/cli/bundle/terranova/tnstate"
2422
"github.com/databricks/cli/libs/auth"
2523
"github.com/databricks/cli/libs/dagrun"
26-
"github.com/databricks/cli/libs/dyn"
2724
"github.com/databricks/cli/libs/fileset"
2825
"github.com/databricks/cli/libs/locker"
2926
"github.com/databricks/cli/libs/log"
@@ -328,39 +325,6 @@ func (b *Bundle) AuthEnv() (map[string]string, error) {
328325
return auth.Env(cfg), nil
329326
}
330327

331-
// GetResourceConfig returns the configuration object for a given resource group/name pair.
332-
// The returned value is a pointer to the concrete struct that represents that resource type.
333-
// When the group or name is not found the second return value is false.
334-
func (b *Bundle) GetResourceConfig(group, name string) (any, bool) {
335-
// Resolve the Go type that represents a single resource in this group.
336-
typ, ok := config.ResourcesTypes[group]
337-
if !ok {
338-
return nil, false
339-
}
340-
341-
// Fetch the raw value from the dynamic representation of the bundle config.
342-
v, err := dyn.GetByPath(
343-
b.Config.Value(),
344-
dyn.NewPath(dyn.Key("resources"), dyn.Key(group), dyn.Key(name)),
345-
)
346-
if err != nil {
347-
return nil, false
348-
}
349-
350-
// json-round-trip into a value of the concrete resource type to ensure proper handling of ForceSendFields
351-
bytes, err := json.Marshal(v.AsAny())
352-
if err != nil {
353-
return nil, false
354-
}
355-
356-
typedConfigPtr := reflect.New(typ)
357-
if err := json.Unmarshal(bytes, typedConfigPtr.Interface()); err != nil {
358-
return nil, false
359-
}
360-
361-
return typedConfigPtr.Interface(), true
362-
}
363-
364328
func (b *Bundle) StateFilename() string {
365329
if b.DirectDeployment {
366330
return resourcesFilename

bundle/bundle_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,17 @@ func TestBundleGetResourceConfigJobsPointer(t *testing.T) {
165165

166166
b := &Bundle{Config: rootCfg}
167167

168-
res, ok := b.GetResourceConfig("jobs", "my_job")
168+
res, ok := b.Config.GetResourceConfig("jobs", "my_job")
169169
require.True(t, ok, "expected to find jobs.my_job in config")
170170

171171
_, isJob := res.(*resources.Job)
172172
assert.True(t, isJob, "expected *resources.Job, got %T", res)
173173

174-
res, ok = b.GetResourceConfig("jobs", "not_found")
174+
res, ok = b.Config.GetResourceConfig("jobs", "not_found")
175175
require.False(t, ok)
176176
require.Nil(t, res)
177177

178-
res, ok = b.GetResourceConfig("not_found", "my_job")
178+
res, ok = b.Config.GetResourceConfig("not_found", "my_job")
179179
require.False(t, ok)
180180
require.Nil(t, res)
181181
}

bundle/config/root.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package config
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"fmt"
78
"os"
9+
"reflect"
810
"strings"
911

1012
"github.com/databricks/cli/bundle/config/resources"
@@ -567,6 +569,39 @@ func (r Root) GetLocations(path string) []dyn.Location {
567569
return v.Locations()
568570
}
569571

572+
// GetResourceConfig returns the configuration object for a given resource group/name pair.
573+
// The returned value is a pointer to the concrete struct that represents that resource type.
574+
// When the group or name is not found the second return value is false.
575+
func (r *Root) GetResourceConfig(group, name string) (any, bool) {
576+
// Resolve the Go type that represents a single resource in this group.
577+
typ, ok := ResourcesTypes[group]
578+
if !ok {
579+
return nil, false
580+
}
581+
582+
// Fetch the raw value from the dynamic representation of the bundle config.
583+
v, err := dyn.GetByPath(
584+
r.Value(),
585+
dyn.NewPath(dyn.Key("resources"), dyn.Key(group), dyn.Key(name)),
586+
)
587+
if err != nil {
588+
return nil, false
589+
}
590+
591+
// json-round-trip into a value of the concrete resource type to ensure proper handling of ForceSendFields
592+
bytes, err := json.Marshal(v.AsAny())
593+
if err != nil {
594+
return nil, false
595+
}
596+
597+
typedConfigPtr := reflect.New(typ)
598+
if err := json.Unmarshal(bytes, typedConfigPtr.Interface()); err != nil {
599+
return nil, false
600+
}
601+
602+
return typedConfigPtr.Interface(), true
603+
}
604+
570605
// Value returns the dynamic configuration value of the root object. This value
571606
// is the source of truth and is kept in sync with values in the typed configuration.
572607
func (r Root) Value() dyn.Value {

bundle/terranova/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (m *terranovaApplyMutator) Apply(ctx context.Context, b *bundle.Bundle) dia
100100
return false
101101
}
102102

103-
config, ok := b.GetResourceConfig(node.Group, node.Key)
103+
config, ok := b.Config.GetResourceConfig(node.Group, node.Key)
104104
if !ok {
105105
logdiag.LogError(ctx, fmt.Errorf("%s: internal error when reading config", errorPrefix))
106106
return false

bundle/terranova/plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func CalculatePlanForDeploy(ctx context.Context, b *bundle.Bundle) error {
141141
settings: settings,
142142
}
143143

144-
config, ok := b.GetResourceConfig(pl.group, pl.resourceName)
144+
config, ok := b.Config.GetResourceConfig(pl.group, pl.resourceName)
145145
if !ok {
146146
logdiag.LogError(ctx, fmt.Errorf("%s: internal error: cannot read config", errorPrefix))
147147
return false

0 commit comments

Comments
 (0)